Add GetRaceIndexByName into PlayerRace and use it.

Factorize some code in CclDefineGameSounds.
This commit is contained in:
Joris 2012-08-31 15:27:00 +02:00
parent 331d43bee9
commit 9ba391ceab
5 changed files with 95 additions and 251 deletions

View file

@ -86,7 +86,7 @@ void CleanModules()
CleanFonts();
CleanTriggers();
FreeAi();
CleanRaces();
PlayerRaces.Clean();
CleanConstructions();
CleanDecorations();
CleanMissiles();

View file

@ -243,10 +243,14 @@ public:
memset(Visible, 0, sizeof(Visible));
}
void Clean();
int GetRaceIndexByName(const char *raceName) const;
public:
bool Visible[MAX_RACES]; /// race should be visible in pulldown
std::string Name[MAX_RACES]; /// race names
std::string Display[MAX_RACES]; /// text to display in pulldown
unsigned int Count; /// number of races
std::string Name[MAX_RACES]; /// race names
std::string Display[MAX_RACES]; /// text to display in pulldown
unsigned int Count; /// number of races
};
@ -341,8 +345,6 @@ extern int PlayerColorIndexCount;
extern void InitPlayers();
/// Clean up players
extern void CleanPlayers();
/// Clean up races
extern void CleanRaces();
/// Save players
extern void SavePlayers(CFile &file);

View file

