| 1 |
-- ____ _ __ |
| 2 |
-- / __ )____ _____ | | / /___ ___________ |
| 3 |
-- / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
-- / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
-- /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
-- |
| 7 |
-- A futuristic real-time strategy game. |
| 8 |
-- This file is part of Bos Wars. |
| 9 |
-- |
| 10 |
-- broke.lua |
| 11 |
-- Define the AI that has the following philosophy: |
| 12 |
-- Start broke, i.e. this AI presumes you start with |
| 13 |
-- no energy, no magma, and a crew of only a few engineers. |
| 14 |
-- |
| 15 |
-- (c) Copyright 2011 by Michiel van der Wulp |
| 16 |
-- |
| 17 |
-- This program is free software; you can redistribute it and/or modify |
| 18 |
-- it under the terms of the GNU General Public License as published by |
| 19 |
-- the Free Software Foundation; either version 2 of the License, or |
| 20 |
-- (at your option) any later version. |
| 21 |
-- |
| 22 |
-- This program is distributed in the hope that it will be useful, |
| 23 |
-- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 |
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 |
-- GNU General Public License for more details. |
| 26 |
-- |
| 27 |
-- You should have received a copy of the GNU General Public License |
| 28 |
-- along with this program; if not, write to the Free Software |
| 29 |
-- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 30 |
-- |
| 31 |
-- $Id: $ |
| 32 |
-- |
| 33 |
|
| 34 |
-- What we registered in AiTypes. |
| 35 |
local this_ai_type |
| 36 |
|
| 37 |
-- Same as AiState[AiPlayer()]. Valid only during AiLoop. |
| 38 |
local state |
| 39 |
|
| 40 |
local function AiLoop(funcs) |
| 41 |
state = AiState[AiPlayer()] |
| 42 |
while (true) do |
| 43 |
local ret = funcs[state.loop_pos]() |
| 44 |
if (ret) then |
| 45 |
break |
| 46 |
end |
| 47 |
state.loop_pos = state.loop_pos + 1 |
| 48 |
end |
| 49 |
return true |
| 50 |
end |
| 51 |
|
| 52 |
local function LocalDebugPrint(text) |
| 53 |
-- DebugPrint(this_ai_type.Ident .. " player " .. AiPlayer() .. " " .. text) |
| 54 |
end |
| 55 |
|
| 56 |
local function InitAiScripts_broke() |
| 57 |
AiState[AiPlayer()] = { |
| 58 |
loop_pos = 1, |
| 59 |
loop_start = nil, |
| 60 |
build_order = nil, |
| 61 |
} |
| 62 |
end |
| 63 |
|
| 64 |
local function CheckPower() |
| 65 |
local nuke = UnitTypeByIdent("unit-nukepowerplant") |
| 66 |
local count = Players[AiPlayer()].UnitTypesCount[nuke.Slot] |
| 67 |
if (count ~= 0) then |
| 68 |
return nil |
| 69 |
end |
| 70 |
return "unit-powerplant" |
| 71 |
end |
| 72 |
|
| 73 |
local function GetBuildOrder() |
| 74 |
local order = {} |
| 75 |
|
| 76 |
if (not AiHotSpotExists()) then |
| 77 |
order[1] = CheckPower() |
| 78 |
order[2] = nil |
| 79 |
elseif (Players[AiPlayer()].MagmaStored < 300) then |
| 80 |
order[1] = "unit-magmapump" |
| 81 |
order[2] = CheckPower() |
| 82 |
else |
| 83 |
order[1] = CheckPower() |
| 84 |
order[2] = "unit-magmapump" |
| 85 |
end |
| 86 |
|
| 87 |
return order |
| 88 |
end |
| 89 |
|
| 90 |
local ai_funcs = { |
| 91 |
-- Build magma pump or power plant first depending on resources |
| 92 |
function() |
| 93 |
state.build_order = GetBuildOrder() |
| 94 |
return AiNeed(state.build_order[1]) |
| 95 |
end, |
| 96 |
function() |
| 97 |
LocalDebugPrint("waits for " .. state.build_order[1]); |
| 98 |
return false |
| 99 |
end, |
| 100 |
function() |
| 101 |
return AiWait(state.build_order[1]) |
| 102 |
end, |
| 103 |
function() |
| 104 |
if (state.build_order[2] ~= nil) then |
| 105 |
return AiNeed(state.build_order[2]) |
| 106 |
else |
| 107 |
return false |
| 108 |
end |
| 109 |
end, |
| 110 |
function() |
| 111 |
if (state.build_order[2] ~= nil) then |
| 112 |
LocalDebugPrint("waits for " .. state.build_order[2]); |
| 113 |
end |
| 114 |
return false |
| 115 |
end, |
| 116 |
function() |
| 117 |
if (state.build_order[2] ~= nil) then |
| 118 |
return AiWait(state.build_order[2]) |
| 119 |
else |
| 120 |
return false |
| 121 |
end |
| 122 |
end, |
| 123 |
|
| 124 |
function() return AiNeed("unit-nukepowerplant") end, |
| 125 |
function() |
| 126 |
LocalDebugPrint("waits for nuke powerplant."); |
| 127 |
return false |
| 128 |
end, |
| 129 |
function() return AiWait("unit-nukepowerplant") end, |
| 130 |
|
| 131 |
function() return AiSet("unit-vault", 1) end, |
| 132 |
function() return AiSet("unit-engineer", 5) end, |
| 133 |
|
| 134 |
function() |
| 135 |
if (AiHotSpotExists()) then |
| 136 |
return AiSet("unit-magmapump", 4) |
| 137 |
else |
| 138 |
return false -- do nothing |
| 139 |
end |
| 140 |
end, |
| 141 |
function() return AiSleep((SyncRand(500)+500)*GameSettings.Difficulty) end, |
| 142 |
function() return AiSet("unit-nukepowerplant", 5) end, |
| 143 |
function() return AiSleep((SyncRand(500)+500)*GameSettings.Difficulty) end, |
| 144 |
|
| 145 |
function() return AiSet("unit-camp", 2) end, |
| 146 |
|
| 147 |
-- Defense |
| 148 |
function() return AiForce(0, {"unit-assault", 4}) end, |
| 149 |
function() return AiWaitForce(0) end, |
| 150 |
function() |
| 151 |
LocalDebugPrint("force 0 finished."); |
| 152 |
return false |
| 153 |
end, |
| 154 |
function() return AiSet("unit-camp", 2) end, |
| 155 |
|
| 156 |
-- Attack wave |
| 157 |
function() return AiForce(1, {"unit-grenadier", 6}) end, |
| 158 |
function() return AiWaitForce(1) end, |
| 159 |
function() |
| 160 |
LocalDebugPrint("force 1 (6 grenadiers) finished."); |
| 161 |
return false |
| 162 |
end, |
| 163 |
function() return AiSleep((SyncRand(500)+500)*GameSettings.Difficulty) end, |
| 164 |
function() return AiAttackWithForce(1) end, |
| 165 |
function() |
| 166 |
LocalDebugPrint("force 1 (6 grenadiers) attacking."); |
| 167 |
return false; |
| 168 |
end, |
| 169 |
|
| 170 |
-- Bigger attack wave |
| 171 |
function() return AiForce(1, {"unit-assault", 8, "unit-grenadier", 2}) end, |
| 172 |
function() return AiWaitForce(1) end, |
| 173 |
|
| 174 |
function() |
| 175 |
LocalDebugPrint("force 1 (8 assaults, 2 grenadiers) finished."); |
| 176 |
return false; |
| 177 |
end, |
| 178 |
function() return AiAttackWithForce(1) end, |
| 179 |
|
| 180 |
function() return AiSet("unit-engineer", 8) end, |
| 181 |
function() return AiSet("unit-biggunturret", 1) end, |
| 182 |
function() return AiSet("unit-vfac", 1) end, |
| 183 |
function() return AiSet("unit-gturret", 2) end, |
| 184 |
|
| 185 |
function() |
| 186 |
LocalDebugPrint("waiting for vfac."); |
| 187 |
return false |
| 188 |
end, |
| 189 |
function() return AiWait("unit-vfac") end, |
| 190 |
|
| 191 |
-- Attack wave |
| 192 |
function() |
| 193 |
LocalDebugPrint("vfac finished."); |
| 194 |
return false |
| 195 |
end, |
| 196 |
function() return AiForce(1, {"unit-assault", SyncRand(5)+1, |
| 197 |
"unit-buggy", SyncRand(2)+1}) end, |
| 198 |
|
| 199 |
function() |
| 200 |
LocalDebugPrint("waiting for force 1 (assaults + buggies)."); |
| 201 |
return false |
| 202 |
end, |
| 203 |
function() return AiWaitForce(1) end, |
| 204 |
function() return AiSleep((SyncRand(500)+500)*GameSettings.Difficulty) end, |
| 205 |
function() return AiAttackWithForce(1) end, |
| 206 |
|
| 207 |
function() |
| 208 |
LocalDebugPrint("attacking with force 1."); |
| 209 |
return false; |
| 210 |
end, |
| 211 |
function() return AiSet("unit-biggunturret", 6) end, |
| 212 |
function() return AiSet("unit-vfac", 2) end, |
| 213 |
function() return AiSet("unit-gturret", 10) end, |
| 214 |
|
| 215 |
-- Start flying: |
| 216 |
function() |
| 217 |
LocalDebugPrint("more biggunturret, vfac, gturret."); |
| 218 |
return false |
| 219 |
end, |
| 220 |
function() return AiSet("unit-aircraftfactory", 2) end, |
| 221 |
|
| 222 |
-- ============================================================ |
| 223 |
|
| 224 |
function() |
| 225 |
LocalDebugPrint("is starting loop."); |
| 226 |
state.loop_start = state.loop_pos; |
| 227 |
return false |
| 228 |
end, |
| 229 |
|
| 230 |
-- Attack wave (land) |
| 231 |
function() return AiForce(1, {"unit-assault", 4}) end, |
| 232 |
|
| 233 |
-- Attack wave (air) |
| 234 |
function() return AiForce(2, {"unit-jet", 2}) end, |
| 235 |
|
| 236 |
-- Attack wave (vehicles) |
| 237 |
function() return AiForce(3, {"unit-rtank", 1, |
| 238 |
"unit-tank", 3}) end, |
| 239 |
|
| 240 |
-- Attack wave (mixed land) |
| 241 |
function() return AiForce(4, {"unit-buggy", 2, |
| 242 |
"unit-artil", 1}) end, |
| 243 |
|
| 244 |
-- Attack wave (air) |
| 245 |
function() return AiForce(5, {"unit-chopper", 1}) end, |
| 246 |
|
| 247 |
-- Attack wave (land, slow) |
| 248 |
function() return AiForce(6, {"unit-bazoo", 6}) end, |
| 249 |
|
| 250 |
-- Defense force 0 is sent to a building under attack |
| 251 |
function() return AiForce(0, {"unit-assault", 4, |
| 252 |
"unit-bazoo", 3, |
| 253 |
"unit-grenadier", 3}) end, |
| 254 |
|
| 255 |
-- wait 10 to 40 s: |
| 256 |
function() return AiSleep((SyncRand(900)+300) * GameSettings.Difficulty) end, |
| 257 |
|
| 258 |
function() |
| 259 |
local f |
| 260 |
for f=1,6 do |
| 261 |
if (AiCheckForce(f)) then |
| 262 |
LocalDebugPrint("is attacking with force " .. f .. "."); |
| 263 |
-- AiWaitForce(f); |
| 264 |
return AiAttackWithForce(f) |
| 265 |
end |
| 266 |
end |
| 267 |
return AiSleep((SyncRand(100)+40) * GameSettings.Difficulty) |
| 268 |
end, |
| 269 |
|
| 270 |
function() |
| 271 |
LocalDebugPrint("Reached the end of AI script and will loop"); |
| 272 |
state.loop_pos = state.loop_start - 1; -- AiLoop will immediately increment it. |
| 273 |
return false |
| 274 |
end, |
| 275 |
} |
| 276 |
|
| 277 |
local function AiBroke() |
| 278 |
LocalDebugPrint("Script position " .. AiState[AiPlayer()].loop_pos); |
| 279 |
return AiLoop(ai_funcs) |
| 280 |
end |
| 281 |
|
| 282 |
this_ai_type = { |
| 283 |
Ident = "ai-broke", |
| 284 |
Name = _("Broke"), |
| 285 |
Init = InitAiScripts_broke, |
| 286 |
EachSecond = AiBroke, |
| 287 |
} |
| 288 |
DefineAiType(this_ai_type) |