Debian Codegraph @

src/cdogs/powerup.c

raw symbols SCIP
1 /*
2 Copyright (c) 2014-2015, 2020 Cong Xu
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10 Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 POSSIBILITY OF SUCH DAMAGE.
25 */
26 #include "powerup.h"
27
28 #include <float.h>
29 #include <math.h>
30
31 #include "ai_utils.h"
32 #include "ammo.h"
33 #include "game_events.h"
34 #include "gamedata.h"
35 #include "net_util.h"
36 #include "pickup.h"
37
38 #define TIME_DECAY_EXPONENT 1.04
39 #define HEALTH_W 6
40 #define HEALTH_H 6
41 #define MAX_TILES_PER_PICKUP 300
42
43 void PowerupSpawnerInit(PowerupSpawner *p, Map *map)
44 {
45 memset(p, 0, sizeof *p);
46 p->map = map;
47 p->timer = 0;
48 p->numPickups = 0;
49 p->Enabled = true;
50 }
51 void PowerupSpawnerTerminate(PowerupSpawner *p)
52 {
53 CFREE(p->Data);
54 }
55
56 static bool TryPlacePickup(PowerupSpawner *p);
57 void PowerupSpawnerUpdate(PowerupSpawner *p, const int ticks)
58 {
59 // Don't spawn pickups if not allowed
60 if (!p->Enabled)
61 {
62 return;
63 }
64
65 double scalar = p->RateScaleFunc(p->Data);
66 if (scalar == DBL_MAX)
67 {
68 p->timeUntilNextSpawn = INT_MAX;
69 }
70 else
71 {
72 // Scale down over time
73 scalar *= pow(TIME_DECAY_EXPONENT, p->numPickups);
74
75 p->timeUntilNextSpawn = (int)floor(scalar * p->SpawnTime);
76 }
77
78 // Update time
79 p->timer += ticks;
80
81 // Attempt to add pickup if time reached, and we haven't placed too many
82 if (p->timer >= p->timeUntilNextSpawn &&
83 p->map->NumExplorableTiles / MAX_TILES_PER_PICKUP + 1 > p->numPickups)
84 {
85 p->timer -= p->timeUntilNextSpawn;
86
87 if (TryPlacePickup(p))
88 {
89 p->numPickups++;
90 }
91 }
92 }
93 static bool TryPlacePickup(PowerupSpawner *p)
94 {
95 const struct vec2i size = svec2i(HEALTH_W, HEALTH_H);
96 // Attempt to place one in out-of-sight area
97 for (int i = 0; i < 100; i++)
98 {
99 const struct vec2 v = MapGenerateFreePosition(p->map, size);
100 const TActor *closestPlayer = AIGetClosestPlayer(v);
101 if (!svec2_is_zero(v) &&
102 (!closestPlayer ||
103 svec2_distance_squared(v, closestPlayer->Pos) >= SQUARED(150)))
104 {
105 p->PlaceFunc(v, p->Data);
106 return true;
107 }
108 }
109 // Attempt to place one anyway
110 for (int i = 0; i < 100; i++)
111 {
112 const struct vec2 v = MapGenerateFreePosition(p->map, size);
113 if (!svec2_is_zero(v))
114 {
115 p->PlaceFunc(v, p->Data);
116 return true;
117 }
118 }
119 return false;
120 }
121
122 void PowerupSpawnerRemoveOne(PowerupSpawner *p)
123 {
124 p->numPickups = MAX(0, p->numPickups - 1);
125 }
126
127 #define HEALTH_SPAWN_TIME (20 * FPS_FRAMELIMIT)
128
129 static double HealthScale(void *data);
130 static void HealthPlace(const struct vec2 pos, void *data);
131 void HealthSpawnerInit(PowerupSpawner *p, Map *map)
132 {
133 PowerupSpawnerInit(p, map);
134 p->Enabled = AreHealthPickupsAllowed(gCampaign.Entry.Mode) &&
135 ConfigGetBool(&gConfig, "Game.HealthPickups") &&
136 !gCampaign.IsClient;
137 p->SpawnTime = HEALTH_SPAWN_TIME;
138 p->RateScaleFunc = HealthScale;
139 p->PlaceFunc = HealthPlace;
140
141 // Update once
142 PowerupSpawnerUpdate(p, 0);
143 }
144 static double HealthScale(void *data)
145 {
146 UNUSED(data);
147 // Update time until next spawn based on:
148 // Damage taken (find player with lowest health)
149 int minHealth = CampaignGetMaxHP(&gCampaign);
150 int maxHealth = minHealth;
151 CA_FOREACH(const PlayerData, p, gPlayerDatas)
152 if (!IsPlayerAlive(p))
153 {
154 continue;
155 }
156 const TActor *player = ActorGetByUID(p->ActorUID);
157 minHealth = MIN(minHealth, player->health);
158 CA_FOREACH_END()
159 // Double spawn rate if near 0 health
160 return (minHealth + maxHealth) / (maxHealth * 2.0);
161 }
162 static void HealthPlace(const struct vec2 pos, void *data)
163 {
164 UNUSED(data);
165 GameEvent e = GameEventNew(GAME_EVENT_ADD_PICKUP);
166 e.u.AddPickup.Pos = Vec2ToNet(pos);
167 strcpy(e.u.AddPickup.PickupClass, "health");
168 e.u.AddPickup.IsRandomSpawned = true;
169 GameEventsEnqueue(&gGameEvents, e);
170 }
171
172 #define AMMO_SPAWN_TIME (20 * FPS_FRAMELIMIT)
173
174 static double AmmoScale(void *data);
175 static void AmmoPlace(const struct vec2 pos, void *data);
176 void AmmoSpawnerInit(PowerupSpawner *p, Map *map, const int ammoId)
177 {
178 PowerupSpawnerInit(p, map);
179 // TODO: disable ammo spawners unless classic mode
180 p->Enabled = gCampaign.Setting.Ammo && !gCampaign.IsClient;
181 p->SpawnTime = AMMO_SPAWN_TIME;
182 p->RateScaleFunc = AmmoScale;
183 p->PlaceFunc = AmmoPlace;
184 CMALLOC(p->Data, sizeof ammoId);
185 *(int *)p->Data = ammoId;
186
187 // Update once
188 PowerupSpawnerUpdate(p, 0);
189 }
190 static double AmmoScale(void *data)
191 {
192 const int ammoId = *(int *)data;
193 // Make sure at least one player has a gun that uses this ammo
194 const int numPlayersWithAmmo = PlayersNumUseAmmo(ammoId);
195 if (numPlayersWithAmmo == 0)
196 {
197 // No players have guns that use this ammo; never spawn
198 return DBL_MAX;
199 }
200
201 // Update time until next spawn based on:
202 // Ammo left (find player with lowest ammo)
203 int minVal = AmmoGetById(&gAmmo, ammoId)->Max;
204 int maxVal = minVal;
205 CA_FOREACH(const PlayerData, p, gPlayerDatas)
206 if (!IsPlayerAlive(p))
207 {
208 continue;
209 }
210 const TActor *player = ActorGetByUID(p->ActorUID);
211 minVal = MIN(minVal, *(int *)CArrayGet(&player->ammo, ammoId));
212 CA_FOREACH_END()
213 // 10-fold spawn rate if near 0 ammo
214 return (minVal * 9.0 + maxVal) / (maxVal * 10.0) / numPlayersWithAmmo;
215 }
216 static void AmmoPlace(const struct vec2 pos, void *data)
217 {
218 const int ammoId = *(int *)data;
219 GameEvent e = GameEventNew(GAME_EVENT_ADD_PICKUP);
220 e.u.AddPickup.Pos = Vec2ToNet(pos);
221 const Ammo *a = AmmoGetById(&gAmmo, ammoId);
222 sprintf(e.u.AddPickup.PickupClass, "ammo_%s", a->Name);
223 e.u.AddPickup.IsRandomSpawned = true;
224 GameEventsEnqueue(&gGameEvents, e);
225 }