I'm trying to create a mod for a Solar mat. (Attached.)
They way it works is that I create a hidden solar panel with the placement of a tile.
My problem is that I group and delete hidden entities by identifying the position. Because I'm using a combination of Tile and Entities, I'm having a problem getting the same location.
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
group_entities(cantor(position.x,position.y), { solar_mat, create_sm_pole, create_sm_panel })
end
end
end
---------------------------------------------
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
if tile and player.mining_state.mining and tile_name == "bi-solar-mat" then
local tile_position = player.mining_state.position
--game.print("Tile "..tile_name..", pos "..serpent.line(tile_position))
writeDebug("Solar Mat Removed")
--- Solar Map has been removed
local pos_hash = cantor(tile_position.x, tile_position.y)
local entity_group = getGroup_entities(pos_hash)
if entity_group then
writeDebug("Made it here")
for ix, vx in ipairs(entity_group) do
if vx == entity then
--vx.destroy()
else
vx.destroy()
end
end
else
writeDebug("Nope!")
end
ungroup_entities(pos_hash)
end
end
Currently I'm not deleting the hidden solar panel when mining the mat. and I believe the problem is that my position when placing the mat and when mining it is not matching up.
Could someone maybe review what I did and point out where I'm going wrong?