UFO: Alien Invasion
Toggle main menu visibility
Loading...
Searching...
No Matches
cp_component.cpp
Go to the documentation of this file.
1
5
6
/*
7
Copyright (C) 2002-2025 UFO: Alien Invasion.
8
9
This program is free software; you can redistribute it and/or
10
modify it under the terms of the GNU General Public License
11
as published by the Free Software Foundation; either version 2
12
of the License, or (at your option) any later version.
13
14
This program is distributed in the hope that it will be useful,
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
18
See the GNU General Public License for more details.
19
20
You should have received a copy of the GNU General Public License
21
along with this program; if not, write to the Free Software
22
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
*/
24
25
#include "
../../cl_shared.h
"
26
#include "
../../../shared/parse.h
"
27
#include "
cp_campaign.h
"
28
30
struct
component_type_data_t
{
31
char
id
[
MAX_VAR
];
32
char
amount
[
MAX_VAR
];
33
char
numbercrash
[
MAX_VAR
];
34
};
35
37
static
const
value_t
components_type_vals
[] = {
38
{
"id"
,
V_STRING
, offsetof(
component_type_data_t
,
id
), 0},
39
{
"amount"
,
V_STRING
, offsetof(
component_type_data_t
, amount), 0},
40
{
"numbercrash"
,
V_STRING
, offsetof(
component_type_data_t
, numbercrash), 0},
41
{
nullptr
,
V_NULL
, 0, 0}
42
};
43
50
void
COMP_ParseComponents
(
const
char
*
name
,
const
char
** text)
51
{
52
components_t
* comp;
53
const
char
* errhead =
"COMP_ParseComponents: unexpected end of file."
;
54
const
char
* token;
55
56
/* get body */
57
token =
Com_Parse
(text);
58
if
(!*text || *token !=
'{'
) {
59
cgi
->Com_Printf(
"COMP_ParseComponents: \"%s\" components def without body ignored.\n"
,
name
);
60
return
;
61
}
62
if
(
ccs
.numComponents >=
MAX_ASSEMBLIES
) {
63
cgi
->Com_Printf(
"COMP_ParseComponents: too many technology entries. limit is %i.\n"
,
MAX_ASSEMBLIES
);
64
return
;
65
}
66
67
/* New components-entry (next free entry in global comp-list) */
68
comp = &
ccs
.components[
ccs
.numComponents];
69
ccs
.numComponents++;
70
71
OBJZERO
(*comp);
72
73
/* name is not used */
74
75
do
{
76
/* get the name type */
77
token =
cgi
->Com_EParse(text, errhead,
name
);
78
if
(!*text)
79
break
;
80
if
(*token ==
'}'
)
81
break
;
82
83
/* get values */
84
if
(
Q_streq
(token,
"aircraft"
)) {
85
token =
cgi
->Com_EParse(text, errhead,
name
);
86
if
(!*text)
87
break
;
88
89
/* set standard values */
90
Q_strncpyz
(comp->
assemblyId
, token,
sizeof
(comp->
assemblyId
));
91
comp->
assemblyItem
=
INVSH_GetItemByIDSilent
(comp->
assemblyId
);
92
if
(comp->
assemblyItem
)
93
cgi
->Com_DPrintf(
DEBUG_CLIENT
,
"COMP_ParseComponents: linked item: %s with components: %s\n"
, token, comp->
assemblyId
);
94
}
else
if
(
Q_streq
(token,
"item"
)) {
95
/* Defines what items need to be collected for this item to be researchable. */
96
if
(comp->
numItemtypes
<
MAX_COMP
) {
97
/* Parse block */
98
component_type_data_t
itemTokens;
99
OBJZERO
(itemTokens);
100
if
(
cgi
->Com_ParseBlock (
"item"
, text, &itemTokens,
components_type_vals
,
nullptr
)) {
101
if
(itemTokens.
id
[0] ==
'\0'
)
102
cgi
->Com_Error(
ERR_DROP
,
"COMP_ParseComponents: \"item\" token id is missing.\n"
);
103
if
(itemTokens.
amount
[0] ==
'\0'
)
104
cgi
->Com_Error(
ERR_DROP
,
"COMP_ParseComponents: \"amount\" token id is missing.\n"
);
105
if
(itemTokens.
numbercrash
[0] ==
'\0'
)
106
cgi
->Com_Error(
ERR_DROP
,
"COMP_ParseComponents: \"numbercrash\" token id is missing.\n"
);
107
108
comp->
items
[comp->
numItemtypes
] =
INVSH_GetItemByID
(itemTokens.
id
);
/* item id -> item pointer */
109
110
/* Parse number of items. */
111
comp->
itemAmount
[comp->
numItemtypes
] = atoi(itemTokens.
amount
);
112
/* If itemcount needs to be scaled */
113
if
(itemTokens.
numbercrash
[0] ==
'%'
)
114
comp->
itemAmount2
[comp->
numItemtypes
] =
COMP_ITEMCOUNT_SCALED
;
115
else
116
comp->
itemAmount2
[comp->
numItemtypes
] = atoi(itemTokens.
numbercrash
);
117
119
/* comp->item_idx[comp->numItemtypes] = xxx */
120
121
comp->
numItemtypes
++;
122
}
123
}
else
{
124
cgi
->Com_Printf(
"COMP_ParseComponents: \"%s\" Too many 'items' defined. Limit is %i - ignored.\n"
,
name
,
MAX_COMP
);
125
}
126
}
else
if
(
Q_streq
(token,
"time"
)) {
127
/* Defines how long disassembly lasts. */
128
token =
Com_Parse
(text);
129
comp->
time
= atoi(token);
130
}
else
{
131
cgi
->Com_Printf(
"COMP_ParseComponents: Error in \"%s\" - unknown token: \"%s\".\n"
,
name
, token);
132
}
133
}
while
(*text);
134
135
if
(comp->
assemblyId
[0] ==
'\0'
) {
136
cgi
->Com_Error(
ERR_DROP
,
"COMP_ParseComponents: component \"%s\" is not applied to any aircraft.\n"
,
name
);
137
}
138
}
139
145
components_t
*
COMP_GetComponentsByID
(
const
char
*
id
)
146
{
147
for
(
int
i
= 0;
i
<
ccs
.numComponents;
i
++) {
148
components_t
* comp = &
ccs
.components[
i
];
149
if
(
Q_streq
(comp->
assemblyId
,
id
)) {
150
return
comp;
151
}
152
}
153
cgi
->Com_Error(
ERR_DROP
,
"COMP_GetComponentsByID: could not find components id for: %s"
,
id
);
154
}
cl_shared.h
Share stuff between the different cgame implementations.
ERR_DROP
#define ERR_DROP
Definition
common.h:211
ccs
ccs_t ccs
Definition
cp_campaign.cpp:63
cp_campaign.h
Header file for single player campaign control.
cgi
const cgame_import_t * cgi
Definition
cp_cgame_callbacks.cpp:39
components_type_vals
static const value_t components_type_vals[]
Definition
cp_component.cpp:37
COMP_GetComponentsByID
components_t * COMP_GetComponentsByID(const char *id)
Returns components definition by ID.
Definition
cp_component.cpp:145
COMP_ParseComponents
void COMP_ParseComponents(const char *name, const char **text)
Parses one "components" entry in a .ufo file and writes it into the next free entry in xxxxxxxx (comp...
Definition
cp_component.cpp:50
COMP_ITEMCOUNT_SCALED
#define COMP_ITEMCOUNT_SCALED
Definition
cp_component.h:30
MAX_ASSEMBLIES
#define MAX_ASSEMBLIES
Definition
cp_component.h:28
MAX_COMP
#define MAX_COMP
Definition
cp_component.h:29
DEBUG_CLIENT
#define DEBUG_CLIENT
Definition
defines.h:59
INVSH_GetItemByID
const objDef_t * INVSH_GetItemByID(const char *id)
Returns the item that belongs to the given id or nullptr if it wasn't found.
Definition
inv_shared.cpp:282
INVSH_GetItemByIDSilent
const objDef_t * INVSH_GetItemByIDSilent(const char *id)
Returns the item that belongs to the given id or nullptr if it wasn't found.
Definition
inv_shared.cpp:249
Com_Parse
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
parse.h
Shared parsing functions.
i
QGL_EXTERN GLint i
Definition
r_gl.h:113
name
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition
r_gl.h:110
V_NULL
@ V_NULL
Definition
scripts.h:49
V_STRING
@ V_STRING
Definition
scripts.h:58
Q_streq
#define Q_streq(a, b)
Definition
shared.h:136
OBJZERO
#define OBJZERO(obj)
Definition
shared.h:178
MAX_VAR
#define MAX_VAR
Definition
shared.h:36
Q_strncpyz
void Q_strncpyz(char *dest, const char *src, size_t destsize)
Safe strncpy that ensures a trailing zero.
Definition
shared.cpp:457
component_type_data_t
Definition
cp_component.cpp:30
component_type_data_t::amount
char amount[MAX_VAR]
Definition
cp_component.cpp:32
component_type_data_t::numbercrash
char numbercrash[MAX_VAR]
Definition
cp_component.cpp:33
component_type_data_t::id
char id[MAX_VAR]
Definition
cp_component.cpp:31
components_t
The definition of a "components" entry (i.e. an assembly of several items) parsed from a ufo-file.
Definition
cp_component.h:35
components_t::time
int time
Definition
cp_component.h:39
components_t::numItemtypes
int numItemtypes
Definition
cp_component.h:41
components_t::items
const objDef_t * items[MAX_COMP]
Definition
cp_component.h:42
components_t::itemAmount2
int itemAmount2[MAX_COMP]
Definition
cp_component.h:44
components_t::assemblyId
char assemblyId[MAX_VAR]
Definition
cp_component.h:36
components_t::itemAmount
int itemAmount[MAX_COMP]
Definition
cp_component.h:43
components_t::assemblyItem
const objDef_t * assemblyItem
Definition
cp_component.h:37
value_t
Definition
scripts.h:167
src
client
cgame
campaign
cp_component.cpp
Generated on __DATE__ __TIME__ for UFO: Alien Invasion by
1.17.0