Esempio n. 1
0
/**
** Save the complete map.
**
** @param file Output file.
*/
void CMap::Save(CFile *file) const
{
	int w;
	int h;
	int i;

	file->printf("\n--- -----------------------------------------\n");
	file->printf("--- MODULE: map $Id: map_save.cpp 7598 2006-10-07 16:32:39Z jsalmon3 $\n");

	file->printf("LoadTileModels(\"%s\")\n\n", this->TileModelsFileName);
	
	file->printf("StratagusMap(\n");

	file->printf("  \"version\", \"" StratagusFormatString "\",\n",
		StratagusFormatArgs(StratagusVersion));
	file->printf("  \"description\", \"%s\",\n", this->Info.Description.c_str());

	file->printf("  \"the-map\", {\n");

	file->printf("  \"size\", {%d, %d},\n", this->Info.MapWidth, this->Info.MapHeight);
	file->printf("  \"%s\",\n", this->NoFogOfWar ? "no-fog-of-war" : "fog-of-war");
	file->printf("  \"filename\", \"%s\",\n", this->Info.Filename.c_str());

	file->printf("  \"map-fields\", {\n");
	for (h = 0; h < this->Info.MapHeight; ++h) {
		file->printf("  -- %d\n", h);
		for (w = 0; w < this->Info.MapWidth; ++w) {
			CMapField* mf;

			mf = &this->Fields[h * this->Info.MapWidth + w];
			file->printf("  {%3d, %3d,", mf->Tile, mf->SeenTile);
			if (mf->Value) {
				file->printf(" %d,", mf->Value);
			}
			for (i = 0; i < PlayerMax; ++i) {
				if (mf->Visible[i] == 1) {
					file->printf(" \"explored\", %d,", i);
				}
			}
			if (mf->Flags & MapFieldHuman) {
				file->printf(" \"human\",");
			}
			if (mf->Flags & MapFieldLandAllowed) {
				file->printf(" \"land\",");
			}
			if (mf->Flags & MapFieldCoastAllowed) {
				file->printf(" \"coast\",");
			}
			if (mf->Flags & MapFieldWaterAllowed) {
				file->printf(" \"water\",");
			}
			if (mf->Flags & MapFieldNoBuilding) {
				file->printf(" \"mud\",");
			}
			if (mf->Flags & MapFieldUnpassable) {
				file->printf(" \"block\",");
			}
			if (mf->Flags & MapFieldWall) {
				file->printf(" \"wall\",");
			}
			if (mf->Flags & MapFieldRocks) {
				file->printf(" \"rock\",");
			}
			if (mf->Flags & MapFieldForest) {
				file->printf(" \"wood\",");
			}
#if 1
			// Not Required for save
			// These are required for now, UnitType::FieldFlags is 0 until
			// UpdateStats is called which is after the game is loaded
			if (mf->Flags & MapFieldLandUnit) {
				file->printf(" \"ground\",");
			}
			if (mf->Flags & MapFieldAirUnit) {
				file->printf(" \"air\",");
			}
			if (mf->Flags & MapFieldSeaUnit) {
				file->printf(" \"sea\",");
			}
			if (mf->Flags & MapFieldBuilding) {
				file->printf(" \"building\",");
			}
#endif
			if (w & 1) {
				file->printf("},\n");
			} else {
				file->printf("}, ");
			}
		}
	}
	file->printf("}})\n");
}
Esempio n. 2
0
/**
**  Parse a map.
**
**  @param l  Lua state.
*/
static int CclStratagusMap(lua_State *l)
{
	const char *value;
	int args;
	int j;
	int subargs;
	int k;

	//
	//  Parse the list: (still everything could be changed!)
	//

	args = lua_gettop(l);
	for (j = 0; j < args; ++j) {
		value = LuaToString(l, j + 1);
		++j;

		if (!strcmp(value, "version")) {
			char buf[32];

			value = LuaToString(l, j + 1);
			snprintf(buf, sizeof(buf), StratagusFormatString, StratagusFormatArgs(StratagusVersion));
			if (strcmp(buf, value)) {
				fprintf(stderr, "Warning not saved with this version.\n");
			}
		} else if (!strcmp(value, "uid")) {
			Map.Info.MapUID = LuaToNumber(l, j + 1);
		} else if (!strcmp(value, "description")) {
			Map.Info.Description = LuaToString(l, j + 1);
		} else if (!strcmp(value, "the-map")) {
			if (!lua_istable(l, j + 1)) {
				LuaError(l, "incorrect argument");
			}
			subargs = lua_objlen(l, j + 1);
			for (k = 0; k < subargs; ++k) {
				lua_rawgeti(l, j + 1, k + 1);
				value = LuaToString(l, -1);
				lua_pop(l, 1);
				++k;

				if (!strcmp(value, "size")) {
					lua_rawgeti(l, j + 1, k + 1);
					if (!lua_istable(l, -1)) {
						LuaError(l, "incorrect argument");
					}
					lua_rawgeti(l, -1, 1);
					Map.Info.MapWidth = LuaToNumber(l, -1);
					lua_pop(l, 1);
					lua_rawgeti(l, -1, 2);
					Map.Info.MapHeight = LuaToNumber(l, -1);
					lua_pop(l, 1);
					lua_pop(l, 1);

					delete[] Map.Fields;
					Map.Fields = new CMapField[Map.Info.MapWidth * Map.Info.MapHeight];
					Map.Visible[0] = new unsigned[Map.Info.MapWidth * Map.Info.MapHeight / 2];
					memset(Map.Visible[0], 0, Map.Info.MapWidth * Map.Info.MapHeight / 2 * sizeof(unsigned));
					// FIXME: this should be CreateMap or InitMap?
				} else if (!strcmp(value, "fog-of-war")) {
					Map.NoFogOfWar = false;
					--k;
				} else if (!strcmp(value, "no-fog-of-war")) {
					Map.NoFogOfWar = true;
					--k;
				} else if (!strcmp(value, "filename")) {
					lua_rawgeti(l, j + 1, k + 1);
					Map.Info.Filename = LuaToString(l, -1);
					lua_pop(l, 1);
				} else if (!strcmp(value, "map-fields")) {
					int i;
					int subsubargs;
					int subk;

					lua_rawgeti(l, j + 1, k + 1);
					if (!lua_istable(l, -1)) {
						LuaError(l, "incorrect argument");
					}

					subsubargs = lua_objlen(l, -1);
					if (subsubargs != Map.Info.MapWidth * Map.Info.MapHeight) {
						fprintf(stderr, "Wrong tile table length: %d\n", subsubargs);
					}
					i = 0;
					for (subk = 0; subk < subsubargs; ++subk) {
						int args2;
						int j2;

						lua_rawgeti(l, -1, subk + 1);
						if (!lua_istable(l, -1)) {
							LuaError(l, "incorrect argument");
						}
						args2 = lua_objlen(l, -1);
						j2 = 0;

						lua_rawgeti(l, -1, j2 + 1);
						Map.Fields[i].Tile = LuaToNumber(l, -1);
						lua_pop(l, 1);
						++j2;
						lua_rawgeti(l, -1, j2 + 1);
						Map.Fields[i].SeenTile = LuaToNumber(l, -1);
						lua_pop(l, 1);
						++j2;
						for (; j2 < args2; ++j2) {
							lua_rawgeti(l, -1, j2 + 1);
							if (lua_isnumber(l, -1)) {
								Map.Fields[i].Value = LuaToNumber(l, -1);
								lua_pop(l, 1);
								continue;
							}
							value = LuaToString(l, -1);
							lua_pop(l, 1);
							if (!strcmp(value, "explored")) {
								++j2;
								lua_rawgeti(l, -1, j2 + 1);
								Map.Fields[i].Visible[(int)LuaToNumber(l, -1)] = 1;
								lua_pop(l, 1);
							} else if (!strcmp(value, "human")) {
								Map.Fields[i].Flags |= MapFieldHuman;

							} else if (!strcmp(value, "land")) {
								Map.Fields[i].Flags |= MapFieldLandAllowed;
							} else if (!strcmp(value, "coast")) {
								Map.Fields[i].Flags |= MapFieldCoastAllowed;
							} else if (!strcmp(value, "water")) {
								Map.Fields[i].Flags |= MapFieldWaterAllowed;

							} else if (!strcmp(value, "mud")) {
								Map.Fields[i].Flags |= MapFieldNoBuilding;
							} else if (!strcmp(value, "block")) {
								Map.Fields[i].Flags |= MapFieldUnpassable;

							} else if (!strcmp(value, "wall")) {
								Map.Fields[i].Flags |= MapFieldWall;
							} else if (!strcmp(value, "rock")) {
								Map.Fields[i].Flags |= MapFieldRocks;
							} else if (!strcmp(value, "wood")) {
								Map.Fields[i].Flags |= MapFieldForest;

							} else if (!strcmp(value, "ground")) {
								Map.Fields[i].Flags |= MapFieldLandUnit;
							} else if (!strcmp(value, "air")) {
								Map.Fields[i].Flags |= MapFieldAirUnit;
							} else if (!strcmp(value, "sea")) {
								Map.Fields[i].Flags |= MapFieldSeaUnit;
							} else if (!strcmp(value, "building")) {
								Map.Fields[i].Flags |= MapFieldBuilding;

							} else {
							   LuaError(l, "Unsupported tag: %s" _C_ value);
							}
						}
						lua_pop(l, 1);
						++i;
					}
					lua_pop(l, 1);
				} else {
				   LuaError(l, "Unsupported tag: %s" _C_ value);
				}
			}

		} else {
		   LuaError(l, "Unsupported tag: %s" _C_ value);
		}
	}

	return 0;
}