UFO: Alien Invasion
Loading...
Searching...
No Matches
cp_component.cpp
Go to the documentation of this file.
1
5
6/*
7Copyright (C) 2002-2025 UFO: Alien Invasion.
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
18See the GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23*/
24
25#include "../../cl_shared.h"
27#include "cp_campaign.h"
28
35
37static 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
50void 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));
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] == '%')
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
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}
Share stuff between the different cgame implementations.
#define ERR_DROP
Definition common.h:211
ccs_t ccs
Header file for single player campaign control.
const cgame_import_t * cgi
static const value_t components_type_vals[]
components_t * COMP_GetComponentsByID(const char *id)
Returns components definition by ID.
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...
#define COMP_ITEMCOUNT_SCALED
#define MAX_ASSEMBLIES
#define MAX_COMP
#define DEBUG_CLIENT
Definition defines.h:59
const objDef_t * INVSH_GetItemByID(const char *id)
Returns the item that belongs to the given id or nullptr if it wasn't found.
const objDef_t * INVSH_GetItemByIDSilent(const char *id)
Returns the item that belongs to the given id or nullptr if it wasn't found.
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 GLint i
Definition r_gl.h:113
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition r_gl.h:110
@ V_NULL
Definition scripts.h:49
@ V_STRING
Definition scripts.h:58
#define Q_streq(a, b)
Definition shared.h:136
#define OBJZERO(obj)
Definition shared.h:178
#define MAX_VAR
Definition shared.h:36
void Q_strncpyz(char *dest, const char *src, size_t destsize)
Safe strncpy that ensures a trailing zero.
Definition shared.cpp:457
char numbercrash[MAX_VAR]
The definition of a "components" entry (i.e. an assembly of several items) parsed from a ufo-file.
const objDef_t * items[MAX_COMP]
int itemAmount2[MAX_COMP]
char assemblyId[MAX_VAR]
int itemAmount[MAX_COMP]
const objDef_t * assemblyItem