/** ** Define race names ** ** @param l Lua state. */ 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); if (!strcmp(value, "race")) { ++j; if (!lua_istable(l, j + 1)) { LuaError(l, "incorrect argument"); } subargs = luaL_getn(l, j + 1); i = PlayerRaces.Count++; PlayerRaces.Name[i] = NULL; PlayerRaces.Display[i] = NULL; PlayerRaces.Visible[i] = 0; for (k = 0; k < subargs; ++k) { lua_rawgeti(l, j + 1, k + 1); value = LuaToString(l, -1); lua_pop(l, 1); if (!strcmp(value, "name")) { ++k; lua_rawgeti(l, j + 1, k + 1); PlayerRaces.Name[i] = new_strdup(LuaToString(l, -1)); lua_pop(l, 1); } else if (!strcmp(value, "display")) { ++k; lua_rawgeti(l, j + 1, k + 1); PlayerRaces.Display[i] = new_strdup(LuaToString(l, -1)); lua_pop(l, 1); } else if (!strcmp(value, "visible")) { PlayerRaces.Visible[i] = 1; } else { LuaError(l, "Unsupported tag: %s" _C_ value); } } } else { LuaError(l, "Unsupported tag: %s" _C_ value); } } return 0; }
/** ** Load a MNG ** ** @param name Name of the MNG file */ int Mng::Load(const char *name) { mng_retcode myretcode; char buf[PATH_MAX]; LibraryFileName(name, buf, sizeof(buf)); this->name = new_strdup(buf); handle = mng_initialize(this, my_alloc, my_free, MNG_NULL); if (handle == MNG_NULL) { return -1; } mng_setcb_openstream(handle, my_openstream); mng_setcb_closestream(handle, my_closestream); mng_setcb_readdata(handle, my_readdata); mng_setcb_processheader(handle, my_processheader); mng_setcb_processmend(handle, my_processmend); mng_setcb_getcanvasline(handle, my_getcanvasline); mng_setcb_refresh(handle, my_refresh); mng_setcb_gettickcount(handle, my_gettickcount); mng_setcb_settimer(handle, my_settimer); mng_setcb_errorproc(handle, my_errorproc); mng_read(handle); if (surface && iteration != 0x7fffffff) { myretcode = mng_display(handle); } if (!surface || iteration == 0x7fffffff) { return -1; } return 0; }
/** ** Retrieves the value of the parameter at position paramNumber ** ** @param reply The reply from the metaserver ** @param pos The parameter number ** @param value The returned value ** ** @returns -1 if error. */ int GetMetaParameter(char *reply, int pos, char **value) { // Take Care for OK/ERR *value = strchr(reply, '\n'); (*value)++; while (pos-- && *value) { *value = strchr(*value, '\n'); if (*value) { (*value)++; } } if (!*value) { // Parameter our of bounds return -1; } if (*value[0] == '\n') { (*value)++; } char *endline = strchr(*value, '\n'); if (!endline) { return -1; } *endline = '\0'; *value = new_strdup(*value); *endline = '\n'; return 0; }
/** ** 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; }
int GenericQuery:: addCustomAND (const char *value) { char *x = new_strdup (value); if (!x) return Q_MEMORY_ERROR; customANDConstraints.Append (x); return Q_OK; }
/** ** Check for button enabled, if unit's variables pass the condition check. ** ** @param unit Pointer to unit for button. ** @param button Pointer to button to check/enable. ** ** @return True if enabled. */ bool ButtonCheckUnitVariable(const CUnit &unit, const ButtonAction &button) { char *buf = new_strdup(button.AllowStr.c_str()); for (const char *var = strtok(buf, ","); var; var = strtok(NULL, ",")) { const char *type = strtok(NULL, ","); const char *binop = strtok(NULL, ","); const char *value = strtok(NULL, ","); const int index = UnitTypeVar.VariableNameLookup[var];// User variables if (index == -1) { fprintf(stderr, "Bad variable name '%s'\n", var); Exit(1); return false; } int varValue; if (!strcmp(type, "Value")) { varValue = unit.Variable[index].Value; } else if (!strcmp(type, "Max")) { varValue = unit.Variable[index].Max; } else if (!strcmp(type, "Increase")) { varValue = unit.Variable[index].Increase; } else if (!strcmp(type, "Enable")) { varValue = unit.Variable[index].Enable; } else if (!strcmp(type, "Percent")) { varValue = unit.Variable[index].Value * 100 / unit.Variable[index].Max; } else { fprintf(stderr, "Bad variable type '%s'\n", type); Exit(1); return false; } const int cmpValue = atoi(value); bool cmpResult = false; if (!strcmp(binop, ">")) { cmpResult = varValue > cmpValue; } else if (!strcmp(binop, ">=")) { cmpResult = varValue >= cmpValue; } else if (!strcmp(binop, "<")) { cmpResult = varValue < cmpValue; } else if (!strcmp(binop, "<=")) { cmpResult = varValue <= cmpValue; } else if (!strcmp(binop, "==")) { cmpResult = varValue == cmpValue; } else if (!strcmp(binop, "!=")) { cmpResult = varValue != cmpValue; } else { fprintf(stderr, "Bad compare type '%s'\n", binop); Exit(1); return false; } if (cmpResult == false) { delete[] buf; return false; } } delete[] buf; return true; }
void GenericQuery:: copyStringCategory (List<char> &to, List<char> &from) { char *item; clearStringCategory (to); from.Rewind (); while ((item = from.Next ())) to.Append (new_strdup (item)); }
/** ** Check for button enabled, if all units are available. ** ** @param unit Pointer to unit for button. ** @param button Pointer to button to check/enable. ** ** @return True if enabled. */ bool ButtonCheckUnitsAnd(const CUnit &unit, const ButtonAction &button) { CPlayer *player = unit.Player; char *buf = new_strdup(button.AllowStr.c_str()); for (const char *s = strtok(buf, ","); s; s = strtok(NULL, ",")) { if (!player->HaveUnitTypeByIdent(s)) { delete[] buf; return false; } } delete[] buf; return true; }
int GenericQuery:: addString (const int cat, const char *value) { char *x; if (cat >= 0 && cat < stringThreshold) { x = new_strdup (value); if (!x) return Q_MEMORY_ERROR; stringConstraints [cat].Append (x); return Q_OK; } return Q_INVALID_CATEGORY; }
/** ** Define player colors ** ** @param l Lua state. */ static int CclDefinePlayerColors(lua_State *l) { int i; int args; int j; int numcolors; LuaCheckArgs(l, 1); if (!lua_istable(l, 1)) { LuaError(l, "incorrect argument"); } args = luaL_getn(l, 1); for (i = 0; i < args; ++i) { lua_rawgeti(l, 1, i + 1); delete[] PlayerColorNames[i / 2]; PlayerColorNames[i / 2] = new_strdup(LuaToString(l, -1)); lua_pop(l, 1); ++i; lua_rawgeti(l, 1, i + 1); if (!lua_istable(l, -1)) { LuaError(l, "incorrect argument"); } numcolors = luaL_getn(l, -1); if (numcolors != PlayerColorIndexCount) { LuaError(l, "You should use %d colors (See DefinePlayerColorIndex())" _C_ PlayerColorIndexCount); } for (j = 0; j < numcolors; ++j) { lua_rawgeti(l, -1, j + 1); if (!lua_istable(l, -1) || luaL_getn(l, -1) != 3) { LuaError(l, "incorrect argument"); } lua_rawgeti(l, -1, 1); lua_rawgeti(l, -2, 2); lua_rawgeti(l, -3, 3); PlayerColorsRGB[i / 2][j].r = LuaToNumber(l, -3); PlayerColorsRGB[i / 2][j].g = LuaToNumber(l, -2); PlayerColorsRGB[i / 2][j].b = LuaToNumber(l, -1); lua_pop(l, 3 + 1); } } return 0; }
/** ** Parse the construction. ** ** @param l Lua state. ** ** @note make this more flexible */ static int CclDefineConstruction(lua_State *l) { const char *value; std::string str; CConstruction *construction; std::vector<CConstruction *>::iterator i; int subargs; int k; LuaCheckArgs(l, 2); if (!lua_istable(l, 2)) { LuaError(l, "incorrect argument"); } // Slot identifier str = LuaToString(l, 1); for (i = Constructions.begin(); i != Constructions.end(); ++i) { if ((*i)->Ident == str) { // Redefine construction = *i; break; } } if (i == Constructions.end()) { construction = new CConstruction; Constructions.push_back(construction); } construction->Ident = str; // // Parse the arguments, in tagged format. // lua_pushnil(l); while (lua_next(l, 2)) { int files; value = LuaToString(l, -2); if ((files = !strcmp(value, "Files")) || !strcmp(value, "ShadowFiles")) { char *file; int w; int h; file = NULL; w = 0; h = 0; if (!lua_istable(l, -1)) { LuaError(l, "incorrect argument"); } lua_pushnil(l); while (lua_next(l, -2)) { value = LuaToString(l, -2); if (!strcmp(value, "File")) { file = new_strdup(LuaToString(l, -1)); } else if (!strcmp(value, "Size")) { if (!lua_istable(l, -1) || luaL_getn(l, -1) != 2) { LuaError(l, "incorrect argument"); } lua_rawgeti(l, -1, 1); w = LuaToNumber(l, -1); lua_pop(l, 1); lua_rawgeti(l, -1, 2); h = LuaToNumber(l, -1); lua_pop(l, 1); } else { LuaError(l, "Unsupported tag: %s" _C_ value); } lua_pop(l, 1); } if (files) { construction->File.File = file; construction->File.Width = w; construction->File.Height = h; } else { construction->ShadowFile.File = file; construction->ShadowFile.Width = w; construction->ShadowFile.Height = h; } } else if (!strcmp(value, "Constructions")) { subargs = luaL_getn(l, -1); for (k = 0; k < subargs; ++k) { int percent; ConstructionFileType file; int frame; CConstructionFrame **cframe; percent = 0; file = ConstructionFileConstruction; frame = 0; lua_rawgeti(l, -1, k + 1); if (!lua_istable(l, -1)) { LuaError(l, "incorrect argument"); } lua_pushnil(l); while (lua_next(l, -2)) { value = LuaToString(l, -2); if (!strcmp(value, "Percent")) { percent = LuaToNumber(l, -1); } else if (!strcmp(value, "File")) { value = LuaToString(l, -1); if (!strcmp(value, "construction")) { file = ConstructionFileConstruction; } else if (!strcmp(value, "main")) { file = ConstructionFileMain; } else { LuaError(l, "Unsupported tag: %s" _C_ value); } } else if (!strcmp(value, "Frame")) { frame = LuaToNumber(l, -1); } else { LuaError(l, "Unsupported tag: %s" _C_ value); } lua_pop(l, 1); } lua_pop(l, 1); cframe = &construction->Frames; while (*cframe) { cframe = &((*cframe)->Next); } (*cframe) = new CConstructionFrame; (*cframe)->Percent = percent; (*cframe)->File = file; (*cframe)->Frame = frame; (*cframe)->Next = NULL; } } else { LuaError(l, "Unsupported tag: %s" _C_ value); } lua_pop(l, 1); } return 0; }
/** ** Define the sprite to show variables. ** ** @param l Lua_state */ static int CclDefineSprites(lua_State *l) { const char *name; // name of the current sprite. int args; // number of arguments. int i; // iterator on argument. const char *key; // Current key of the lua table. int index; // Index of the Sprite. args = lua_gettop(l); for (i = 0; i < args; ++i) { Decoration deco; lua_pushnil(l); name = 0; while (lua_next(l, i + 1)) { key = LuaToString(l, -2); // key name if (!strcmp(key, "Name")) { name = LuaToString(l, -1); } else if (!strcmp(key, "File")) { deco.File = LuaToString(l, -1); } else if (!strcmp(key, "Offset")) { if (!lua_istable(l, -1) || luaL_getn(l, -1) != 2) { LuaError(l, "incorrect argument"); } lua_rawgeti(l, -1, 1); // offsetX lua_rawgeti(l, -2, 2); // offsetY deco.HotX = LuaToNumber(l, -2); deco.HotY = LuaToNumber(l, -1); lua_pop(l, 2); // Pop offsetX and Y } else if (!strcmp(key, "Size")) { if (!lua_istable(l, -1) || luaL_getn(l, -1) != 2) { LuaError(l, "incorrect argument"); } lua_rawgeti(l, -1, 1); // Width lua_rawgeti(l, -2, 2); // Height deco.Width = LuaToNumber(l, -2); deco.Height = LuaToNumber(l, -1); lua_pop(l, 2); // Pop Width and Height } else { // Error. LuaError(l, "incorrect field '%s' for the DefineSprite." _C_ key); } lua_pop(l, 1); // pop the value; } if (name == 0) { LuaError(l, "CclDefineSprites requires the Name flag for sprite."); } index = GetSpriteIndex(name); if (index == -1) { // new sprite. index = DecoSprite.SpriteArray.size(); DecoSprite.Name.push_back(new_strdup(name)); DecoSprite.SpriteArray.push_back(deco); } else { DecoSprite.SpriteArray[index].File.clear(); DecoSprite.SpriteArray[index] = deco; } // Now verify validity. if (DecoSprite.SpriteArray[index].File.empty()) { LuaError(l, "CclDefineSprites requires the File flag for sprite."); } // FIXME check if file is valid with good size ? } return 0; }