Debian Codegraph @

src/cdogs/quick_play.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2013-2017, 2019-2021, 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 "quick_play.h"
29
30 #include <assert.h>
31
32 #include "actors.h"
33 #include "door.h"
34 #include "files.h"
35
36 // Random mission parameters
37 #define NUM_MISSIONS 3
38 QuickPlayQuantity missionSizes[NUM_MISSIONS] = {
39 QUICKPLAY_QUANTITY_SMALL, QUICKPLAY_QUANTITY_MEDIUM,
40 QUICKPLAY_QUANTITY_LARGE};
41 const char *missionTitles[NUM_MISSIONS] = {
42 "Rumble in the Cyberpen",
43 "Revenge of the Cyberzombies",
44 "Rise of the Cyberdog",
45 };
46 const char *missionDescriptions[NUM_MISSIONS] = {
47 "You are an elite Cyberdog. Your task is to kill your enemies!",
48 "The dastardly cyberzombies have gathered! Kill them before they get you!",
49 "End of the line! Kill the remaining enemies to defeat them once and for all!",
50 };
51 const int missionNumEnemies[NUM_MISSIONS] = {2, 4, 6};
52 const int missionKillCountMin[NUM_MISSIONS] = {3, 5, 7};
53
54 // Generate a random partition of an integer `total` into a pair of ints x, y
55 // With the restrictions that neither x, y are less than min, and
56 // neither x, y are greater than max
57 static struct vec2i GenerateRandomPairPartitionWithRestrictions(
58 int total, int min, int max)
59 {
60 struct vec2i v;
61 int xLow, xHigh;
62
63 // Check for invalid input
64 // Can't proceed if exactly half of total is greater than max,
65 // or if total less than min
66 if ((total + 1) / 2 > max || total < min)
67 {
68 assert(0 && "invalid random pair partition input");
69 return svec2i(total / 2, total - (total / 2));
70 }
71
72 // Find range of x first
73 // Must be at least min, or total - max
74 // Must be at most max, or total - min
75 xLow = MAX(min, total - max);
76 xHigh = MIN(max, total - min);
77 v.x = xLow + (rand() % (xHigh - xLow + 1));
78 v.y = total - v.x;
79 assert(v.x >= min);
80 assert(v.y >= min);
81 assert(v.x <= max);
82 assert(v.y <= max);
83 return v;
84 }
85
86 static struct vec2i GenerateQuickPlayMapSize(QuickPlayQuantity size)
87 {
88 const int minMapDim = 16;
89 const int maxMapDim = 64;
90 // Map sizes based on total dimensions (width + height)
91 // Small: 32 - 64
92 // Medium: 64 - 96
93 // Large: 96 - 128
94 // Restrictions: at least 16, at most 64 per side
95 switch (size)
96 {
97 case QUICKPLAY_QUANTITY_ANY:
98 return GenerateRandomPairPartitionWithRestrictions(
99 RAND_INT(32, 128 + 1), minMapDim, maxMapDim);
100 case QUICKPLAY_QUANTITY_SMALL:
101 return GenerateRandomPairPartitionWithRestrictions(
102 RAND_INT(32, 64 + 1), minMapDim, maxMapDim);
103 case QUICKPLAY_QUANTITY_MEDIUM:
104 return GenerateRandomPairPartitionWithRestrictions(
105 RAND_INT(64, 96 + 1), minMapDim, maxMapDim);
106 case QUICKPLAY_QUANTITY_LARGE:
107 return GenerateRandomPairPartitionWithRestrictions(
108 RAND_INT(96, 128 + 1), minMapDim, maxMapDim);
109 default:
110 assert(0 && "invalid quick play map size config");
111 return svec2i_zero();
112 }
113 }
114
115 // Generate a quick play parameter based on the quantity setting, and various
116 // thresholds
117 // e.g. if qty is "low", generate random number between low and medium
118 static int GenerateQuickPlayParam(
119 QuickPlayQuantity qty, int low, int medium, int high, int max)
120 {
121 switch (qty)
122 {
123 case QUICKPLAY_QUANTITY_ANY:
124 return RAND_INT(low, max + 1);
125 case QUICKPLAY_QUANTITY_SMALL:
126 return RAND_INT(low, medium + 1);
127 case QUICKPLAY_QUANTITY_MEDIUM:
128 return RAND_INT(medium, high + 1);
129 case QUICKPLAY_QUANTITY_LARGE:
130 return RAND_INT(high, max + 1);
131 default:
132 CASSERT(false, "unknown quick play quantity");
133 return 0;
134 }
135 }
136 static float GenerateQuickPlayParamFloat(
137 QuickPlayQuantity qty, const float low, const float medium,
138 const float high, const float max)
139 {
140 switch (qty)
141 {
142 case QUICKPLAY_QUANTITY_ANY:
143 return RAND_FLOAT(low, max);
144 case QUICKPLAY_QUANTITY_SMALL:
145 return RAND_FLOAT(low, medium);
146 case QUICKPLAY_QUANTITY_MEDIUM:
147 return RAND_FLOAT(medium, high);
148 case QUICKPLAY_QUANTITY_LARGE:
149 return RAND_FLOAT(high, max);
150 default:
151 CASSERT(false, "unknown quick play quantity");
152 return 0;
153 }
154 }
155
156 static void SetupQuickPlayEnemy(
157 Character *enemy, const WeaponClass *wc, const bool isBg)
158 {
159 CharacterShuffleAppearance(enemy);
160 enemy->Gun = wc;
161 enemy->speed = GenerateQuickPlayParamFloat(
162 ConfigGetEnum(&gConfig, "QuickPlay.EnemySpeed"), 0.25f, 0.4f, 0.7f, 1);
163 if (WeaponClassIsShortRange(enemy->Gun))
164 {
165 enemy->speed = enemy->speed * 1.25f;
166 }
167 if (WeaponClassIsShortRange(enemy->Gun))
168 {
169 enemy->bot->probabilityToMove = 35 + (rand() % 35);
170 }
171 else
172 {
173 enemy->bot->probabilityToMove = 30 + (rand() % 30);
174 }
175 enemy->bot->probabilityToTrack = 10 + (rand() % 60);
176 if (!WeaponClassCanShoot(enemy->Gun))
177 {
178 enemy->bot->probabilityToShoot = 0;
179 }
180 else if (WeaponClassIsHighDPS(enemy->Gun))
181 {
182 enemy->bot->probabilityToShoot = 1 + (rand() % 3);
183 }
184 else
185 {
186 enemy->bot->probabilityToShoot = 1 + (rand() % 6);
187 }
188 enemy->bot->actionDelay = rand() % (50 + 1);
189 enemy->maxHealth = GenerateQuickPlayParam(
190 ConfigGetEnum(&gConfig, "QuickPlay.EnemyHealth"), 10, 20, 40, 60);
191 enemy->excessHealth = enemy->maxHealth * 2;
192 enemy->flags = 0;
193 if (isBg)
194 {
195 enemy->flags |= FLAGS_AWAKEALWAYS;
196 if (RAND_BOOL())
197 {
198 enemy->flags |= FLAGS_GOOD_GUY;
199 }
200 }
201 }
202
203 static void SetupQuickPlayEnemies(
204 const int numEnemies, CharacterStore *store, const bool isBg)
205 {
206 for (int i = 0; i < numEnemies; i++)
207 {
208 const WeaponClass *wc;
209
210 for (;;)
211 {
212 wc = CArrayGet(
213 &gWeaponClasses.Guns, rand() % (int)gWeaponClasses.Guns.size);
214 if (!wc->IsRealGun)
215 {
216 continue;
217 }
218 // make at least one of each type of enemy:
219 // - Short range weapon
220 // - Long range weapon
221 // - High explosive weapon
222 if (i == 0 && !WeaponClassIsShortRange(wc))
223 {
224 continue;
225 }
226 if (i == 1 && !WeaponClassIsLongRange(wc))
227 {
228 continue;
229 }
230 if (i == 2 &&
231 ConfigGetBool(&gConfig, "QuickPlay.EnemiesWithExplosives") &&
232 !WeaponClassIsHighDPS(wc))
233 {
234 continue;
235 }
236
237 if (!ConfigGetBool(&gConfig, "QuickPlay.EnemiesWithExplosives") &&
238 WeaponClassIsHighDPS(wc))
239 {
240 continue;
241 }
242 break;
243 }
244 Character *ch = CharacterStoreAddOther(store);
245 SetupQuickPlayEnemy(ch, wc, isBg);
246 }
247 }
248
249 static void AddMission(
250 CArray *missions, PicManager *pm, const CharacterStore *cs, const int idx,
251 const bool isBg);
252 static void RandomMissionTileClasses(MissionTileClasses *mtc, PicManager *pm);
253 static RoomParams RandomRoomParams(void);
254 static color_t RandomBGColor(void);
255 void SetupQuickPlayCampaign(CampaignSetting *setting, const bool isBg)
256 {
257 CharacterStoreTerminate(&setting->characters);
258 CharacterStoreInit(&setting->characters);
259 int c = GenerateQuickPlayParam(
260 ConfigGetEnum(&gConfig, "QuickPlay.EnemyCount"), 3, 5, 8, 12);
261 SetupQuickPlayEnemies(c, &setting->characters, isBg);
262
263 CFREE(setting->Title);
264 CSTRDUP(setting->Title, "Quick play");
265 CFREE(setting->Author);
266 CSTRDUP(setting->Author, "");
267 CFREE(setting->Description);
268 CSTRDUP(setting->Description, "");
269 setting->RandomPickups = true;
270 setting->DoorOpenTicks = FPS_FRAMELIMIT;
271 for (int i = 0; i < NUM_MISSIONS; i++)
272 {
273 AddMission(
274 &setting->Missions, &gPicManager, &setting->characters, i, isBg);
275 }
276 }
277 static void RandomStyle(char *style, const CArray *styleNames);
278 static void AddMission(
279 CArray *missions, PicManager *pm, const CharacterStore *cs, const int idx,
280 const bool isBg)
281 {
282 Mission m;
283 MissionInit(&m);
284 CSTRDUP(m.Title, missionTitles[idx]);
285 CSTRDUP(m.Description, missionDescriptions[idx]);
286 RandomStyle(m.ExitStyle, &pm->exitStyleNames);
287 RandomStyle(m.KeyStyle, &pm->keyStyleNames);
288 if (isBg)
289 {
290 // Make sure map is big enough to cover window
291 m.Size = svec2i_add(
292 svec2i_divide(
293 gGraphicsDevice.cachedConfig.Res,
294 svec2i(TILE_WIDTH, TILE_HEIGHT)),
295 svec2i_one());
296 }
297 else
298 {
299 m.Size = GenerateQuickPlayMapSize(missionSizes[idx]);
300 }
301 do
302 {
303 m.Type = (MapType)(rand() % MAPTYPE_COUNT);
304 }
305 // Can't randomly generate static maps
306 while (m.Type == MAPTYPE_STATIC);
307 switch (m.Type)
308 {
309 case MAPTYPE_CLASSIC:
310 RandomMissionTileClasses(&m.u.Classic.TileClasses, pm);
311 m.u.Classic.Walls = GenerateQuickPlayParam(
312 ConfigGetEnum(&gConfig, "QuickPlay.WallCount"), 0, 5, 15, 30);
313 m.u.Classic.WallLength = GenerateQuickPlayParam(
314 ConfigGetEnum(&gConfig, "QuickPlay.WallLength"), 1, 3, 6, 12);
315 m.u.Classic.CorridorWidth = rand() % 3 + 1;
316 m.u.Classic.Rooms = RandomRoomParams();
317 m.u.Classic.Squares = GenerateQuickPlayParam(
318 ConfigGetEnum(&gConfig, "QuickPlay.SquareCount"), 0, 1, 3, 6);
319 m.u.Classic.ExitEnabled = RAND_BOOL();
320 m.u.Classic.Doors.Enabled = RAND_BOOL();
321 m.u.Classic.Doors.Min = 1;
322 m.u.Classic.Doors.Max = 6;
323 m.u.Classic.Pillars.Count = rand() % 5;
324 m.u.Classic.Pillars.Min = rand() % 3 + 1;
325 m.u.Classic.Pillars.Max = rand() % 3 + m.u.Classic.Pillars.Min;
326 break;
327 case MAPTYPE_CAVE:
328 // TODO: quickplay configs for cave type
329 RandomMissionTileClasses(&m.u.Cave.TileClasses, pm);
330 m.u.Cave.FillPercent = rand() % 40 + 10;
331 m.u.Cave.Repeat = rand() % 6;
332 m.u.Cave.R1 = rand() % 2 + 4;
333 m.u.Cave.R2 = rand() % 5 - 1;
334 m.u.Cave.CorridorWidth = rand() % 3 + 1;
335 m.u.Cave.Rooms = RandomRoomParams();
336 m.u.Cave.Squares = GenerateQuickPlayParam(
337 ConfigGetEnum(&gConfig, "QuickPlay.SquareCount"), 0, 1, 3, 6);
338 m.u.Cave.ExitEnabled = RAND_BOOL();
339 m.u.Cave.DoorsEnabled = RAND_BOOL();
340 break;
341 case MAPTYPE_INTERIOR:
342 // TODO: quickplay configs for interior type
343 RandomMissionTileClasses(&m.u.Interior.TileClasses, pm);
344 m.u.Interior.CorridorWidth = rand() % 3 + 1;
345 m.u.Interior.Rooms = RandomRoomParams();
346 m.u.Interior.ExitEnabled = RAND_BOOL();
347 m.u.Interior.Doors.Enabled = RAND_BOOL();
348 m.u.Interior.Doors.Min = 1;
349 m.u.Interior.Doors.Max = 6;
350 m.u.Interior.Pillars.Count = rand() % 5;
351 m.u.Interior.Pillars.Min = rand() % 3 + 1;
352 m.u.Interior.Pillars.Max = rand() % 3 + m.u.Interior.Pillars.Min;
353 break;
354 default:
355 CASSERT(false, "unknown map type");
356 break;
357 }
358
359 for (int i = 0; i < MIN(missionNumEnemies[idx], (int)cs->OtherChars.size);
360 i++)
361 {
362 // TODO: select enemies
363 CArrayPushBack(&m.Enemies, &i);
364 }
365
366 int c = 0;
367
368 if (!isBg)
369 {
370 CArrayPushBack(&m.SpecialChars, &c);
371 Objective o;
372 CSTRDUP(o.Description, "Kill the enemies");
373 o.Type = OBJECTIVE_KILL;
374 o.u.Index = 0;
375 o.Count = RAND_INT(
376 missionKillCountMin[idx], missionKillCountMin[idx] * 3 / 2);
377 o.Required = RAND_INT(MAX(1, o.Count / 2), o.Count);
378 o.Flags = OBJECTIVE_POSKNOWN;
379 CArrayPushBack(&m.Objectives, &o);
380 }
381
382 c = GenerateQuickPlayParam(
383 ConfigGetEnum(&gConfig, "QuickPlay.ItemCount"), 0, 10, 20, 40);
384 for (int i = 0; i < c; i++)
385 {
386 MapObjectDensity mop;
387 mop.M = IndexMapObject(rand() % MapObjectsCount(&gMapObjects));
388 if (mop.M->Type == MAP_OBJECT_TYPE_PICKUP_SPAWNER)
389 {
390 mop.Density = 1;
391 }
392 else
393 {
394 mop.Density = GenerateQuickPlayParam(
395 ConfigGetEnum(&gConfig, "QuickPlay.ItemCount"), 0, 5, 10, 20);
396 }
397 CArrayPushBack(&m.MapObjectDensities, &mop);
398 }
399 m.EnemyDensity = 10 / (int)m.Enemies.size;
400 CA_FOREACH(const WeaponClass, wc, gWeaponClasses.Guns)
401 if (wc->IsRealGun)
402 {
403 CArrayPushBack(&m.Weapons, &wc);
404 }
405 CA_FOREACH_END()
406 m.WeaponPersist = true;
407
408 CArrayPushBack(missions, &m);
409 }
410 static void RandomMissionTileClasses(MissionTileClasses *mtc, PicManager *pm)
411 {
412 char style[CDOGS_FILENAME_MAX];
413 RandomStyle(style, &pm->wallStyleNames);
414 TileClassInit(
415 &mtc->Wall, pm, &gTileWall, style,
416 TileClassBaseStyleType(TILE_CLASS_WALL), RandomBGColor(),
417 RandomBGColor());
418 RandomStyle(style, &pm->tileStyleNames);
419 TileClassInit(
420 &mtc->Floor, pm, &gTileFloor, style,
421 TileClassBaseStyleType(TILE_CLASS_FLOOR), RandomBGColor(),
422 RandomBGColor());
423 RandomStyle(style, &pm->tileStyleNames);
424 TileClassInit(
425 &mtc->Room, pm, &gTileRoom, style,
426 TileClassBaseStyleType(TILE_CLASS_FLOOR), RandomBGColor(),
427 RandomBGColor());
428 RandomStyle(style, &pm->doorStyleNames);
429 TileClassInit(
430 &mtc->Door, pm, &gTileDoor, style,
431 TileClassBaseStyleType(TILE_CLASS_DOOR), colorWhite, colorWhite);
432 }
433 static RoomParams RandomRoomParams(void)
434 {
435 RoomParams r;
436 r.Count = GenerateQuickPlayParam(
437 ConfigGetEnum(&gConfig, "QuickPlay.RoomCount"), 0, 2, 5, 12);
438 r.Min = rand() % 10 + 5;
439 r.Max = rand() % 10 + r.Min;
440 r.Edge = 1;
441 r.Overlap = 1;
442 r.Walls = rand() % 5;
443 r.WallLength = rand() % 6 + 1;
444 r.WallPad = rand() % 4 + 1;
445 return r;
446 }
447 static void RandomStyle(char *style, const CArray *styleNames)
448 {
449 const int idx = rand() % styleNames->size;
450 strcpy(style, *(char **)CArrayGet(styleNames, idx));
451 }
452 static color_t RandomBGColor(void)
453 {
454 color_t c;
455 c.r = rand() % 128;
456 c.g = rand() % 128;
457 c.b = rand() % 128;
458 c.a = 255;
459 return c;
460 }