cp_rank.c

Go to the documentation of this file.
00001 
00005 /*
00006 Copyright (C) 2002-2010 UFO: Alien Invasion.
00007 
00008 This program is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU General Public License
00010 as published by the Free Software Foundation; either version 2
00011 of the License, or (at your option) any later version.
00012 
00013 This program is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016 
00017 See the GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00023 */
00024 
00025 #include "../cl_shared.h"
00026 #include "../../shared/parse.h"
00027 #include "cp_rank.h"
00028 #include "cp_campaign.h"
00029 
00035 int CL_GetRankIdx (const char* rankID)
00036 {
00037     int i;
00038 
00039     for (i = 0; i < ccs.numRanks; i++) {
00040         if (!strcmp(ccs.ranks[i].id, rankID))
00041             return i;
00042     }
00043 
00044     return -1;
00045 }
00046 
00053 rank_t *CL_GetRankByIdx (const int index)
00054 {
00055     if (index < 0 || index >= ccs.numRanks)
00056         return NULL;
00057 
00058     return &ccs.ranks[index];
00059 }
00060 
00061 static const value_t rankValues[] = {
00062     {"name", V_TRANSLATION_STRING, offsetof(rank_t, name), 0},
00063     {"shortname", V_TRANSLATION_STRING, offsetof(rank_t, shortname), 0},
00064     {"image", V_CLIENT_HUNK_STRING, offsetof(rank_t, image), 0},
00065     {"mind", V_INT, offsetof(rank_t, mind), MEMBER_SIZEOF(rank_t, mind)},
00066     {"killed_enemies", V_INT, offsetof(rank_t, killedEnemies), MEMBER_SIZEOF(rank_t, killedEnemies)},
00067     {"killed_others", V_INT, offsetof(rank_t, killedOthers), MEMBER_SIZEOF(rank_t, killedOthers)},
00068     {"factor", V_FLOAT, offsetof(rank_t, factor), MEMBER_SIZEOF(rank_t, factor)},
00069     {NULL, 0, 0, 0}
00070 };
00071 
00076 void CL_ParseRanks (const char *name, const char **text)
00077 {
00078     rank_t *rank;
00079     const char *errhead = "CL_ParseRanks: unexpected end of file (medal/rank ";
00080     const char *token;
00081     const value_t *v;
00082     int i;
00083 
00084     /* get name list body body */
00085     token = Com_Parse(text);
00086 
00087     if (!*text || *token != '{') {
00088         Com_Printf("CL_ParseRanks: rank/medal \"%s\" without body ignored\n", name);
00089         return;
00090     }
00091 
00092     for (i = 0; i < ccs.numRanks; i++) {
00093         if (!strcmp(name, ccs.ranks[i].name)) {
00094             Com_Printf("CL_ParseRanks: Rank with same name '%s' already loaded.\n", name);
00095             return;
00096         }
00097     }
00098     /* parse ranks */
00099     if (ccs.numRanks >= MAX_RANKS) {
00100         Com_Printf("CL_ParseRanks: Too many rank descriptions, '%s' ignored.\n", name);
00101         ccs.numRanks = MAX_RANKS;
00102         return;
00103     }
00104 
00105     rank = &ccs.ranks[ccs.numRanks++];
00106     memset(rank, 0, sizeof(*rank));
00107     rank->id = Mem_PoolStrDup(name, cp_campaignPool, 0);
00108 
00109     do {
00110         /* get the name type */
00111         token = Com_EParse(text, errhead, name);
00112         if (!*text)
00113             break;
00114         if (*token == '}')
00115             break;
00116         for (v = rankValues; v->string; v++)
00117             if (!strcmp(token, v->string)) {
00118                 /* found a definition */
00119                 token = Com_EParse(text, errhead, name);
00120                 if (!*text)
00121                     return;
00122                 switch (v->type) {
00123                 case V_CLIENT_HUNK_STRING:
00124                     Mem_PoolStrDupTo(token, (char**) ((char*)rank + (int)v->ofs), cp_campaignPool, 0);
00125                     break;
00126                 default:
00127                     Com_EParseValue(rank, token, v->type, v->ofs, v->size);
00128                     break;
00129                 }
00130                 break;
00131             }
00132 
00133         if (!strcmp(token, "type")) {
00134             /* employeeType_t */
00135             token = Com_EParse(text, errhead, name);
00136             if (!*text)
00137                 return;
00138             /* error check is performed in E_GetEmployeeType function */
00139             rank->type = E_GetEmployeeType(token);
00140         } else if (!v->string)
00141             Com_Printf("CL_ParseRanks: unknown token \"%s\" ignored (medal/rank %s)\n", token, name);
00142     } while (*text);
00143 
00144     if (!strlen(rank->name))
00145         Com_Error(ERR_DROP, "CL_ParseRanks: name is missing for rank %s", rank->id);
00146 
00147     if (!strlen(rank->shortname))
00148         Q_strncpyz(rank->shortname, rank->name, sizeof(rank->shortname));
00149 }
00150 

Generated by  doxygen 1.6.2