1require "common"
2
3
4NukeBehaviour = class(Behaviour)
5
6local DebugEnabled = false
7
8local function EchoDebug(inStr)
9	if DebugEnabled then
10		game:SendToConsole("NukeBehaviour: " .. inStr)
11	end
12end
13
14local CMD_STOCKPILE = 100
15local CMD_ATTACK = 20
16
17function NukeBehaviour:Init()
18	local uname = self.unit:Internal():Name()
19	if uname == "armemp" then
20		self.stunning = true
21	elseif uname == "cortron" then
22		self.tactical = true
23	end
24	self.stockpileTime = nukeList[uname]
25	self.position = self.unit:Internal():GetPosition()
26	self.range = unitTable[uname].groundRange
27    self.lastStockpileFrame = 0
28    self.lastLaunchFrame = 0
29    self.gotTarget = false
30    self.finished = false
31end
32
33function NukeBehaviour:UnitBuilt(unit)
34	if unit.engineID == self.unit.engineID then
35		self.finished = true
36	end
37end
38
39function NukeBehaviour:UnitCreated(unit)
40
41end
42
43function NukeBehaviour:UnitIdle(unit)
44
45end
46
47function NukeBehaviour:Update()
48	if not self.active then return end
49
50	local f = game:Frame()
51
52	if self.finished then
53		if f > self.lastLaunchFrame + 100 then
54			self.gotTarget = false
55			if ai.needNukes and ai.canNuke then
56				local bestCell
57				if self.tactical then
58					bestCell = ai.targethandler:GetBestBombardCell(self.position, self.range, 2500)
59				elseif self.stunning then
60					bestCell = ai.targethandler:GetBestBombardCell(self.position, self.range, 3000) -- only targets threats
61				else
62					bestCell = ai.targethandler:GetBestNukeCell()
63				end
64				if bestCell ~= nil then
65					local position = bestCell.pos
66					local floats = api.vectorFloat()
67					-- populate with x, y, z of the position
68					floats:push_back(position.x)
69					floats:push_back(position.y)
70					floats:push_back(position.z)
71					self.unit:Internal():ExecuteCustomCommand(CMD_ATTACK, floats)
72					self.gotTarget = true
73					EchoDebug("got target")
74				end
75			end
76			self.lastLaunchFrame = f
77		end
78		if self.gotTarget then
79			if self.lastStockpileFrame == 0 or f > self.lastStockpileFrame + self.stockpileTime then
80				local floats = api.vectorFloat()
81				floats:push_back(1)
82				self.unit:Internal():ExecuteCustomCommand(CMD_STOCKPILE, floats)
83				self.lastStockpileFrame = f
84			end
85		end
86	end
87end
88
89function NukeBehaviour:Activate()
90	self.active = true
91end
92
93function NukeBehaviour:Deactivate()
94	self.active = false
95end
96
97function NukeBehaviour:Priority()
98	return 100
99end
100
101function NukeBehaviour:UnitDead(unit)
102
103end
104