[+] Added support for new NumberDesc field, PlayerData, to get player's data

This commit is contained in:
cybermind 2014-05-29 17:35:30 +06:00
parent 7e02a25714
commit 6dc6dbf29e
6 changed files with 68 additions and 5 deletions

View file

@ -76,6 +76,13 @@ int GetPlayerData(const int player, const char *prop, const char *arg)
Exit(1);
}
return Players[player].MaxResources[resId];
} else if (!strcmp(prop, "Incomes")) {
const int resId = GetResourceIdByName(arg);
if (resId == -1) {
fprintf(stderr, "Invalid resource \"%s\"", arg);
Exit(1);
}
return Players[player].Incomes[resId];
} else if (!strcmp(prop, "UnitTypesCount")) {
const std::string unit(arg);
CUnitType *type = UnitTypeByIdent(unit);

View file

@ -120,7 +120,9 @@ enum ENumber {
ENumber_UnitStat, /// Property of Unit.
ENumber_TypeStat, /// Property of UnitType.
ENumber_NumIf /// If cond then Number1 else Number2.
ENumber_NumIf, /// If cond then Number1 else Number2.
ENumber_PlayerData /// Numeric Player Data
};
/// All possible value for a unit.
@ -231,6 +233,11 @@ struct NumberDesc {
NumberDesc *BTrue; /// Number if Cond is true.
NumberDesc *BFalse; /// Number if Cond is false.
} NumIf; /// conditional string.
struct {
NumberDesc *Player; /// Number of player
StringDesc *DataType; /// Player's data
StringDesc *ResType; /// Resource type
} PlayerData; /// conditional string.
} D;
};

View file

@ -211,6 +211,7 @@ enum {
SHIELDPERMEABILITY_INDEX,
SHIELDPIERCING_INDEX,
ISALIVE_INDEX,
PLAYER_INDEX,
NVARALREADYDEFINED
};

View file

