Debian Codegraph @

scripts/ais/attackdefense.lua

raw
1 -- ____ _ __
2 -- / __ )____ _____ | | / /___ ___________
3 -- / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 -- / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 -- /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 --
7 -- A futuristic real-time strategy game.
8 -- This file is part of Bos Wars.
9 --
10 -- attackdefense.lua - Define a hard to beat AI
11 -- that needs many resources and enough space,
12 -- takes care of a good defense,
13 -- and attacks with everything available.
14 --
15 -- (c) Copyright 2009-2010 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: attackdefense.lua $
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_attackdefense()
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 function() AiDebug(false) return false end,
92
93 -- Build magma pump or power plant first depending on resources
94 function()
95 state.build_order = GetBuildOrder()
96 if (state.build_order[1] ~= nil) then
97 return AiNeed(state.build_order[1])
98 else
99 return false
100 end
101 end,
102 function()
103 if (state.build_order[1] ~= nil) then
104 return AiWait(state.build_order[1])
105 else
106 return false
107 end
108 end,
109 function()
110 if (state.build_order[2] ~= nil) then
111 return AiNeed(state.build_order[2])
112 else
113 return false
114 end
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 AiSet("unit-engineer", 2) end,
125 function() return AiNeed("unit-vault") end,
126 function() return AiNeed("unit-magmapump") end,
127 function() return AiWait("unit-vault") end,
128
129 function() return AiNeed("unit-camp") end,
130 function() return AiWait("unit-camp") end,
131 function() return AiSet("unit-engineer", 3) end,
132
133 -- Defense
134 -- force 0 is sent to a building under attack
135 function() return AiForce(0, {"unit-assault", 2}) end,
136 function() return AiWaitForce(0) end,
137
138 function() return AiNeed("unit-powerplant") end,
139 function() return AiNeed("unit-magmapump") end,
140
141 -- First contact - Small Attack wave
142 function() return AiForce(1, {"unit-assault", 4}) end,
143 function() return AiWaitForce(1) end,
144 function() return AiNeed("unit-magmapump") end,
145 -- Since this 1st force is very small, it comes very soon.
146 -- Hence, we wait a bit longer to make it easier for beginners
147 -- And we make it random, so that when there are multiple opponents,
148 -- they do not all come at the same time
149 function() return AiSleep((SyncRand(1000)+400) * GameSettings.Difficulty) end,
150 function() return AiAttackWithForce(1) end,
151
152 -- Defense
153 function() return AiNeed("unit-vfac") end,
154 function() return AiNeed("unit-gturret") end,
155 function() return AiSleep((SyncRand(200)+100)*GameSettings.Difficulty) end,
156
157 -- Attack wave
158 function() return AiForce(2, {"unit-assault", 5, "unit-rtank", 3}) end,
159 function() return AiWaitForce(2) end,
160 function() return AiSleep((SyncRand(120)+50)*GameSettings.Difficulty) end,
161 function() return AiAttackWithForce(2) end,
162
163 function() return AiNeed("unit-powerplant") end,
164 function() return AiWait("unit-powerplant") end,
165 function() return AiNeed("unit-magmapump") end,
166 function() return AiWait("unit-magmapump") end,
167 function() return AiNeed("unit-biggunturret") end,
168 function() return AiSleep((SyncRand(100)+40)*GameSettings.Difficulty) end,
169
170 -- Defense
171 function() return AiNeed("unit-nukepowerplant") end,
172 function() return AiNeed("unit-harvester") end,
173 function() return AiForce(0, {"unit-assault", 2,
174 "unit-bazoo", 1,
175 "unit-buggy", 1}) end,
176 function() return AiWaitForce(0) end,
177 function() return AiSleep((SyncRand(100)+40)*GameSettings.Difficulty) end,
178
179 -- Bigger attack wave
180 function() return AiNeed("unit-magmapump") end,
181 function() return AiForce(3, {"unit-assault", 3,
182 "unit-grenadier", 3,
183 "unit-tank", 2,
184 "unit-rtank", 1}) end,
185 function() return AiWaitForce(3) end,
186 function() return AiSleep((SyncRand(30)+50)*GameSettings.Difficulty) end,
187 function() return AiAttackWithForce(3) end,
188
189 -- Defense
190 function() return AiSet("unit-engineer", 6) end,
191 function() return AiSet("unit-gturret", 4) end,
192 function() return AiSet("unit-camp", 2) end,
193 function() return AiSet("unit-biggunturret", 2) end,
194 function() return AiSet("unit-magmapump", 5) end,
195 function() return AiSet("unit-vfac", 2) end,
196 function() return AiNeed("unit-aircraftfactory") end,
197 -- function() return AiWait("unit-aircraftfactory") end,
198
199 -- ============================================================
200
201 -- Attack wave
202 function()
203 LocalDebugPrint("is starting loop.");
204 state.loop_start = state.loop_pos;
205 LocalDebugPrint("is making 6 bazoos.");
206 return AiForce(4, {"unit-bazoo", 6})
207 end,
208 function() return AiWaitForce(4) end,
209 function() return AiSleep((SyncRand(100)+40)*GameSettings.Difficulty) end,
210 function()
211 LocalDebugPrint("is attacking with force 4 (6 bazoos).");
212 return AiAttackWithForce(4)
213 end,
214
215 function() return AiForce(4, {"unit-grenadier", 7}) end,
216 function() return AiWaitForce(4) end,
217 function() return AiSleep((SyncRand(100)+40)*GameSettings.Difficulty) end,
218 function() return AiAttackWithForce(4) end,
219
220 function() return AiForce(4, {"unit-tank", 3}) end,
221 function() return AiWaitForce(4) end,
222 function() return AiSleep((SyncRand(100)+40)*GameSettings.Difficulty) end,
223 function() return AiAttackWithForce(4) end,
224
225 function() return AiForce(4, {"unit-jet", 2}) end,
226 function() return AiWaitForce(4) end,
227 function() return AiSleep((SyncRand(100)+40)*GameSettings.Difficulty) end,
228 function() return AiAttackWithForce(4) end,
229
230 function() return AiForce(4, {"unit-rtank", 2}) end,
231 function() return AiWaitForce(4) end,
232 function() return AiSleep((SyncRand(100)+40)*GameSettings.Difficulty) end,
233 function() return AiAttackWithForce(4) end,
234
235 -- Defense (resources and static guns)
236 function() return AiSet("unit-nukepowerplant", 2) end,
237 function() return AiSet("unit-gturret", 5) end,
238 function() return AiSet("unit-vfac", 2) end,
239 function() return AiSet("unit-camp", 2) end,
240 function() return AiSet("unit-engineer", 8) end,
241 function() return AiSet("unit-magmapump",7) end,
242 function() return AiSet("unit-biggunturret", 3) end,
243
244 -- Defense force 0 is sent to a building under attack
245 function() return AiForce(0, {"unit-assault", 3,
246 "unit-bazoo", 2,
247 "unit-buggy", 2,
248 "unit-jet", 1,
249 "unit-tank", 1}) end,
250
251 -- Attack wave (for buildings with weak defense)
252 function() return AiForce(5, {"unit-assault", 4,
253 "unit-bazoo", 3,
254 "unit-rtank", 1,
255 "unit-jet", 1,
256 "unit-bomber", 1,
257 "unit-tank", 2}) end,
258 function() return AiWaitForce(5) end,
259 function() return AiSleep((SyncRand(100)+40)*GameSettings.Difficulty) end,
260 function() return AiAttackWithForce(5) end,
261
262 -- Attack wave (slow units)
263 function() return AiForce(6, {"unit-bazoo", 12,
264 "unit-artil", 2,
265 "unit-chopper", 2}) end,
266 function() return AiWaitForce(6) end,
267 function() return AiSleep((SyncRand(100)+40)*GameSettings.Difficulty) end,
268 function() return AiAttackWithForce(6) end,
269
270 -- Attack wave (fast units)
271 function() return AiForce(7, {"unit-grenadier", 6,
272 "unit-buggy", 2,
273 "unit-heli", 1,
274 "unit-jet", 1}) end,
275 function() return AiWaitForce(7) end,
276 function() return AiSleep((SyncRand(100)+40)*GameSettings.Difficulty) end,
277 function() return AiAttackWithForce(7) end,
278
279 -- Defense
280 function() return AiSet("unit-biggunturret", 6) end,
281 -- Let's not build too many cannons, since the
282 -- AI places them mostly in useless locations:
283 function() return AiSet("unit-cannon", 1) end,
284 -- A radar is needed for the cannon to see its targets:
285 function() return AiSet("unit-radar", 1) end,
286
287 function()
288 LocalDebugPrint("Reached the end of AI script and will loop");
289 state.loop_pos = state.loop_start - 1; -- AiLoop will immediately increment it.
290 return false
291 end,
292 }
293
294 local function AiAttackDefense()
295 LocalDebugPrint("Script position " .. AiState[AiPlayer()].loop_pos);
296 return AiLoop(ai_funcs)
297 end
298
299 this_ai_type = {
300 Ident = "ai-attackdefense",
301 Name = _("Attack/Defense"),
302 Init = InitAiScripts_attackdefense,
303 EachSecond = AiAttackDefense,
304 }
305 DefineAiType(this_ai_type)