/** ** Gets the player data. ** ** @param player Player number. ** @param prop Player's property. ** @param arg Additional argument (for resource and unit). ** ** @return Returning value (only integer). */ int GetPlayerData(const int player, const char *prop, const char *arg) { if (!strcmp(prop, "RaceName")) { return Players[player].Race; } else if (!strcmp(prop, "Resources")) { const int resId = GetResourceIdByName(arg); if (resId == -1) { fprintf(stderr, "Invalid resource \"%s\"", arg); Exit(1); } return Players[player].Resources[resId] + Players[player].StoredResources[resId]; } else if (!strcmp(prop, "StoredResources")) { const int resId = GetResourceIdByName(arg); if (resId == -1) { fprintf(stderr, "Invalid resource \"%s\"", arg); Exit(1); } return Players[player].StoredResources[resId]; } else if (!strcmp(prop, "MaxResources")) { const int resId = GetResourceIdByName(arg); if (resId == -1) { fprintf(stderr, "Invalid resource \"%s\"", arg); Exit(1); } return Players[player].MaxResources[resId]; } else if (!strcmp(prop, "UnitTypesCount")) { const std::string unit(arg); CUnitType *type = UnitTypeByIdent(unit); return Players[player].UnitTypesCount[type->Slot]; } else if (!strcmp(prop, "AiEnabled")) { return Players[player].AiEnabled; } else if (!strcmp(prop, "TotalNumUnits")) { return Players[player].GetUnitCount(); } else if (!strcmp(prop, "NumBuildings")) { return Players[player].NumBuildings; } else if (!strcmp(prop, "Supply")) { return Players[player].Supply; } else if (!strcmp(prop, "Demand")) { return Players[player].Demand; } else if (!strcmp(prop, "UnitLimit")) { return Players[player].UnitLimit; } else if (!strcmp(prop, "BuildingLimit")) { return Players[player].BuildingLimit; } else if (!strcmp(prop, "TotalUnitLimit")) { return Players[player].TotalUnitLimit; } else if (!strcmp(prop, "Score")) { return Players[player].Score; } else if (!strcmp(prop, "TotalUnits")) { return Players[player].TotalUnits; } else if (!strcmp(prop, "TotalBuildings")) { return Players[player].TotalBuildings; } else if (!strcmp(prop, "TotalResources")) { const int resId = GetResourceIdByName(arg); if (resId == -1) { fprintf(stderr, "Invalid resource \"%s\"", arg); Exit(1); } return Players[player].TotalResources[resId]; } else if (!strcmp(prop, "TotalRazings")) { return Players[player].TotalRazings; } else if (!strcmp(prop, "TotalKills")) { return Players[player].TotalKills; } else { fprintf(stderr, "Invalid field: %s" _C_ prop); Exit(1); } return 0; }
/** ** Define a new upgrade modifier. ** ** @param l List of modifiers. */ static int CclDefineModifier(lua_State *l) { const int args = lua_gettop(l); CUpgradeModifier *um = new CUpgradeModifier; memset(um->ChangeUpgrades, '?', sizeof(um->ChangeUpgrades)); memset(um->ApplyTo, '?', sizeof(um->ApplyTo)); um->Modifier.Variables = new CVariable[UnitTypeVar.GetNumberVariable()]; um->ModifyPercent = new int[UnitTypeVar.GetNumberVariable()]; memset(um->ModifyPercent, 0, UnitTypeVar.GetNumberVariable() * sizeof(int)); std::string upgrade_ident = LuaToString(l, 1); um->UpgradeId = UpgradeIdByIdent(upgrade_ident); if (um->UpgradeId == -1) { LuaError(l, "Error when defining upgrade modifier: upgrade \"%s\" doesn't exist." _C_ upgrade_ident.c_str()); } for (int j = 1; j < args; ++j) { if (!lua_istable(l, j + 1)) { LuaError(l, "incorrect argument"); } const char *key = LuaToString(l, j + 1, 1); #if 0 // To be removed. must modify lua file. if (!strcmp(key, "attack-range")) { key = "AttackRange"; } else if (!strcmp(key, "sight-range")) { key = "SightRange"; } else if (!strcmp(key, "basic-damage")) { key = "BasicDamage"; } else if (!strcmp(key, "piercing-damage")) { key = "PiercingDamage"; } else if (!strcmp(key, "armor")) { key = "Armor"; } else if (!strcmp(key, "hit-points")) { key = "HitPoints"; } #endif if (!strcmp(key, "regeneration-rate")) { um->Modifier.Variables[HP_INDEX].Increase = LuaToNumber(l, j + 1, 2); } else if (!strcmp(key, "cost")) { if (!lua_istable(l, j + 1) || lua_rawlen(l, j + 1) != 2) { LuaError(l, "incorrect argument"); } const char *value = LuaToString(l, j + 1, 1); const int resId = GetResourceIdByName(l, value); um->Modifier.Costs[resId] = LuaToNumber(l, j + 1, 2); } else if (!strcmp(key, "storing")) { if (!lua_istable(l, j + 1) || lua_rawlen(l, j + 1) != 2) { LuaError(l, "incorrect argument"); } const char *value = LuaToString(l, j + 1, 1); const int resId = GetResourceIdByName(l, value); um->Modifier.Storing[resId] = LuaToNumber(l, j + 1, 2); } else if (!strcmp(key, "allow-unit")) { const char *value = LuaToString(l, j + 1, 2); if (!strncmp(value, "unit-", 5)) { um->ChangeUnits[UnitTypeIdByIdent(value)] = LuaToNumber(l, j + 1, 3); } else { LuaError(l, "unit expected"); } } else if (!strcmp(key, "allow")) { const char *value = LuaToString(l, j + 1, 2); if (!strncmp(value, "upgrade-", 8)) { um->ChangeUpgrades[UpgradeIdByIdent(value)] = LuaToNumber(l, j + 1, 3); } else { LuaError(l, "upgrade expected"); } } else if (!strcmp(key, "apply-to")) { const char *value = LuaToString(l, j + 1, 2); um->ApplyTo[UnitTypeIdByIdent(value)] = 'X'; } else if (!strcmp(key, "convert-to")) { const char *value = LuaToString(l, j + 1, 2); um->ConvertTo = UnitTypeByIdent(value); } else if (!strcmp(key, "research-speed")) { um->SpeedResearch = LuaToNumber(l, j + 1, 2); } else { int index = UnitTypeVar.VariableNameLookup[key]; // variable index; if (index != -1) { if (lua_rawlen(l, j + 1) == 3) { const char *value = LuaToString(l, j + 1, 3); if (!strcmp(value, "Percent")) { um->ModifyPercent[index] = LuaToNumber(l, j + 1, 2); } } else { lua_rawgeti(l, j + 1, 2); if (lua_istable(l, -1)) { DefineVariableField(l, um->Modifier.Variables + index, -1); } else if (lua_isnumber(l, -1)) { um->Modifier.Variables[index].Enable = 1; um->Modifier.Variables[index].Value = LuaToNumber(l, -1); um->Modifier.Variables[index].Max = LuaToNumber(l, -1); } else { LuaError(l, "bad argument type for '%s'\n" _C_ key); } lua_pop(l, 1); } } else { LuaError(l, "wrong tag: %s" _C_ key); } } } UpgradeModifiers[NumUpgradeModifiers++] = um; return 0; }