Debian Codegraph @

scripts/ais/naval.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 -- naval.lua - Define a AI that defends itself, and
11 -- attacks over sea.
12 --
13 -- (c) Copyright 2010 by Michiel van der Wulp
14 --
15 -- This program is free software; you can redistribute it and/or modify
16 -- it under the terms of the GNU General Public License as published by
17 -- the Free Software Foundation; either version 2 of the License, or
18 -- (at your option) any later version.
19 --
20 -- This program is distributed in the hope that it will be useful,
21 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
22 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 -- GNU General Public License for more details.
24 --
25 -- You should have received a copy of the GNU General Public License
26 -- along with this program; if not, write to the Free Software
27 -- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 --
29 -- $Id: naval.lua $
30 --
31
32 -- What we registered in AiTypes.
33 local this_ai_type
34
35 -- Same as AiState[AiPlayer()]. Valid only during AiLoop.
36 local state
37
38 local function AiLoop(funcs)
39 state = AiState[AiPlayer()]
40 while (true) do
41 local ret = funcs[state.loop_pos]()
42 if (ret) then
43 break
44 end
45 state.loop_pos = state.loop_pos + 1
46 end
47 return true
48 end
49
50 local function LocalDebugPrint(text)
51 -- DebugPrint(this_ai_type.Ident .. " player " .. AiPlayer() .. " " .. text)
52 end
53
54 local function InitAiScripts_naval()
55 AiState[AiPlayer()] = {
56 loop_pos = 1,
57 loop_start = nil,
58 }
59 end
60
61 local ai_funcs = {
62 -- Use all 4 engineers to help building a magnapump:
63 function() return AiNeed("unit-magmapump") end,
64 function() return AiNeed("unit-magmapump") end,
65 function() return AiNeed("unit-magmapump") end,
66 function() return AiNeed("unit-magmapump") end,
67 -- The above statement is repeated 4 times:
68 -- Here the engineers cooperate on building one magmapump.
69 -- Using this technique in other cases fails, since
70 -- sometimes the engineers build more than one unit!
71 -- This also does not work with the AiSet() function.
72 function() return AiWait("unit-magmapump") end,
73 function() -- debug only
74 LocalDebugPrint("Magnapump finished.");
75 return false
76 end,
77
78 function() return AiNeed("unit-nukepowerplant") end,
79 -- Do not start building anything else until we have the energy:
80 function() return AiWait("unit-nukepowerplant") end,
81 function() -- debug only
82 LocalDebugPrint("NukePowerplant finished.");
83 return false
84 end,
85
86 -- there is not enough real estate for a vault
87 -- function() return AiNeed("unit-vault") end,
88 -- function() return AiWait("unit-vault") end,
89 -- function() -- debug only
90 -- LocalDebugPrint("Vault finished.");
91 -- return false
92 -- end,
93
94 function() return AiNeed("unit-shipyard") end,
95 function() return AiNeed("unit-shipyard") end,
96
97 function() return AiNeed("unit-gturret") end,
98
99 function() return AiWait("unit-shipyard") end,
100 function() -- debug only
101 LocalDebugPrint("Shipyard finished.");
102 return false
103 end,
104
105 function() return AiForce(0, {"unit-destroyer", 2}) end,
106 function() return AiWaitForce(0) end,
107 function() return AiAttackWithForce(0) end,
108 function() -- debug only
109 LocalDebugPrint("Attack force of 2 destroyers is ready - attacking.");
110 return false
111 end,
112
113 function() return AiSet("unit-magmapump",2) end,
114 function() return AiSet("unit-nukepowerplant", 1) end,
115 function() return AiSet("unit-engineer", 4) end,
116 function() return AiSet("unit-gturret", 4) end,
117
118 -- ============================================================
119
120 function()
121 LocalDebugPrint("is starting loop.");
122 state.loop_start = state.loop_pos;
123 return false
124 end,
125
126 function() return AiForce(0, {"unit-destroyer", 3}) end,
127 function() return AiWaitForce(0) end,
128 function() return AiAttackWithForce(0) end,
129
130 -- Always attack as soon as the 3 destroyers are finished:
131 --function()
132 -- if (AiCheckForce(0)) then
133 -- AiWaitForce(0);
134 -- return AiAttackWithForce(0)
135 -- end
136 -- return true
137 --end,
138
139 function()
140 LocalDebugPrint("Reached the end of AI script and will loop");
141 state.loop_pos = state.loop_start - 1; -- AiLoop will immediately increment it.
142 return false
143 end,
144 }
145
146 local function AiNaval()
147 LocalDebugPrint("Script position " .. AiState[AiPlayer()].loop_pos);
148 return AiLoop(ai_funcs)
149 end
150
151 this_ai_type = {
152 Ident = "ai-naval",
153 Name = _("Naval"),
154 Init = InitAiScripts_naval,
155 EachSecond = AiNaval,
156 }
157 DefineAiType(this_ai_type)