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-2021 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 #pragma once
50 
51 #include <stdbool.h>
52 
53 #include "config.h"
54 #include "map_object.h"
55 #include "mission_static.h"
56 #include "objective.h"
57 #include "proto/msg.pb.h"
58 #include "sys_config.h"
59 
60 #define ObjectiveFromThing(f) ((((f)&THING_OBJECTIVE) >> OBJECTIVE_SHIFT) - 1)
61 #define ObjectiveToThing(o) (((o) + 1) << OBJECTIVE_SHIFT)
62 
63 #define KEY_COUNT 4
64 
65 typedef enum
66 {
67 	MAPTYPE_CLASSIC,
68 	MAPTYPE_STATIC,
69 	MAPTYPE_CAVE,
70 	MAPTYPE_INTERIOR,
71 	MAPTYPE_COUNT
72 } MapType;
73 const char *MapTypeStr(MapType t);
74 MapType StrMapType(const char *s);
75 
76 // Keys that have been collected in this level
77 // Applies to all players
78 #define FLAGS_KEYCARD_YELLOW 0x1
79 #define FLAGS_KEYCARD_GREEN 0x2
80 #define FLAGS_KEYCARD_BLUE 0x4
81 #define FLAGS_KEYCARD_RED 0x8
82 int StrKeycard(const char *s);
83 color_t KeyColor(const int flags);
84 
85 typedef struct
86 {
87 	TileClass Wall;
88 	TileClass Floor;
89 	TileClass Room;
90 	TileClass Door;
91 } MissionTileClasses;
92 typedef struct
93 {
94 	const MapObject *M;
95 	int Density;
96 } MapObjectDensity;
97 typedef struct
98 {
99 	int Count;
100 	int Min;
101 	int Max;
102 	bool Edge;
103 	bool Overlap;
104 	int Walls;
105 	int WallLength;
106 	int WallPad;
107 } RoomParams;
108 typedef struct
109 {
110 	bool Enabled;
111 	int Min;
112 	int Max;
113 	bool RandomPos;
114 } DoorParams;
115 typedef struct
116 {
117 	int Count;
118 	int Min;
119 	int Max;
120 } PillarParams;
121 typedef struct
122 {
123 	char *Title;
124 	char *Description;
125 	MapType Type;
126 	struct vec2i Size;
127 
128 	// styles
129 	char ExitStyle[CDOGS_FILENAME_MAX];
130 	char KeyStyle[CDOGS_FILENAME_MAX];
131 
132 	CArray Objectives;		   // of Objective
133 	CArray Enemies;			   // of int (character index)
134 	CArray SpecialChars;	   // of int
135 	CArray MapObjectDensities; // of MapObjectDensity
136 
137 	int EnemyDensity;
138 	CArray Weapons; // of WeaponClass *
139 
140 	struct
141 	{
142 		MusicSourceType Type;
143 		union {
144 			char *Filename;
145 			MusicChunk Chunk;
146 		} Data;
147 	} Music;
148 
149 	bool WeaponPersist;
150 	bool SkipDebrief;
151 
152 	union {
153 		// Classic
154 		struct
155 		{
156 			// TODO: multiple tile classes
157 			MissionTileClasses TileClasses;
158 			int Walls;
159 			int WallLength;
160 			int CorridorWidth;
161 			RoomParams Rooms;
162 			int Squares;
163 			bool ExitEnabled;
164 			DoorParams Doors;
165 			PillarParams Pillars;
166 		} Classic;
167 		MissionStatic Static;
168 		// Cave
169 		struct
170 		{
171 			// TODO: multiple tile classes
172 			MissionTileClasses TileClasses;
173 			int FillPercent;
174 			int Repeat;
175 			int R1;
176 			int R2;
177 			int CorridorWidth;
178 			RoomParams Rooms;
179 			int Squares;
180 			bool ExitEnabled;
181 			bool DoorsEnabled;
182 		} Cave;
183 		struct
184 		{
185 			// TODO: multiple tile classes
186 			MissionTileClasses TileClasses;
187 			int CorridorWidth;
188 			RoomParams Rooms;
189 			bool ExitEnabled;
190 			DoorParams Doors;
191 			PillarParams Pillars;
192 		} Interior;
193 	} u;
194 } Mission;
195 
196 typedef enum
197 {
198 	MISSION_STATE_WAITING,
199 	MISSION_STATE_PLAY,
200 	MISSION_STATE_PICKUP
201 } MissionState;
202 
203 struct MissionOptions
204 {
205 	int index;
206 	int KeyFlags;
207 
208 	Mission *missionData;
209 	CArray Weapons; // of WeaponClass *
210 	int time;
211 	// Time when players first entered pickup area
212 	int pickupTime;
213 	MissionState state;
214 	// Whether the mission has loaded
215 	bool HasStarted;
216 	// Whether the mission has begun (can complete objectives etc.)
217 	bool HasBegun;
218 	bool isDone;
219 	int DoneCounter;
220 	int NextMission;
221 	bool MissionCompleted;
222 };
223 
224 void MissionInit(Mission *m);
225 void MissionCopy(Mission *dst, const Mission *src);
226 void MissionTerminate(Mission *m);
227 
228 MissionTileClasses *MissionGetTileClasses(Mission *m);
229 
230 void SetupMission(Mission *m, struct MissionOptions *mo, int missionIndex);
231 void MissionSetupTileClasses(PicManager *pm, const MissionTileClasses *mtc);
232 void MissionTileClassesInitDefault(MissionTileClasses *mtc);
233 void MissionTileClassesCopy(
234 	MissionTileClasses *dst, const MissionTileClasses *src);
235 void MissionTileClassesTerminate(MissionTileClasses *mtc);
236 
237 void MissionSetMessageIfComplete(struct MissionOptions *options);
238 // If object is a mission objective, send an update event
239 void UpdateMissionObjective(
240 	const struct MissionOptions *options, const int flags,
241 	const ObjectiveType type, const int count);
242 bool MissionCanBegin(void);
243 void MissionBegin(struct MissionOptions *m, const NGameBegin gb);
244 bool CanCompleteMission(const struct MissionOptions *options);
245 bool MissionAllObjectivesComplete(const struct MissionOptions *mo);
246 bool IsMissionComplete(const struct MissionOptions *mo);
247 // Returns exit index or -1 if not all players in same exit
248 int AllSurvivingPlayersInSameExit(void);
249 bool MissionNeedsMoreRescuesInExit(const struct MissionOptions *mo);
250 bool MissionHasRequiredObjectives(const struct MissionOptions *mo);
251 void MissionDone(struct MissionOptions *mo, const NMissionEnd end);
252 
253 // Count the number of keys in the flags
254 int KeycardCount(int flags);
255 
256 void MissionStaticAddObjective(
257 	Mission *m, MissionStatic *ms, const int idx, const int idx2,
258 	const struct vec2i pos, const bool force);
259 bool MissionStaticTryRemoveObjective(
260 	Mission *m, MissionStatic *ms, const struct vec2i pos);
261