goals:
tiberium should:
- grow
spread
damage player
damage buildings
transform trees into sporetrees (1 of x ; x=100?)
transform adjacent ore into tiberium
replace groundtiles with TiberiumGroundTiles on creation / spread
grow and spread shall update in chunks. The existing mod uses an array of all tiberiumtiles and updates one tile per tick. Hence, the time needed for an updatecycle grows and grows - and takes way too long, but saves performance.
I want to update all tiles of one chunk per tick (compromise of speed and performance) or, depending on performance, onNthTick.
This is the code I struggle with, it is in working state:
(the rampant sniplets are from here: viewtopic.php?t=31745 )
Code: Select all
script.on_init(function()
--- VARIABLES
global.TD = true -- TiberiumDebug
global.numChunks = 0 -- number of chunks
global.tibOreType = "tiberium-ore" -- ore type
global.oreList = {} -- tiles with tiberium ore
global.oreListIndex = 0 -- for cycling through the list
global.cred = {r=1, g=0, b=0, a=1} -- red color
global.tibOrePos = {} -- position of ore
global.tibGrowthAmount = 250 -- how much should tiberium grow at once
global.tibGrowthRate = 3 -- number of seconds when tiberium grows
global.tickCounter = 0 -- counter to calculate if tiberium should grow
global.chunkCounter = 0
global.allChunks={} -- table with all chunks stored
global.Counter=0
global.minTibSpreadAmount = 500 -- Amount a tile must have to spread tib Default = 500
global.damageForceName = "tiberium"
game.create_force(global.damageForceName)
global.contactDamage = 1 --how much damage should be applied to objects over tiberium?
global.maxGrothAmount = 6000 -- how much tiberium can exist on 1 tile
global.allChunks = game.surfaces[1].get_chunks()
global.currentChunkPointer=1
end)--.on_init(function()
local chunks
local currentChunk
function onChunkGenerated(event) -- from mod rampant
if (event.surface.index == 1) then
chunks[#chunks+1] = event
end
end
--initial chunk scan
script.on_event(defines.events.on_chunk_generated, function(event)
-- discarded for new approach
--[[local entities = game.surfaces[1].find_entities_filtered{area = event.area, name = global.tibOreType} -- is there tiberium on chunk
for i=1,#entities,1 do
table.insert(global.oreList, entities[i]) -- add found tiberium to list
end--]]
global.numChunks = global.numChunks + 1
global.allChunks = game.surfaces[1].get_chunks()
for chunk in global.allChunks do -- from mod rampant
onChunkGenerated({ surface = global.allChunks,
area = { left_top = { x = chunk.x * 32,
y = chunk.y * 32 }}})
end
if global.TD == true then
--game.print("Number of Chunks: "..tostring(global.numChunks))
end
end) -- .on_chunk_generated
function doOneStep(chunks) -- from mod rampant
local chunk = chunks[global.currentChunk]
.... doProcessing ....
global.currentChunkPointer = global.currentChunkPointer + 1
if (global.currentChunkPointer == #chunks) then
global.currentChunkPointer = 1
end
end
function tiberium() -- function for growing tiberium
game.print("entered function tiberium()")
for i=1, #global.tibOrePos, 1 do
if global.tibOrePos[i].amount <= global.maxGrothAmount then
global.tibOrePos[i].amount = global.tibOrePos[i].amount+global.tibGrowthAmount
end
--check for spread
if global.tibOrePos[i].amount >= global.minTibSpreadAmount then --spread value reached?
--add new ores to empty adjacent squares
orePositions = {{ global.tibOrePos[i].position.x + 1, global.tibOrePos[i].position.y }, { global.tibOrePos[i].position.x - 1, global.tibOrePos[i].position.y },
{ global.tibOrePos[i].position.x, global.tibOrePos[i].position.y + 1 }, { global.tibOrePos[i].position.x, global.tibOrePos[i].position.y - 1 }}
for i=1,4,1 do
--check if adjacent square is blocked
local tile = game.surfaces[1].get_tile(orePositions[i][1], orePositions[i][2])
if tile.valid and not tile.collides_with("player-layer") then
local adjacentEntities = game.surfaces[1].find_entities_filtered{position = orePositions[i]}
if #adjacentEntities == 0 then
--add new ore here
local newOre = game.surfaces[1].create_entity{name = global.tibOreType, position = orePositions[i], amount = 1}
--table.insert(global.oreList, newOre)
end
end
end
end
end
end -- tiberium
script.on_event(defines.events.on_tick, function(event)
--check if players are over tiberium, damage them if they are unarmored
for i, player in pairs (game.players) do
local playerPositionOre = game.surfaces[1].find_entities_filtered{name = global.tibOreType, position = game.players[i].position}
if #playerPositionOre > 0 and game.players[i] and game.players[i].valid and game.players[i].character and not game.players[i].character.vehicle then
game.players[i].character.damage(global.contactDamage, game.forces.tiberium, "acid")
end
--if player is holding tiberium products, add damage
--[[local inventory = game.players[i].get_inventory(defines.inventory.item_main)
if inventory then
for p=1,#global.tiberiumProducts,1 do
if inventory.get_item_count(global.tiberiumProducts[p]) > 0 then
game.players[i].character.damage(0.1, game.forces.tiberium, "acid")
break
end
end
end--]]
end
--counter for growing tiberium
global.tickCounter = global.tickCounter + 1
if global.tickCounter >= global.tibGrowthRate*60 then -- growthtime in seconds
tiberium() -- function for growing tiberium
global.tickCounter = 0 -- restart counter
end
global.chunkCounter = global.chunkCounter + 1
-- this funtions updates all tiberiumtiles at once
--global.tibOrePos=game.surfaces[1].find_entities_filtered{name = global.tibOreType} -- array with position of ore
--for i=1, #global.tibOrePos, 1 do
-- global.tibOrePos[i].amount = global.tibOrePos[i].amount+global.tibGrowthAmount
--end
end) -- on_tick
What needs to be done:
cycle through chunks and update the within tiberiumtiles