@ -39,6 +39,7 @@
#include "script.h"
#include "animation/animation_setplayervar.h"
#include "font.h"
#include "game.h"
#include "iocompat.h"
@ -736,6 +737,20 @@ NumberDesc *CclParseNumberDesc(lua_State *l)
res->D.NumIf.BFalse = CclParseNumberDesc(l);
}
lua_pop(l, 1); // table.
} else if (!strcmp(key, "PlayerData")) {
res->e = ENumber_PlayerData;
if (lua_rawlen(l, -1) != 2 && lua_rawlen(l, -1) != 3) {
LuaError(l, "Bad number of args in PlayerData\n");
}
lua_rawgeti(l, -1, 1); // Player.
res->D.PlayerData.Player = CclParseNumberDesc(l);
lua_rawgeti(l, -1, 2); // DataType.
res->D.PlayerData.DataType = CclParseStringDesc(l);
if (lua_rawlen(l, -1) == 3) {
lua_rawgeti(l, -1, 3); // Res type.
res->D.PlayerData.ResType = CclParseStringDesc(l);
}
lua_pop(l, 1); // table.
} else {
lua_pop(l, 1);
LuaError(l, "unknow condition '%s'"_C_ key);
@ -995,6 +1010,11 @@ int EvalNumber(const NumberDesc *number)
} else {
return 0;
}
case ENumber_PlayerData : // getplayerdata(player, data, res);
int player = EvalNumber(number->D.PlayerData.Player);
std::string data = EvalString(number->D.PlayerData.DataType);
std::string res = EvalString(number->D.PlayerData.ResType);
return GetPlayerData(player, data.c_str(), res.c_str());
}
return 0;
}
@ -1176,6 +1196,14 @@ void FreeNumberDesc(NumberDesc *number)
FreeNumberDesc(number->D.NumIf.BFalse);
delete number->D.NumIf.BFalse;
break;
case ENumber_PlayerData : // getplayerdata(player, data, res);
FreeNumberDesc(number->D.PlayerData.Player);
delete number->D.PlayerData.Player;
FreeStringDesc(number->D.PlayerData.DataType);
delete number->D.PlayerData.DataType;
FreeStringDesc(number->D.PlayerData.ResType);
delete number->D.PlayerData.ResType;
break;
}
}
@ -1794,6 +1822,22 @@ static int CclNumIf(lua_State *l)
return Alias(l, "NumIf");
}
/**
** Return equivalent lua table for PlayerData.
** {"PlayerData", {arg1}}
**
** @param l Lua state.
**
** @return equivalent lua table.
*/
static int CclPlayerData(lua_State *l)
{
if (lua_gettop(l) != 2 && lua_gettop(l) != 3) {
LuaError(l, "Bad number of arg for PlayerData()\n");
}
return Alias(l, "PlayerData");
}
/**
** Return equivalent lua table for PlayerName.
** {"PlayerName", {arg1}}
@ -1847,6 +1891,7 @@ static void AliasRegister()
lua_register(Lua, "Line", CclLine);
lua_register(Lua, "GameInfo", CclGameInfo);
lua_register(Lua, "PlayerName", CclPlayerName);
lua_register(Lua, "PlayerData", CclPlayerData);
lua_register(Lua, "If", CclIf);
lua_register(Lua, "NumIf", CclNumIf);

View file

@ -1022,8 +1022,6 @@ static int CclGetUnitVariable(lua_State *l)
const char *const value = LuaToString(l, 2);
if (!strcmp(value, "RegenerationRate")) {
lua_pushnumber(l, unit->Variable[HP_INDEX].Increase);
} else if (!strcmp(value, "Player")) {
lua_pushnumber(l, unit->Player->Index);
} else if (!strcmp(value, "Ident")) {
lua_pushstring(l, unit->Type->Ident.c_str());
} else if (!strcmp(value, "ResourcesHeld")) {

View file

@ -131,6 +131,7 @@ static const char POISON_KEY[] = "Poison";
static const char SHIELDPERMEABILITY_KEY[] = "ShieldPermeability";
static const char SHIELDPIERCING_KEY[] = "ShieldPiercing";
static const char ISALIVE_KEY[] = "IsAlive";
static const char PLAYER_KEY[] = "Player";
/*----------------------------------------------------------------------------
-- Functions
@ -167,7 +168,7 @@ CUnitTypeVar::CVariableKeys::CVariableKeys()
BASICDAMAGE_KEY, POSX_KEY, POSY_KEY, TARGETPOSX_KEY, TARGETPOSY_KEY, RADARRANGE_KEY,
RADARJAMMERRANGE_KEY, AUTOREPAIRRANGE_KEY, BLOODLUST_KEY, HASTE_KEY,
SLOW_KEY, INVISIBLE_KEY, UNHOLYARMOR_KEY, SLOT_KEY, SHIELD_KEY, POINTS_KEY,
MAXHARVESTERS_KEY, POISON_KEY, SHIELDPERMEABILITY_KEY, SHIELDPIERCING_KEY, ISALIVE_KEY
MAXHARVESTERS_KEY, POISON_KEY, SHIELDPERMEABILITY_KEY, SHIELDPIERCING_KEY, ISALIVE_KEY, PLAYER_KEY
};
for (int i = 0; i < NVARALREADYDEFINED; ++i) {
@ -1561,7 +1562,7 @@ void UpdateUnitVariables(CUnit &unit)
|| i == INVISIBLE_INDEX || i == UNHOLYARMOR_INDEX || i == HP_INDEX
|| i == SHIELD_INDEX || i == POINTS_INDEX || i == MAXHARVESTERS_INDEX
|| i == POISON_INDEX || i == SHIELDPERMEABILITY_INDEX || i == SHIELDPIERCING_INDEX
|| i == ISALIVE_INDEX) {
|| i == ISALIVE_INDEX || i == PLAYER_INDEX) {
continue;
}
unit.Variable[i].Value = 0;
@ -1638,6 +1639,10 @@ void UpdateUnitVariables(CUnit &unit)
unit.Variable[ISALIVE_INDEX].Value = unit.IsAlive() ? 1 : 0;
unit.Variable[ISALIVE_INDEX].Max = 1;
// Player
unit.Variable[PLAYER_INDEX].Value = unit.Player->Index;
unit.Variable[PLAYER_INDEX].Max = PlayerMax;
for (int i = 0; i < NVARALREADYDEFINED; i++) { // default values
unit.Variable[i].Enable &= unit.Variable[i].Max > 0;
if (unit.Variable[i].Value > unit.Variable[i].Max) {