Debian Codegraph @

src/cdogs/objective.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (C) 1995 Ronny Wester
5 Copyright (C) 2003 Jeremy Chin
6 Copyright (C) 2003-2007 Lucas Martin-King
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 This file incorporates work covered by the following copyright and
23 permission notice:
24
25 Copyright (c) 2014, 2016-2017, 2019, 2023 Cong Xu
26 All rights reserved.
27
28 Redistribution and use in source and binary forms, with or without
29 modification, are permitted provided that the following conditions are met:
30
31 Redistributions of source code must retain the above copyright notice, this
32 list of conditions and the following disclaimer.
33 Redistributions in binary form must reproduce the above copyright notice,
34 this list of conditions and the following disclaimer in the documentation
35 and/or other materials provided with the distribution.
36
37 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
41 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47 POSSIBILITY OF SUCH DAMAGE.
48 */
49 #include "objective.h"
50
51 #include "json_utils.h"
52 #include "utils.h"
53
54 const char *ObjectiveTypeStr(const ObjectiveType t)
55 {
56 switch (t)
57 {
58 T2S(OBJECTIVE_KILL, "Kill");
59 T2S(OBJECTIVE_COLLECT, "Collect");
60 T2S(OBJECTIVE_DESTROY, "Destroy");
61 T2S(OBJECTIVE_RESCUE, "Rescue");
62 T2S(OBJECTIVE_INVESTIGATE, "Explore");
63 default:
64 return "";
65 }
66 }
67 ObjectiveType StrObjectiveType(const char *s)
68 {
69 S2T(OBJECTIVE_KILL, "Kill");
70 S2T(OBJECTIVE_COLLECT, "Collect");
71 S2T(OBJECTIVE_DESTROY, "Destroy");
72 S2T(OBJECTIVE_RESCUE, "Rescue");
73 S2T(OBJECTIVE_INVESTIGATE, "Explore");
74 CASSERT(false, "unknown objective name");
75 return OBJECTIVE_KILL;
76 }
77 color_t ObjectiveTypeColor(const ObjectiveType t)
78 {
79 switch (t)
80 {
81 case OBJECTIVE_KILL: // fallthrough
82 case OBJECTIVE_DESTROY:
83 return colorRed;
84 case OBJECTIVE_COLLECT: // fallthrough
85 case OBJECTIVE_RESCUE:
86 return colorGreen;
87 case OBJECTIVE_INVESTIGATE:
88 return colorCyan;
89 default:
90 CASSERT(false, "unknown objective type");
91 // Shouldn't get here but use a different colour in case
92 return colorYellow;
93 }
94 }
95
96 void ObjectiveSetPickup(Objective *o, const PickupClass *p)
97 {
98 CASSERT(o->Type == OBJECTIVE_COLLECT, "invalid objective type");
99 CArrayTerminate(&o->u.Pickups);
100 CArrayInit(&o->u.Pickups, sizeof(const PickupClass *));
101 CArrayPushBack(&o->u.Pickups, &p);
102 }
103
104 void ObjectiveSetDestroy(Objective *o, const MapObject *mo)
105 {
106 CASSERT(o->Type == OBJECTIVE_DESTROY, "invalid objective type");
107 CArrayTerminate(&o->u.MapObjects);
108 CArrayInit(&o->u.MapObjects, sizeof(const MapObject *));
109 CArrayPushBack(&o->u.MapObjects, &mo);
110 }
111
112 void ObjectiveLoadJSON(Objective *o, json_t *node, const int version)
113 {
114 memset(o, 0, sizeof *o);
115 o->Description = GetString(node, "Description");
116 JSON_UTILS_LOAD_ENUM(o->Type, node, "Type", StrObjectiveType);
117 if (version < 8)
118 {
119 // Index numbers used for all objective classes; convert them
120 // to their class handles
121 int index;
122 LoadInt(&index, node, "Index");
123 switch (o->Type)
124 {
125 case OBJECTIVE_COLLECT:
126 ObjectiveSetPickup(o, IntPickupClass(index));
127 break;
128 case OBJECTIVE_DESTROY:
129 ObjectiveSetDestroy(o, IntMapObject(index));
130 break;
131 default:
132 o->u.Index = index;
133 break;
134 }
135 }
136 else
137 {
138 char *tmp = NULL;
139 switch (o->Type)
140 {
141 case OBJECTIVE_COLLECT:
142 LoadStr(&tmp, node, "Pickup");
143 if (tmp)
144 {
145 ObjectiveSetPickup(o, StrPickupClass(tmp));
146 CFREE(tmp);
147 }
148 else
149 {
150 CArrayInit(&o->u.Pickups, sizeof(const PickupClass *));
151 const json_t *child = json_find_first_label(node, "Pickups");
152 if (child && child->child)
153 {
154 for (child = child->child->child; child; child = child->next)
155 {
156 const PickupClass *p = StrPickupClass(child->text);
157 CASSERT(p != NULL, "Cannot load pickup class");
158 CArrayPushBack(&o->u.Pickups, &p);
159 }
160 }
161 }
162 break;
163 case OBJECTIVE_DESTROY:
164 LoadStr(&tmp, node, "MapObject");
165 if (tmp)
166 {
167 ObjectiveSetDestroy(o, StrMapObject(tmp));
168 CFREE(tmp);
169 }
170 else
171 {
172 CArrayInit(&o->u.MapObjects, sizeof(const MapObject *));
173 const json_t *child = json_find_first_label(node, "MapObjects");
174 if (child && child->child)
175 {
176 for (child = child->child->child; child; child = child->next)
177 {
178 const MapObject *mo = StrMapObject(child->text);
179 CASSERT(mo != NULL, "Cannot load map object");
180 CArrayPushBack(&o->u.MapObjects, &mo);
181 }
182 }
183 }
184 break;
185 default:
186 LoadInt(&o->u.Index, node, "Index");
187 break;
188 }
189 }
190 LoadInt(&o->Count, node, "Count");
191 LoadInt(&o->Required, node, "Required");
192 LoadInt(&o->Flags, node, "Flags");
193 if (version < 14)
194 {
195 // Classic objective flags: rescue always in locked rooms, kill always
196 // anywhere
197 if (o->Type == OBJECTIVE_RESCUE)
198 {
199 o->Flags |= OBJECTIVE_HIACCESS;
200 o->Flags &= ~OBJECTIVE_NOACCESS;
201 }
202 else if (o->Type == OBJECTIVE_KILL)
203 {
204 o->Flags &= ~(OBJECTIVE_HIACCESS | OBJECTIVE_NOACCESS);
205 }
206 }
207 }
208 void ObjectiveSetup(Objective *o)
209 {
210 // Set objective colours based on type
211 o->color = ObjectiveTypeColor(o->Type);
212 o->placed = 0;
213 o->done = 0;
214 }
215 void ObjectiveCopy(Objective *dst, const Objective *src)
216 {
217 memcpy(dst, src, sizeof *dst);
218 if (src->Description)
219 {
220 CSTRDUP(dst->Description, src->Description);
221 }
222 switch (src->Type)
223 {
224 case OBJECTIVE_COLLECT:
225 memset(&dst->u.Pickups, 0, sizeof(dst->u.Pickups));
226 CArrayCopy(&dst->u.Pickups, &src->u.Pickups);
227 break;
228 case OBJECTIVE_DESTROY:
229 memset(&dst->u.MapObjects, 0, sizeof(dst->u.MapObjects));
230 CArrayCopy(&dst->u.MapObjects, &src->u.MapObjects);
231 break;
232 default:
233 break;
234 }
235 }
236 void ObjectiveTerminate(Objective *o)
237 {
238 CFREE(o->Description);
239 switch (o->Type)
240 {
241 case OBJECTIVE_COLLECT:
242 CArrayTerminate(&o->u.Pickups);
243 break;
244 case OBJECTIVE_DESTROY:
245 CArrayTerminate(&o->u.MapObjects);
246 break;
247 default:
248 break;
249 }
250 }
251
252 bool ObjectiveIsRequired(const Objective *o)
253 {
254 return o->Required > 0;
255 }
256 bool ObjectiveIsComplete(const Objective *o)
257 {
258 return o->done >= o->Required;
259 }
260 bool ObjectiveIsPerfect(const Objective *o)
261 {
262 // Don't count objectives that are fully required anyway
263 return o->done == o->Count && o->done > o->Required;
264 }
265
266 PlacementAccessFlags ObjectiveGetPlacementAccessFlags(const Objective *o)
267 {
268 if (o->Flags & OBJECTIVE_HIACCESS)
269 {
270 return PLACEMENT_ACCESS_LOCKED;
271 }
272 if (o->Flags & OBJECTIVE_NOACCESS)
273 {
274 return PLACEMENT_ACCESS_NOT_LOCKED;
275 }
276 return PLACEMENT_ACCESS_ANY;
277 }