UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
cp_rank.cpp
Go to the documentation of this file.
1 
5 /*
6 Copyright (C) 2002-2020 UFO: Alien Invasion.
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17 See the GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23 
24 #include "../../cl_shared.h"
25 #include "../../../shared/parse.h"
26 #include "cp_rank.h"
27 #include "cp_campaign.h"
28 
34 int CL_GetRankIdx (const char* rankID)
35 {
36  for (int i = 0; i < ccs.numRanks; i++) {
37  if (Q_streq(ccs.ranks[i].id, rankID))
38  return i;
39  }
40 
41  return -1;
42 }
43 
51 {
52  if (index < 0 || index >= ccs.numRanks)
53  return nullptr;
54 
55  return &ccs.ranks[index];
56 }
57 
58 static const value_t rankValues[] = {
59  {"name", V_TRANSLATION_STRING, offsetof(rank_t, name), 0},
60  {"shortname", V_TRANSLATION_STRING, offsetof(rank_t, shortname), 0},
61  {"image", V_HUNK_STRING, offsetof(rank_t, image), 0},
62  {"mind", V_INT, offsetof(rank_t, mind), MEMBER_SIZEOF(rank_t, mind)},
63  {"killed_enemies", V_INT, offsetof(rank_t, killedEnemies), MEMBER_SIZEOF(rank_t, killedEnemies)},
64  {"killed_others", V_INT, offsetof(rank_t, killedOthers), MEMBER_SIZEOF(rank_t, killedOthers)},
65  {"factor", V_FLOAT, offsetof(rank_t, factor), MEMBER_SIZEOF(rank_t, factor)},
66  {"level", V_INT, offsetof(rank_t, level), MEMBER_SIZEOF(rank_t, level)},
67  {nullptr, V_NULL, 0, 0}
68 };
69 
74 void CL_ParseRanks (const char* name, const char** text)
75 {
76  rank_t* rank;
77  const char* errhead = "CL_ParseRanks: unexpected end of file (medal/rank ";
78  const char* token;
79 
80  /* get name list body body */
81  token = Com_Parse(text);
82 
83  if (!*text || *token != '{') {
84  cgi->Com_Printf("CL_ParseRanks: rank/medal \"%s\" without body ignored\n", name);
85  return;
86  }
87 
88  for (int i = 0; i < ccs.numRanks; i++) {
89  if (Q_streq(name, ccs.ranks[i].name)) {
90  cgi->Com_Printf("CL_ParseRanks: Rank with same name '%s' already loaded.\n", name);
91  return;
92  }
93  }
94  /* parse ranks */
95  if (ccs.numRanks >= MAX_RANKS) {
96  cgi->Com_Printf("CL_ParseRanks: Too many rank descriptions, '%s' ignored.\n", name);
98  return;
99  }
100 
101  rank = &ccs.ranks[ccs.numRanks++];
102  OBJZERO(*rank);
103  rank->id = cgi->PoolStrDup(name, cp_campaignPool, 0);
104  rank->level = -1;
105 
106  do {
107  /* get the name type */
108  token = cgi->Com_EParse(text, errhead, name);
109  if (!*text)
110  break;
111  if (*token == '}')
112  break;
113 
114  if (cgi->Com_ParseBlockToken(name, text, rank, rankValues, cp_campaignPool, token)) {
115  continue;
116  } else if (Q_streq(token, "type")) {
117  /* employeeType_t */
118  token = cgi->Com_EParse(text, errhead, name);
119  if (!*text)
120  return;
121  /* error check is performed in E_GetEmployeeType function */
122  rank->type = E_GetEmployeeType(token);
123  } else
124  cgi->Com_Printf("CL_ParseRanks: unknown token \"%s\" ignored (medal/rank %s)\n", token, name);
125  } while (*text);
126 
127  if (rank->image == nullptr || !strlen(rank->image))
128  cgi->Com_Error(ERR_DROP, "CL_ParseRanks: image is missing for rank %s", rank->id);
129 
130  if (rank->name == nullptr || !strlen(rank->name))
131  cgi->Com_Error(ERR_DROP, "CL_ParseRanks: name is missing for rank %s", rank->id);
132 
133  if (rank->shortname == nullptr || !strlen(rank->shortname))
134  rank->shortname = rank->name;
135 
136  if (rank->level == -1)
137  cgi->Com_Error(ERR_DROP, "CL_ParseRanks: level is missing for rank %s", rank->id);
138 }
employeeType_t E_GetEmployeeType(const char *type)
Convert string to employeeType_t.
const char * id
Definition: cp_rank.h:30
Describes a rank that a recruit can gain.
Definition: cp_rank.h:29
int CL_GetRankIdx(const char *rankID)
Get the index of the given rankID in ccs.ranks array.
Definition: cp_rank.cpp:34
rank_t ranks[MAX_RANKS]
Definition: cp_campaign.h:367
int level
Definition: cp_rank.h:40
int numRanks
Definition: cp_campaign.h:369
char * image
Definition: cp_aircraft.h:122
const char * shortname
Definition: cp_rank.h:32
rank_t * CL_GetRankByIdx(const int index)
Returns a rank at an index.
Definition: cp_rank.cpp:50
memPool_t * cp_campaignPool
Definition: cp_campaign.cpp:61
#define ERR_DROP
Definition: common.h:211
#define OBJZERO(obj)
Definition: shared.h:178
const cgame_import_t * cgi
ccs_t ccs
Definition: cp_campaign.cpp:62
int type
Definition: cp_rank.h:34
const char * name
Definition: cp_rank.h:31
QGL_EXTERN GLuint index
Definition: r_gl.h:110
static const value_t rankValues[]
Definition: cp_rank.cpp:58
const char * Com_Parse(const char *data_p[], char *target, size_t size, bool replaceWhitespaces)
Parse a token out of a string.
Definition: parse.cpp:107
Definition: scripts.h:49
QGL_EXTERN GLint i
Definition: r_gl.h:113
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition: r_gl.h:110
#define MEMBER_SIZEOF(TYPE, MEMBER)
Definition: scripts.h:34
Header file for single player campaign control.
Definition: scripts.h:52
const char *IMPORT * Com_EParse(const char **text, const char *errhead, const char *errinfo)
#define Q_streq(a, b)
Definition: shared.h:136
#define MAX_RANKS
Definition: cp_rank.h:26
void CL_ParseRanks(const char *name, const char **text)
Parse medals and ranks defined in the medals.ufo file.
Definition: cp_rank.cpp:74
char *IMPORT * PoolStrDup(const char *in, memPool_t *pool, const int tagNum)
const char * image
Definition: cp_rank.h:33
level_locals_t level
Definition: g_main.cpp:38