@ -209,6 +209,28 @@ static int CclPlaySound(lua_State *l)
return 0;
}
static void SetSoundConfigRace(lua_State *l, int j, SoundConfig soundConfigs[])
{
if (!lua_istable(l, j + 1) || lua_rawlen(l, j + 1) != 2) {
LuaError(l, "incorrect argument");
}
lua_rawgeti(l, j + 1, 1);
const char *raceName = LuaToString(l, -1);
lua_pop(l, 1);
const int raceIndex = PlayerRaces.GetRaceIndexByName(raceName);
if (raceIndex == -1) {
LuaError(l, "Unknown race: %s" _C_ raceName);
}
lua_rawgeti(l, j + 1, 2);
LuaUserData *data = NULL;
if (!lua_isuserdata(l, -1)
|| (data = (LuaUserData *)lua_touserdata(l, -1))->Type != LuaSoundType) {
LuaError(l, "Sound id expected");
}
lua_pop(l, 1);
soundConfigs[raceIndex].Sound = (CSound *)data->Data;
}
/**
** Glue between c and scheme. Allows to specify some global game sounds
** in a ccl file.
@ -218,7 +240,6 @@ static int CclPlaySound(lua_State *l)
static int CclDefineGameSounds(lua_State *l)
{
//FIXME: should allow to define ALL the game sounds
unsigned int i;
const int args = lua_gettop(l);
for (int j = 0; j < args; ++j) {
@ -241,111 +262,27 @@ static int CclDefineGameSounds(lua_State *l)
}
GameSounds.Docking.Sound = (CSound *)data->Data;
} else if (!strcmp(value, "placement-error")) {
if (!lua_istable(l, j + 1) || lua_rawlen(l, j + 1) != 2) {
LuaError(l, "incorrect argument");
}
lua_rawgeti(l, j + 1, 1);
value = LuaToString(l, -1);
lua_pop(l, 1);
for (i = 0; i < PlayerRaces.Count; ++i) {
if (!strcmp(PlayerRaces.Name[i].c_str(), value)) {
break;
}
}
if (i == PlayerRaces.Count) {
LuaError(l, "Unknown race: %s" _C_ value);
}
lua_rawgeti(l, j + 1, 2);
if (!lua_isuserdata(l, -1)
|| (data = (LuaUserData *)lua_touserdata(l, -1))->Type != LuaSoundType) {
LuaError(l, "Sound id expected");
}
lua_pop(l, 1);
GameSounds.PlacementError[i].Sound = (CSound *)data->Data;
SetSoundConfigRace(l, j, GameSounds.PlacementError);
} else if (!strcmp(value, "placement-success")) {
if (!lua_istable(l, j + 1) || lua_rawlen(l, j + 1) != 2) {
LuaError(l, "incorrect argument");
}
lua_rawgeti(l, j + 1, 1);
value = LuaToString(l, -1);
lua_pop(l, 1);
for (i = 0; i < PlayerRaces.Count; ++i) {
if (!strcmp(PlayerRaces.Name[i].c_str(), value)) {
break;
}
}
if (i == PlayerRaces.Count) {
LuaError(l, "Unknown race: %s" _C_ value);
}
lua_rawgeti(l, j + 1, 2);
if (!lua_isuserdata(l, -1)
|| (data = (LuaUserData *)lua_touserdata(l, -1))->Type != LuaSoundType) {
LuaError(l, "Sound id expected");
}
lua_pop(l, 1);
GameSounds.PlacementSuccess[i].Sound = (CSound *)data->Data;
SetSoundConfigRace(l, j, GameSounds.PlacementSuccess);
} else if (!strcmp(value, "work-complete")) {
if (!lua_istable(l, j + 1) || lua_rawlen(l, j + 1) != 2) {
LuaError(l, "incorrect argument");
}
lua_rawgeti(l, j + 1, 1);
value = LuaToString(l, -1);
lua_pop(l, 1);
for (i = 0; i < PlayerRaces.Count; ++i) {
if (!strcmp(PlayerRaces.Name[i].c_str(), value)) {
break;
}
}
if (i == PlayerRaces.Count) {
LuaError(l, "Unknown race: %s" _C_ value);
}
lua_rawgeti(l, j + 1, 2);
if (!lua_isuserdata(l, -1)
|| (data = (LuaUserData *)lua_touserdata(l, -1))->Type != LuaSoundType) {
LuaError(l, "Sound id expected");
}
lua_pop(l, 1);
GameSounds.WorkComplete[i].Sound = (CSound *)data->Data;
SetSoundConfigRace(l, j, GameSounds.WorkComplete);
} else if (!strcmp(value, "research-complete")) {
if (!lua_istable(l, j + 1) || lua_rawlen(l, j + 1) != 2) {
LuaError(l, "incorrect argument");
}
lua_rawgeti(l, j + 1, 1);
value = LuaToString(l, -1);
lua_pop(l, 1);
for (i = 0; i < PlayerRaces.Count; ++i) {
if (!strcmp(PlayerRaces.Name[i].c_str(), value)) {
break;
}
}
if (i == PlayerRaces.Count) {
LuaError(l, "Unknown race: %s" _C_ value);
}
lua_rawgeti(l, j + 1, 2);
if (!lua_isuserdata(l, -1)
|| (data = (LuaUserData *)lua_touserdata(l, -1))->Type != LuaSoundType) {
LuaError(l, "Sound id expected");
}
lua_pop(l, 1);
GameSounds.ResearchComplete[i].Sound = (CSound *)data->Data;
SetSoundConfigRace(l, j, GameSounds.ResearchComplete);
} else if (!strcmp(value, "not-enough-res")) {
if (!lua_istable(l, j + 1) || lua_rawlen(l, j + 1) != 3) {
LuaError(l, "incorrect argument");
}
lua_rawgeti(l, j + 1, 1);
value = LuaToString(l, -1);
const char *resName = LuaToString(l, -1);
lua_pop(l, 1);
const int resId = GetResourceIdByName(l, value);
const int resId = GetResourceIdByName(l, resName);
lua_rawgeti(l, j + 1, 2);
value = LuaToString(l, -1);
const char *raceName = LuaToString(l, -1);
lua_pop(l, 1);
for (i = 0; i < PlayerRaces.Count; ++i) {
if (!strcmp(PlayerRaces.Name[i].c_str(), value)) {
break;
}
}
if (i == PlayerRaces.Count) {
LuaError(l, "Unknown race: %s" _C_ value);
const int raceIndex = PlayerRaces.GetRaceIndexByName(raceName);
if (raceIndex == -1) {
LuaError(l, "Unknown race: %s" _C_ raceName);
}
lua_rawgeti(l, j + 1, 3);
if (!lua_isuserdata(l, -1)
@ -353,73 +290,13 @@ static int CclDefineGameSounds(lua_State *l)
LuaError(l, "Sound id expected");
}
lua_pop(l, 1);
GameSounds.NotEnoughRes[i][resId].Sound = (CSound *)data->Data;
GameSounds.NotEnoughRes[raceIndex][resId].Sound = (CSound *)data->Data;
} else if (!strcmp(value, "not-enough-food")) {
if (!lua_istable(l, j + 1) || lua_rawlen(l, j + 1) != 2) {
LuaError(l, "incorrect argument");
}
lua_rawgeti(l, j + 1, 1);
value = LuaToString(l, -1);
lua_pop(l, 1);
for (i = 0; i < PlayerRaces.Count; ++i) {
if (!strcmp(PlayerRaces.Name[i].c_str(), value)) {
break;
}
}
if (i == PlayerRaces.Count) {
LuaError(l, "Unknown race: %s" _C_ value);
}
lua_rawgeti(l, j + 1, 2);
if (!lua_isuserdata(l, -1)
|| (data = (LuaUserData *)lua_touserdata(l, -1))->Type != LuaSoundType) {
LuaError(l, "Sound id expected");
}
lua_pop(l, 1);
GameSounds.NotEnoughFood[i].Sound = (CSound *)data->Data;
SetSoundConfigRace(l, j, GameSounds.NotEnoughFood);
} else if (!strcmp(value, "rescue")) {
if (!lua_istable(l, j + 1) || lua_rawlen(l, j + 1) != 2) {
LuaError(l, "incorrect argument");
}
lua_rawgeti(l, j + 1, 1);
value = LuaToString(l, -1);
lua_pop(l, 1);
for (i = 0; i < PlayerRaces.Count; ++i) {
if (!strcmp(PlayerRaces.Name[i].c_str(), value)) {
break;
}
}
if (i == PlayerRaces.Count) {
LuaError(l, "Unknown race: %s" _C_ value);
}
lua_rawgeti(l, j + 1, 2);
if (!lua_isuserdata(l, -1)
|| (data = (LuaUserData *)lua_touserdata(l, -1))->Type != LuaSoundType) {
LuaError(l, "Sound id expected");
}
lua_pop(l, 1);
GameSounds.Rescue[i].Sound = (CSound *)data->Data;
SetSoundConfigRace(l, j, GameSounds.Rescue);
} else if (!strcmp(value, "building-construction")) {
if (!lua_istable(l, j + 1) || lua_rawlen(l, j + 1) != 2) {
LuaError(l, "incorrect argument");
}
lua_rawgeti(l, j + 1, 1);
value = LuaToString(l, -1);
lua_pop(l, 1);
for (i = 0; i < PlayerRaces.Count; ++i) {
if (!strcmp(PlayerRaces.Name[i].c_str(), value)) {
break;
}
}
if (i == PlayerRaces.Count) {
LuaError(l, "Unknown race: %s" _C_ value);
}
lua_rawgeti(l, j + 1, 2);
if (!lua_isuserdata(l, -1)
|| (data = (LuaUserData *)lua_touserdata(l, -1))->Type != LuaSoundType) {
LuaError(l, "Sound id expected");
}
lua_pop(l, 1);
GameSounds.BuildingConstruction[i].Sound = (CSound *)data->Data;
SetSoundConfigRace(l, j, GameSounds.BuildingConstruction);
} else if (!strcmp(value, "chat-message")) {
if (!lua_isuserdata(l, j + 1)
|| (data = (LuaUserData *)lua_touserdata(l, j + 1))->Type != LuaSoundType) {
@ -440,12 +317,10 @@ static int CclDefineGameSounds(lua_State *l)
*/
static int CclSetGlobalSoundRange(lua_State *l)
{
int d;
LuaCheckArgs(l, 1);
// FIXME: check for errors
d = LuaToNumber(l, 1);
int d = LuaToNumber(l, 1);
if (d > 0) {
DistanceSilent = d;
}

View file

@ -330,6 +330,30 @@ int PlayerColorIndexCount;
-- Functions
----------------------------------------------------------------------------*/
/**
** Clean up the PlayerRaces names.
*/
void PlayerRace::Clean()
{
for (unsigned int i = 0; i != this->Count; ++i) {
this->Name[i].clear();
this->Display[i].clear();
this->Visible[i] = false;
}
this->Count = 0;
}
int PlayerRace::GetRaceIndexByName(const char *raceName) const
{
for (unsigned int i = 0; i != this->Count; ++i) {
if (this->Name[i].compare(raceName) == 0) {
return i;
}
}
return -1;
}
/**
** Init players.
*/
@ -370,18 +394,6 @@ void FreePlayerColors()
}
#endif
/**
** Clean up the PlayerRaces names.
*/
void CleanRaces()
{
for (unsigned int i = 0; i < PlayerRaces.Count; ++i) {
PlayerRaces.Name[i].clear();
PlayerRaces.Display[i].clear();
}
PlayerRaces.Count = 0;
}
/**
** Save state of players to file.
**

View file

@ -120,17 +120,10 @@ void CPlayer::Load(lua_State *l)
LuaError(l, "Unsupported tag: %s" _C_ value);
}
} else if (!strcmp(value, "race")) {
unsigned int i;
value = LuaToString(l, j + 1);
for (i = 0; i < PlayerRaces.Count; ++i) {
if (!strcmp(value, PlayerRaces.Name[i].c_str())) {
this->Race = i;
break;
}
}
if (i == PlayerRaces.Count) {
LuaError(l, "Unsupported race: %s" _C_ value);
const char* raceName = LuaToString(l, j + 1);
this->Race = PlayerRaces.GetRaceIndexByName(raceName);
if (this->Race == -1) {
LuaError(l, "Unsupported race: %s" _C_ raceName);
}
} else if (!strcmp(value, "ai-name")) {
this->AiName = LuaToString(l, j + 1);
@ -425,10 +418,8 @@ static int CclGetThisPlayer(lua_State *l)
*/
static int CclSetThisPlayer(lua_State *l)
{
int plynr;
LuaCheckArgs(l, 1);
plynr = LuaToNumber(l, 1);
int plynr = LuaToNumber(l, 1);
ThisPlayer = &Players[plynr];
lua_pushnumber(l, plynr);
@ -456,10 +447,8 @@ static int CclSetMaxSelectable(lua_State *l)
*/
static int CclSetAllPlayersUnitLimit(lua_State *l)
{
int i;
LuaCheckArgs(l, 1);
for (i = 0; i < PlayerMax; ++i) {
for (int i = 0; i < PlayerMax; ++i) {
Players[i].UnitLimit = LuaToNumber(l, 1);
}
@ -474,10 +463,8 @@ static int CclSetAllPlayersUnitLimit(lua_State *l)
*/
static int CclSetAllPlayersBuildingLimit(lua_State *l)
{
int i;
LuaCheckArgs(l, 1);
for (i = 0; i < PlayerMax; ++i) {
for (int i = 0; i < PlayerMax; ++i) {
Players[i].BuildingLimit = LuaToNumber(l, 1);
}
@ -492,10 +479,8 @@ static int CclSetAllPlayersBuildingLimit(lua_State *l)
*/
static int CclSetAllPlayersTotalUnitLimit(lua_State *l)
{
int i;
LuaCheckArgs(l, 1);
for (i = 0; i < PlayerMax; ++i) {
for (int i = 0; i < PlayerMax; ++i) {
Players[i].TotalUnitLimit = LuaToNumber(l, 1);
}
@ -512,14 +497,10 @@ static int CclSetAllPlayersTotalUnitLimit(lua_State *l)
*/
static int CclSetDiplomacy(lua_State *l)
{
int plynr;
int base;
const char *state;
LuaCheckArgs(l, 3);
base = LuaToNumber(l, 1);
plynr = LuaToNumber(l, 3);
state = LuaToString(l, 2);
const int base = LuaToNumber(l, 1);
const int plynr = LuaToNumber(l, 3);
const char *state = LuaToString(l, 2);
if (!strcmp(state, "allied")) {
SendCommandDiplomacy(base, DiplomacyAllied, plynr);
@ -530,7 +511,6 @@ static int CclSetDiplomacy(lua_State *l)
} else if (!strcmp(state, "enemy")) {
SendCommandDiplomacy(base, DiplomacyEnemy, plynr);
}
return 0;
}
@ -555,15 +535,11 @@ static int CclDiplomacy(lua_State *l)
*/
static int CclSetSharedVision(lua_State *l)
{
int plynr;
int base;
bool shared;
LuaCheckArgs(l, 3);
base = LuaToNumber(l, 1);
shared = LuaToBoolean(l, 2);
plynr = LuaToNumber(l, 3);
const int base = LuaToNumber(l, 1);
const bool shared = LuaToBoolean(l, 2);
const int plynr = LuaToNumber(l, 3);
SendCommandSharedVision(base, shared, plynr);
@ -589,28 +565,18 @@ static int CclSharedVision(lua_State *l)
*/
static int CclDefineRaceNames(lua_State *l)
{
int i;
int j;
int k;
int args;
int subargs;
const char *value;
PlayerRaces.Count = 0;
args = lua_gettop(l);
for (j = 0; j < args; ++j) {
value = LuaToString(l, j + 1);
PlayerRaces.Clean();
int args = lua_gettop(l);
for (int j = 0; j < args; ++j) {
const char *value = LuaToString(l, j + 1);
if (!strcmp(value, "race")) {
++j;
if (!lua_istable(l, j + 1)) {
LuaError(l, "incorrect argument");
}
subargs = lua_rawlen(l, j + 1);
i = PlayerRaces.Count++;
PlayerRaces.Name[i].clear();
PlayerRaces.Display[i].clear();
PlayerRaces.Visible[i] = false;
for (k = 0; k < subargs; ++k) {
int subargs = lua_rawlen(l, j + 1);
int i = PlayerRaces.Count++;
for (int k = 0; k < subargs; ++k) {
lua_rawgeti(l, j + 1, k + 1);
value = LuaToString(l, -1);
lua_pop(l, 1);
@ -852,32 +818,21 @@ static int CclGetPlayerData(lua_State *l)
*/
static int CclSetPlayerData(lua_State *l)
{
CPlayer *p;
const char *data;
if (lua_gettop(l) < 3) {
LuaError(l, "incorrect argument");
}
lua_pushvalue(l, 1);
p = CclGetPlayer(l);
CPlayer *p = CclGetPlayer(l);
lua_pop(l, 1);
data = LuaToString(l, 2);
const char *data = LuaToString(l, 2);
if (!strcmp(data, "Name")) {
p->SetName(LuaToString(l, 3));
} else if (!strcmp(data, "RaceName")) {
unsigned int i;
const char *racename;
const char *racename = LuaToString(l, 3);
p->Race = PlayerRaces.GetRaceIndexByName(racename);
racename = LuaToString(l, 3);
p->Race = 0;
for (i = 0; i < PlayerRaces.Count; ++i) {
if (!strcmp(racename, PlayerRaces.Name[i].c_str())) {
p->Race = i;
break;
}
}
if (i == PlayerRaces.Count) {
if (p->Race == -1) {
LuaError(l, "invalid race name '%s'" _C_ racename);
}
} else if (!strcmp(data, "Resources")) {