Debian Codegraph @

scripts/ais/spacious.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 -- spacious.lua
11 -- Define a AI that needs not too many resources, for areas with lots of space.
12 -- It attacks with fast equipment, also through the air.
13 --
14 -- (c) Copyright 2010 by Michiel van der Wulp
15 --
16 -- This program is free software; you can redistribute it and/or modify
17 -- it under the terms of the GNU General Public License as published by
18 -- the Free Software Foundation; either version 2 of the License, or
19 -- (at your option) any later version.
20 --
21 -- This program is distributed in the hope that it will be useful,
22 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
23 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 -- GNU General Public License for more details.
25 --
26 -- You should have received a copy of the GNU General Public License
27 -- along with this program; if not, write to the Free Software
28 -- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 --
30 -- $Id: spacious.lua $
31 --
32
33 -- What we registered in AiTypes.
34 local this_ai_type
35
36 -- Same as AiState[AiPlayer()]. Valid only during AiLoop.
37 local state
38
39 local function AiLoop(funcs)
40 state = AiState[AiPlayer()]
41 while (true) do
42 local ret = funcs[state.loop_pos]()
43 if (ret) then
44 break
45 end
46 state.loop_pos = state.loop_pos + 1
47 end
48 return true
49 end
50
51 local function LocalDebugPrint(text)
52 -- DebugPrint(this_ai_type.Ident .. " player " .. AiPlayer() .. " " .. text)
53 end
54
55 local function InitAiScripts_spacious()
56 AiState[AiPlayer()] = {
57 loop_pos = 1,
58 loop_start = nil,
59 }
60 end
61
62 local ai_funcs = {
63 -- If we loose an engineer, we want to keep the number of engineers on 3:
64 function() return AiSet("unit-engineer", 3) end,
65
66 -- Build magma pump and power plant
67 function() return AiNeed("unit-magmapump") end,
68 function() return AiSleep((SyncRand(300)+10) * GameSettings.Difficulty) end,
69 function() return AiNeed("unit-powerplant") end,
70 function() return AiSleep((SyncRand(300)+20) * GameSettings.Difficulty) end,
71 -- and build a camp, so that we can attack a.s.a.p.:
72 function() return AiNeed("unit-camp") end,
73
74 function() return AiWait("unit-magmapump") end,
75 function() return AiWait("unit-powerplant") end,
76 function() return AiWait("unit-camp") end,
77
78 -- First contact - Very Small Attack wave
79 function()
80 LocalDebugPrint("now has the basic resources to start building an attack force.");
81 return AiForce(1, {"unit-assault", 2})
82 end,
83 -- In the meanwhile, the engineers can build a second magnapump:
84 function() return AiSet("unit-magmapump", 2) end,
85 -- wait a bit, then build a nukepowerplant:
86 function() return AiSleep((SyncRand(300)+30) * GameSettings.Difficulty) end,
87 function() return AiNeed("unit-nukepowerplant") end,
88 -- now wait for the first attackwave to be ready:
89 function() return AiWaitForce(1) end,
90 -- We wait a bit to make it easier for beginners.
91 -- And we make it random, so that when there are multiple opponents,
92 -- they do not all come at the same time.
93 function() return AiSleep((SyncRand(700)+300) * GameSettings.Difficulty) end,
94 function()
95 LocalDebugPrint("is attacking with force 1a.");
96 return AiAttackWithForce(1)
97 end,
98
99 -- Get more resources and a second camp:
100 function() return AiSet("unit-camp", 2) end,
101 function() return AiSet("unit-powerplant", 2) end,
102 function() return AiSet("unit-magmapump", 3) end,
103
104 function() return AiForce(1, {"unit-assault", 3}) end,
105 function() return AiWaitForce(1) end,
106 function()
107 LocalDebugPrint("is attacking with force 1b.");
108 return AiAttackWithForce(1)
109 end,
110
111 function() return AiSet("unit-nukepowerplant", 2) end,
112
113 -- force 0 is sent to a building under attack
114 function() return AiForce(0, {"unit-assault", 4}) end,
115
116 -- We need a few more engineers, so that they are spread out better:
117 function() return AiSet("unit-engineer", 6) end,
118 function() return AiNeed("unit-vfac") end,
119 function() return AiSet("unit-gturret", 2) end,
120
121 -- Small Attack wave
122 function() return AiForce(1, {"unit-assault", 4}) end,
123 function() return AiWaitForce(1) end,
124 function() return AiSet("unit-magmapump", 5) end,
125
126 function() return AiSleep((SyncRand(600)+400) * GameSettings.Difficulty) end,
127 function()
128 LocalDebugPrint("is attacking with force 1c.");
129 return AiAttackWithForce(1)
130 end,
131
132 -- Attack wave
133 function() return AiSleep((SyncRand(600)+400) * GameSettings.Difficulty) end,
134 function() return AiForce(4, {"unit-buggy", 2, "unit-rtank", 2}) end,
135 function() return AiWaitForce(4) end,
136 function() return AiSleep((SyncRand(600)+400) * GameSettings.Difficulty) end,
137 function()
138 LocalDebugPrint("is attacking with force 1d.");
139 return AiAttackWithForce(4)
140 end,
141
142 function() return AiSet("unit-nukepowerplant", 3) end,
143 function() return AiSet("unit-magmapump", 7) end,
144 function() return AiNeed("unit-biggunturret") end,
145 function() return AiSleep((SyncRand(600)+400) * GameSettings.Difficulty) end,
146
147 -- Defense
148 function() return AiSet("unit-nukepowerplant", 4) end,
149 function() return AiNeed("unit-harvester") end,
150 function() return AiForce(0, {"unit-assault", 4,
151 "unit-bazoo", 2,
152 "unit-grenadier", 2}) end,
153
154 function() return AiSet("unit-magmapump", 9) end,
155
156 -- Build some more defense and start flying:
157 function() return AiNeed("unit-aircraftfactory") end,
158 function() return AiSet("unit-gturret", 3) end,
159 function() return AiSet("unit-biggunturret", 2) end,
160 function() return AiSet("unit-vfac", 2) end,
161 -- We want to fly, so let's wait for an aircraftfactory to be built:
162 function() return AiWait("unit-aircraftfactory") end,
163 function() return AiSet("unit-aircraftfactory", 2) end,
164
165 -- ============================================================
166
167 function()
168 LocalDebugPrint("is starting loop.");
169 state.loop_start = state.loop_pos;
170 return false
171 end,
172
173 -- Attack wave (air)
174 function() return AiForce(2, {"unit-jet", 2}) end,
175
176
177 -- Defense force 0 is sent to a building under attack
178 function() return AiForce(0, {"unit-assault", 4,
179 "unit-bazoo", 3,
180 "unit-grenadier", 3}) end,
181
182 -- Attack wave (air)
183 function() return AiForce(5, {"unit-chopper", 1}) end,
184
185 -- Attack wave (land)
186 function() return AiForce(1, {"unit-assault", 4}) end,
187
188 -- Attack wave (vehicles)
189 function() return AiForce(3, {"unit-rtank", 1,
190 "unit-tank", 3}) end,
191
192 -- Attack wave (mixed land)
193 function() return AiForce(4, {"unit-buggy", 2,
194 "unit-rtank", 2}) end,
195
196 -- Attack wave (land)
197 function() return AiForce(6, {"unit-bazoo", 6}) end,
198
199 -- wait 10 to 40 s:
200 function() return AiSleep((SyncRand(900)+300) * GameSettings.Difficulty) end,
201
202 function()
203 local f
204 for f=1,6 do
205 if (AiCheckForce(f)) then
206 LocalDebugPrint("is attacking with force " .. f .. ".");
207 AiWaitForce(f);
208 return AiAttackWithForce(f)
209 end
210 end
211 return AiSleep((SyncRand(100)+40) * GameSettings.Difficulty)
212 end,
213
214 -- Let's not build too many cannons, since the
215 -- AI places them mostly in useless locations:
216 function() return AiSet("unit-cannon", 1) end,
217 -- A radar is needed for the cannon to see its targets:
218 function() return AiSet("unit-radar", 1) end,
219
220 function()
221 LocalDebugPrint("Reached the end of AI script and will loop");
222 state.loop_pos = state.loop_start - 1; -- AiLoop will immediately increment it.
223 return false
224 end,
225 }
226
227 local function AiSpacious()
228 LocalDebugPrint("Script position " .. AiState[AiPlayer()].loop_pos);
229 return AiLoop(ai_funcs)
230 end
231
232 this_ai_type = {
233 Ident = "ai-spacious",
234 Name = _("Spacious"),
235 Init = InitAiScripts_spacious,
236 EachSecond = AiSpacious,
237 }
238 DefineAiType(this_ai_type)