Debian Codegraph @

Descent3/ship.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/ship.cpp $
21 * $Revision: 19 $
22 * $Date: 5/13/99 3:42p $
23 * $Author: Ardussi $
24 *
25 * $Log: /DescentIII/Main/ship.cpp $
26 *
27 * 19 5/13/99 3:42p Ardussi
28 * changes for compiling on the Mac
29 *
30 * 18 5/12/99 1:57p Jason
31 * fixed yet more buggy/ugly code
32 *
33 * 17 4/11/99 4:46p Matt
34 * Small fixes
35 *
36 * 16 4/11/99 3:04p Matt
37 * Reduced the max number of player ships from 30 to 5.
38 * Re-wrote the static ship name code to be more space efficient.
39 * Took default ship name (the one used to find the ship) out of string
40 * table.
41 *
42 * 15 2/17/99 10:32a Matt
43 * Init physics hit_die_dot for new items
44 *
45 * 14 10/08/98 10:28p Jason
46 * more demo changes for ship
47 *
48 * 13 10/02/98 1:47p Jason
49 * added lod player ships
50 *
51 * 12 8/07/98 2:43p Jeff
52 * flags gets cleared and initialized correctly
53 *
54 * 11 7/31/98 5:23p Jason
55 * added ship armor scalars
56 *
57 * 10 6/15/98 4:00p Jason
58 * replaced monochromatic polymodel lighting with rgb lighting
59 *
60 * 9 4/30/98 11:32a Chris
61 * ClearWB to WBClear
62 *
63 * 8 4/02/98 3:54p Jason
64 * first pass in getting polymodel paging to work
65 *
66 * 7 2/17/98 8:12p Matt
67 * Added looping & release sounds for player weapons
68 *
69 * 6 11/04/97 6:18p Chris
70 * Added support so that the player ship uses the robot's firing dialog.
71 *
72 * 5 10/21/97 11:57a Jason
73 * added ability to set dying model for player ship
74 *
75 * 4 10/20/97 4:46p Jason
76 * changes for explosions
77 *
78 * 3 9/09/97 7:02p Jason
79 * don't reload the models during the remap phase
80 *
81 * 2 8/06/97 12:40p Jason
82 * fixed some potentially serious memory problems
83 *
84 * 6 4/07/97 3:17p Chris
85 * Incremental improvement
86 *
87 * 5 4/07/97 3:13p Chris
88 *
89 * 4 4/02/97 5:21p Jason
90 * added the ability for pages to free the models that they point to
91 *
92 * 3 4/02/97 4:04p Jason
93 * made ships remap properly
94 *
95 * 2 3/31/97 4:34p Jason
96 * added player ship page
97 *
98 * 1 3/31/97 12:21p Jason
99 *
100 * $NoKeywords: $
101 */
102
103 #include "ship.h"
104 #include "pstypes.h"
105 #include "pserror.h"
106 #include "object.h"
107 #include "3d.h"
108 #include <stdlib.h>
109 #include <stdio.h>
110 #include <stdarg.h>
111 #include <string.h>
112 #include "polymodel.h"
113 #include "player.h"
114 #include "robotfire.h"
115
116 #define DEFAULT_SHIP_SIZE 4.0
117
118 ship Ships[MAX_SHIPS];
119 int Num_ships = 0;
120
121 // There are no static ships
122 char *Static_ship_names[1];
123 #define NUM_STATIC_SHIPS 0
124
125 #define SHIP_PYRO_ID 0
126 #define SHIP_PHOENIX_ID 1
127 #define SHIP_MAGNUM_ID 2
128
129 const char *AllowedShips[MAX_SHIPS] = {"Pyro-GL", "Phoenix", "Magnum-AHT"};
130
131 // Sets all ships to unused
132 void InitShips() {
133 for (int i = 0; i < MAX_SHIPS; i++) {
134 memset(&Ships[i], 0, sizeof(ship));
135 }
136 Num_ships = 0;
137 }
138
139 // Allocs a ship for use, returns -1 if error, else index on success
140 int AllocShip() {
141 for (int i = 0; i < MAX_SHIPS; i++) {
142 if (Ships[i].used == 0) {
143 memset(&Ships[i], 0, sizeof(ship));
144 Ships[i].used = 1;
145 Ships[i].size = DEFAULT_SHIP_SIZE;
146 Ships[i].dying_model_handle = -1;
147 Ships[i].med_render_handle = -1;
148 Ships[i].lo_render_handle = -1;
149 Ships[i].med_lod_distance = DEFAULT_MED_LOD_DISTANCE;
150 Ships[i].lo_lod_distance = DEFAULT_LO_LOD_DISTANCE;
151 Ships[i].model_handle = -1;
152 Ships[i].armor_scalar = 1.0;
153 Ships[i].flags = 0;
154
155 Ships[i].phys_info.hit_die_dot = -1; //-1 mean doesn't apply
156
157 // Make sure the weapon battery info is cleared for a new object
158 WBClearInfo(Ships[i].static_wb);
159
160 for (int w = 0; w < MAX_PLAYER_WEAPONS; w++) {
161 Ships[i].firing_sound[w] = -1;
162 // Ships[i].release_sound[w] = -1;
163 }
164
165 Num_ships++;
166 return i;
167 }
168 }
169
170 Int3(); // No ships free!
171 return -1;
172 }
173
174 // Frees ship index n
175 void FreeShip(int n) {
176 ASSERT(Ships[n].used > 0);
177
178 Ships[n].used = 0;
179 Ships[n].name[0] = 0;
180 Num_ships--;
181 }
182
183 // Gets next ship from n that has actually been alloced
184 int GetNextShip(int n) {
185 int i;
186
187 if (Num_ships == 0)
188 return -1;
189
190 if ((n < 0) || (n >= MAX_SHIPS))
191 n = -1;
192
193 for (i = n + 1; i < MAX_SHIPS; i++)
194 if (Ships[i].used)
195 return i;
196 for (i = 0; i < n; i++)
197 if (Ships[i].used)
198 return i;
199
200 // this is the only one
201
202 return n;
203 }
204
205 // Gets previous ship from n that has actually been alloced
206 int GetPrevShip(int n) {
207 int i;
208
209 if (Num_ships == 0)
210 return -1;
211
212 if ((n < 0) || (n >= MAX_SHIPS))
213 n = MAX_SHIPS;
214
215 for (i = n - 1; i >= 0; i--) {
216 if (Ships[i].used)
217 return i;
218 }
219 for (i = MAX_SHIPS - 1; i > n; i--) {
220 if (Ships[i].used)
221 return i;
222 }
223
224 // this is the only one
225 return n;
226 }
227 // Searches thru all ships for a specific name, returns -1 if not found
228 // or index of ship with name
229 int FindShipName(const char *name) {
230 int i;
231
232 ASSERT(name != NULL);
233
234 for (i = 0; i < MAX_SHIPS; i++)
235 if (Ships[i].used && !stricmp(name, Ships[i].name))
236 return i;
237
238 return -1;
239 }
240
241 int LoadShipImage(const char *filename) {
242 int img_handle;
243
244 img_handle = LoadPolyModel(filename, 1);
245
246 return img_handle;
247 }
248
249 // Given a ship handle, returns an index to that ships model
250 int GetShipImage(int handle) {
251 ASSERT(Ships[handle].used > 0);
252
253 return (Ships[handle].model_handle);
254 }
255
256 // This is a very confusing function. It takes all the ships that we have loaded
257 // and remaps then into their proper places (if they are static).
258 void RemapShips() {
259 int i;
260
261 // Loop through the static ships and move them to the correct slots
262 for (i = 0; i < NUM_STATIC_SHIPS; i++) {
263
264 int cur_index = FindShipName(Static_ship_names[i]);
265
266 if (cur_index == -1) {
267 Int3(); // couldn't find statically-mapped ship
268 continue;
269 }
270
271 if (cur_index != i) { // not in right slot
272
273 if (Ships[i].used) { // someone else in this slot, so swap
274 ship tship = Ships[i];
275 Ships[i] = Ships[cur_index];
276 Ships[cur_index] = tship;
277 RemapAllShipObjects(i, MAX_SHIPS);
278 RemapAllShipObjects(cur_index, i);
279 RemapAllShipObjects(MAX_SHIPS, cur_index);
280 } else { // slot is unused, so just take it
281 Ships[i] = Ships[cur_index];
282 Ships[cur_index].used = 0;
283 RemapAllShipObjects(cur_index, i);
284 }
285 }
286 }
287
288 // Now, if any ships are polygon models and those models don't have correct
289 // textures, attempt to reload the model texture list
290 for (i = 0; i < MAX_SHIPS; i++) {
291 if (Ships[i].used) {
292 ASSERT(Ships[i].model_handle != -1);
293 // LoadPolyModel (Poly_models[Ships[i].model_handle].name);
294 }
295 }
296 }
297
298 // goes thru every entity that could possible have a ship index
299 // and changes the old index to the new index
300 void RemapAllShipObjects(int old_index, int new_index) {
301 for (int i = 0; i < MAX_OBJECTS; i++) {
302 if (Objects[i].type == OBJ_PLAYER) {
303 if (Players[Objects[i].id].ship_index == old_index)
304 Players[Objects[i].id].ship_index = new_index;
305 }
306 }
307 }