1require "common"
2
3function IsBomber(unit)
4	local tmpName = unit:Internal():Name()
5	return (bomberList[tmpName] or 0) > 0
6end
7
8BomberBehaviour = class(Behaviour)
9
10function BomberBehaviour:Init()
11	self.lastOrderFrame = game:Frame()
12	local mtype, network = ai.maphandler:MobilityOfUnit(self.unit:Internal())
13	self.mtype = mtype
14	self.name = self.unit:Internal():Name()
15	if unitTable[self.name].submergedRange > 0 then
16		self.weapon = "torpedo"
17	else
18		self.weapon = "bomb"
19	end
20end
21
22function BomberBehaviour:UnitBuilt(unit)
23	if unit.engineID == self.unit.engineID then
24		self.bombing = false
25		self.targetpos = nil
26		ai.bomberhandler:AddRecruit(self)
27	end
28end
29
30function BomberBehaviour:UnitDead(unit)
31	if unit.engineID == self.unit.engineID then
32		-- game:SendToConsole("bomber " .. self.name .. " died")
33		ai.bomberhandler:RemoveRecruit(self)
34		ai.bomberhandler:NeedMore()
35		-- notify the command that area is too hot
36		if self.targetpos then
37			ai.targethandler:AddBadPosition(self.targetpos, self.mtype)
38		end
39	end
40end
41
42function BomberBehaviour:UnitIdle(unit)
43	if unit.engineID == self.unit.engineID then
44		self.bombing = false
45		self.targetpos = nil
46	end
47end
48
49function BomberBehaviour:BombPosition(position)
50	local floats = api.vectorFloat()
51	-- populate with x, y, z of the position
52	floats:push_back(position.x)
53	floats:push_back(position.y)
54	floats:push_back(position.z)
55	self.unit:Internal():ExecuteCustomCommand(CMD_ATTACK, floats)
56end
57
58function BomberBehaviour:BombTarget(target)
59	if target ~= nil then
60		local pos = target.position
61		if pos ~= nil then
62			self.target = target.unitID
63			self.bombing = true
64			self.lastOrderFrame = game:Frame()
65			if self.active then
66				self:BombPosition(pos)
67				self.targetpos = pos
68			end
69		else
70			self.unit:ElectBehaviour()
71		end
72	else
73		self.bombing = false
74		self.unit:ElectBehaviour()
75	end
76end
77
78function BomberBehaviour:Priority()
79	if not self.bombing then
80		return 0
81	else
82		return 100
83	end
84end
85
86function BomberBehaviour:Activate()
87	self.active = true
88	if self.target then
89		self.lastOrderFrame = game:Frame()
90		CustomCommand(self.unit:Internal(), CMD_ATTACK, {self.target})
91		self.target = nil
92		self.targetpos = nil
93	else
94		ai.bomberhandler:AddRecruit(self)
95		self.targetpos = nil
96	end
97end
98
99function BomberBehaviour:Deactivate()
100	self.active = false
101end
102
103function BomberBehaviour:Update()
104	-- retargeting trigger
105	-- if the unit is already in recruit lists, do nothing
106	if ai.bomberhandler:IsRecruit(self) then
107		return
108	end
109	local tmpFrame = game:Frame()
110	if (self.lastOrderFrame or 0) + 900 < tmpFrame then
111		ai.bomberhandler:AddRecruit(self)
112		self.targetpos = nil
113	end
114end