00001
00002
00003
00004
00005
00006
00007
00008
00009
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00030
00031
00032
00033
00034
00035 #include <string.h>
00036 #include <stdio.h>
00037
00038 #include "stratagus.h"
00039 #include "unit.h"
00040 #include "unit_cache.h"
00041 #include "script.h"
00042 #include "map.h"
00043 #include "minimap.h"
00044 #include "ui.h"
00045 #include "player.h"
00046 #include "iolib.h"
00047 #include "video.h"
00048 #include "version.h"
00049
00050
00051
00052
00053
00059 static int CclStratagusMap(lua_State *l)
00060 {
00061 const char *value;
00062 int args;
00063 int subargs;
00064
00065
00066
00067
00068
00069 args = lua_gettop(l);
00070 for (int j = 0; j < args; ++j) {
00071 value = LuaToString(l, j + 1);
00072 ++j;
00073
00074 if (!strcmp(value, "version")) {
00075 char buf[32];
00076
00077 value = LuaToString(l, j + 1);
00078 sprintf_s(buf, sizeof(buf), StratagusFormatString, StratagusFormatArgs(StratagusVersion));
00079 if (strcmp(buf, value)) {
00080 fprintf(stderr, "Warning not saved with this version.\n");
00081 }
00082 } else if (!strcmp(value, "uid")) {
00083 Map.Info.MapUID = LuaToNumber(l, j + 1);
00084 } else if (!strcmp(value, "description")) {
00085 Map.Info.Description = LuaToString(l, j + 1);
00086 } else if (!strcmp(value, "the-map")) {
00087 if (!lua_istable(l, j + 1)) {
00088 LuaError(l, "incorrect argument");
00089 }
00090 subargs = lua_objlen(l, j + 1);
00091 for (int k = 0; k < subargs; ++k) {
00092 lua_rawgeti(l, j + 1, k + 1);
00093 value = LuaToString(l, -1);
00094 lua_pop(l, 1);
00095 ++k;
00096
00097 if (!strcmp(value, "size")) {
00098 lua_rawgeti(l, j + 1, k + 1);
00099 if (!lua_istable(l, -1)) {
00100 LuaError(l, "incorrect argument");
00101 }
00102 lua_rawgeti(l, -1, 1);
00103 Map.Info.MapWidth = LuaToNumber(l, -1);
00104 lua_pop(l, 1);
00105 lua_rawgeti(l, -1, 2);
00106 Map.Info.MapHeight = LuaToNumber(l, -1);
00107 lua_pop(l, 1);
00108 lua_pop(l, 1);
00109
00110 delete[] Map.Fields;
00111 Map.Fields = new CMapField[Map.Info.MapWidth * Map.Info.MapHeight];
00112 Map.Visible[0] = new unsigned[Map.Info.MapWidth * Map.Info.MapHeight / 2];
00113 memset(Map.Visible[0], 0, Map.Info.MapWidth * Map.Info.MapHeight / 2 * sizeof(unsigned));
00114 UnitCache.Init(Map.Info.MapWidth, Map.Info.MapHeight);
00115
00116 } else if (!strcmp(value, "fog-of-war")) {
00117 Map.NoFogOfWar = false;
00118 --k;
00119 } else if (!strcmp(value, "no-fog-of-war")) {
00120 Map.NoFogOfWar = true;
00121 --k;
00122 } else if (!strcmp(value, "filename")) {
00123 lua_rawgeti(l, j + 1, k + 1);
00124 Map.Info.Filename = LuaToString(l, -1);
00125 lua_pop(l, 1);
00126 } else if (!strcmp(value, "map-fields")) {
00127 int i;
00128 int subsubargs;
00129 int subk;
00130
00131 lua_rawgeti(l, j + 1, k + 1);
00132 if (!lua_istable(l, -1)) {
00133 LuaError(l, "incorrect argument");
00134 }
00135
00136 subsubargs = lua_objlen(l, -1);
00137 if (subsubargs != Map.Info.MapWidth * Map.Info.MapHeight) {
00138 fprintf(stderr, "Wrong tile table length: %d\n", subsubargs);
00139 }
00140 i = 0;
00141 for (subk = 0; subk < subsubargs; ++subk) {
00142 int args2;
00143 int j2;
00144
00145 lua_rawgeti(l, -1, subk + 1);
00146 if (!lua_istable(l, -1)) {
00147 LuaError(l, "incorrect argument");
00148 }
00149 args2 = lua_objlen(l, -1);
00150 j2 = 0;
00151
00152 for (; j2 < args2; ++j2) {
00153 lua_rawgeti(l, -1, j2 + 1);
00154 value = LuaToString(l, -1);
00155 lua_pop(l, 1);
00156 if (!strcmp(value, "explored")) {
00157 ++j2;
00158 lua_rawgeti(l, -1, j2 + 1);
00159 Map.Fields[i].Visible[(int)LuaToNumber(l, -1)] = 1;
00160 lua_pop(l, 1);
00161
00162 } else if (!strcmp(value, "land")) {
00163 Map.Fields[i].Flags |= MapFieldLandAllowed;
00164 } else if (!strcmp(value, "coast")) {
00165 Map.Fields[i].Flags |= MapFieldCoastAllowed;
00166 } else if (!strcmp(value, "water")) {
00167 Map.Fields[i].Flags |= MapFieldWaterAllowed;
00168
00169 } else if (!strcmp(value, "mud")) {
00170 Map.Fields[i].Flags |= MapFieldNoBuilding;
00171 } else if (!strcmp(value, "block")) {
00172 Map.Fields[i].Flags |= MapFieldUnpassable;
00173
00174 } else if (!strcmp(value, "ground")) {
00175 Map.Fields[i].Flags |= MapFieldLandUnit;
00176 } else if (!strcmp(value, "air")) {
00177 Map.Fields[i].Flags |= MapFieldAirUnit;
00178 } else if (!strcmp(value, "sea")) {
00179 Map.Fields[i].Flags |= MapFieldSeaUnit;
00180 } else if (!strcmp(value, "building")) {
00181 Map.Fields[i].Flags |= MapFieldBuilding;
00182
00183 } else {
00184 LuaError(l, "Unsupported tag: %s" _C_ value);
00185 }
00186 }
00187 lua_pop(l, 1);
00188 ++i;
00189 }
00190 lua_pop(l, 1);
00191 } else {
00192 LuaError(l, "Unsupported tag: %s" _C_ value);
00193 }
00194 }
00195
00196 } else {
00197 LuaError(l, "Unsupported tag: %s" _C_ value);
00198 }
00199 }
00200
00201 return 0;
00202 }
00203
00209 static int CclRevealMap(lua_State *l)
00210 {
00211 LuaCheckArgs(l, 0);
00212 if (CclInConfigFile || !Map.Fields) {
00213 FlagRevealMap = 1;
00214 } else {
00215 Map.Reveal();
00216 }
00217
00218 return 0;
00219 }
00220
00226 static int CclCenterMap(lua_State *l)
00227 {
00228 LuaCheckArgs(l, 2);
00229 UI.SelectedViewport->Center(
00230 LuaToNumber(l, 1), LuaToNumber(l, 2), TileSizeX / 2, TileSizeY / 2);
00231
00232 return 0;
00233 }
00234
00240 static int CclSetFogOfWar(lua_State *l)
00241 {
00242 LuaCheckArgs(l, 1);
00243 Map.NoFogOfWar = !LuaToBoolean(l, 1);
00244 if (!CclInConfigFile && Map.Fields) {
00245 UpdateFogOfWarChange();
00246
00247
00248 }
00249 return 0;
00250 }
00251
00252 static int CclGetFogOfWar(lua_State *l)
00253 {
00254 LuaCheckArgs(l, 0);
00255 lua_pushboolean(l, !Map.NoFogOfWar);
00256 return 1;
00257 }
00258
00264 static int CclSetMinimapTerrain(lua_State *l)
00265 {
00266 LuaCheckArgs(l, 1);
00267 UI.Minimap.WithTerrain = LuaToBoolean(l, 1);
00268 return 0;
00269 }
00270
00276 static int CclSetFogOfWarGraphics(lua_State *l)
00277 {
00278 std::string FogGraphicFile;
00279
00280 LuaCheckArgs(l, 1);
00281 FogGraphicFile = LuaToString(l, 1);
00282 if (CMap::FogGraphic) {
00283 CGraphic::Free(CMap::FogGraphic);
00284 }
00285 CMap::FogGraphic = CGraphic::New(FogGraphicFile, TileSizeX, TileSizeY);
00286
00287 return 0;
00288 }
00289
00295 static int CclDefinePlayerTypes(lua_State *l)
00296 {
00297 const char *type;
00298 int numplayers;
00299 int i;
00300
00301 numplayers = lua_gettop(l);
00302 if (numplayers < 2) {
00303 LuaError(l, "Not enough players");
00304 }
00305
00306 for (i = 0; i < numplayers && i < PlayerMax; ++i) {
00307 if (lua_isnil(l, i + 1)) {
00308 numplayers = i;
00309 break;
00310 }
00311 type = LuaToString(l, i + 1);
00312 if (!strcmp(type, "neutral")) {
00313 Map.Info.PlayerType[i] = PlayerNeutral;
00314 } else if (!strcmp(type, "nobody")) {
00315 Map.Info.PlayerType[i] = PlayerNobody;
00316 } else if (!strcmp(type, "computer")) {
00317 Map.Info.PlayerType[i] = PlayerComputer;
00318 } else if (!strcmp(type, "person")) {
00319 Map.Info.PlayerType[i] = PlayerPerson;
00320 } else if (!strcmp(type, "rescue-passive")) {
00321 Map.Info.PlayerType[i] = PlayerRescuePassive;
00322 } else if (!strcmp(type, "rescue-active")) {
00323 Map.Info.PlayerType[i] = PlayerRescueActive;
00324 } else {
00325 LuaError(l, "Unsupported tag: %s" _C_ type);
00326 }
00327 }
00328 for (i = numplayers; i < PlayerMax - 1; ++i) {
00329 Map.Info.PlayerType[i] = PlayerNobody;
00330 }
00331 if (numplayers < PlayerMax) {
00332 Map.Info.PlayerType[PlayerMax - 1] = PlayerNeutral;
00333 }
00334 return 0;
00335 }
00336
00340 void MapCclRegister(void)
00341 {
00342 lua_register(Lua, "StratagusMap", CclStratagusMap);
00343 lua_register(Lua, "RevealMap", CclRevealMap);
00344 lua_register(Lua, "CenterMap", CclCenterMap);
00345
00346 lua_register(Lua, "SetFogOfWar", CclSetFogOfWar);
00347 lua_register(Lua, "GetFogOfWar", CclGetFogOfWar);
00348 lua_register(Lua, "SetMinimapTerrain", CclSetMinimapTerrain);
00349
00350 lua_register(Lua, "SetFogOfWarGraphics", CclSetFogOfWarGraphics);
00351
00352 lua_register(Lua, "DefinePlayerTypes", CclDefinePlayerTypes);
00353 }
00354