| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name script_map.cpp - The map ccl functions. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1999-2010 by Lutz Sammer and 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; only version 2 of the License. |
| 17 |
// |
| 18 |
// This program is distributed in the hope that it will be useful, |
| 19 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
// GNU General Public License for more details. |
| 22 |
// |
| 23 |
// You should have received a copy of the GNU General Public License |
| 24 |
// along with this program; if not, write to the Free Software |
| 25 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 26 |
// 02111-1307, USA. |
| 27 |
// |
| 28 |
|
| 29 |
//@{ |
| 30 |
|
| 31 |
/*---------------------------------------------------------------------------- |
| 32 |
-- Includes |
| 33 |
----------------------------------------------------------------------------*/ |
| 34 |
|
| 35 |
#include <string.h> |
| 36 |
#include <stdio.h> |
| 37 |
|
| 38 |
#include "stratagus.h" |
| 39 |
#include "unit.h" |
| 40 |
#include "unit_cache.h" |
| 41 |
#include "script.h" |
| 42 |
#include "map.h" |
| 43 |
#include "minimap.h" |
| 44 |
#include "ui.h" |
| 45 |
#include "player.h" |
| 46 |
#include "iolib.h" |
| 47 |
#include "video.h" |
| 48 |
#include "version.h" |
| 49 |
#include "iocompat.h" |
| 50 |
|
| 51 |
/*---------------------------------------------------------------------------- |
| 52 |
-- Functions |
| 53 |
----------------------------------------------------------------------------*/ |
| 54 |
|
| 55 |
/** |
| 56 |
** Parse a map. |
| 57 |
** |
| 58 |
** @param l Lua state. |
| 59 |
*/ |
| 60 |
static int CclStratagusMap(lua_State *l) |
| 61 |
{ |
| 62 |
const char *value; |
| 63 |
int args; |
| 64 |
int subargs; |
| 65 |
|
| 66 |
// |
| 67 |
// Parse the list: (still everything could be changed!) |
| 68 |
// |
| 69 |
|
| 70 |
args = lua_gettop(l); |
| 71 |
for (int j = 0; j < args; ++j) { |
| 72 |
value = LuaToString(l, j + 1); |
| 73 |
++j; |
| 74 |
|
| 75 |
if (!strcmp(value, "version")) { |
| 76 |
char buf[32]; |
| 77 |
|
| 78 |
value = LuaToString(l, j + 1); |
| 79 |
sprintf_s(buf, sizeof(buf), StratagusFormatString, StratagusFormatArgs(StratagusVersion)); |
| 80 |
if (strcmp(buf, value)) { |
| 81 |
fprintf(stderr, "Warning not saved with this version.\n"); |
| 82 |
} |
| 83 |
} else if (!strcmp(value, "uid")) { |
| 84 |
Map.Info.MapUID = LuaToNumber(l, j + 1); |
| 85 |
} else if (!strcmp(value, "description")) { |
| 86 |
Map.Info.Description = LuaToString(l, j + 1); |
| 87 |
} else if (!strcmp(value, "the-map")) { |
| 88 |
LuaCheckTable(l, j + 1); |
| 89 |
subargs = lua_objlen(l, j + 1); |
| 90 |
for (int k = 0; k < subargs; ++k) { |
| 91 |
value = LuaToString(l, j + 1, k + 1); |
| 92 |
++k; |
| 93 |
|
| 94 |
if (!strcmp(value, "size")) { |
| 95 |
lua_rawgeti(l, j + 1, k + 1); |
| 96 |
LuaCheckTable(l, -1); |
| 97 |
Map.Info.MapWidth = LuaToNumber(l, -1, 1); |
| 98 |
Map.Info.MapHeight = LuaToNumber(l, -1, 2); |
| 99 |
lua_pop(l, 1); |
| 100 |
|
| 101 |
delete[] Map.Fields; |
| 102 |
Map.Fields = new CMapField[Map.Info.MapWidth * Map.Info.MapHeight]; |
| 103 |
Map.Visible[0] = new unsigned[Map.Info.MapWidth * Map.Info.MapHeight / 2]; |
| 104 |
memset(Map.Visible[0], 0, Map.Info.MapWidth * Map.Info.MapHeight / 2 * sizeof(unsigned)); |
| 105 |
UnitCache.Init(Map.Info.MapWidth, Map.Info.MapHeight); |
| 106 |
// FIXME: this should be CreateMap or InitMap? |
| 107 |
} else if (!strcmp(value, "fog-of-war")) { |
| 108 |
Map.NoFogOfWar = false; |
| 109 |
--k; |
| 110 |
} else if (!strcmp(value, "no-fog-of-war")) { |
| 111 |
Map.NoFogOfWar = true; |
| 112 |
--k; |
| 113 |
} else if (!strcmp(value, "filename")) { |
| 114 |
Map.Info.Filename = LuaToString(l, j + 1, k + 1); |
| 115 |
// Load the original map |
| 116 |
char path[PATH_MAX]; |
| 117 |
LibraryFileName(Map.Info.Filename.c_str(), path, sizeof(path)); |
| 118 |
LuaLoadFile(path); |
| 119 |
// Players array will be re-created correctly later |
| 120 |
CleanPlayers(); |
| 121 |
} else if (!strcmp(value, "map-fields")) { |
| 122 |
int i; |
| 123 |
int subsubargs; |
| 124 |
int subk; |
| 125 |
|
| 126 |
lua_rawgeti(l, j + 1, k + 1); |
| 127 |
LuaCheckTable(l, -1); |
| 128 |
|
| 129 |
subsubargs = lua_objlen(l, -1); |
| 130 |
if (subsubargs != Map.Info.MapWidth * Map.Info.MapHeight) { |
| 131 |
fprintf(stderr, "Wrong tile table length: %d\n", subsubargs); |
| 132 |
} |
| 133 |
i = 0; |
| 134 |
for (subk = 0; subk < subsubargs; ++subk) { |
| 135 |
int args2; |
| 136 |
int j2; |
| 137 |
|
| 138 |
lua_rawgeti(l, -1, subk + 1); |
| 139 |
LuaCheckTable(l, -1); |
| 140 |
args2 = lua_objlen(l, -1); |
| 141 |
j2 = 0; |
| 142 |
|
| 143 |
for (; j2 < args2; ++j2) { |
| 144 |
value = LuaToString(l, -1, j2 + 1); |
| 145 |
if (!strcmp(value, "explored")) { |
| 146 |
++j2; |
| 147 |
Map.Fields[i].Visible[LuaToNumber(l, -1, j2 + 1)] = 1; |
| 148 |
|
| 149 |
} else if (!strcmp(value, "land")) { |
| 150 |
Map.Fields[i].Flags |= MapFieldLandAllowed; |
| 151 |
} else if (!strcmp(value, "coast")) { |
| 152 |
Map.Fields[i].Flags |= MapFieldCoastAllowed; |
| 153 |
} else if (!strcmp(value, "pond")) { |
| 154 |
Map.Fields[i].Flags |= MapFieldShallowWater; |
| 155 |
} else if (!strcmp(value, "water")) { |
| 156 |
Map.Fields[i].Flags |= MapFieldDeepWater; |
| 157 |
|
| 158 |
} else if (!strcmp(value, "mud")) { |
| 159 |
Map.Fields[i].Flags |= MapFieldNoBuilding; |
| 160 |
} else if (!strcmp(value, "block")) { |
| 161 |
Map.Fields[i].Flags |= MapFieldUnpassable; |
| 162 |
|
| 163 |
} else if (!strcmp(value, "ground")) { |
| 164 |
Map.Fields[i].Flags |= MapFieldLandUnit; |
| 165 |
} else if (!strcmp(value, "air")) { |
| 166 |
Map.Fields[i].Flags |= MapFieldAirUnit; |
| 167 |
} else if (!strcmp(value, "sea")) { |
| 168 |
Map.Fields[i].Flags |= MapFieldSeaUnit; |
| 169 |
} else if (!strcmp(value, "building")) { |
| 170 |
Map.Fields[i].Flags |= MapFieldBuilding; |
| 171 |
|
| 172 |
} else { |
| 173 |
LuaError(l, "Unsupported tag: %s" _C_ value); |
| 174 |
} |
| 175 |
} |
| 176 |
lua_pop(l, 1); |
| 177 |
++i; |
| 178 |
} |
| 179 |
lua_pop(l, 1); |
| 180 |
} else { |
| 181 |
LuaError(l, "Unsupported tag: %s" _C_ value); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
} else { |
| 186 |
LuaError(l, "Unsupported tag: %s" _C_ value); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
return 0; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
** Reveal the complete map. |
| 195 |
** |
| 196 |
** @param l Lua state. |
| 197 |
*/ |
| 198 |
static int CclRevealMap(lua_State *l) |
| 199 |
{ |
| 200 |
LuaCheckArgs(l, 0); |
| 201 |
if (CclInConfigFile || !Map.Fields) { |
| 202 |
FlagRevealMap = 1; |
| 203 |
} else { |
| 204 |
Map.Reveal(); |
| 205 |
} |
| 206 |
|
| 207 |
return 0; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
** Center the map. |
| 212 |
** |
| 213 |
** @param l Lua state. |
| 214 |
*/ |
| 215 |
static int CclCenterMap(lua_State *l) |
| 216 |
{ |
| 217 |
LuaCheckArgs(l, 2); |
| 218 |
UI.SelectedViewport->Center( |
| 219 |
LuaToNumber(l, 1), LuaToNumber(l, 2), TileSizeX / 2, TileSizeY / 2); |
| 220 |
|
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
** Set fog of war on/off. |
| 226 |
** |
| 227 |
** @param l Lua state. |
| 228 |
*/ |
| 229 |
static int CclSetFogOfWar(lua_State *l) |
| 230 |
{ |
| 231 |
LuaCheckArgs(l, 1); |
| 232 |
Map.NoFogOfWar = !LuaToBoolean(l, 1); |
| 233 |
if (!CclInConfigFile && Map.Fields) { |
| 234 |
UpdateFogOfWarChange(); |
| 235 |
// FIXME: save setting in replay log |
| 236 |
//CommandLog("input", NoUnitP, FlushCommands, -1, -1, NoUnitP, "fow off", -1); |
| 237 |
} |
| 238 |
return 0; |
| 239 |
} |
| 240 |
|
| 241 |
static int CclGetFogOfWar(lua_State *l) |
| 242 |
{ |
| 243 |
LuaCheckArgs(l, 0); |
| 244 |
lua_pushboolean(l, !Map.NoFogOfWar); |
| 245 |
return 1; |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
** Enable display of terrain in minimap. |
| 250 |
** |
| 251 |
** @param l Lua state. |
| 252 |
*/ |
| 253 |
static int CclSetMinimapTerrain(lua_State *l) |
| 254 |
{ |
| 255 |
LuaCheckArgs(l, 1); |
| 256 |
UI.Minimap.WithTerrain = LuaToBoolean(l, 1); |
| 257 |
return 0; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
** Define Fog graphics |
| 262 |
** |
| 263 |
** @param l Lua state. |
| 264 |
*/ |
| 265 |
static int CclSetFogOfWarGraphics(lua_State *l) |
| 266 |
{ |
| 267 |
std::string FogGraphicFile; |
| 268 |
|
| 269 |
LuaCheckArgs(l, 1); |
| 270 |
FogGraphicFile = LuaToString(l, 1); |
| 271 |
if (CMap::FogGraphic) { |
| 272 |
CGraphic::Free(CMap::FogGraphic); |
| 273 |
} |
| 274 |
CMap::FogGraphic = CGraphic::New(FogGraphicFile, TileSizeX, TileSizeY); |
| 275 |
|
| 276 |
return 0; |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
** Define the type of each player available for the map |
| 281 |
** |
| 282 |
** @param l Lua state. |
| 283 |
*/ |
| 284 |
static int CclDefinePlayerTypes(lua_State *l) |
| 285 |
{ |
| 286 |
const char *type; |
| 287 |
int numplayers; |
| 288 |
int i; |
| 289 |
|
| 290 |
numplayers = lua_gettop(l); /* Number of players == number of arguments */ |
| 291 |
if (numplayers < 2) { |
| 292 |
LuaError(l, "Not enough players"); |
| 293 |
} |
| 294 |
|
| 295 |
for (i = 0; i < numplayers && i < PlayerMax; ++i) { |
| 296 |
if (lua_isnil(l, i + 1)) { |
| 297 |
numplayers = i; |
| 298 |
break; |
| 299 |
} |
| 300 |
type = LuaToString(l, i + 1); |
| 301 |
if (!strcmp(type, "neutral")) { |
| 302 |
Map.Info.PlayerType[i] = PlayerNeutral; |
| 303 |
} else if (!strcmp(type, "nobody")) { |
| 304 |
Map.Info.PlayerType[i] = PlayerNobody; |
| 305 |
} else if (!strcmp(type, "computer")) { |
| 306 |
Map.Info.PlayerType[i] = PlayerComputer; |
| 307 |
} else if (!strcmp(type, "person")) { |
| 308 |
Map.Info.PlayerType[i] = PlayerPerson; |
| 309 |
} else if (!strcmp(type, "rescue-passive")) { |
| 310 |
Map.Info.PlayerType[i] = PlayerRescuePassive; |
| 311 |
} else if (!strcmp(type, "rescue-active")) { |
| 312 |
Map.Info.PlayerType[i] = PlayerRescueActive; |
| 313 |
} else { |
| 314 |
LuaError(l, "Unsupported tag: %s" _C_ type); |
| 315 |
} |
| 316 |
} |
| 317 |
for (i = numplayers; i < PlayerMax - 1; ++i) { |
| 318 |
Map.Info.PlayerType[i] = PlayerNobody; |
| 319 |
} |
| 320 |
if (numplayers < PlayerMax) { |
| 321 |
Map.Info.PlayerType[PlayerMax - 1] = PlayerNeutral; |
| 322 |
} |
| 323 |
return 0; |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
** Register CCL features for map. |
| 328 |
*/ |
| 329 |
void MapCclRegister(void) |
| 330 |
{ |
| 331 |
lua_register(Lua, "StratagusMap", CclStratagusMap); |
| 332 |
lua_register(Lua, "RevealMap", CclRevealMap); |
| 333 |
lua_register(Lua, "CenterMap", CclCenterMap); |
| 334 |
|
| 335 |
lua_register(Lua, "SetFogOfWar", CclSetFogOfWar); |
| 336 |
lua_register(Lua, "GetFogOfWar", CclGetFogOfWar); |
| 337 |
lua_register(Lua, "SetMinimapTerrain", CclSetMinimapTerrain); |
| 338 |
|
| 339 |
lua_register(Lua, "SetFogOfWarGraphics", CclSetFogOfWarGraphics); |
| 340 |
|
| 341 |
lua_register(Lua, "DefinePlayerTypes", CclDefinePlayerTypes); |
| 342 |
} |
| 343 |
|
| 344 |
//@} |