Debian Codegraph @

src/cdogs/ammo.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2014, 2016-2019, 2023-2024 Cong Xu
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12 Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include "ammo.h"
29
30 #include <string.h>
31
32 #include "json_utils.h"
33 #include "log.h"
34 #include "pic_manager.h"
35 #include "utils.h"
36
37 AmmoClasses gAmmo;
38
39 // TODO: use map structure?
40 Ammo *StrAmmo(const char *s)
41 {
42 return AmmoGetById(&gAmmo, StrAmmoId(s));
43 }
44 int StrAmmoId(const char *s)
45 {
46 if (s == NULL || strlen(s) == 0)
47 {
48 return 0;
49 }
50 CA_FOREACH(Ammo, a, gAmmo.CustomAmmo)
51 if (strcmp(s, a->Name) == 0)
52 {
53 return _ca_index + (int)gAmmo.Ammo.size;
54 }
55 CA_FOREACH_END()
56 CA_FOREACH(Ammo, a, gAmmo.Ammo)
57 if (strcmp(s, a->Name) == 0)
58 {
59 return _ca_index;
60 }
61 CA_FOREACH_END()
62 CASSERT(false, "cannot parse ammo name");
63 return 0;
64 }
65
66 #define VERSION 2
67 static void LoadAmmo(Ammo *a, json_t *node, const int version);
68 void AmmoInitialize(AmmoClasses *ammo, const char *path)
69 {
70 memset(ammo, 0, sizeof *ammo);
71 CArrayInit(&ammo->Ammo, sizeof(Ammo));
72 CArrayInit(&ammo->CustomAmmo, sizeof(Ammo));
73
74 json_t *root = NULL;
75 enum json_error e;
76
77 char buf[CDOGS_PATH_MAX];
78 GetDataFilePath(buf, path);
79 FILE *f = fopen(buf, "r");
80 if (f == NULL)
81 {
82 LOG(LM_MAIN, LL_ERROR, "Error: cannot load ammo file %s", buf);
83 goto bail;
84 }
85 e = json_stream_parse(f, &root);
86 if (e != JSON_OK)
87 {
88 LOG(LM_MAIN, LL_ERROR, "Error parsing ammo file %s [error %d]", buf,
89 (int)e);
90 goto bail;
91 }
92 AmmoLoadJSON(&ammo->Ammo, root);
93
94 bail:
95 if (f)
96 {
97 fclose(f);
98 }
99 json_free_value(&root);
100 }
101 void AmmoLoadJSON(CArray *ammo, json_t *node)
102 {
103 int version;
104 LoadInt(&version, node, "Version");
105 if (version > VERSION || version <= 0)
106 {
107 CASSERT(false, "cannot read ammo file version");
108 return;
109 }
110
111 json_t *ammoNode = json_find_first_label(node, "Ammo")->child;
112 for (json_t *child = ammoNode->child; child; child = child->next)
113 {
114 Ammo a;
115 LoadAmmo(&a, child, version);
116 CArrayPushBack(ammo, &a);
117 }
118 }
119 static void LoadAmmo(Ammo *a, json_t *node, const int version)
120 {
121 memset(a, 0, sizeof *a);
122 a->Name = GetString(node, "Name");
123 json_t *picNode = json_find_first_label(node, "Pic")->child;
124 if (version < 2)
125 {
126 CPicLoadNormal(&a->Pic, picNode);
127 }
128 else
129 {
130 CPicLoadJSON(&a->Pic, picNode);
131 }
132 a->Sound = GetString(node, "Sound");
133 LoadInt(&a->Amount, node, "Amount");
134 LoadInt(&a->Max, node, "Max");
135 LoadInt(&a->Price, node, "Price");
136 }
137 void AmmoClassesClear(CArray *ammo)
138 {
139 CA_FOREACH(Ammo, a, *ammo)
140 CFREE(a->Name);
141 CFREE(a->Sound);
142 CFREE(a->DefaultGun);
143 CA_FOREACH_END()
144 CArrayClear(ammo);
145 }
146 void AmmoTerminate(AmmoClasses *ammo)
147 {
148 AmmoClassesClear(&ammo->Ammo);
149 CArrayTerminate(&ammo->Ammo);
150 AmmoClassesClear(&ammo->CustomAmmo);
151 CArrayTerminate(&ammo->CustomAmmo);
152 }
153
154 Ammo *AmmoGetById(AmmoClasses *ammo, const int id)
155 {
156 if (id < (int)ammo->Ammo.size)
157 {
158 return CArrayGet(&ammo->Ammo, id);
159 }
160 const int id2 = id - (int)ammo->Ammo.size;
161 if (id2 < (int)ammo->CustomAmmo.size)
162 {
163 return CArrayGet(&ammo->CustomAmmo, id2);
164 }
165 return NULL;
166 }
167
168 int AmmoGetNumClasses(const AmmoClasses *ammo)
169 {
170 return (int)ammo->Ammo.size + (int)ammo->CustomAmmo.size;
171 }
172
173 bool AmmoIsLow(const Ammo *a, const int amount)
174 {
175 // Low ammo if less than or equal to half the default amount
176 return amount <= (a->Amount / 2);
177 }