1require "common" 2 3 4BombardBehaviour = class(Behaviour) 5 6local DebugEnabled = false 7 8local function EchoDebug(inStr) 9 if DebugEnabled then 10 game:SendToConsole("BombardBehaviour: " .. inStr) 11 end 12end 13 14local CMD_ATTACK = 20 15 16local valueThreatThreshold = 1600 -- anything above this level of value+threat will be shot at even if the cannon isn't idle 17 18function BombardBehaviour:Init() 19 self.lastFireFrame = 0 20 local unit = self.unit:Internal() 21 self.position = unit:GetPosition() 22 self.range = unitTable[unit:Name()].groundRange 23end 24 25function BombardBehaviour:UnitCreated(unit) 26 27end 28 29function BombardBehaviour:Fire() 30 if self.target ~= nil then 31 EchoDebug("fire") 32 local floats = api.vectorFloat() 33 -- populate with x, y, z of the position 34 floats:push_back(self.target.x) 35 floats:push_back(self.target.y) 36 floats:push_back(self.target.z) 37 self.unit:Internal():ExecuteCustomCommand(CMD_ATTACK, floats) 38 self.lastFireFrame = game:Frame() 39 end 40end 41 42function BombardBehaviour:UnitIdle(unit) 43 if self.active then 44 local f = game:Frame() 45 if self.lastFireFrame == 0 or f > self.lastFireFrame + 300 then 46 EchoDebug("idle") 47 self:Fire() 48 end 49 end 50end 51 52function BombardBehaviour:Update() 53 if self.active then 54 local f = game:Frame() 55 if self.lastFireFrame == 0 or f > self.lastFireFrame + 900 then 56 EchoDebug("retarget") 57 local bestCell, valueThreat = ai.targethandler:GetBestBombardCell(self.position, self.range) 58 if bestCell ~= nil then 59 self.target = bestCell.pos 60 if valueThreat > valueThreatThreshold then 61 EchoDebug("high priority target: " .. valueThreat) 62 self:Fire() 63 end 64 end 65 end 66 end 67end 68 69function BombardBehaviour:Activate() 70 self.active = true 71end 72 73function BombardBehaviour:Deactivate() 74 self.active = false 75end 76 77function BombardBehaviour:Priority() 78 return 100 79end 80 81function BombardBehaviour:UnitDead(unit) 82 83end 84