/** ** Enable display mana as vertical bar. ** ** Equivalent of ** DefineDecorations({Index = "Mana", HideNeutral = true, ** Method = {"bar", {Width = 3, BorderSize = 1, Orientation = "vertical"}}}) ** For index Mana, Transport, Research, Training, UpgradeTo, Resource. ** ** @param l Lua state */ static int CclShowManaVertical(lua_State *l) { char lua_equiv[] = "DefineDecorations({Index = \"Mana\", HideNeutral = true" "Method = {\"bar\", {Width = 3, BorderSize = 1, Orientation = \"vertical\"}}})\n" "DefineDecorations({Index = \"Transport\", HideNeutral = true," "Method = {\"bar\", {Width = 3, BorderSize = 1, Orientation = \"vertical\"}}})\n" "DefineDecorations({Index = \"Research\", HideNeutral = true," "Method = {\"bar\", {Width = 3, BorderSize = 1, Orientation = \"vertical\"}}})\n" "DefineDecorations({Index = \"Training\", HideNeutral = true," "Method = {\"bar\", {Width = 3, BorderSize = 1, Orientation = \"vertical\"}}})\n" "DefineDecorations({Index = \"UpgradeTo\", HideNeutral = true," "Method = {\"bar\", {Width = 3, BorderSize = 1, Orientation = \"vertical\"}}})\n" "DefineDecorations({Index = \"GiveResource\", HideNeutral = false," "Method = {\"bar\", {Width = 3, BorderSize = 1, Orientation = \"vertical\"}}})\n" "DefineDecorations({Index = \"CarryResource\", HideNeutral = false," "Method = {\"bar\", {Width = 3, BorderSize = 1, Orientation = \"vertical\"}}})\n"; LuaCheckArgs(l, 0); DebugPrint("instead of ShowManaVertical, you should write instead :\n%s\n" _C_ lua_equiv); CclCommand(lua_equiv); return 0; }
/** ** Make new player colors ** ** @param l Lua state. */ static int CclNewPlayerColors(lua_State *l) { LuaCheckArgs(l, 0); SetPlayersPalette(); return 0; }
/** ** Load the SavedGameInfo Header ** ** @param l Lua state. */ static int CclSavedGameInfo(lua_State *l) { const char *value; LuaCheckArgs(l, 1); LuaCheckTable(l, 1); lua_pushnil(l); while (lua_next(l, 1)) { value = LuaToString(l, -2); if (!strcmp(value, "SaveFile")) { if (strcpy_s(CurrentMapPath, sizeof(CurrentMapPath), LuaToString(l, -1)) != 0) { LuaError(l, "SaveFile too long"); } std::string buf = StratagusLibPath; buf += "/"; buf += LuaToString(l, -1); if (LuaLoadFile(buf) == -1) { DebugPrint("Load failed: %s\n" _C_ value); } } else if (!strcmp(value, "SyncHash")) { SyncHash = LuaToNumber(l, -1); } else if (!strcmp(value, "SyncRandSeed")) { SyncRandSeed = LuaToNumber(l, -1); } else { LuaError(l, "Unsupported tag: %s" _C_ value); } lua_pop(l, 1); } return 0; }
/** ** Kill a unit ** ** @param l Lua state. ** ** @return Returns true if a unit was killed. */ static int CclKillUnit(lua_State *l) { LuaCheckArgs(l, 2); lua_pushvalue(l, 1); const CUnitType *unittype = TriggerGetUnitType(l); lua_pop(l, 1); const int plynr = TriggerGetPlayer(l); if (plynr == -1) { CUnitManager::Iterator it = std::find_if(UnitManager.begin(), UnitManager.end(), HasSameUnitTypeAs(unittype)); if (it != UnitManager.end()) { LetUnitDie(**it); lua_pushboolean(l, 1); return 1; } } else { CPlayer &player = Players[plynr]; std::vector<CUnit *>::iterator it = std::find_if(player.UnitBegin(), player.UnitEnd(), HasSameUnitTypeAs(unittype)); if (it != player.UnitEnd()) { LetUnitDie(**it); lua_pushboolean(l, 1); return 1; } } lua_pushboolean(l, 0); return 1; }
/** ** Create a sound. ** ** Glue between c and scheme. This function asks the sound system to ** register a sound under a given name, wiht an associated list of files ** (the list can be replaced by only one file). ** ** @param l Lua state. ** ** @return the sound id of the created sound */ static int CclMakeSound(lua_State *l) { LuaCheckArgs(l, 2); std::string c_name = LuaToString(l, 1); std::vector<std::string> files; CSound *id; if (lua_isstring(l, 2)) { // only one file files.push_back(LuaToString(l, 2)); id = MakeSound(c_name, files); } else if (lua_istable(l, 2)) { // several files const int args = lua_rawlen(l, 2); files.reserve(args); for (int j = 0; j < args; ++j) { lua_rawgeti(l, 2, j + 1); files.push_back(LuaToString(l, -1)); lua_pop(l, 1); } id = MakeSound(c_name, files); } else { LuaError(l, "string or table expected"); return 0; } LuaUserData *data = (LuaUserData *)lua_newuserdata(l, sizeof(LuaUserData)); data->Type = LuaSoundType; data->Data = id; return 1; }
/** ** Set the editor's start location unit ** ** @param l Lua state. */ static int CclSetEditorStartUnit(lua_State *l) { LuaCheckArgs(l, 1); delete[] Editor.StartUnitName; Editor.StartUnitName = new_strdup(LuaToString(l, 1)); return 0; }
/** ** Define a plane. ** ** @param l Lua state. */ static int CclDefinePlane(lua_State *l) { LuaCheckArgs(l, 2); if (!lua_istable(l, 2)) { LuaError(l, "incorrect argument (expected table)"); } std::string plane_name = LuaToString(l, 1); CPlane *plane = GetPlane(plane_name); if (!plane) { plane = new CPlane; plane->Name = plane_name; plane->ID = Planes.size(); Planes.push_back(plane); } // Parse the list: for (lua_pushnil(l); lua_next(l, 2); lua_pop(l, 1)) { const char *value = LuaToString(l, -2); if (!strcmp(value, "Description")) { plane->Description = LuaToString(l, -1); } else if (!strcmp(value, "Background")) { plane->Background = LuaToString(l, -1); } else if (!strcmp(value, "Quote")) { plane->Quote = LuaToString(l, -1); } else { LuaError(l, "Unsupported tag: %s" _C_ value); } } return 0; }
/** ** Enable display mana as mana-sprite. ** ** Equivalent of ** DefineDecorations({Index = "Mana", HideNeutral = true, CenterX = true, ** OffsetPercent = {50, 100}, ** Method = {"sprite", {0}}}) ** For index Mana, Transport, Research, Training, UpgradeTo, Resource. ** Except for ressource which have HideNeutral = false. ** ** @param l Lua state */ static int CclShowManaDot(lua_State *l) { char lua_equiv[] = "DefineDecorations({Index = \"Mana\", HideNeutral = true, CenterX = true," "OffsetPercent = {50, 100},Method = {\"sprite\", {\"sprite-mana\"}}})\n" "DefineDecorations({Index = \"Transport\", HideNeutral = true, CenterX = true," "OffsetPercent = {50, 100},Method = {\"sprite\", {\"sprite-mana\"}}})\n" "DefineDecorations({Index = \"Research\", HideNeutral = true, CenterX = true," "OffsetPercent = {50, 100},Method = {\"sprite\", {\"sprite-mana\"}}})\n" "DefineDecorations({Index = \"Training\", HideNeutral = true, CenterX = true," "OffsetPercent = {50, 100},Method = {\"sprite\", {\"sprite-mana\"}}})\n" "DefineDecorations({Index = \"UpgradeTo\", HideNeutral = true, CenterX = true," "OffsetPercent = {50, 100},Method = {\"sprite\", {\"sprite-mana\"}}})\n" "DefineDecorations({Index = \"GiveResource\", HideNeutral = false, CenterX = true," "OffsetPercent = {50, 100},Method = {\"sprite\", {\"sprite-mana\"}}})\n" "DefineDecorations({Index = \"CarryResource\", HideNeutral = false, CenterX = true," "OffsetPercent = {50, 100},Method = {\"sprite\", {\"sprite-mana\"}}})\n"; LuaCheckArgs(l, 0); DebugPrint("instead of ShowManaDot, you should write instead :\n%s\n" _C_ lua_equiv); CclCommand(lua_equiv); return 0; }
/** ** Build tileset tables like humanWallTable or mixedLookupTable ** ** Called after DefineTileset and only for tilesets that have wall, ** trees and rocks. This function will be deleted when removing ** support of walls and alike in the tileset. */ static int CclBuildTilesetTables(lua_State *l) { LuaCheckArgs(l, 0); Map.Tileset->buildTable(l); return 0; }
/** ** Get the video resolution. ** ** @param l Lua state. */ static int CclGetVideoResolution(lua_State *l) { LuaCheckArgs(l, 0); lua_pushnumber(l, Video.Width); lua_pushnumber(l, Video.Height); return 2; }
/** ** Show Map Location ** ** @param l Lua state. */ static int CclShowMapLocation(lua_State *l) { // Put a unit on map, use its properties, except for // what is listed below LuaCheckArgs(l, 4); const char *unitname = LuaToString(l, 5); CUnitType *unitType = UnitTypeByIdent(unitname); if (!unitType) { DebugPrint("Unable to find UnitType '%s'" _C_ unitname); return 0; } CUnit *target = MakeUnit(*unitType, ThisPlayer); if (target != NULL) { target->Variable[HP_INDEX].Value = 0; target->tilePos.x = LuaToNumber(l, 1); target->tilePos.y = LuaToNumber(l, 2); target->TTL = GameCycle + LuaToNumber(l, 4); target->CurrentSightRange = LuaToNumber(l, 3); //Wyrmgus start UpdateUnitSightRange(*target); //Wyrmgus end MapMarkUnitSight(*target); } else { DebugPrint("Unable to allocate Unit"); } return 0; }
/** ** Checks if dependencies are met. ** ** @return true if the dependencies are met. ** ** @param l Lua state. ** Argument 1: player ** Argument 2: object which we want to check the dependencies of */ static int CclCheckDependency(lua_State *l) { LuaCheckArgs(l, 2); const char *object = LuaToString(l, 2); lua_pop(l, 1); const int plynr = TriggerGetPlayer(l); if (plynr == -1) { LuaError(l, "bad player: %i" _C_ plynr); } const CPlayer *player = &Players[plynr]; if (!strncmp(object, "unit-", 5)) { const CUnitType *unit_type = UnitTypeByIdent(object); if (!unit_type) { LuaError(l, "Invalid unit type: \"%s\"" _C_ object); } lua_pushboolean(l, CheckDependencies(unit_type, player)); } else if (!strncmp(object, "upgrade-", 8)) { const CUpgrade *upgrade = CUpgrade::Get(object); if (!upgrade) { LuaError(l, "Invalid upgrade: \"%s\"" _C_ object); } lua_pushboolean(l, CheckDependencies(upgrade, player)); } else { LuaError(l, "Invalid target of dependency check: \"%s\"" _C_ object); } return 1; }
/** ** Define the group. ** ** @param l Lua state. */ static int CclGroup(lua_State *l) { int i; CUnitGroup *grp; int args; int j; LuaCheckArgs(l, 3); InitGroups(); grp = &Groups[(int)LuaToNumber(l, 1)]; grp->NumUnits = LuaToNumber(l, 2); i = 0; args = lua_objlen(l, 3); for (j = 0; j < args; ++j) { const char *str; lua_rawgeti(l, 3, j + 1); str = LuaToString(l, -1); lua_pop(l, 1); grp->Units[i++] = UnitSlots[strtol(str + 1, NULL, 16)]; } return 0; }
/** ** Define the lua file that will build the map ** ** @param l Lua state. */ static int CclDefineMapSetup(lua_State *l) { LuaCheckArgs(l, 1); Map.Info.Filename = LuaToString(l, 1); return 0; }
/** ** Check if the tile's terrain has a particular flag. ** ** @param l Lua state. ** ** @return True if has the flag, false if not. */ static int CclGetTileTerrainHasFlag(lua_State *l) { LuaCheckArgs(l, 3); const Vec2i pos(LuaToNumber(l, 1), LuaToNumber(l, 2)); //Wyrmgus start if (pos.x < 0 || pos.x >= Map.Info.MapWidth || pos.y < 0 || pos.y >= Map.Info.MapHeight) { lua_pushboolean(l, 0); return 1; } // unsigned short flag = 0; unsigned long flag = 0; //Wyrmgus end const char *flag_name = LuaToString(l, 3); if (!strcmp(flag_name, "water")) { flag = MapFieldWaterAllowed; } else if (!strcmp(flag_name, "land")) { flag = MapFieldLandAllowed; } else if (!strcmp(flag_name, "coast")) { flag = MapFieldCoastAllowed; } else if (!strcmp(flag_name, "no-building")) { flag = MapFieldNoBuilding; } else if (!strcmp(flag_name, "unpassable")) { flag = MapFieldUnpassable; //Wyrmgus start } else if (!strcmp(flag_name, "air-unpassable")) { flag = MapFieldAirUnpassable; } else if (!strcmp(flag_name, "dirt")) { flag = MapFieldDirt; } else if (!strcmp(flag_name, "grass")) { flag = MapFieldGrass; } else if (!strcmp(flag_name, "gravel")) { flag = MapFieldGravel; } else if (!strcmp(flag_name, "mud")) { flag = MapFieldMud; } else if (!strcmp(flag_name, "stone-floor")) { flag = MapFieldStoneFloor; } else if (!strcmp(flag_name, "stumps")) { flag = MapFieldStumps; //Wyrmgus end } else if (!strcmp(flag_name, "wall")) { flag = MapFieldWall; } else if (!strcmp(flag_name, "rock")) { flag = MapFieldRocks; } else if (!strcmp(flag_name, "forest")) { flag = MapFieldForest; } const CMapField &mf = *Map.Field(pos); if (mf.getFlag() & flag) { lua_pushboolean(l, 1); } else { lua_pushboolean(l, 0); } return 1; }
/** ** Define player colors ** ** @param l Lua state. */ static int CclDefinePlayerColors(lua_State *l) { LuaCheckArgs(l, 1); if (!lua_istable(l, 1)) { LuaError(l, "incorrect argument"); } const int args = lua_rawlen(l, 1); for (int i = 0; i < args; ++i) { PlayerColorNames[i / 2] = LuaToString(l, 1, i + 1); ++i; lua_rawgeti(l, 1, i + 1); if (!lua_istable(l, -1)) { LuaError(l, "incorrect argument"); } const int numcolors = lua_rawlen(l, -1); if (numcolors != PlayerColorIndexCount) { LuaError(l, "You should use %d colors (See DefinePlayerColorIndex())" _C_ PlayerColorIndexCount); } for (int j = 0; j < numcolors; ++j) { lua_rawgeti(l, -1, j + 1); PlayerColorsRGB[i / 2][j].Parse(l); lua_pop(l, 1); } } return 0; }
/** ** Dump some AI debug informations. ** ** @param l Lua state. */ static int CclAiDump(lua_State *l) { LuaCheckArgs(l, 0); // // Script // printf("------\n"); for (int i = 0; i < MaxCosts; ++i) { printf("%s(%4d) ", DefaultResourceNames[i].c_str(), AiPlayer->Player->Resources[i]); } printf("\n"); printf("%d:", AiPlayer->Player->Index); #if 0 gh_display(gh_car(AiPlayer->Script)); #endif // // Requests // size_t n = AiPlayer->UnitTypeRequests.size(); printf("UnitTypeRequests(%u):\n", static_cast<unsigned int>(n)); for (size_t i = 0; i < n; ++i) { printf("%s ", AiPlayer->UnitTypeRequests[i].Type->Ident.c_str()); } printf("\n"); n = AiPlayer->UpgradeToRequests.size(); printf("UpgradeToRequests(%u):\n", static_cast<unsigned int>(n)); for (size_t i = 0; i < n; ++i) { printf("%s ", AiPlayer->UpgradeToRequests[i]->Ident.c_str()); } printf("\n"); n = AiPlayer->ResearchRequests.size(); printf("ResearchRequests(%u):\n", static_cast<unsigned int>(n)); for (size_t i = 0; i < n; ++i) { printf("%s ", AiPlayer->ResearchRequests[i]->Ident.c_str()); } printf("\n"); // Building queue printf("Building queue:\n"); for (size_t i = 0; i < AiPlayer->UnitTypeBuilt.size(); ++i) { const AiBuildQueue &queue = AiPlayer->UnitTypeBuilt[i]; printf("%s(%d/%d) ", queue.Type->Ident.c_str(), queue.Made, queue.Want); } printf("\n"); // PrintForce for (size_t i = 0; i < AiPlayer->Force.Size(); ++i) { printf("Force(%u%s%s):\n", static_cast<unsigned int>(i), AiPlayer->Force[i].Completed ? ",complete" : ",recruit", AiPlayer->Force[i].Attacking ? ",attack" : ""); for (size_t j = 0; j < AiPlayer->Force[i].UnitTypes.size(); ++j) { const AiUnitType &aut = AiPlayer->Force[i].UnitTypes[j]; printf("%s(%d) ", aut.Type->Ident.c_str(), aut.Want); } printf("\n"); } lua_pushboolean(l, 0); return 1; }
/** ** Set the metaserver to use for internet play. ** ** @param l Lua state. */ int CclSetMetaServer(lua_State *l) { LuaCheckArgs(l, 2); MasterHost = LuaToString(l, 1); MasterPort = LuaToNumber(l, 2); return 0; }
/** ** Center the map. ** ** @param l Lua state. */ static int CclCenterMap(lua_State *l) { LuaCheckArgs(l, 2); const Vec2i pos(LuaToNumber(l, 1), LuaToNumber(l, 2)); UI.SelectedViewport->Center(Map.TilePosToMapPixelPos_Center(pos)); return 0; }
/** ** Need a unit. ** ** @param l Lua state. ** ** @return Number of return values */ static int CclAiNeed(lua_State *l) { LuaCheckArgs(l, 1); InsertUnitTypeRequests(CclGetUnitType(l), 1); lua_pushboolean(l, 0); return 1; }
/** ** Set MaxSelectable ** ** @param l Lua state. */ static int CclSetMaxSelectable(lua_State *l) { LuaCheckArgs(l, 1); MaxSelectable = LuaToNumber(l, 1); lua_pushnumber(l, MaxSelectable); return 1; }
/** ** Center the map. ** ** @param l Lua state. */ static int CclCenterMap(lua_State *l) { LuaCheckArgs(l, 2); UI.SelectedViewport->Center( LuaToNumber(l, 1), LuaToNumber(l, 2), TileSizeX / 2, TileSizeY / 2); return 0; }
/** ** Player has the quantity of rescued unit-type near to unit-type. */ static int CclIfRescuedNearUnit(lua_State *l) { LuaCheckArgs(l, 5); lua_pushvalue(l, 1); const int plynr = TriggerGetPlayer(l); lua_pop(l, 1); const char *op = LuaToString(l, 2); const int q = LuaToNumber(l, 3); lua_pushvalue(l, 4); const CUnitType *unittype = TriggerGetUnitType(l); lua_pop(l, 1); const CUnitType *ut2 = CclGetUnitType(l); if (!unittype || !ut2) { LuaError(l, "CclIfRescuedNearUnit: not a unit-type valid"); } CompareFunction compare = GetCompareFunction(op); if (!compare) { LuaError(l, "Illegal comparison operation in if-rescued-near-unit: %s" _C_ op); } // Get all unit types 'near'. std::vector<CUnit *> table; FindUnitsByType(*ut2, table); for (size_t i = 0; i != table.size(); ++i) { CUnit ¢erUnit = *table[i]; std::vector<CUnit *> around; SelectAroundUnit(centerUnit, 1, around); // Count the requested units int s = 0; for (size_t j = 0; j != around.size(); ++j) { CUnit &unit = *around[j]; if (unit.RescuedFrom) { // only rescued units // Check unit type if (unittype == ANY_UNIT || (unittype == ALL_FOODUNITS && !unit.Type->Building) || (unittype == ALL_BUILDINGS && unit.Type->Building) || (unittype == unit.Type)) { // Check the player if (plynr == -1 || plynr == unit.Player->Index) { ++s; } } } } if (compare(s, q)) { lua_pushboolean(l, 1); return 1; } } lua_pushboolean(l, 0); return 1; }
/** ** Disable display of full bars/dots. ** ** @param l Lua state */ static int CclShowNoFull(lua_State *l) { LuaCheckArgs(l, 0); for (std::vector<CDecoVar *>::iterator i = UnitTypeVar.DecoVar.begin(); i < UnitTypeVar.DecoVar.end(); ++i) { (*i)->ShowWhenMax = 0; } return 0; }
/** ** Set ThisPlayer. ** ** @param l Lua state. */ static int CclSetThisPlayer(lua_State *l) { LuaCheckArgs(l, 1); int plynr = LuaToNumber(l, 1); ThisPlayer = &Players[plynr]; lua_pushnumber(l, plynr); return 1; }
/** ** Enable energy bars and dots only for selected units ** ** @param l Lua state */ static int CclShowEnergySelected(lua_State *l) { LuaCheckArgs(l, 0); for (std::vector<CDecoVar *>::const_iterator i = UnitTypeVar.DecoVar.begin(); i < UnitTypeVar.DecoVar.end(); ++i) { (*i)->ShowOnlySelected = true; } return 0; }
/** ** Upgrade an unit to an new unit-type. ** ** @param l Lua state. */ static int CclAiUpgradeTo(lua_State *l) { LuaCheckArgs(l, 1); CUnitType *type = CclGetUnitType(l); InsertUpgradeToRequests(type); lua_pushboolean(l, 0); return 1; }
static int CclSetMaxOpenGLTexture(lua_State *l) { LuaCheckArgs(l, 1); #if defined(USE_OPENGL) || defined(USE_GLES) if (CclInConfigFile) { GLMaxTextureSizeOverride = LuaToNumber(l, 1); } #endif return 0; }
static int CclSetUseTextureCompression(lua_State *l) { LuaCheckArgs(l, 1); #if defined(USE_OPENGL) || defined(USE_GLES) if (CclInConfigFile) { UseGLTextureCompression = LuaToBoolean(l, 1); } #endif return 0; }
/** ** Define the starting viewpoint for a given player. ** ** @param l Lua state. */ static int CclSetStartView(lua_State *l) { LuaCheckArgs(l, 3); const int p = LuaToNumber(l, 1); Players[p].StartPos.x = LuaToNumber(l, 2); Players[p].StartPos.y = LuaToNumber(l, 3); return 0; }