mirror of
https://github.com/veden/Rampant.git
synced 2024-12-30 21:19:46 +02:00
147 lines
5.1 KiB
Lua
147 lines
5.1 KiB
Lua
local entityUtils = {}
|
|
|
|
-- imports
|
|
|
|
local mapUtils = require("MapUtils")
|
|
local constants = require("Constants")
|
|
|
|
-- constants
|
|
|
|
local BUILDING_PHEROMONES = constants.BUILDING_PHEROMONES
|
|
|
|
local PLAYER_BASE_GENERATOR = constants.PLAYER_BASE_GENERATOR
|
|
|
|
local DEFINES_DIRECTION_EAST = defines.direction.east
|
|
local DEFINES_WIRE_TYPE_RED = defines.wire_type.red
|
|
local DEFINES_WIRE_TYPE_GREEN = defines.wire_type.green
|
|
|
|
-- imported functions
|
|
|
|
local getChunkByIndex = mapUtils.getChunkByIndex
|
|
|
|
local mFloor = math.floor
|
|
|
|
-- module code
|
|
|
|
function entityUtils.getEntityOverlapChunks(regionMap, entity)
|
|
local boundingBox = entity.prototype.selection_box or entity.prototype.collision_box;
|
|
|
|
local leftTopChunk
|
|
local rightTopChunk
|
|
local leftBottomChunk
|
|
local rightBottomChunk
|
|
|
|
if (boundingBox ~= nil) then
|
|
local center = entity.position
|
|
local topXOffset
|
|
local topYOffset
|
|
|
|
local bottomXOffset
|
|
local bottomYOffset
|
|
|
|
if (entity.direction == DEFINES_DIRECTION_EAST) then
|
|
topXOffset = boundingBox.left_top.y
|
|
topYOffset = boundingBox.left_top.x
|
|
bottomXOffset = boundingBox.right_bottom.y
|
|
bottomYOffset = boundingBox.right_bottom.x
|
|
else
|
|
topXOffset = boundingBox.left_top.x
|
|
topYOffset = boundingBox.left_top.y
|
|
bottomXOffset = boundingBox.right_bottom.x
|
|
bottomYOffset = boundingBox.right_bottom.y
|
|
end
|
|
|
|
local leftTopChunkX = mFloor((center.x + topXOffset) * 0.03125)
|
|
local leftTopChunkY = mFloor((center.y + topYOffset) * 0.03125)
|
|
|
|
-- used to force things on chunk boundary to not spill over 0.0001
|
|
local rightTopChunkX = mFloor((center.x + bottomXOffset - 0.0001) * 0.03125)
|
|
local rightTopChunkY = leftTopChunkY
|
|
|
|
-- used to force things on chunk boundary to not spill over 0.0001
|
|
local leftBottomChunkX = leftTopChunkX
|
|
local leftBottomChunkY = mFloor((center.y + bottomYOffset - 0.0001) * 0.03125)
|
|
|
|
local rightBottomChunkX = rightTopChunkX
|
|
local rightBottomChunkY = leftBottomChunkY
|
|
|
|
leftTopChunk = getChunkByIndex(regionMap, leftTopChunkX, leftTopChunkY)
|
|
if (leftTopChunkX ~= rightTopChunkX) then
|
|
rightTopChunk = getChunkByIndex(regionMap, rightTopChunkX, rightTopChunkY)
|
|
end
|
|
if (leftTopChunkY ~= leftBottomChunkY) then
|
|
leftBottomChunk = getChunkByIndex(regionMap, leftBottomChunkX, leftBottomChunkY)
|
|
end
|
|
if (leftTopChunkX ~= rightBottomChunkX) and (leftTopChunkY ~= rightBottomChunkY) then
|
|
rightBottomChunk = getChunkByIndex(regionMap, rightBottomChunkX, rightBottomChunkY)
|
|
end
|
|
end
|
|
return leftTopChunk, rightTopChunk, leftBottomChunk, rightBottomChunk
|
|
end
|
|
|
|
function entityUtils.addRemovePlayerEntity(regionMap, entity, natives, addObject, creditNatives)
|
|
local leftTop, rightTop, leftBottom, rightBottom
|
|
local entityValue
|
|
if (BUILDING_PHEROMONES[entity.type] ~= nil) and (entity.force.name == "player") then
|
|
entityValue = BUILDING_PHEROMONES[entity.type]
|
|
|
|
leftTop, rightTop, leftBottom, rightBottom = entityUtils.getEntityOverlapChunks(regionMap, entity)
|
|
if not addObject then
|
|
if creditNatives then
|
|
natives.points = natives.points + entityValue
|
|
end
|
|
entityValue = -entityValue
|
|
end
|
|
if leftTop then
|
|
leftTop[PLAYER_BASE_GENERATOR] = leftTop[PLAYER_BASE_GENERATOR] + entityValue
|
|
end
|
|
if rightTop then
|
|
rightTop[PLAYER_BASE_GENERATOR] = rightTop[PLAYER_BASE_GENERATOR] + entityValue
|
|
end
|
|
if leftBottom then
|
|
leftBottom[PLAYER_BASE_GENERATOR] = leftBottom[PLAYER_BASE_GENERATOR] + entityValue
|
|
end
|
|
if rightBottom then
|
|
rightBottom[PLAYER_BASE_GENERATOR] = rightBottom[PLAYER_BASE_GENERATOR] + entityValue
|
|
end
|
|
end
|
|
end
|
|
|
|
function entityUtils.makeImmortalEntity(surface, entity)
|
|
local repairPosition = entity.position
|
|
local repairName = entity.name
|
|
local repairForce = entity.force
|
|
local repairDirection = entity.direction
|
|
|
|
local wires
|
|
if (entity.type == "electric-pole") then
|
|
wires = entity.neighbours
|
|
end
|
|
entity.destroy()
|
|
local newEntity = surface.create_entity({position=repairPosition,
|
|
name=repairName,
|
|
direction=repairDirection,
|
|
force=repairForce})
|
|
if wires then
|
|
for connectType,neighbourGroup in pairs(wires) do
|
|
if connectType == "copper" then
|
|
for _,v in pairs(neighbourGroup) do
|
|
newEntity.connect_neighbour(v);
|
|
end
|
|
elseif connectType == "red" then
|
|
for _,v in pairs(neighbourGroup) do
|
|
newEntity.connect_neighbour({wire = DEFINES_WIRE_TYPE_RED, target_entity = v});
|
|
end
|
|
elseif connectType == "green" then
|
|
for _,v in pairs(neighbourGroup) do
|
|
newEntity.connect_neighbour({wire = DEFINES_WIRE_TYPE_GREEN, target_entity = v});
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
newEntity.destructible = false
|
|
end
|
|
|
|
return entityUtils
|