1require "attackers"
2
3function IsAttacker(unit)
4	for i,name in ipairs(attackerlist) do
5		if name == unit:Internal():Name() then
6			return true
7		end
8	end
9	return false
10end
11
12AttackerBehaviour = class(Behaviour)
13
14function AttackerBehaviour:Init()
15	--game:SendToConsole("attacker!")
16end
17
18function AttackerBehaviour:UnitBuilt(unit)
19	if unit.engineID == self.unit.engineID then
20		self:SetInitialState()
21		self.attacking = false
22		ai.attackhandler:AddRecruit(self)
23	end
24end
25
26
27function AttackerBehaviour:UnitDead(unit)
28	if unit.engineID == self.unit.engineID then
29		ai.attackhandler:RemoveRecruit(self)
30	end
31end
32
33function AttackerBehaviour:UnitIdle(unit)
34	if unit.engineID == self.unit.engineID then
35		self.attacking = false
36		ai.attackhandler:AddRecruit(self)
37	end
38end
39
40function AttackerBehaviour:AttackCell(cell)
41	p = api.Position()
42	p.x = cell.posx
43	p.z = cell.posz
44	p.y = 0
45	self.target = p
46	self.attacking = true
47	if self.active then
48		self.unit:Internal():MoveAndFire(self.target)
49	else
50		self.unit:ElectBehaviour()
51	end
52end
53
54function AttackerBehaviour:Priority()
55	if not self.attacking then
56		return 0
57	else
58		return 100
59	end
60end
61
62function AttackerBehaviour:Activate()
63	self.active = true
64	if self.target then
65		self.unit:Internal():MoveAndFire(self.target)
66		self.target = nil
67	else
68		--ai.attackhandler:AddRecruit(self)
69	end
70end
71
72function AttackerBehaviour:SetInitialState()
73	local CMD_FIRE_STATE = 45
74	local CMD_MOVE_STATE = 50
75	local CMD_RETREAT = 34223
76	local thisUnit = self.unit
77	if thisUnit then
78		local floats = api.vectorFloat()
79		floats:push_back(2)
80		thisUnit:Internal():ExecuteCustomCommand(CMD_MOVE_STATE, floats)
81		thisUnit:Internal():ExecuteCustomCommand(CMD_FIRE_STATE, floats)
82
83		local isHeavy = thisUnit:Internal():GetMaxHealth() >= 1250
84		if (isHeavy) then
85			thisUnit:Internal():ExecuteCustomCommand(CMD_RETREAT, floats)
86		end
87	end
88end
89