Debian Codegraph @

src/cdogs/mission.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) 2013-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 "mission.h"
50
51 #include <assert.h>
52 #include <stdlib.h>
53 #include <string.h>
54
55 #include "actors.h"
56 #include "color.h"
57 #include "defs.h"
58 #include "door.h"
59 #include "files.h"
60 #include "game_events.h"
61 #include "gamedata.h"
62 #include "map.h"
63 #include "map_new.h"
64 #include "music.h"
65 #include "net_util.h"
66 #include "objs.h"
67 #include "palette.h"
68 #include "particle.h"
69 #include "pic_manager.h"
70 #include "pickup.h"
71 #include "triggers.h"
72
73 color_t colorDoor = {172, 172, 172, 255};
74 color_t colorYellowDoor = {252, 224, 0, 255};
75 color_t colorGreenDoor = {0, 252, 0, 255};
76 color_t colorBlueDoor = {0, 252, 252, 255};
77 color_t colorRedDoor = {132, 0, 0, 255};
78
79 int StrKeycard(const char *s)
80 {
81 S2T(FLAGS_KEYCARD_YELLOW, "yellow");
82 S2T(FLAGS_KEYCARD_GREEN, "green");
83 S2T(FLAGS_KEYCARD_BLUE, "blue");
84 S2T(FLAGS_KEYCARD_RED, "red");
85 return 0;
86 }
87 const char *KeycardStr(const int d)
88 {
89 switch (d)
90 {
91 T2S(FLAGS_KEYCARD_YELLOW, "yellow");
92 T2S(FLAGS_KEYCARD_GREEN, "green");
93 T2S(FLAGS_KEYCARD_BLUE, "blue");
94 T2S(FLAGS_KEYCARD_RED, "red");
95 default:
96 return "";
97 }
98 }
99 color_t KeyColor(const int flags)
100 {
101 switch (flags)
102 {
103 case FLAGS_KEYCARD_YELLOW:
104 return colorYellowDoor;
105 case FLAGS_KEYCARD_GREEN:
106 return colorGreenDoor;
107 case FLAGS_KEYCARD_BLUE:
108 return colorBlueDoor;
109 case FLAGS_KEYCARD_RED:
110 return colorRedDoor;
111 default:
112 return colorDoor;
113 }
114 }
115
116 const char *MapTypeStr(MapType t)
117 {
118 switch (t)
119 {
120 T2S(MAPTYPE_CLASSIC, "Classic");
121 T2S(MAPTYPE_STATIC, "Static");
122 T2S(MAPTYPE_CAVE, "Cave");
123 T2S(MAPTYPE_INTERIOR, "Interior");
124 default:
125 return "";
126 }
127 }
128 MapType StrMapType(const char *s)
129 {
130 S2T(MAPTYPE_CLASSIC, "Classic");
131 S2T(MAPTYPE_STATIC, "Static");
132 S2T(MAPTYPE_CAVE, "Cave");
133 S2T(MAPTYPE_INTERIOR, "Interior");
134 return MAPTYPE_CLASSIC;
135 }
136
137 void MissionInit(Mission *m)
138 {
139 memset(m, 0, sizeof *m);
140 // Initialise with default styles
141 strcpy(m->ExitStyle, IntExitStyle(0));
142 strcpy(m->KeyStyle, IntKeyStyle(0));
143 CArrayInit(&m->Objectives, sizeof(Objective));
144 CArrayInit(&m->Enemies, sizeof(int));
145 CArrayInit(&m->SpecialChars, sizeof(int));
146 CArrayInit(&m->MapObjectDensities, sizeof(MapObjectDensity));
147 CArrayInit(&m->PickupCounts, sizeof(PickupCount));
148 CArrayInit(&m->Weapons, sizeof(const WeaponClass *));
149 m->Type = MAPTYPE_CLASSIC;
150 m->u.Classic.ExitEnabled = true;
151 MissionTileClassesInitDefault(&m->u.Classic.TileClasses);
152 }
153
154 static const MissionTileClasses *MissionGetTileClassesC(const Mission *m)
155 {
156 switch (m->Type)
157 {
158 case MAPTYPE_CLASSIC:
159 return &m->u.Classic.TileClasses;
160 case MAPTYPE_STATIC:
161 return NULL;
162 case MAPTYPE_CAVE:
163 return &m->u.Cave.TileClasses;
164 case MAPTYPE_INTERIOR:
165 return &m->u.Interior.TileClasses;
166 default:
167 return NULL;
168 }
169 }
170 void MissionCopy(Mission *dst, const Mission *src)
171 {
172 if (src == NULL)
173 {
174 return;
175 }
176 MissionTerminate(dst);
177 MissionInit(dst);
178 if (src->Title)
179 {
180 CSTRDUP(dst->Title, src->Title);
181 }
182 if (src->Description)
183 {
184 CSTRDUP(dst->Description, src->Description);
185 }
186 dst->Size = src->Size;
187
188 strcpy(dst->ExitStyle, src->ExitStyle);
189 strcpy(dst->KeyStyle, src->KeyStyle);
190
191 CA_FOREACH(const Objective, srco, src->Objectives)
192 Objective dsto;
193 ObjectiveCopy(&dsto, srco);
194 CArrayPushBack(&dst->Objectives, &dsto);
195 CA_FOREACH_END()
196 CArrayCopy(&dst->Enemies, &src->Enemies);
197 CArrayCopy(&dst->SpecialChars, &src->SpecialChars);
198 CArrayCopy(&dst->MapObjectDensities, &src->MapObjectDensities);
199 CArrayCopy(&dst->PickupCounts, &src->PickupCounts);
200
201 dst->EnemyDensity = src->EnemyDensity;
202 CArrayCopy(&dst->Weapons, &src->Weapons);
203
204 dst->Music = src->Music;
205 switch (src->Music.Type)
206 {
207 case MUSIC_SRC_GENERAL:
208 break;
209 case MUSIC_SRC_DYNAMIC:
210 if (src->Music.Data.Filename)
211 {
212 CSTRDUP(dst->Music.Data.Filename, src->Music.Data.Filename);
213 }
214 break;
215 case MUSIC_SRC_CHUNK:
216 // TODO: can't copy music chunks, only used by editor anyway
217 memset(&dst->Music.Data.Chunk, 0, sizeof dst->Music.Data.Chunk);
218 break;
219 default:
220 CASSERT(false, "unsupported music type");
221 break;
222 }
223
224 dst->WeaponPersist = src->WeaponPersist;
225 dst->SkipDebrief = src->SkipDebrief;
226
227 memcpy(&dst->u, &src->u, sizeof dst->u);
228 switch (src->Type)
229 {
230 case MAPTYPE_CLASSIC:
231 MissionTileClassesCopy(
232 MissionGetTileClasses(dst), MissionGetTileClassesC(src));
233 break;
234 case MAPTYPE_STATIC:
235 MissionStaticCopy(&dst->u.Static, &src->u.Static);
236 break;
237 case MAPTYPE_CAVE:
238 MissionTileClassesCopy(
239 MissionGetTileClasses(dst), MissionGetTileClassesC(src));
240 break;
241 case MAPTYPE_INTERIOR:
242 MissionTileClassesCopy(
243 MissionGetTileClasses(dst), MissionGetTileClassesC(src));
244 break;
245 default:
246 break;
247 }
248
249 // Copy type at the end so we can do type-specific conversions before this
250 dst->Type = src->Type;
251 }
252
253 void MissionTerminate(Mission *m)
254 {
255 if (m == NULL)
256 return;
257 CFREE(m->Title);
258 CFREE(m->Description);
259 CA_FOREACH(Objective, o, m->Objectives)
260 ObjectiveTerminate(o);
261 CA_FOREACH_END()
262 CArrayTerminate(&m->Objectives);
263 CArrayTerminate(&m->Enemies);
264 CArrayTerminate(&m->SpecialChars);
265 CArrayTerminate(&m->MapObjectDensities);
266 CArrayTerminate(&m->PickupCounts);
267 CArrayTerminate(&m->Weapons);
268 switch (m->Type)
269 {
270 case MAPTYPE_CLASSIC:
271 MissionTileClassesTerminate(MissionGetTileClasses(m));
272 break;
273 case MAPTYPE_STATIC:
274 MissionStaticTerminate(&m->u.Static);
275 break;
276 case MAPTYPE_CAVE:
277 MissionTileClassesTerminate(MissionGetTileClasses(m));
278 break;
279 case MAPTYPE_INTERIOR:
280 MissionTileClassesTerminate(MissionGetTileClasses(m));
281 break;
282 default:
283 CASSERT(false, "unknown map type");
284 break;
285 }
286 switch (m->Music.Type)
287 {
288 case MUSIC_SRC_DYNAMIC:
289 CFREE(m->Music.Data.Filename);
290 break;
291 case MUSIC_SRC_CHUNK:
292 MusicChunkTerminate(&m->Music.Data.Chunk);
293 break;
294 default:
295 break;
296 }
297 memset(m, 0, sizeof *m);
298 }
299
300 MissionTileClasses *MissionGetTileClasses(Mission *m)
301 {
302 switch (m->Type)
303 {
304 case MAPTYPE_CLASSIC:
305 return &m->u.Classic.TileClasses;
306 case MAPTYPE_STATIC:
307 return NULL;
308 case MAPTYPE_CAVE:
309 return &m->u.Cave.TileClasses;
310 case MAPTYPE_INTERIOR:
311 return &m->u.Interior.TileClasses;
312 default:
313 return NULL;
314 }
315 }
316
317 // +-----------------------+
318 // | And now the code... |
319 // +-----------------------+
320
321 static void SetupBadguysForMission(Mission *mission)
322 {
323 CharacterStore *s = &gCampaign.Setting.characters;
324
325 CharacterStoreResetOthers(s);
326
327 if (s->OtherChars.size == 0)
328 {
329 return;
330 }
331
332 CA_FOREACH(const Objective, o, mission->Objectives)
333 if (o->Type == OBJECTIVE_RESCUE)
334 {
335 CharacterStoreAddPrisoner(s, o->u.Index);
336 break; // TODO: multiple prisoners
337 }
338 CA_FOREACH_END()
339
340 CA_FOREACH(int, e, mission->Enemies)
341 CharacterStoreAddBaddie(s, *e);
342 CA_FOREACH_END()
343
344 CA_FOREACH(int, sc, mission->SpecialChars)
345 CharacterStoreAddSpecial(s, *sc);
346 CA_FOREACH_END()
347 }
348
349 static void SetupObjectives(Mission *m)
350 {
351 CA_FOREACH(Objective, o, m->Objectives)
352 ObjectiveSetup(o);
353 CASSERT(_ca_index < OBJECTIVE_MAX_OLD, "too many objectives");
354 CA_FOREACH_END()
355 }
356
357 static void SetupWeapons(CArray *to, CArray *from)
358 {
359 CArrayCopy(to, from);
360 }
361
362 void SetupMission(Mission *m, struct MissionOptions *mo, int missionIndex)
363 {
364 MissionOptionsInit(mo);
365 mo->index = missionIndex;
366 mo->missionData = m;
367
368 ActorsInit();
369 ObjsInit();
370 MobObjsInit();
371 PickupsInit();
372 ParticlesInit(&gParticles);
373 WatchesInit();
374 SetupObjectives(m);
375 SetupBadguysForMission(m);
376 SetupWeapons(&mo->Weapons, &m->Weapons);
377 }
378 void MissionSetupTileClasses(
379 Map *m, PicManager *pm, const MissionTileClasses *mtc)
380 {
381 SetupWallTileClasses(m, pm, &mtc->Wall);
382 SetupFloorTileClasses(m, pm, &mtc->Floor);
383 SetupFloorTileClasses(m, pm, &mtc->Room);
384 SetupDoorTileClasses(m, pm, &mtc->Door);
385 }
386 void MissionTileClassesInitDefault(MissionTileClasses *mtc)
387 {
388 TileClassInit(
389 &mtc->Wall, &gPicManager, &gTileWall, IntWallStyle(0),
390 TileClassBaseStyleType(TILE_CLASS_WALL), colorBattleshipGrey,
391 colorOfficeGreen);
392 TileClassInit(
393 &mtc->Floor, &gPicManager, &gTileFloor, IntFloorStyle(0),
394 TileClassBaseStyleType(TILE_CLASS_FLOOR), colorGravel,
395 colorOfficeGreen);
396 TileClassInit(
397 &mtc->Room, &gPicManager, &gTileRoom, IntRoomStyle(0),
398 TileClassBaseStyleType(TILE_CLASS_FLOOR), colorDoveGray,
399 colorOfficeGreen);
400 TileClassInit(
401 &mtc->Door, &gPicManager, &gTileDoor, IntDoorStyle(0),
402 TileClassBaseStyleType(TILE_CLASS_DOOR), colorWhite, colorWhite);
403 }
404 void MissionTileClassesCopy(
405 MissionTileClasses *dst, const MissionTileClasses *src)
406 {
407 if (dst == NULL || src == NULL)
408 return;
409 TileClassCopy(&dst->Door, &src->Door);
410 TileClassCopy(&dst->Floor, &src->Floor);
411 TileClassCopy(&dst->Wall, &src->Wall);
412 TileClassCopy(&dst->Room, &src->Room);
413 }
414 void MissionTileClassesTerminate(MissionTileClasses *mtc)
415 {
416 TileClassTerminate(&mtc->Wall);
417 TileClassTerminate(&mtc->Floor);
418 TileClassTerminate(&mtc->Room);
419 TileClassTerminate(&mtc->Door);
420 }
421
422 static int ObjectiveActorsAlive(const int objective);
423 void MissionSetMessageIfComplete(struct MissionOptions *options)
424 {
425 if (!gCampaign.IsClient)
426 {
427 if (CanCompleteMission(options) && !gMission.MissionCompleted)
428 {
429 GameEvent msg = GameEventNew(GAME_EVENT_MISSION_COMPLETE);
430 msg.u.MissionComplete = NMakeMissionComplete(options);
431 GameEventsEnqueue(&gGameEvents, msg);
432 }
433 else if (options->HasBegun && gCampaign.Entry.Mode == GAME_MODE_NORMAL)
434 {
435 // Check if the game is impossible to end
436 // i.e. not enough rescue objectives left alive
437 CA_FOREACH(const Objective, o, options->missionData->Objectives)
438 if (o->Type == OBJECTIVE_RESCUE)
439 {
440 if (ObjectiveActorsAlive(_ca_index) < o->Required)
441 {
442 GameEvent e = GameEventNew(GAME_EVENT_MISSION_END);
443 e.u.MissionEnd.Delay = GAME_OVER_DELAY;
444 strcpy(e.u.MissionEnd.Msg, "Mission failed");
445 GameEventsEnqueue(&gGameEvents, e);
446 }
447 }
448 CA_FOREACH_END()
449 }
450 }
451 }
452 // Get the number of actors alive for an objective
453 static int ObjectiveActorsAlive(const int objective)
454 {
455 int count = 0;
456 CA_FOREACH(const TActor, a, gActors)
457 if (a->isInUse && a->health > 0 &&
458 ObjectiveFromThing(a->thing.flags) == objective)
459 {
460 count++;
461 }
462 CA_FOREACH_END()
463 return count;
464 }
465
466 bool MissionHasRequiredObjectives(const struct MissionOptions *mo)
467 {
468 CA_FOREACH(const Objective, o, mo->missionData->Objectives)
469 if (ObjectiveIsRequired(o))
470 return true;
471 CA_FOREACH_END()
472 return false;
473 }
474
475 void UpdateMissionObjective(
476 const struct MissionOptions *options, const int flags,
477 const ObjectiveType type, const int count)
478 {
479 if (!(flags & THING_OBJECTIVE))
480 {
481 return;
482 }
483 const int idx = ObjectiveFromThing(flags);
484 const Objective *o = CArrayGet(&options->missionData->Objectives, idx);
485 if (o->Type != type)
486 {
487 return;
488 }
489 if (!gCampaign.IsClient)
490 {
491 GameEvent e = GameEventNew(GAME_EVENT_OBJECTIVE_UPDATE);
492 e.u.ObjectiveUpdate.ObjectiveId = idx;
493 e.u.ObjectiveUpdate.Count = count;
494 GameEventsEnqueue(&gGameEvents, e);
495 }
496 }
497
498 bool MissionCanBegin(void)
499 {
500 // Need at least two players to begin PVP
501 if (IsPVP(gCampaign.Entry.Mode))
502 {
503 return GetNumPlayers(PLAYER_ALIVE_OR_DYING, false, false) > 1;
504 }
505 // Otherwise, just one player will do
506 return GetNumPlayers(PLAYER_ALIVE_OR_DYING, false, false) > 0;
507 }
508
509 void MissionBegin(struct MissionOptions *m, const NGameBegin gb)
510 {
511 m->HasBegun = true;
512 m->state = MISSION_STATE_PLAY;
513 switch (m->missionData->Music.Type)
514 {
515 case MUSIC_SRC_GENERAL:
516 MusicPlayGeneral(&gSoundDevice.music, MUSIC_GAME);
517 break;
518 case MUSIC_SRC_DYNAMIC:
519 MusicPlayFile(
520 &gSoundDevice.music, MUSIC_GAME, gCampaign.Entry.Path,
521 m->missionData->Music.Data.Filename);
522 break;
523 case MUSIC_SRC_CHUNK:
524 MusicPlayFromChunk(
525 &gSoundDevice.music, MUSIC_GAME,
526 &m->missionData->Music.Data.Chunk);
527 break;
528 default:
529 CASSERT(false, "unsupported music type");
530 break;
531 }
532 const char *musicErrorMsg = MusicGetErrorMessage(&gSoundDevice.music);
533 if (strlen(musicErrorMsg) > 0)
534 {
535 // Display music error message for 2 seconds
536 GameEvent e = GameEventNew(GAME_EVENT_SET_MESSAGE);
537 strncat(
538 e.u.SetMessage.Message, musicErrorMsg,
539 sizeof e.u.SetMessage.Message - 1);
540 e.u.SetMessage.Ticks = 2000;
541 GameEventsEnqueue(&gGameEvents, e);
542 }
543 m->time = gb.MissionTime;
544 m->pickupTime = 0;
545 }
546
547 bool CanCompleteMission(const struct MissionOptions *options)
548 {
549 // Can't complete if not started yet
550 if (!options->HasBegun)
551 {
552 return false;
553 }
554
555 // Death is the only escape from PVP and quick play
556 if (IsPVP(gCampaign.Entry.Mode))
557 {
558 // If we're in deathmatch with 1 player only, never complete the game
559 // Instead we'll be showing a "waiting for players..." message
560 return GetNumPlayers(PLAYER_ANY, false, false) > 1 &&
561 GetNumPlayers(PLAYER_ALIVE_OR_DYING, false, false) <= 1;
562 }
563
564 return MissionAllObjectivesComplete(options);
565 }
566
567 bool MissionAllObjectivesComplete(const struct MissionOptions *mo)
568 {
569 // Check all objective counts are enough
570 CA_FOREACH(const Objective, o, mo->missionData->Objectives)
571 if (!ObjectiveIsComplete(o))
572 return false;
573 CA_FOREACH_END()
574 return true;
575 }
576
577 static bool MoreRescuesNeeded(const struct MissionOptions *mo, const int exit);
578
579 bool IsMissionComplete(const struct MissionOptions *mo)
580 {
581 if (!CanCompleteMission(mo))
582 {
583 return false;
584 }
585
586 // Check if dogfight is complete
587 if (IsPVP(gCampaign.Entry.Mode) &&
588 GetNumPlayers(PLAYER_ALIVE_OR_DYING, false, false) <= 1)
589 {
590 // Also check that only one player has lives left
591 int numPlayersWithLives = 0;
592 CA_FOREACH(const PlayerData, p, gPlayerDatas)
593 if (p->Lives > 0)
594 numPlayersWithLives++;
595 CA_FOREACH_END()
596 if (numPlayersWithLives <= 1)
597 {
598 return true;
599 }
600 }
601
602 const int exit = AllSurvivingPlayersInSameExit();
603 if (MoreRescuesNeeded(mo, exit))
604 {
605 return false;
606 }
607
608 return true;
609 }
610
611 bool MissionNeedsMoreRescuesInExit(const struct MissionOptions *mo)
612 {
613 const int exit = AllSurvivingPlayersInSameExit();
614 return CanCompleteMission(mo) && exit != -1 && MoreRescuesNeeded(mo, exit);
615 }
616
617 int AllSurvivingPlayersInSameExit(void)
618 {
619 // Check that all surviving players are in same exit, and return that exit
620 // Return -1 otherwise
621 // Note: players are still in the exit area if they are dying there;
622 // this is the basis for the "resurrection penalty"
623 int exit = -1;
624 CA_FOREACH(const PlayerData, p, gPlayerDatas)
625 if (!IsPlayerAliveOrDying(p))
626 continue;
627 const TActor *player = ActorGetByUID(p->ActorUID);
628 const int playerExit = MapIsTileInExit(&gMap, &player->thing, exit);
629 if (playerExit == -1)
630 return -1;
631 exit = playerExit;
632 CA_FOREACH_END()
633 return exit;
634 }
635
636 static bool MoreRescuesNeeded(const struct MissionOptions *mo, const int exit)
637 {
638 int rescuesRequired = 0;
639 // Find number of rescues required
640 // TODO: support multiple rescue objectives
641 CA_FOREACH(const Objective, o, mo->missionData->Objectives)
642 if (o->Type == OBJECTIVE_RESCUE)
643 {
644 rescuesRequired = o->Required;
645 break;
646 }
647 CA_FOREACH_END()
648 // Check that enough prisoners are in exit zone
649 if (rescuesRequired > 0)
650 {
651 int prisonersRescued = 0;
652 CA_FOREACH(const TActor, a, gActors)
653 if (!a->isInUse)
654 continue;
655 if (CharacterIsPrisoner(
656 &gCampaign.Setting.characters, ActorGetCharacter(a)) &&
657 MapIsTileInExit(&gMap, &a->thing, exit) == exit)
658 {
659 prisonersRescued++;
660 }
661 CA_FOREACH_END()
662 if (prisonersRescued < rescuesRequired)
663 {
664 return true;
665 }
666 }
667 return false;
668 }
669
670 void MissionDone(struct MissionOptions *mo, const NMissionEnd end)
671 {
672 mo->isDone = true;
673 mo->DoneCounter = end.Delay;
674 gCampaign.IsQuit = end.IsQuit;
675 mo->NextMission = end.Mission;
676 }
677
678 int KeycardCount(int flags)
679 {
680 int count = 0;
681 if (flags & FLAGS_KEYCARD_RED)
682 count++;
683 if (flags & FLAGS_KEYCARD_BLUE)
684 count++;
685 if (flags & FLAGS_KEYCARD_GREEN)
686 count++;
687 if (flags & FLAGS_KEYCARD_YELLOW)
688 count++;
689 return count;
690 }
691
692 void MissionStaticAddObjective(
693 Mission *m, MissionStatic *ms, const int idx, const int idx2,
694 const struct vec2i pos, const bool force)
695 {
696 CASSERT(m->Type == MAPTYPE_STATIC, "mission is not static type");
697 if (!force)
698 {
699 // Remove any objectives already there
700 MissionStaticTryRemoveObjective(m, ms, pos);
701 }
702
703 // Check if the objective already has an entry, and add to its list
704 // of positions
705 bool hasAdded = false;
706 PositionIndex pi = {pos, idx2};
707 for (int i = 0; i < (int)ms->Objectives.size; i++)
708 {
709 ObjectivePositions *op = CArrayGet(&ms->Objectives, i);
710 if (op->Index == idx)
711 {
712 CArrayPushBack(&op->PositionIndices, &pi);
713 hasAdded = true;
714 break;
715 }
716 }
717 // If not, create a new entry
718 if (!hasAdded)
719 {
720 ObjectivePositions newOp;
721 newOp.Index = idx;
722 CArrayInit(&newOp.PositionIndices, sizeof(PositionIndex));
723 CArrayPushBack(&newOp.PositionIndices, &pi);
724 CArrayPushBack(&ms->Objectives, &newOp);
725 }
726 // Increase number of objectives
727 Objective *o = CArrayGet(&m->Objectives, idx);
728 o->Count++;
729 }
730 bool MissionStaticTryRemoveObjective(
731 Mission *m, MissionStatic *ms, const struct vec2i pos)
732 {
733 CA_FOREACH(ObjectivePositions, op, ms->Objectives)
734 for (int j = 0; j < (int)op->PositionIndices.size; j++)
735 {
736 PositionIndex *pi = CArrayGet(&op->PositionIndices, j);
737 if (svec2i_is_equal(pi->Position, pos))
738 {
739 CArrayDelete(&op->PositionIndices, j);
740 // Decrease number of objectives
741 Objective *o = CArrayGet(&m->Objectives, op->Index);
742 o->Count--;
743 CASSERT(o->Count >= 0, "removing unknown objective");
744 if (op->PositionIndices.size == 0)
745 {
746 CArrayTerminate(&op->PositionIndices);
747 CArrayDelete(&ms->Objectives, _ca_index);
748 }
749 return true;
750 }
751 }
752 CA_FOREACH_END()
753 return false;
754 }