Help with removing hidden entities when mining a tile

Place to get help with not working mods / modding interface.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Help with removing hidden entities when mining a tile

Post by TheSAguy »

Hi,

I'm trying to remove hidden entities when I mine a tile and can't figure it out, since the tile logic works a little different that the normal entity logic.

Here is my control. I'm not sure how to find the hidden entity when I mine the tile. Could someone please look at what I'm doing wrong here:

Remove Function

Code: Select all

---------------------------------------------
local function Tile_Remove(event)
	

     local player = game.players[event.player_index]
     local tile_name = event.item_stack.name
     local tile = game.item_prototypes[tile_name].place_as_tile_result

	 --- Solar Map has been removed
     if tile and player.mining_state.mining and tile_name == "bi-solar-mat" then
		local tile_position = player.mining_state.position 
		
		writeDebug("Solar Mat Removed")
		
		local robot = event.robot
		local surface = player and player.surface or robot.surface	
		local radius = 0.15
		local area = {{tile_position.x - radius, tile_position.y - radius}, {tile_position.x + radius, tile_position.y + radius}}
			
		--- Remove the Hidden Pole
		if surface.find_entity('bi_solar_pole', tile_position) then -- Does not find the hidden pole
		
		--if surface.find_entities_filtered({area = area, name = "bi_solar_pole"}) then 
			for _, o in pairs(surface.find_entities_filtered({area = area, name = "bi_solar_pole"})) do o.destroy() end	
			for _, o in pairs(surface.find_entities_filtered({area = area, name = "bi_solar_pole"})) do o.die() end	

			writeDebug("bi_solar_pole Removed")
		else
			writeDebug("bi_solar_pole not found")				
		end
			
		--- Remove the Hidden Pole			
		if surface.find_entities_filtered({area = area, name = "bi_solar-panel_for_Solar-Mat"}) then  -- It says it finds the hidden panel, but you can actully type anything here and it will find it....
		--if surface.find_entities_filtered({area = area, name = "BS!!!"}) then -- It said it found "BS"...
			for _, o in pairs(surface.find_entities_filtered({area = area, name = "bi_solar-panel_for_Solar-Mat"})) do o.die() end	

			writeDebug("bi_solar-panel_for_Solar-Mat Removed")
		else
			writeDebug("bi_solar-panel_for_Solar-Mat not found")				
		end
				
	  
     end

	
end
Built function. This seems to work as intended.

Code: Select all


local function Player_Tile_Built(event)

	local player = game.players[event.player_index]
	local robot = event.robot
	local surface = player and player.surface or robot.surface
	local position = event.positions

	for i, position in ipairs(position) do
		local currentTilename = surface.get_tile(position.x,position.y).name
		
		if currentTilename == "bi-solar-mat" then
			writeDebug("Solar Mat has been built")
			
			local force = event.force
			local solar_mat = surface.get_tile(position.x,position.y)
			local sm_pole_name = "bi_solar_pole"  
			local sm_panel_name = "bi_solar-panel_for_Solar-Mat"  
			  
			local create_sm_pole = surface.create_entity({name = sm_pole_name, position = position, force = force})
			local create_sm_panel = surface.create_entity({name = sm_panel_name, position = position, force = force})
			  
			--create_sm_pole.minable = false
			--create_sm_pole.destructible = false
			--create_sm_panel.minable = false
			--create_sm_panel.destructible = false
			
			
		end
	end	
		
end

	
local function Robot_Tile_Built(event)


	local robot = event.robot
	local surface = robot.surface
	local position = event.positions

	for i, position in ipairs(position) do
		local currentTilename = surface.get_tile(position.x,position.y).name
		
		if currentTilename == "bi-solar-mat" then
			writeDebug("Solar Mat has been built")
			
			local force = event.force
			local solar_mat = surface.get_tile(position.x,position.y)
			local sm_pole_name = "bi_solar_pole"  
			local sm_panel_name = "bi_solar-panel_for_Solar-Mat"  
			  
			local create_sm_pole = surface.create_entity({name = sm_pole_name, position = position, force = force})
			local create_sm_panel = surface.create_entity({name = sm_panel_name, position = position, force = force})
			  
			--create_sm_pole.minable = false
			--create_sm_pole.destructible = false
			--create_sm_panel.minable = false
			--create_sm_panel.destructible = false
				  
			group_entities(cantor(position.x,position.y), { solar_mat, create_sm_pole, create_sm_panel })	  			
			
		end
	end	
		
