Page 1 of 1

findentitiesfiltered table

Posted: Sun Jul 07, 2013 1:59 am
by midnight109
how can I get like a layout as to how the returned data is set? It says on the wiki it makes a table, but how is the table stored, names, ect?
I am trying to use

Code: Select all

local ResourceCounter = game.findentitiesfiltered{area = {-10, -10}, {10, 10}, type="resource"}
But crashes out saying bad argument #1 table expected, got number

I am trying to make a simple table, with 4 entries, one for coal, iron, copper, and stone, and a means of saying how much is in the given area for each one

Re: findentitiesfiltered table

Posted: Sun Jul 07, 2013 2:11 am
by FreeER
midnight109 wrote:But crashes out saying bad argument #1 table expected, got number
It expects a table of tables lol, like so

Code: Select all

local ResourceCounter = game.findentitiesfiltered{area = {{-10, -10}, {10, 10}}, type="resource"}
edit: I just updated the wiki example so that it is correct

Re: findentitiesfiltered table

Posted: Sun Jul 07, 2013 6:42 pm
by midnight109
Well, code Almost works, kinda unknown how its doing these areas <.< tell it scan a box it seems to scan 2 blocks :P
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)

Re: findentitiesfiltered table

Posted: Sun Jul 07, 2013 7:39 pm
by FreeER
hm, I honestly can not see anything that would prevent this code from working...hopefully someone else will notice something (or slpwnd/kovarex will come up and say that one of the functions is semi-broken in this release lol)

Re: findentitiesfiltered table

Posted: Sun Jul 07, 2013 8:03 pm
by drs9999
I found this two mistakes:

Code: Select all

               pos.x = b.position.x
               pos.y = b.position.x
and

Code: Select all

pos1.x = g.position.x
                  pos1.y = g.position.x
Maybe that were the only problems, but it is hard to say, it might be easier to backtrack issues when you upload your mod's folder so we can test it

Re: findentitiesfiltered table

Posted: Sun Jul 07, 2013 10:16 pm
by rk84
Some tips:

-variable naming. In my eyes some names are too alike and its not easy to see typos in your script. I recommend checking all lines with word glob or new. I can tell that mining entities can possibly mess up your entity reference tables.

-Instead saving player reference to glob, you can use "game.player", for ticks you can use "game.tick/event.tick" and to get length of array table use # or getn().
Example.

Code: Select all

if #game.findentitiesfiltered{area=area, type="resource"} > 0 then
  --has resource
end
-Just a heads up but using two findentitiesfiltered and triple loop in ontick-event might be performance killer in long run.

Re: findentitiesfiltered table

Posted: Mon Jul 08, 2013 1:20 am
by midnight109
https://www.dropbox.com/s/owuianwjxbugrbj/ralmodpk1.rar

As for the var names I plan on changing everything when I stop rewriting the whole thing :P
One of my problems with programming has always been if its messed up code and not in a rush i usually just delete the whole section and start over trying to improve while fixing so i usually use generic temp variable names like a, b, f, g, pos pos1 just as quick refrences for the immediet section then when it works >.> key work when it works :P I usually tear it part by part rename thing parse the lines, see if I can move this or that into seperate areas or functions to clean it up

In the end when I get it working, I am hopeing to have little code in the game ticks section, i preffer to keep most peices in thier own area so the event tick and other events just watches counts ect another function runs the part or area so less buggy when adding on >.> but im to used to c++ im usually going back over everything i code in lua and finding myself with tons of ; at the end of everything im also good at finding using multi commands per line for some if statments :P like if a = b then do this; do that; do this over here; end as long as small commands to save line space and make things together :P

This version has some debugs so everything you do it should tell you including everything you mine :P