UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
cp_component.cpp
Go to the documentation of this file.
1 
6 /*
7 Copyright (C) 2002-2020 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 
31  char id[MAX_VAR];
32  char amount[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  }
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) */
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 }
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
int numComponents
Definition: cp_campaign.h:311
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
const objDef_t * items[MAX_COMP]
Definition: cp_component.h:42
#define MAX_ASSEMBLIES
Definition: cp_component.h:28
components_t * COMP_GetComponentsByID(const char *id)
Returns components definition by ID.
void Q_strncpyz(char *dest, const char *src, size_t destsize)
Safe strncpy that ensures a trailing zero.
Definition: shared.cpp:457
#define ERR_DROP
Definition: common.h:211
#define DEBUG_CLIENT
Definition: defines.h:59
#define OBJZERO(obj)
Definition: shared.h:178
components_t components[MAX_ASSEMBLIES]
Definition: cp_campaign.h:310
#define MAX_VAR
Definition: shared.h:36
char assemblyId[MAX_VAR]
Definition: cp_component.h:36
const cgame_import_t * cgi
int itemAmount2[MAX_COMP]
Definition: cp_component.h:44
ccs_t ccs
Definition: cp_campaign.cpp:62
const objDef_t * assemblyItem
Definition: cp_component.h:37
static const value_t components_type_vals[]
The definition of a "components" entry (i.e. an assembly of several items) parsed from a ufo-file...
Definition: cp_component.h:35
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
char amount[MAX_VAR]
int itemAmount[MAX_COMP]
Definition: cp_component.h:43
QGL_EXTERN GLint i
Definition: r_gl.h:113
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition: r_gl.h:110
char numbercrash[MAX_VAR]
#define MAX_COMP
Definition: cp_component.h:29
Header file for single player campaign control.
const char *IMPORT * Com_EParse(const char **text, const char *errhead, const char *errinfo)
#define Q_streq(a, b)
Definition: shared.h:136
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
Definition: cp_component.h:30