end


I've attached the mod here.
Thanks.
Attachments
Solar_Mat_0.0.2.zip
Mod
(192.89 KiB) Downloaded 61 times
Last edited by TheSAguy on Fri Jun 09, 2017 1:36 pm, edited 2 times in total.
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Help with removing hidden entities when mining a tile

Post by darkfrei »

I'm not sure, but here

Code: Select all

--- Remove the Hidden Pole
		if surface.find_entity('bi_solar_pole', tile_position) then -- Does not find the hidden pole
		
		--if surface.find_entities_filtered({area = area, name = "bi_solar_pole"}) then 
			for _, o in pairs(surface.find_entities_filtered({area = area, name = "bi_solar_pole"})) do o.destroy() end	
			for _, o in pairs(surface.find_entities_filtered({area = area, name = "bi_solar_pole"})) do o.die() end	
96: surface.find_entity('bi_solar_pole', tile_position) can't work with entities without collision box.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Help with removing hidden entities when mining a tile

Post by TheSAguy »

darkfrei wrote:I'm not sure, but here


96: surface.find_entity('bi_solar_pole', tile_position) can't work with entities without collision box.
Thanks for the replay Darkfrei, but both the hidden entities have a collision box of:

Code: Select all

    collision_box = {{-0.15, -0.15}, {0.15, 0.15}},
I'm really stumped at why I can't find and remove the hidden entities.., :roll:
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Help with removing hidden entities when mining a tile

Post by darkfrei »

I think the best way for good performance is making own global table and writing all entities/tiles to this table.
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Help with removing hidden entities when mining a tile

Post by darkfrei »

Code: Select all

		local myShift = 0.5
		local area = {{tile_position.x - radius - myShift, tile_position.y - radius - myShift}, {tile_position.x + radius - myShift, tile_position.y + radius - myShift}}
		local my_pole = surface.find_entity('bi_solar_pole', {tile_position.x  - myShift, tile_position.y - myShift})
			
		--- Remove the Hidden Pole
		if my_pole then -- Does not find the hidden pole
You have placed it out_of_grid.
There is different between map position and entity position. It was shifted to 0.5 and it works!
Attachments
Solar_Mat_0.0.2.zip
patched!
(195.34 KiB) Downloaded 59 times
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Help with removing hidden entities when mining a tile

Post by TheSAguy »

Thanks so much Darkfrei!!

I think I got it working now!
Attachments
Solar_Mat_0.0.4.zip
Solar Mat
(192.91 KiB) Downloaded 61 times
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: [Resolved] Help with removing hidden entities when mining a tile

Post by darkfrei »

Image
Image
Image

Here: If you place concrete, then your poles are still here. If you delete concrete, then they are still here too.

And collisons, you can switch it off by your poles. Try to go thru or with car.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: [Resolved] Help with removing hidden entities when mining a tile

Post by TheSAguy »

Ugh, thanks darkfrei,
So somehow I need to remove my poles when another tile replaces my tile....
Do you know how to do that?
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: [Resolved] Help with removing hidden entities when mining a tile

Post by darkfrei »

TheSAguy wrote:Ugh, thanks darkfrei,
So somehow I need to remove my poles when another tile replaces my tile....
Do you know how to do that?
If player has placed any tile, but not yours, search your poles and destroy them if exist.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: [Resolved] Help with removing hidden entities when mining a tile

Post by TheSAguy »

Does the below seem the best way of doing it?
Was just wondering if there was a more efficient way of doing it.

Code: Select all

