UFO: Alien Invasion
Loading...
Searching...
No Matches
cp_rank.cpp
Go to the documentation of this file.
1
4
5/*
6Copyright (C) 2002-2025 UFO: Alien Invasion.
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
17See the GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22*/
23
24#include "../../cl_shared.h"
26#include "cp_rank.h"
27#include "cp_campaign.h"
28
34int 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
58static 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
74void 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);
97 ccs.numRanks = MAX_RANKS;
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}
Share stuff between the different cgame implementations.
#define ERR_DROP
Definition common.h:211
memPool_t * cp_campaignPool
ccs_t ccs
Header file for single player campaign control.
const cgame_import_t * cgi
employeeType_t E_GetEmployeeType(const char *type)
Convert string to employeeType_t.
rank_t * CL_GetRankByIdx(const int index)
Returns a rank at an index.
Definition cp_rank.cpp:50
int CL_GetRankIdx(const char *rankID)
Get the index of the given rankID in ccs.ranks array.
Definition cp_rank.cpp:34
void CL_ParseRanks(const char *name, const char **text)
Parse medals and ranks defined in the medals.ufo file.
Definition cp_rank.cpp:74
static const value_t rankValues[]
Definition cp_rank.cpp:58
#define MAX_RANKS
Definition cp_rank.h:26
level_locals_t level
Definition g_main.cpp:38
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
Shared parsing functions.
QGL_EXTERN GLuint index
Definition r_gl.h:110
QGL_EXTERN GLint i
Definition r_gl.h:113
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition r_gl.h:110
@ V_FLOAT
Definition scripts.h:54
@ V_TRANSLATION_STRING
Definition scripts.h:59
@ V_HUNK_STRING
Definition scripts.h:69
@ V_NULL
Definition scripts.h:49
@ V_INT
Definition scripts.h:52
#define MEMBER_SIZEOF(TYPE, MEMBER)
Definition scripts.h:34
#define Q_streq(a, b)
Definition shared.h:136
#define OBJZERO(obj)
Definition shared.h:178
Describes a rank that a recruit can gain.
Definition cp_rank.h:29
int level
Definition cp_rank.h:40
const char * name
Definition cp_rank.h:31
int type
Definition cp_rank.h:34
const char * id
Definition cp_rank.h:30
const char * image
Definition cp_rank.h:33
const char * shortname
Definition cp_rank.h:32