| 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/gameevent.cpp $ |
| 21 | * $Revision: 12 $ |
| 22 | * $Date: 2/10/99 1:47p $ |
| 23 | * $Author: Matt $ |
| 24 | * |
| 25 | * Code for event system |
| 26 | * |
| 27 | * $Log: /DescentIII/main/gameevent.cpp $ |
| 28 | * |
| 29 | * 12 2/10/99 1:47p Matt |
| 30 | * Changed object handle symbolic constants |
| 31 | * |
| 32 | * 11 1/06/99 1:46p Jeff |
| 33 | * added object detonator code (auto cancel game event on object death) |
| 34 | * |
| 35 | * 10 10/14/98 5:14p Jason |
| 36 | * added better comments |
| 37 | * |
| 38 | * 9 10/13/98 12:03p Kevin |
| 39 | * Changed use of preprocessors for debug, etc. |
| 40 | * |
| 41 | * 8 5/26/98 2:49p Jason |
| 42 | * fixed FOV bug |
| 43 | * |
| 44 | * 7 3/31/98 3:49p Jason |
| 45 | * added memory lib |
| 46 | * |
| 47 | * 6 1/23/98 12:08p Jeff |
| 48 | * Added spew effect system |
| 49 | * |
| 50 | * 5 12/01/97 9:56a Chris |
| 51 | * Added some warnings for overflow of events |
| 52 | * |
| 53 | * 4 11/18/97 4:37p Jason |
| 54 | * implemented some more changes to event system |
| 55 | * |
| 56 | * 3 11/12/97 1:14p Jason |
| 57 | * redid the game event system to be much more flexible |
| 58 | * |
| 59 | * 2 9/22/97 6:18p Matt |
| 60 | * Removed include of stub.h |
| 61 | * |
| 62 | * $NoKeywords: $ |
| 63 | */ |
| 64 | |
| 65 | #include "object.h"cxx . . $ `<file>/Descent3/object.h`/ |
| 66 | #include "game.h"cxx . . $ `<file>/Descent3/game.h`/ |
| 67 | #include "gameevent.h"cxx . . $ `<file>/Descent3/gameevent.h`/ |
| 68 | #include "descent.h"cxx . . $ `<file>/Descent3/descent.h`/ |
| 69 | #include "gameloop.h"cxx . . $ `<file>/Descent3/gameloop.h`/ |
| 70 | #include "spew.h"cxx . . $ `<file>/Descent3/spew.h`/ |
| 71 | #include <string.h> |
| 72 | // #include <malloc.h> |
| 73 | #include "mem.h"cxx . . $ `<file>/mem/mem.h`/ |
| 74 | #include "player.h"cxx . . $ `<file>/Descent3/player.h`/ |
| 75 | |
| 76 | int Num_eventscxx . . $ Num_events. = 0; |
| 77 | game_eventcxx . . $ game_event# GameEventcxx . . $ GameEvent.[MAX_EVENTScxx . . $ `Descent3/gameevent.h:25:9`!]; |
| 78 | |
| 79 | // Inits the event system |
| 80 | void InitEventscxx . . $ InitEvents(49f6e7a06ebc5aa8).() { |
| 81 | int i; |
| 82 | for (i = 0; i < MAX_EVENTScxx . . $ `Descent3/gameevent.h:25:9`!; i++) { |
| 83 | GameEventcxx . . $ GameEvent.[i].usedcxx . . $ game_event#used. = 0; |
| 84 | GameEventcxx . . $ GameEvent.[i].datacxx . . $ game_event#data. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Clears the event list |
| 89 | void ClearAllEventscxx . . $ ClearAllEvents(49f6e7a06ebc5aa8).() { |
| 90 | int i; |
| 91 | for (i = 0; i < MAX_EVENTScxx . . $ `Descent3/gameevent.h:25:9`!; i++) { |
| 92 | if (GameEventcxx . . $ GameEvent.[i].usedcxx . . $ game_event#used.) |
| 93 | FreeEventcxx . . $ FreeEvent(d4f767463ce0a6b3).(i); |
| 94 | } |
| 95 | |
| 96 | SpewInitcxx . . $ SpewInit(49f6e7a06ebc5aa8).(); |
| 97 | Render_zoomcxx . . $ Render_zoom. = D3_DEFAULT_ZOOMcxx . . $ `Descent3/descent.h:178:9`!; |
| 98 | } |
| 99 | |
| 100 | // Returns an index of an allocated event |
| 101 | // or -1 if all events are spoken for |
| 102 | int AllocEventcxx . . $ AllocEvent(b126dc7c1de90089).() { |
| 103 | int i; |
| 104 | static int first_time = 1; |
| 105 | |
| 106 | if (first_time) { |
| 107 | InitEventscxx . . $ InitEvents(49f6e7a06ebc5aa8).(); |
| 108 | first_time = 0; |
| 109 | } |
| 110 | |
| 111 | for (i = 0; i < MAX_EVENTScxx . . $ `Descent3/gameevent.h:25:9`!; i++) { |
| 112 | if (GameEventcxx . . $ GameEvent.[i].usedcxx . . $ game_event#used. == 0) { |
| 113 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(GameEvent[i].data == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); |
| 114 | memset(&GameEventcxx . . $ GameEvent.[i], 0, sizeof(game_eventcxx . . $ game_event#)); |
| 115 | GameEventcxx . . $ GameEvent.[i].usedcxx . . $ game_event#used. = 1; |
| 116 | GameEventcxx . . $ GameEvent.[i].objhandle_detonatorcxx . . $ game_event#objhandle_detonator. = OBJECT_HANDLE_NONEcxx . . $ `Descent3/object_external.h:156:9`!; |
| 117 | Num_eventscxx . . $ Num_events.++; |
| 118 | return i; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return -1; // no events free! |
| 123 | } |
| 124 | |
| 125 | // Frees an event for use by others |
| 126 | void FreeEventcxx . . $ FreeEvent(d4f767463ce0a6b3).(int index) { |
| 127 | if (GameEventcxx . . $ GameEvent.[index].usedcxx . . $ game_event#used. != 1) { |
| 128 | Int3cxx . . $ `misc/pserror.h:247:9`!(); // Get Jason, trying to free an event that is not used |
| 129 | return; |
| 130 | } |
| 131 | GameEventcxx . . $ GameEvent.[index].usedcxx . . $ game_event#used. = 0; |
| 132 | if (GameEventcxx . . $ GameEvent.[index].datacxx . . $ game_event#data. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) { |
| 133 | mem_freecxx . . $ `mem/mem.h:80:9`!(GameEvent[index].data); |
| 134 | GameEventcxx . . $ GameEvent.[index].datacxx . . $ game_event#data. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 135 | } |
| 136 | |
| 137 | Num_eventscxx . . $ Num_events.--; |
| 138 | } |
| 139 | |
| 140 | // Processes all pending events, removing the ones that are expired |
| 141 | void ProcessNormalEventscxx . . $ ProcessNormalEvents(49f6e7a06ebc5aa8).() { |
| 142 | int i, count = 0; |
| 143 | bool skip_event; |
| 144 | |
| 145 | for (i = 0; i < MAX_EVENTScxx . . $ `Descent3/gameevent.h:25:9`! && count < Num_eventscxx . . $ Num_events.; i++) { |
| 146 | if (GameEventcxx . . $ GameEvent.[i].usedcxx . . $ game_event#used.) { |
| 147 | count++; |
| 148 | skip_event = false; |
| 149 | |
| 150 | if (GameEventcxx . . $ GameEvent.[i].objhandle_detonatorcxx . . $ game_event#objhandle_detonator. != OBJECT_HANDLE_NONEcxx . . $ `Descent3/object_external.h:156:9`!) { |
| 151 | // this object has a detonator, see if the object is still alive |
| 152 | objectcxx . . $ object# *obj = ObjGetcxx . . $ ObjGet(11672e354c0b263a).(GameEventcxx . . $ GameEvent.[i].objhandle_detonatorcxx . . $ game_event#objhandle_detonator.); |
| 153 | if (!obj || (obj->typecxx . . $ object#type. == OBJ_GHOSTcxx . . $ `Descent3/object_external.h:172:9`!) || |
| 154 | (obj->typecxx . . $ object#type. == OBJ_PLAYERcxx . . $ `Descent3/object_external.h:164:9`! && Playerscxx . . $ Players.[obj->idcxx . . $ object#id.].flagscxx . . $ player#flags. & (PLAYER_FLAGS_DYINGcxx . . $ `Descent3/player_external.h:80:9`! | PLAYER_FLAGS_DEADcxx . . $ `Descent3/player_external.h:81:9`!))) { |
| 155 | // the object has died, kill the event |
| 156 | FreeEventcxx . . $ FreeEvent(d4f767463ce0a6b3).(i); |
| 157 | skip_event = true; |
| 158 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Game Event %d cancelled\n", i); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | if (GameEventcxx . . $ GameEvent.[i].typecxx . . $ game_event#type. != RENDER_EVENTcxx . . $ `Descent3/gameevent.h:29:9`! && GameEventcxx . . $ GameEvent.[i].end_timecxx . . $ game_event#end_time. <= Gametimecxx . . $ Gametime. && !skip_event) { |
| 163 | HandleEventcxx . . $ HandleEvent(48320f9021c27e4d).(&GameEventcxx . . $ GameEvent.[i]); |
| 164 | FreeEventcxx . . $ FreeEvent(d4f767463ce0a6b3).(i); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | SpewEmitAllcxx . . $ SpewEmitAll(49f6e7a06ebc5aa8).(); |
| 170 | } |
| 171 | |
| 172 | // Goes through all events and returns the first index of the id matching the argument passed. |
| 173 | // If not match found, returns -1 |
| 174 | int FindEventIDcxx . . $ FindEventID(7864480464b09eea).(int id) { |
| 175 | int i; |
| 176 | |
| 177 | for (i = 0; i < MAX_EVENTScxx . . $ `Descent3/gameevent.h:25:9`!; i++) { |
| 178 | if (GameEventcxx . . $ GameEvent.[i].usedcxx . . $ game_event#used. && GameEventcxx . . $ GameEvent.[i].idcxx . . $ game_event#id. == id) |
| 179 | return i; |
| 180 | } |
| 181 | |
| 182 | return -1; |
| 183 | } |
| 184 | |
| 185 | // Processes all pending events, removing the ones that are expired |
| 186 | void ProcessRenderEventscxx . . $ ProcessRenderEvents(49f6e7a06ebc5aa8).() { |
| 187 | int i, count = 0; |
| 188 | |
| 189 | for (i = 0; i < MAX_EVENTScxx . . $ `Descent3/gameevent.h:25:9`! && count < Num_eventscxx . . $ Num_events.; i++) { |
| 190 | if (GameEventcxx . . $ GameEvent.[i].usedcxx . . $ game_event#used.) { |
| 191 | count++; |
| 192 | if (GameEventcxx . . $ GameEvent.[i].typecxx . . $ game_event#type. == RENDER_EVENTcxx . . $ `Descent3/gameevent.h:29:9`! && GameEventcxx . . $ GameEvent.[i].end_timecxx . . $ game_event#end_time. <= Gametimecxx . . $ Gametime.) { |
| 193 | HandleEventcxx . . $ HandleEvent(48320f9021c27e4d).(&GameEventcxx . . $ GameEvent.[i]); |
| 194 | FreeEventcxx . . $ FreeEvent(d4f767463ce0a6b3).(i); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // Adds new event to the event list. The event will trigger at Gametime+length. |
| 201 | // data and size represent event specific data - if there is no event specific data |
| 202 | // then set data to NULL and size to 0 |
| 203 | // Subfunction is the function that is called when the event is up |
| 204 | int CreateNewEventcxx . . $ CreateNewEvent(85af21da76cb0316).(int type, int id, float length, void *data, int size, void (*subfunction)(int eventnum, void *data), |
| 205 | int objhandle_detonate) { |
| 206 | int num; |
| 207 | game_eventcxx . . $ game_event# *ge; |
| 208 | |
| 209 | num = AllocEventcxx . . $ AllocEvent(b126dc7c1de90089).(); |
| 210 | if (num == -1) { |
| 211 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "WARNING: Event (type %d, id %d) did not get allocated\n", type, id); |
| 212 | return -1; // couldn't do this event, no slots free |
| 213 | } |
| 214 | |
| 215 | ge = &GameEventcxx . . $ GameEvent.[num]; |
| 216 | |
| 217 | ge->typecxx . . $ game_event#type. = type; |
| 218 | ge->idcxx . . $ game_event#id. = id; |
| 219 | ge->start_timecxx . . $ game_event#start_time. = Gametimecxx . . $ Gametime.; |
| 220 | ge->end_timecxx . . $ game_event#end_time. = Gametimecxx . . $ Gametime. + length; |
| 221 | ge->frame_borncxx . . $ game_event#frame_born. = FrameCountcxx . . $ FrameCount.; |
| 222 | ge->objhandle_detonatorcxx . . $ game_event#objhandle_detonator. = objhandle_detonate; |
| 223 | |
| 224 | if (size) { |
| 225 | ge->datacxx . . $ game_event#data. = (void *)mem_malloccxx . . $ `mem/mem.h:79:9`!(size); |
| 226 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(ge->data); |
| 227 | memcpy(ge->datacxx . . $ game_event#data., data, size); |
| 228 | } |
| 229 | |
| 230 | ge->subfunctioncxx . . $ game_event#subfunction. = subfunction; |
| 231 | |
| 232 | return num; |
| 233 | } |
| 234 | |
| 235 | // Does a specific action according to an event |
| 236 | void HandleEventcxx . . $ HandleEvent(48320f9021c27e4d).(game_eventcxx . . $ game_event# *ge) { |
| 237 | switch (ge->typecxx . . $ game_event#type.) { |
| 238 | case RENDER_EVENTcxx . . $ `Descent3/gameevent.h:29:9`!: |
| 239 | ge->subfunctioncxx . . $ game_event#subfunction.(ge - GameEventcxx . . $ GameEvent., ge->datacxx . . $ game_event#data.); |
| 240 | break; |
| 241 | default: |
| 242 | ge->subfunctioncxx . . $ game_event#subfunction.(ge - GameEventcxx . . $ GameEvent., ge->datacxx . . $ game_event#data.); |
| 243 | break; |
| 244 | } |
| 245 | } |