----- Solar Mat stuff
local function Player_Tile_Built(event)

	local player = game.players[event.player_index]
	local surface = player and player.surface
	local position = event.positions


	for i, position in ipairs(position) do
		local currentTilename = surface.get_tile(position.x,position.y).name
		
		if currentTilename == "bi-solar-mat" then
			writeDebug("Solar Mat has been built")
			
			local force = event.force
			local solar_mat = surface.get_tile(position.x,position.y)
			local sm_pole_name = "bi_solar_pole"  
			local sm_panel_name = "bi_solar-panel_for_Solar-Mat"  
			  
			local create_sm_pole = surface.create_entity({name = sm_pole_name, position = position, force = force})
			local create_sm_panel = surface.create_entity({name = sm_panel_name, position = position, force = force})
			  
			create_sm_pole.minable = false
			create_sm_pole.destructible = false
			create_sm_panel.minable = false
			create_sm_panel.destructible = false
		
		else	
		
			local radius = 0.5
			local area = {{position.x - radius, position.y - radius}, {position.x + radius, position.y + radius}}
			writeDebug("NOT Solar Mat")
			local entities = surface.find_entities(area)
			local entity1 = entities[1]
			entity1 = surface.find_entities_filtered{area=area, name="bi_solar_pole", limit=1}
				
			if entity1 then 		
			
				for _, o in pairs(surface.find_entities_filtered({area = area, name = "bi_solar_pole"})) do o.destroy() end	

				writeDebug("bi_solar_pole Removed")
			else
				writeDebug("bi_solar_pole not found")				
			end
				
			--- Remove the Hidden Solar Panel		
			local entity2 = entities[1]
			entity2 = surface.find_entities_filtered{area=area, name="bi_solar-panel_for_Solar-Mat", limit=1}	
			
			if entity2 then 
					
				for _, o in pairs(surface.find_entities_filtered({area = area, name = "bi_solar-panel_for_Solar-Mat"})) do o.destroy() end	

				writeDebug("bi_solar-panel_for_Solar-Mat Removed")
			else
				writeDebug("bi_solar-panel_for_Solar-Mat not found")				
			end


			
		end
	end	
		
end

Robot:

Code: Select all

	
local function Robot_Tile_Built(event)


	local robot = event.robot
	local surface = robot.surface
	local position = event.positions

	for i, position in ipairs(position) do
		local currentTilename = surface.get_tile(position.x,position.y).name
		
		if currentTilename == "bi-solar-mat" then
			writeDebug("Solar Mat has been built")
			
			local force = event.force
			local solar_mat = surface.get_tile(position.x,position.y)
			local sm_pole_name = "bi_solar_pole"  
			local sm_panel_name = "bi_solar-panel_for_Solar-Mat"  
			  
			local create_sm_pole = surface.create_entity({name = sm_pole_name, position = position, force = force})
			local create_sm_panel = surface.create_entity({name = sm_panel_name, position = position, force = force})
			  
			create_sm_pole.minable = false
			create_sm_pole.destructible = false
			create_sm_panel.minable = false
			create_sm_panel.destructible = false
		
		else
		
				
			local radius = 0.5
			local area = {{position.x - radius, position.y - radius}, {position.x + radius, position.y + radius}}
			writeDebug("NOT Solar Mat")
			local entities = surface.find_entities(area)
			local entity1 = entities[1]
			entity1 = surface.find_entities_filtered{area=area, name="bi_solar_pole", limit=1}
				
			if entity1 then 		
			
				for _, o in pairs(surface.find_entities_filtered({area = area, name = "bi_solar_pole"})) do o.destroy() end	

				writeDebug("bi_solar_pole Removed")
			else
				writeDebug("bi_solar_pole not found")				
			end
				
			--- Remove the Hidden Solar Panel		
			local entity2 = entities[1]
			entity2 = surface.find_entities_filtered{area=area, name="bi_solar-panel_for_Solar-Mat", limit=1}	
			
			if entity2 then 
					
				for _, o in pairs(surface.find_entities_filtered({area = area, name = "bi_solar-panel_for_Solar-Mat"})) do o.destroy() end	

				writeDebug("bi_solar-panel_for_Solar-Mat Removed")
			else
				writeDebug("bi_solar-panel_for_Solar-Mat not found")				
			end


		
		end
	end	
		
end


Attachments
control.lua
CONTROL
(25.62 KiB) Downloaded 64 times
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Help with removing hidden entities when mining a tile

Post by darkfrei »

Code: Select all

local create_sm_pole = surface.create_entity({name = sm_pole_name, position = position, force = force})
         local create_sm_panel = surface.create_entity({name = sm_panel_name, position = position, force = force})
I think that you must place all entities in the middle of the square. It means, all coordinates are need +0.5 here.

You can use functions for easy code reading and using.
Post Reply

Return to “Modding help”