2022-01-15 00:08:58 +02:00
|
|
|
-- Copyright (C) 2022 veden
|
|
|
|
|
|
|
|
-- This program is free software: you can redistribute it and/or modify
|
|
|
|
-- it under the terms of the GNU General Public License as published by
|
|
|
|
-- the Free Software Foundation, either version 3 of the License, or
|
|
|
|
-- (at your option) any later version.
|
|
|
|
|
|
|
|
-- This program is distributed in the hope that it will be useful,
|
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
-- GNU General Public License for more details.
|
|
|
|
|
|
|
|
-- You should have received a copy of the GNU General Public License
|
|
|
|
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2019-02-16 06:17:30 +02:00
|
|
|
if (unitUtilsG) then
|
|
|
|
return unitUtilsG
|
|
|
|
end
|
2019-02-13 07:50:25 +02:00
|
|
|
local unitUtils = {}
|
|
|
|
|
|
|
|
-- imports
|
|
|
|
|
|
|
|
local constants = require("Constants")
|
2021-12-09 08:01:54 +02:00
|
|
|
local chunkPropertyUtils = require("ChunkPropertyUtils")
|
2019-02-13 07:50:25 +02:00
|
|
|
|
|
|
|
-- constants
|
|
|
|
|
2021-12-09 08:01:54 +02:00
|
|
|
local DEFINES_WIRE_TYPE_RED = defines.wire_type.red
|
|
|
|
local DEFINES_WIRE_TYPE_GREEN = defines.wire_type.green
|
|
|
|
|
|
|
|
local ENERGY_THIEF_CONVERSION_TABLE = constants.ENERGY_THIEF_CONVERSION_TABLE
|
|
|
|
local ENERGY_THIEF_LOOKUP = constants.ENERGY_THIEF_LOOKUP
|
|
|
|
|
2019-02-13 07:50:25 +02:00
|
|
|
local ENERGY_THIEF_DRAIN_CRYSTALS = constants.ENERGY_THIEF_DRAIN_CRYSTALS
|
|
|
|
|
|
|
|
-- imported functions
|
|
|
|
|
2021-12-09 08:01:54 +02:00
|
|
|
local setDrainPylons = chunkPropertyUtils.setDrainPylons
|
|
|
|
|
2019-02-13 07:50:25 +02:00
|
|
|
-- module code
|
|
|
|
|
2021-12-09 08:01:54 +02:00
|
|
|
local function convertTypeToDrainCrystal(evolutionFactor, entity)
|
2019-02-13 07:50:25 +02:00
|
|
|
if (entity == "pole") then
|
|
|
|
return "crystal-drain-pole-rampant"
|
|
|
|
else
|
|
|
|
if (entity == "smallUnit") then
|
|
|
|
if (evolutionFactor < 0.25) then
|
|
|
|
return ENERGY_THIEF_DRAIN_CRYSTALS[1]
|
|
|
|
elseif (evolutionFactor < 0.50) then
|
|
|
|
return ENERGY_THIEF_DRAIN_CRYSTALS[2]
|
|
|
|
elseif (evolutionFactor < 0.75) then
|
|
|
|
return ENERGY_THIEF_DRAIN_CRYSTALS[3]
|
|
|
|
else
|
|
|
|
return ENERGY_THIEF_DRAIN_CRYSTALS[4]
|
|
|
|
end
|
|
|
|
elseif (entity == "unit") then
|
|
|
|
if (evolutionFactor < 0.25) then
|
|
|
|
return ENERGY_THIEF_DRAIN_CRYSTALS[4]
|
|
|
|
elseif (evolutionFactor < 0.50) then
|
|
|
|
return ENERGY_THIEF_DRAIN_CRYSTALS[5]
|
|
|
|
elseif (evolutionFactor < 0.75) then
|
|
|
|
return ENERGY_THIEF_DRAIN_CRYSTALS[6]
|
|
|
|
else
|
|
|
|
return ENERGY_THIEF_DRAIN_CRYSTALS[7]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if (evolutionFactor < 0.25) then
|
|
|
|
return ENERGY_THIEF_DRAIN_CRYSTALS[7]
|
|
|
|
elseif (evolutionFactor < 0.50) then
|
|
|
|
return ENERGY_THIEF_DRAIN_CRYSTALS[8]
|
|
|
|
elseif (evolutionFactor < 0.75) then
|
|
|
|
return ENERGY_THIEF_DRAIN_CRYSTALS[9]
|
|
|
|
else
|
|
|
|
return ENERGY_THIEF_DRAIN_CRYSTALS[10]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-09 08:01:54 +02:00
|
|
|
function unitUtils.createDrainPylon(map, cause, entity, entityType)
|
|
|
|
if ((cause and ENERGY_THIEF_LOOKUP[cause.name]) or (not cause)) then
|
|
|
|
local conversion = ENERGY_THIEF_CONVERSION_TABLE[entityType]
|
|
|
|
if conversion then
|
|
|
|
local newEntity = map.surface.create_entity({
|
|
|
|
position=entity.position,
|
|
|
|
name=convertTypeToDrainCrystal(entity.force.evolution_factor, conversion),
|
|
|
|
direction=entity.direction
|
|
|
|
})
|
|
|
|
if (conversion == "pole") then
|
|
|
|
local targetEntity = map.surface.create_entity({
|
|
|
|
position=entity.position,
|
|
|
|
name="pylon-target-rampant",
|
|
|
|
direction=entity.direction
|
|
|
|
})
|
|
|
|
targetEntity.backer_name = ""
|
|
|
|
local wires = entity.neighbours
|
|
|
|
if wires then
|
|
|
|
for _,v in pairs(wires.copper) do
|
|
|
|
if (v.valid) then
|
|
|
|
newEntity.connect_neighbour(v);
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for _,v in pairs(wires.red) do
|
|
|
|
if (v.valid) then
|
|
|
|
newEntity.connect_neighbour({
|
|
|
|
wire = DEFINES_WIRE_TYPE_RED,
|
|
|
|
target_entity = v
|
|
|
|
});
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for _,v in pairs(wires.green) do
|
|
|
|
if (v.valid) then
|
|
|
|
newEntity.connect_neighbour({
|
|
|
|
wire = DEFINES_WIRE_TYPE_GREEN,
|
|
|
|
target_entity = v
|
|
|
|
});
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
setDrainPylons(map, targetEntity, newEntity)
|
|
|
|
elseif newEntity.backer_name then
|
|
|
|
newEntity.backer_name = ""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-16 06:17:30 +02:00
|
|
|
unitUtilsG = unitUtils
|
2019-02-13 07:50:25 +02:00
|
|
|
return unitUtils
|