Debian Codegraph @

scripts/ais/tankrush.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 -- tankrush.lua - Define the AI.
11 --
12 -- (c) Copyright 2007 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: tankrush.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_tankrush()
54 AiState[AiPlayer()] = {
55 loop_pos = 1,
56 loop_start = nil,
57 build_order = nil,
58 }
59 end
60
61 local function GetBuildOrder()
62 local order = {}
63
64 if (not AiHotSpotExists()) then
65 order[1] = "unit-powerplant"
66 order[2] = nil
67 elseif (Players[AiPlayer()].MagmaStored < 300) then
68 order[1] = "unit-magmapump"
69 order[2] = "unit-powerplant"
70 else
71 order[1] = "unit-powerplant"
72 order[2] = "unit-magmapump"
73 end
74
75 return order
76 end
77
78 local ai_funcs = {
79 function() AiDebug(false) return false end,
80
81 -- Build magma pump or power plant first depending on resources
82 function()
83 state.build_order = GetBuildOrder()
84 return AiNeed(state.build_order[1])
85 end,
86 function()
87 return AiWait(state.build_order[1])
88 end,
89 function()
90 if (state.build_order[2] ~= nil) then
91 return AiNeed(state.build_order[2])
92 else
93 return false
94 end
95 end,
96 function()
97 if (state.build_order[2] ~= nil) then
98 return AiWait(state.build_order[2])
99 else
100 return false
101 end
102 end,
103
104 function() return AiSet("unit-engineer", 2) end,
105 function() return AiNeed("unit-vault") end,
106 function() return AiNeed("unit-magmapump") end,
107 function() return AiWait("unit-vault") end,
108
109 function() return AiNeed("unit-vfac") end,
110 function() return AiWait("unit-vfac") end,
111 function() return AiSet("unit-engineer", 3) end,
112
113 -- Defense
114 -- function() return AiForce(0, {"unit-assault", 4}) end,
115 -- function() return AiWaitForce(0) end,
116
117 function() return AiNeed("unit-powerplant") end,
118 function() return AiNeed("unit-magmapump") end,
119 function() return AiNeed("unit-magmapump") end,
120 function() return AiNeed("unit-vfac") end,
121 function() return AiSleep(150*GameSettings.Difficulty) end,
122
123 -- Attack wave
124 function() return AiForce(1, {"unit-tank", 1, "unit-rtank", 1}) end,
125 function() return AiWaitForce(1) end,
126 function() return AiSleep(50*GameSettings.Difficulty) end,
127 function() return AiAttackWithForce(1) end,
128
129 -- Bigger attack wave
130 function() return AiNeed("unit-magmapump") end,
131 function() return AiNeed("unit-magmapump") end,
132 function() return AiForce(1, {"unit-tank", 2, "unit-rtank", 2}) end,
133 function() return AiWaitForce(1) end,
134 function() return AiAttackWithForce(1) end,
135
136 -- ============================================================
137
138 function()
139 LocalDebugPrint("is starting loop.");
140 state.loop_start = state.loop_pos;
141 return false
142 end,
143
144 function() return AiForce(1, {"unit-tank", 4, "unit-rtank", 4}) end,
145 function() return AiWaitForce(1) end, -- wait until attack party is completed
146 function() return AiSleep(50*GameSettings.Difficulty) end,
147 function() return AiAttackWithForce(1) end,
148
149 function()
150 LocalDebugPrint("Reached the end of AI script and will loop");
151 state.loop_pos = state.loop_start - 1; -- AiLoop will immediately increment it.
152 return false
153 end,
154 }
155
156 local function AiTankRush()
157 LocalDebugPrint("Script position " .. AiState[AiPlayer()].loop_pos);
158 return AiLoop(ai_funcs)
159 end
160
161 this_ai_type = {
162 Ident = "ai-tankrush",
163 Name = _("Tank rush"),
164 Init = InitAiScripts_tankrush,
165 EachSecond = AiTankRush,
166 }
167 DefineAiType(this_ai_type)