Page 1 of 1
Lua Gadget: Detect what a builder was building when it died?
Posted: 11 Jul 2011, 20:58
by yanom
So I've been helping out with the Lua AI for Conflict Terra. And I need a way to detect what (if anything) a constructor unit was building when it dies (perhaps in the gadget:unitDestroyed function?), so that it's job can be rescheduled to another builder.
Re: Lua Gadget: Detect what a builder was building when it died?
Posted: 11 Jul 2011, 21:03
by knorke
unitID_that_unit_is_building = Spring.GetUnitIsBuilding (unitID)
Re: Lua Gadget: Detect what a builder was building when it died?
Posted: 11 Jul 2011, 21:18
by yanom
knorke wrote:unitID_that_unit_is_building = Spring.GetUnitIsBuilding (unitID)
so, unitID_that_unit_is_building is the ID of the unit (still in nanoframe ) that (fully alive) unit unitID is building , right?
Does this still count if the builder is walking to the buildsite and has not yet started on the structure?
Re: Lua Gadget: Detect what a builder was building when it died?
Posted: 11 Jul 2011, 21:21
by FLOZi
yanom wrote:knorke wrote:unitID_that_unit_is_building = Spring.GetUnitIsBuilding (unitID)
so, unitID_that_unit_is_building is the ID of the unit (still in nanoframe ) that (fully alive) unit unitID is building , right?
Yes.
Does this still count if the builder is walking to the buildsite and has not yet started on the structure?
No. For that, you'll have to grab the command queue of the destroyed unit, and loop over it looking for build commands.
Re: Lua Gadget: Detect what a builder was building when it died?
Posted: 11 Jul 2011, 21:55
by zwzsg
Since it is for an AI, you might find it simpler to store in table when the order is given, and check that table when it dies.
Like:
local BuilderOnMission={} -- table indexed by unitID
local DoItAgain={} -- table indexed by 1...n
...
function gadget:GameFrame(n)
...
Spring.GiveOrderToUnit(...
BuilderOnMission[UnitID]={some data that will help you know what and where it was building, like the cmd id and cmd parameters}
...
for Index,OrderToGiveAgain in ipairs(DoItAgain) do
-- type here some code to find a suitable builder and reissue order
...
table.remove(OrderToGiveAgain,index)
...
end
end
function gadget:UnitDestroyed(...
if BuilderOnMission[UnitID] then
table.insert(DoItAgain)=BuilderOnMission[UnitID]
BuilderOnMission[UnitID]=nil
end
end
Re: Lua Gadget: Detect what a builder was building when it died?
Posted: 11 Jul 2011, 22:26
by knorke
yes, what zwzsg said.
also because GetUnitIsBuilding will probally fail on morphing/cloneing.
Re: Lua Gadget: Detect what a builder was building when it died?
Posted: 11 Jul 2011, 22:27
by yanom
FLOZi wrote:
No. For that, you'll have to grab the command queue of the destroyed unit, and loop over it looking for build commands.
how do I do that?
Re: Lua Gadget: Detect what a builder was building when it died?
Posted: 11 Jul 2011, 22:54
by FLOZi
You don't. Do it the way zwzsg suggested, it's a much better implementation.
Re: Lua Gadget: Detect what a builder was building when it died?
Posted: 12 Jul 2011, 00:51
by yanom
... this is beyond me.
I've never programmed much before.
But I'll keep trying...