Place entity with lua without removing ghost

Place to get help with not working mods / modding interface.
User avatar
bigmond
Inserter
Inserter
Posts: 28
Joined: Fri Aug 30, 2019 11:11 am
Contact:

Place entity with lua without removing ghost

Post by bigmond »

I've been playing with Factorios commands.

Currently I am trying to spawn a large number of rocks with lua.

The rocks spawn in a rectangulare grid centerd around the player.

Wherever a rock spawns the closest player object withing a 5 radius is damaged, possibly destroyed.

The problem comes that if one of the rocks is placed on a ghost, the ghost is destroyed. I woudl like any ghosts to be preserved.

Is there an easy way to do this.

Code: Select all

local player = game.players["BigMonD"]
local surface = player.surface           
local boxSize = 75
local pos = {x=player.position.x,y=player.position.y}
local rocks = {'sand-rock-big','sand-rock-big','rock-big', 'rock-big', 'rock-huge'}
local rock =  nil
local xWidth = math.random(boxSize/10, boxSize)/2
local yWidth = math.random(boxSize/10, boxSize)/2
local i = 0 - xWidth
local j = 0 - yWidth
local p = nil
local density = 200.000;
local place = false
local maxDamage = 500
local source = "player"

while (i < xWidth) and (j < yWidth) do
    p = {x = pos.x + i, y = pos.y + j}
   
    if not place then
        place = density > math.random(0,1000)
    end

    if place then
        rock = rocks[math.random(1,#rocks)]
        surface.create_entity{name=rock, position=p}
        
        local collider = surface.find_entities_filtered{position = p,radius = 5 ,force = "player",limit = 1} 
    
        game.print("collider type: " .. type(collider))
    
        if collider[1] == nil then
            game.print("Collider nil")          
        else
            
            if collider[1].health ~= nil then 
                game.print("Collider found")
                if collider[1].type == "character" then
                    collider[1].damage(math.random(5,20), source)            
                else
                    collider[1].damage(math.random(50,200), source)            
                end
                
            else
                game.print("Ghost")
            end
        end
        
        place = false
    
    end
    

 
    i = i + 1
    if i == xWidth then
        i = 0 - xWidth
        j = j + 1
    end
    
end

game.print("The ground shakes violently!!!!!", {r=1,g=0.2,b=0})
Please no more spagetti, I cant eat any more.
Post Reply

Return to “Modding help”