Debian Codegraph @

scripts/ais/default.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 -- default.lua - Define the AI.
11 --
12 -- (c) Copyright 2009 by Jimmy Salmon
13 --
14 -- This program is free software; you can redistribute it and/or modify
15 -- it under the terms of the GNU General Public License as published by
16 -- the Free Software Foundation; either version 2 of the License, or
17 -- (at your option) any later version.
18 --
19 -- This program is distributed in the hope that it will be useful,
20 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
21 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 -- GNU General Public License for more details.
23 --
24 -- You should have received a copy of the GNU General Public License
25 -- along with this program; if not, write to the Free Software
26 -- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 --
28 -- $Id: rush.lua 626 2006-11-25 18:10:03Z feb $
29 --
30
31 -- What we registered in AiTypes.
32 local this_ai_type
33
34 -- Same as AiState[AiPlayer()]. Valid only during AiLoop.
35 local state
36
37 local function AiLoop(funcs)
38 state = AiState[AiPlayer()]
39 while (true) do
40 local ret = funcs[state.loop_pos]()
41 if (ret) then
42 break
43 end
44 state.loop_pos = state.loop_pos + 1
45 end
46 return true
47 end
48
49 local function LocalDebugPrint(text)
50 -- DebugPrint(this_ai_type.Ident .. " player " .. AiPlayer() .. " " .. text)
51 end
52
53 local function InitAiScripts_default()
54 AiState[AiPlayer()] = {
55 loop_pos = 1,
56 loop_start = nil,
57 build_order = nil,
58 }
59 end
60
61 local function CheckPower()
62 local nuke = UnitTypeByIdent("unit-nukepowerplant")
63 local count = Players[AiPlayer()].UnitTypesCount[nuke.Slot]
64 if (count ~= 0) then
65 return nil
66 end
67 return "unit-powerplant"
68 end
69
70 local function GetBuildOrder()
71 local order = {}
72
73 if (not AiHotSpotExists()) then
74 order[1] = CheckPower()
75 order[2] = nil
76 elseif (Players[AiPlayer()].MagmaStored < 300) then
77 order[1] = "unit-magmapump"
78 order[2] = CheckPower()
79 else
80 order[1] = CheckPower()
81 order[2] = "unit-magmapump"
82 end
83
84 return order
85 end
86
87 local ai_funcs = {
88 function() AiDebug(false) return false end,
89
90 -- Build magma pump or power plant first depending on resources
91 function()
92 state.build_order = GetBuildOrder()
93 if (state.build_order[1] ~= nil) then
94 return AiNeed(state.build_order[1])
95 else
96 return false
97 end
98 end,
99 function()
100 if (state.build_order[1] ~= nil) then
101 return AiWait(state.build_order[1])
102 else
103 return false
104 end
105 end,
106 function()
107 if (state.build_order[2] ~= nil) then
108 return AiNeed(state.build_order[2])
109 else
110 return false
111 end
112 end,
113 function()
114 if (state.build_order[2] ~= nil) then
115 return AiWait(state.build_order[2])
116 else
117 return false
118 end
119 end,
120
121 function() return AiSet("unit-engineer", 2) end,
122 function() return AiNeed("unit-vault") end,
123 function() return AiNeed("unit-magmapump") end,
124 function() return AiWait("unit-vault") end,
125
126 function() return AiNeed("unit-camp") end,
127 function() return AiWait("unit-camp") end,
128 function() return AiSet("unit-engineer", 3) end,
129
130 -- Defense
131 function() return AiForce(0, {"unit-assault", 2}) end,
132 function() return AiWaitForce(0) end,
133
134 function() return AiNeed("unit-powerplant") end,
135 function() return AiNeed("unit-magmapump") end,
136 function() return AiNeed("unit-magmapump") end,
137 function() return AiNeed("unit-vfac") end,
138 function() return AiNeed("unit-gturret") end,
139 function() return AiSleep(150*GameSettings.Difficulty) end,
140
141 -- Attack wave
142 function() return AiForce(1, {"unit-assault", 5, "unit-rtank", 3}) end,
143 function() return AiWaitForce(1) end,
144 function() return AiSleep(50*GameSettings.Difficulty) end,
145 function() return AiAttackWithForce(1) end,
146
147 function() return AiNeed("unit-powerplant") end,
148 function() return AiNeed("unit-magmapump") end,
149 function() return AiNeed("unit-biggunturret") end,
150 function() return AiSleep(50*GameSettings.Difficulty) end,
151
152 -- Bigger attack wave
153 function() return AiNeed("unit-magmapump") end,
154 function() return AiForce(1, {"unit-assault", 3,
155 "unit-grenadier", 3,
156 "unit-tank", 3,
157 "unit-rtank", 3}) end,
158 function() return AiWaitForce(1) end,
159 function() return AiAttackWithForce(1) end,
160
161 -- ============================================================
162
163 function()
164 LocalDebugPrint("is starting loop.");
165 state.loop_start = state.loop_pos;
166 return false
167 end,
168
169 function() return AiForce(1, {"unit-bazoo", 10,
170 "unit-grenadier", 10,
171 "unit-tank", 4,
172 "unit-rtank", 4}) end,
173 function() return AiWaitForce(1) end,
174 function() return AiSleep(50*GameSettings.Difficulty) end,
175 function() return AiAttackWithForce(1) end,
176
177 function()
178 LocalDebugPrint("Reached the end of AI script and will loop");
179 state.loop_pos = state.loop_start - 1; -- AiLoop will immediately increment it.
180 return false
181 end,
182 }
183
184 local function AiDefault()
185 LocalDebugPrint("Script position " .. AiState[AiPlayer()].loop_pos);
186 return AiLoop(ai_funcs)
187 end
188
189 this_ai_type = {
190 Ident = "ai-default",
191 Name = _("Default"),
192 Init = InitAiScripts_default,
193 EachSecond = AiDefault,
194 }
195 DefineAiType(this_ai_type)