Well, code Almost works, kinda unknown how its doing these areas <.< tell it scan a box it seems to scan 2 blocks

For some reason I figured a solution to the odd numbers I was getting that I couldnt understand where they was comming from, I changed the model layout to use ... a new entity, more or less just a small-electric-pole modified image a bit, and decided I wanted to make this findentitiesfiltered scan its powering area for drills, then for each drill it finds, use the number in the search radius to scan the resources >.> for some reason no go <.< It seems like it will not find any drills, and the few times it did trigger it found resources somewhere else not on that drill >.> after looking over and over, cant make heads or tails of why its doing it, rather my code sucks, proboly since I know nada on lua, but also could be a possible glitch/bug or using functions as not intended so figure I would ask for some opinions
first I am using 2 tick counts, I want to scan resources, and scan the attack data seperate as well as run some checks at diffrent times, so I am using 2 counters
I left in the radar code for the atk warning system just in case thier is conflict with it
It SHOULD run a search every 100 ticks to see if the sensor for the mine watching is thier and find any drills near it in a 10x10 area, around the pole/sensor then for each mine it finds, search, in this case 2.49 around each side of the minefor its availible resources, in this case just printing it to the screen.
I get no crashes, but it seems to not make it to the Drills area, as if its not finding a drill at all even though next to the sensor I placed 4 basic drills on each cornerright to the sensor so no mater what direction it searches it should find a drill?
Code: Select all
game.oninit(function()
glob.tickCount = 0
glob.tickCount1 = 0
glob.player = game.getplayer()
glob.newradarbuilt = false
glob.newMineDet = false
glob.newradarNum = 0
glob.newMineWatcher = 0
gt = game.gettext
glob.MineWatcher = {}
glob.NewRad = {}
end)
game.onload(function()
if glob.tickCount == nil then glob.tickCount = 0 end
if glob.tickCount1 == nil then glob.tickCount1 = 0 end
if glob.newMineDet == nil then glob.newMineDet = true end
if glob.newradarbuilt == nil then glob.newradarbuilt = false end
if glob.player == nil then glob.player = game.getplayer() end
if glob.newradarNum == nil then glob.newradarNum = 0 end
if gt == nil then gt = game.gettext end
if glob.NewRad == nil then glob.NewRad = {} end
if glob.newMineWatcher == nil then glob.newMineWatcher = 0 end
if glob.MineWatcher == nil then glob.MineWatcher = {} end
end)
game.onevent(defines.events.onbuiltentity, function(event)
if event.createdentity.name == "new-radar" then
glob.newradarNum = glob.newradarNum + 1
glob.NewRad[glob.newradarNum] = event.createdentity
if glob.newradarbuilt == false then
glob.newradarbuilt = true
glob.player.print("The warning radar is built... you will be warned of all pending attacks")
end
end
if event.createdentity.name == "new-minedet" then
glob.newMineWatcher = glob.newMineWatcher + 1
glob.player.print("Mine Watcher Activated - Number: ".. string.format("%d", glob.newMineWatcher))
glob.MineWatcher[glob.MineWatcher] = event.createdentity
if glob.newMineDet == false then
glob.newMineDet = true
end
end
end)
game.onevent(defines.events.onplayermineditem, function(event)
glob.player.print(event.itemstack.name)
if event.itemstack.name == "new-radar" then
if glob.newradarNum >= 1 then
table.remove(glob.NewRad, glob.newradarNum)
glob.newradarNum = glob.newradarNum - 1
if glob.newradarNum <= 0 then
glob.newradarbuilt = false
glob.player.print("Warning Systems disabled!")
end
end
end
if event.itemstack.name == "new-minedet" then
if glob.newMineWatcher >= 1 then
for k,v in pairs(glob.NewRad) do
if not v.isvalid() then
table.remove(glob.MineWatcher, k)
glob.newMineWatcher = glob.newMineWatcher - 1
if glob.newMineWatcher == 0 then
glob.newMineDet = false
end
end
end
if glob.newMineWatcher <= 0 then
glob.newMineDet = false
glob.player.print("No Mine Watchers remaining!")
end
end
end
end)
game.onevent(defines.events.ontick, function(event)
glob.tickCount1 = glob.tickCount1 + 1
glob.tickCount = glob.tickCount + 1
if glob.tickCount1 >= 100 then
glob.tickCount1 = 0
if glob.newradarbuilt then
for k,v in pairs(glob.NewRad) do
if not v.isvalid() then
table.remove(glob.NewRad, k)
glob.newradarNum = glob.newradarNum - 1
if glob.newradarNum == 0 then
glob.newradarbuilt = false
end
end
end
end
if glob.newMineDet then
glob.player.print("Num of Mine Dets: " ..string.format("%d", glob.newMineWatcher))
local TIron = 0
local TCoal = 0
local TCopper = 0
local TStone = 0
for a,b in pairs(glob.MineWatcher) do
if not b.isvalid() then
-- table.remove(glob.MineWatcher, a)
glob.newMineWatcher = glob.newMineWatcher - 1
if glob.newMineWatcher == 0 then
glob.newMineDet = false
end
else
local Drills, ResourceCount, pos, pose, posz, pos1, pose1, posz1 = {}, {}, {}, {}, {}, {}, {}, {}, {}
pos.x = b.position.x
pos.y = b.position.x
pose.x = pos.x - 5
pose.y = pos.y - 5
posz.x = pos.x + 5
posz.y = pos.y + 5
Drills = game.findentitiesfiltered{area = {pose, posz}, name = "basic-mining-drill"}
for f,g in pairs(Drills) do
glob.player.print("Here")
pos1.x = g.position.x
pos1.y = g.position.x
pose1.x = pos1.x - 2.49
pose1.y = pos1.y - 2.49
posz1.x = pos1.x + 2.49
posz1.y = pos1.y + 2.49
ResourceCount = game.findentitiesfiltered{area = {pose1, posz1}, type="resource"}
for w,e in pairs(ResourceCount) do
if e.name == "iron-ore" then TIron = TIron + e.amount end
if e.name == "coal" then TCoal = TCoal + e.amount end
if e.name == "copper-ore" then TCopper = TCopper + e.amount end
if e.name == "stone" then TStone = TStone + e.amount end
end
glob.player.print("Coal = " .. string.format("%d", TCoal) .. " Iron = " .. string.format("%d", TIron) .. " Copper = " .. string.format("%d", TCopper) .. " Stone = " .. string.format("%d", TStone))
end
--glob.player.print(serpent.block(ResourceCount))
--end
--glob.player.print("Coal = " .. string.format("%d,%d,%d,%d", TCoal, TIron, TCopper, TStone))
end
end
end
if glob.newradarbuilt then
if glob.tickCount >= 3600 then
local attackdata = remote.call("freeplay", "getattackdata")
local secondsleft = math.floor(attackdata.untilnextattack/ 60)
local atkcount = math.floor(attackdata.attackcount)
local minutes = math.floor((secondsleft)/60)
local seconds = math.floor(secondsleft - 60*minutes)
if minutes < 1 then
glob.player.print("WARNING ATTACK IS IMMINENT! SIZE OF FORCE EXPECTED: " .. string.format("%d", atkcount))
else
glob.player.print("Time until next attack wave: " .. string.format("%d:%02d", minutes, seconds))
end
end
glob.tickCount = 0
end
end
end)