Debian Codegraph @

src/prep_equip.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2013-2018, 2020-2023 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 "prep_equip.h"
29
30 #include <cdogs/net_client.h>
31 #include <cdogs/net_server.h>
32 #include <cdogs/player.h>
33
34 #include "autosave.h"
35 #include "prep.h"
36 #include "equip_menu.h"
37
38 static void AddPlayerWeapons(CArray *weapons, const WeaponClass **guns);
39 static void RemoveUnavailableWeapons(
40 const WeaponClass **guns, const CArray *weapons);
41 static void AddDefaultGuns(PlayerData *p, const int idx, const CArray *guns);
42 typedef struct
43 {
44 EquipMenu menus[MAX_LOCAL_PLAYERS];
45 EventWaitResult waitResult;
46 } PlayerEquipData;
47 static void PlayerEquipTerminate(GameLoopData *data);
48 static void PlayerEquipOnExit(GameLoopData *data);
49 static GameLoopResult PlayerEquipUpdate(GameLoopData *data, LoopRunner *l);
50 static void PlayerEquipDraw(GameLoopData *data);
51 GameLoopData *PlayerEquip(void)
52 {
53 PlayerEquipData *data;
54 CCALLOC(data, sizeof *data);
55 const CampaignSave *save = AutosaveGetCampaign(&gAutosave, gCampaign.Entry.Path);
56 const Mission *m = CampaignGetCurrentMission(&gCampaign);
57 if (save != NULL && gCampaign.MissionIndex > 0)
58 {
59 PlayerSavesApply(&save->Players, m->WeaponPersist);
60 }
61 data->waitResult = EVENT_WAIT_CONTINUE;
62 for (int i = 0, idx = 0; i < (int)gPlayerDatas.size; i++, idx++)
63 {
64 PlayerData *p = CArrayGet(&gPlayerDatas, i);
65 if (!p->IsLocal)
66 {
67 idx--;
68 continue;
69 }
70
71 CArray weapons;
72 CArrayInit(&weapons, sizeof(WeaponClass *));
73 CArrayCopy(&weapons, &gMission.Weapons);
74 if (gMission.missionData->WeaponPersist)
75 {
76 // Add player's current weapons into available weapons
77 AddPlayerWeapons(&weapons, p->guns);
78 }
79 else
80 {
81 // Remove unavailable weapons from players inventories
82 RemoveUnavailableWeapons(p->guns, &weapons);
83 }
84
85 // Add default guns if the player has no weapons
86 if (PlayerGetNumWeapons(p) == 0)
87 {
88 AddDefaultGuns(p, idx, &weapons);
89 }
90
91 // Get previous mission's weapons
92 const CArray *prevWeapons = NULL;
93 if (gCampaign.Entry.Mode == GAME_MODE_NORMAL &&
94 gCampaign.MissionIndex > 0)
95 {
96 const Mission *prevMission = CArrayGet(
97 &gCampaign.Setting.Missions, gCampaign.MissionIndex - 1);
98 prevWeapons = &prevMission->Weapons;
99 }
100
101 // Special case: reset player lives
102 // Player.Lives is modified dynamically in game, so if we
103 // are replaying, we need to reset if we have no player save
104 if (save == NULL || gCampaign.MissionIndex == 0)
105 {
106 p->Lives = CampaignGetLives(&gCampaign);
107 }
108
109 EquipMenuCreate(
110 &data->menus[idx], &weapons, prevWeapons,
111 GetNumPlayers(PLAYER_ANY, false, true), idx, p->UID,
112 &gEventHandlers, &gGraphicsDevice);
113 CArrayTerminate(&weapons);
114 }
115
116 return GameLoopDataNew(
117 data, PlayerEquipTerminate, NULL, PlayerEquipOnExit, NULL,
118 PlayerEquipUpdate, PlayerEquipDraw);
119 }
120 static bool HasWeapon(const CArray *weapons, const WeaponClass *wc);
121 static void RemoveUnavailableWeapons(
122 const WeaponClass **guns, const CArray *weapons)
123 {
124 for (int i = 0; i < MAX_WEAPONS; i++)
125 {
126 if (!HasWeapon(weapons, guns[i]))
127 {
128 guns[i] = NULL;
129 }
130 }
131 }
132 static void AddPlayerWeapons(CArray *weapons, const WeaponClass **guns)
133 {
134 for (int i = 0; i < MAX_WEAPONS; i++)
135 {
136 if (guns[i] != NULL && !HasWeapon(weapons, guns[i]))
137 {
138 CArrayPushBack(weapons, &guns[i]);
139 }
140 }
141 }
142 static void AddDefaultGuns(PlayerData *p, const int idx, const CArray *guns)
143 {
144 const char *defaultGuns[MAX_LOCAL_PLAYERS][MAX_WEAPONS] = {
145 {"Shotgun", "Machine gun", "Knife", "Shrapnel bombs"},
146 {"Powergun", "Flamer", "Knife", "Grenades"},
147 {"Sniper rifle", "Pulse rifle", "Knife", "Molotovs"},
148 {"Machine gun", "Flamer", "Knife", "Dynamite"},
149 };
150
151 if (!gCampaign.Setting.BuyAndSell)
152 {
153 // Attempt to give the player default guns; if the guns are missing,
154 // find guns available in the mission Look for the default guns
155 for (int i = 0; i < MAX_WEAPONS; i++)
156 {
157 const WeaponClass *defaultWC = NULL;
158 CA_FOREACH(const WeaponClass *, wc, *guns)
159 const char *gunName = defaultGuns[idx][i];
160 if (strcmp((*wc)->name, gunName) == 0)
161 {
162 defaultWC = *wc;
163 }
164 CA_FOREACH_END()
165 if (defaultWC != NULL)
166 {
167 PlayerAddWeapon(p, defaultWC);
168 }
169 }
170 // Look for guns available in the mission
171 CA_FOREACH(const WeaponClass *, wc, *guns)
172 PlayerAddWeapon(p, *wc);
173 CA_FOREACH_END()
174 }
175 PlayerAddMinimalWeapons(p);
176 }
177 static bool HasWeapon(const CArray *weapons, const WeaponClass *wc)
178 {
179 CA_FOREACH(const WeaponClass *, wc2, *weapons)
180 if (wc == *wc2)
181 {
182 return true;
183 }
184 CA_FOREACH_END()
185 return false;
186 }
187 static void PlayerEquipTerminate(GameLoopData *data)
188 {
189 PlayerEquipData *pData = data->Data;
190
191 for (int i = 0; i < GetNumPlayers(PLAYER_ANY, false, true); i++)
192 {
193 EquipMenuTerminate(&pData->menus[i]);
194 }
195 CFREE(pData);
196 }
197 static void PlayerEquipOnExit(GameLoopData *data)
198 {
199 PlayerEquipData *pData = data->Data;
200
201 if (pData->waitResult == EVENT_WAIT_OK)
202 {
•203 for (int i = 0; i < (int)gPlayerDatas.size; i++)
204 {
205 const PlayerData *p = CArrayGet(&gPlayerDatas, i);
206 if (!p->IsLocal)
207 {
208 continue;
209 }
210 NPlayerData pd = NMakePlayerData(p);
211 // Update player definitions
212 if (gCampaign.IsClient)
213 {
214 NetClientSendMsg(&gNetClient, GAME_EVENT_PLAYER_DATA, &pd);
215 }
216 else
217 {
218 NetServerSendMsg(
219 &gNetServer, NET_SERVER_BCAST, GAME_EVENT_PLAYER_DATA,
220 &pd);
221 }
222 }
223 }
224 else
225 {
226 CampaignUnload(&gCampaign);
227 }
228 }
229 static GameLoopResult PlayerEquipUpdate(GameLoopData *data, LoopRunner *l)
230 {
231 PlayerEquipData *pData = data->Data;
232
233 // If no human players, don't show equip screen
234 if (GetNumPlayers(PLAYER_ANY, false, true) == 0 ||
235 gCampaign.Setting.SkipWeaponMenu)
236 {
237 pData->waitResult = EVENT_WAIT_OK;
238 goto bail;
239 }
240
241 // Check if anyone pressed escape
242 int cmds[MAX_LOCAL_PLAYERS];
243 memset(cmds, 0, sizeof cmds);
244 GetPlayerCmds(&gEventHandlers, &cmds);
245 if (EventIsEscape(
246 &gEventHandlers, cmds, GetMenuCmd(&gEventHandlers, false)))
247 {
248 pData->waitResult = EVENT_WAIT_CANCEL;
249 goto bail;
250 }
251
252 // Update menus
253 bool isDone = true;
254 for (int i = 0; i < GetNumPlayers(PLAYER_ANY, false, true); i++)
255 {
256 EquipMenuUpdate(&pData->menus[i], cmds[i]);
257 isDone = isDone && EquipMenuIsDone(&pData->menus[i]);
258 }
259 if (isDone)
260 {
261 pData->waitResult = EVENT_WAIT_OK;
262 goto bail;
263 }
264
265 return UPDATE_RESULT_DRAW;
266
267 bail:
268 if (pData->waitResult == EVENT_WAIT_OK)
269 {
270 LoopRunnerChange(l, ScreenWaitForGameStart());
271 }
272 else
273 {
274 LoopRunnerPop(l);
275 }
276 return UPDATE_RESULT_OK;
277 }
278 static void PlayerEquipDraw(GameLoopData *data)
279 {
280 const PlayerEquipData *pData = data->Data;
281 BlitClearBuf(&gGraphicsDevice);
282 for (int i = 0; i < GetNumPlayers(PLAYER_ANY, false, true); i++)
283 {
284 EquipMenuDraw(&pData->menus[i]);
285 }
286 BlitUpdateFromBuf(&gGraphicsDevice, gGraphicsDevice.screen);
287 }