| 1 |
local SCRIPTED_ACTIONS = { { 'fighter', 'fighter', 'fighter', 'fighter', 'bomber' }, |
| 2 |
{ 'fighter', 'fighter', 'fighter', 'bomber' }, |
| 3 |
{ 'fighter', 'fighter', 'bomber' }, |
| 4 |
{ 'fighter', 'fighter', 'fighter', 'frigate' } } |
| 5 |
|
| 6 |
local action_index = 0 |
| 7 |
local frames_to_hold = 0 |
| 8 |
local accumulated_frames = 0 |
| 9 |
local frames_to_wait = 0 |
| 10 |
local script_index = 0 |
| 11 |
|
| 12 |
self.resources.harvest_rate = self.resources.harvest_rate * 0.8 |
| 13 |
|
| 14 |
local function next_script() |
| 15 |
script_index = math.ceil(math.random() * table.getn(SCRIPTED_ACTIONS)) |
| 16 |
|
| 17 |
end |
| 18 |
|
| 19 |
local function next_action() |
| 20 |
accumulated_frames = 0 |
| 21 |
|
| 22 |
action_index = action_index + 1 |
| 23 |
if action_index > table.getn(SCRIPTED_ACTIONS[script_index]) then |
| 24 |
action_index = 1 |
| 25 |
next_script() |
| 26 |
end |
| 27 |
|
| 28 |
|
| 29 |
frames_to_hold = (self.production.UNIT_COSTS[SCRIPTED_ACTIONS[script_index][action_index]] - self.production.UNIT_COSTS['fighter']) / self.production.BUILDING_SPEED + 2 + math.random() * 60 |
| 30 |
|
| 31 |
|
| 32 |
if not self.resources then return end |
| 33 |
local missing_resources = (self.production.UNIT_COSTS[SCRIPTED_ACTIONS[script_index][action_index]] - self.resources.amount) / self.resources.harvest_rate |
| 34 |
frames_to_wait = math.max(missing_resources + 1, 0) + math.random() * 60 |
| 35 |
|
| 36 |
|
| 37 |
end |
| 38 |
|
| 39 |
|
| 40 |
next_script() |
| 41 |
next_action() |
| 42 |
|
| 43 |
function update() |
| 44 |
|
| 45 |
local found_enemy = false |
| 46 |
for _, actor in ipairs(game.actors.get('factory')) do |
| 47 |
if actor.ship and actor.ship.player ~= self.ship.player then found_enemy = true end |
| 48 |
end |
| 49 |
if not found_enemy then |
| 50 |
self.production.button_held = false |
| 51 |
return |
| 52 |
end |
| 53 |
|
| 54 |
accumulated_frames = accumulated_frames + 1 |
| 55 |
|
| 56 |
self.production.button_held = accumulated_frames > frames_to_wait and accumulated_frames < frames_to_hold + frames_to_wait |
| 57 |
if accumulated_frames > frames_to_hold + frames_to_wait then |
| 58 |
next_action() |
| 59 |
end |
| 60 |
end |