| 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 | |
| 19 | // generic.cpp |
| 20 | // 0.1 |
| 21 | #include <cstdio> |
| 22 | #include <cstdlib> |
| 23 | #include <cstring> |
| 24 | #include <cstdarg> |
| 25 | #include "osiris_import.h" |
| 26 | #include "osiris_common.h" |
| 27 | |
| 28 | #include "module.h" |
| 29 | |
| 30 | #ifdef __cplusplus |
| 31 | extern "C" { |
| 32 | #endif |
| 33 | DLLEXPORT char STDCALL InitializeDLL(tOSIRISModuleInit *func_list); |
| 34 | DLLEXPORT void STDCALL ShutdownDLL(void); |
| 35 | DLLEXPORT int STDCALL GetGOScriptID(const char *name, uint8_t isdoor); |
| 36 | DLLEXPORT void STDCALLPTR CreateInstance(int id); |
| 37 | DLLEXPORT void STDCALL DestroyInstance(int id, void *ptr); |
| 38 | DLLEXPORT int16_t STDCALL CallInstanceEvent(int id, void *ptr, int event, tOSIRISEventInfo *data); |
| 39 | DLLEXPORT int STDCALL SaveRestoreState(void *file_ptr, uint8_t saving_state); |
| 40 | #ifdef __cplusplus |
| 41 | } |
| 42 | #endif |
| 43 | |
| 44 | int String_table_size = 0; |
| 45 | char **String_table = NULL; |
| 46 | static const char *_Error_string = "!!ERROR MISSING STRING!!"; |
| 47 | static const char *_Empty_string = ""; |
| 48 | const char *GetStringFromTable(int index) { |
| 49 | if ((index < 0) || (index >= String_table_size)) |
| 50 | return _Error_string; |
| 51 | if (!String_table[index]) |
| 52 | return _Empty_string; |
| 53 | return String_table[index]; |
| 54 | } |
| 55 | #define TXT(x) GetStringFromTable(x) |
| 56 | #define TXT_DOORLOCKED TXT(0) //"Door Locked!" |
| 57 | #define TXT_RAPIDFIRE TXT(1) //"Rapid Fire!" |
| 58 | #define TXT_FORCEWALL TXT(2) //"Force Wall!" |
| 59 | #define TXT_FORCEWALLEJECT TXT(3) //"Force Wall Ejected!" |
| 60 | #define TXT_FORCEWALLDONE TXT(4) //"Force Wall Done!" |
| 61 | #define TXT_WINGNUT TXT(5) //"WingNut!" |
| 62 | #define TXT_RAPIDFIREOFF TXT(6) //"Rapid Fire Off!" |
| 63 | // ================ |
| 64 | // Helper Functions |
| 65 | // ================ |
| 66 | void AddHudMessage(int player_objhandle, int color, const char *format, ...); |
| 67 | |
| 68 | #define ID_RAPIDFIRE 0 |
| 69 | #define ID_FORCEWALL 1 |
| 70 | #define ID_WINGNUT 2 |
| 71 | #define NUM_IDS 3 |
| 72 | |
| 73 | // Common Powerups |
| 74 | // Weapon Powerups |
| 75 | // Ammo Powerups |
| 76 | #define GENERIC_POWERUP_SCRIPTID NUM_IDS |
| 77 | // Generic Door Script |
| 78 | #define GENERIC_DOOR_SCRIPTID NUM_IDS + 1 |
| 79 | |
| 80 | struct tScriptIDInfo { |
| 81 | int id; |
| 82 | const char *name; |
| 83 | }; |
| 84 | |
| 85 | tScriptIDInfo ScriptIDInfo[NUM_IDS] = { |
| 86 | {ID_RAPIDFIRE, "RapidFire"}, {ID_FORCEWALL, "Forcewall"}, {ID_WINGNUT, "GoWingNut"}}; |
| 87 | |
| 88 | class GenericScript { |
| 89 | public: |
| 90 | GenericScript() {} |
| 91 | virtual int16_t CallEvent(int event, tOSIRISEventInfo *data); |
| 92 | |
| 93 | protected: |
| 94 | }; |
| 95 | |
| 96 | class GenericDoor : public GenericScript { |
| 97 | public: |
| 98 | GenericDoor() {} |
| 99 | int16_t CallEvent(int event, tOSIRISEventInfo *data); |
| 100 | }; |
| 101 | |
| 102 | struct forcewallmemory { |
| 103 | int object_handle; |
| 104 | }; |
| 105 | |
| 106 | class ForceWallScript : public GenericScript { |
| 107 | public: |
| 108 | ForceWallScript(); |
| 109 | int16_t CallEvent(int event, tOSIRISEventInfo *data); |
| 110 | forcewallmemory *memory; |
| 111 | }; |
| 112 | |
| 113 | class WingNutScript : public GenericScript { |
| 114 | public: |
| 115 | WingNutScript() {} |
| 116 | int16_t CallEvent(int event, tOSIRISEventInfo *data); |
| 117 | }; |
| 118 | |
| 119 | struct tPlayerTimerInfo { |
| 120 | int timer_handle; |
| 121 | }; |
| 122 | tPlayerTimerInfo RapidFirePlayerTimers[MAX_PLAYERS]; |
| 123 | |
| 124 | struct rapidfirememory { |
| 125 | int collided_object; |
| 126 | uint8_t collided_pnum; |
| 127 | }; |
| 128 | |
| 129 | class RapidFireScript : public GenericScript { |
| 130 | public: |
| 131 | RapidFireScript(); |
| 132 | |
| 133 | int16_t CallEvent(int event, tOSIRISEventInfo *data); |
| 134 | rapidfirememory *memory; |
| 135 | }; |
| 136 | |
| 137 | // InitializeDLL |
| 138 | // Purpose: |
| 139 | // This function gets called when the DLL first gets loaded. It will only be called once (until the |
| 140 | // DLL is unloaded). Passed in is a struct of data passed from the game needed for the DLL to interact |
| 141 | // with D3. Usually this function will just call osicommon_Initialize(), which sets up the imported |
| 142 | // functions. However, you can alloc some memory or whatever in this function, and free it in ShutdownDLL(). |
| 143 | // Note: You cannot call any imported functions until osicommon_Initialize() is called. |
| 144 | // Returns 1 if initialization went ok, 0 if there was an error and the DLL should not be loaded. |
| 145 | char STDCALL InitializeDLL(tOSIRISModuleInit *func_list) { |
| 146 | osicommon_Initialize((tOSIRISModuleInit *)func_list); |
| 147 | if (func_list->game_checksum != CHECKSUM) { |
| 148 | mprintf(0, "Game-Checksum FAIL!!! (%ul!=%ul)\n", func_list->game_checksum, CHECKSUM); |
| 149 | mprintf(0, "RECOMPILE YOUR SCRIPTS!!!\n"); |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | String_table_size = func_list->string_count; |
| 154 | String_table = func_list->string_table; |
| 155 | |
| 156 | int i; |
| 157 | |
| 158 | // initialize rapid fire script data |
| 159 | for (i = 0; i < MAX_PLAYERS; i++) { |
| 160 | RapidFirePlayerTimers[i].timer_handle = -1; |
| 161 | } |
| 162 | |
| 163 | return 1; |
| 164 | } |
| 165 | |
| 166 | // ShutdownDLL |
| 167 | // Purpose: |
| 168 | // This function is called right before a DLL is about to be unloaded from memory. You can free |
| 169 | // any unfree'd memory, or anything else you need to do. Don't worry about destroying any instances |
| 170 | // of scripts, as they will all be automatically destroyed before this function is called. The |
| 171 | // same goes for any memory allocated with Scrpt_MemAlloc(), as this will automatically be freed |
| 172 | // when a scripts instance is destroyed. |
| 173 | void STDCALL ShutdownDLL(void) {} |
| 174 | |
| 175 | // GetGOScriptID |
| 176 | // Purpose: |
| 177 | // Given the name of the object (from it's pagename), this function will search through it's |
| 178 | // list of General Object Scripts for a script with a matching name (to see if there is a script |
| 179 | // for that type/id of object within this DLL). If a matching scriptname is found, a UNIQUE ID |
| 180 | // is to be returned back to Descent 3. This ID will be used from here on out for all future |
| 181 | // interaction with the DLL. Since doors are not part of the generic object's, it's possible |
| 182 | // for a door to have the same name as a generic object (OBJ_POWERUP, OBJ_BUILDING, OBJ_CLUTTER |
| 183 | // or OBJ_ROBOT), therefore, a 1 is passed in for isdoor if the given object name refers to a |
| 184 | // door, else it is a 0. The return value is the unique identifier, else -1 if the script |
| 185 | // does not exist in the DLL. |
| 186 | int STDCALL GetGOScriptID(const char *name, uint8_t isdoor) { |
| 187 | if (!isdoor) { |
| 188 | for (int i = 0; i < NUM_IDS; i++) { |
| 189 | if (!stricmp(ScriptIDInfo[i].name, name)) |
| 190 | return ScriptIDInfo[i].id; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | return (isdoor) ? GENERIC_DOOR_SCRIPTID : GENERIC_POWERUP_SCRIPTID; |
| 195 | } |
| 196 | |
| 197 | // CreateInstance |
| 198 | // Purpose: |
| 199 | // Given an ID from a call to GetGOScriptID(), this function will create a new instance for that |
| 200 | // particular script (by allocating and initializing memory, etc.). A pointer to this instance |
| 201 | // is to be returned back to Descent 3. This pointer will be passed around, along with the ID |
| 202 | // for CallInstanceEvent() and DestroyInstance(). Return NULL if there was an error. |
| 203 | void STDCALLPTR CreateInstance(int id) { |
| 204 | switch (id) { |
| 205 | case GENERIC_POWERUP_SCRIPTID: |
| 206 | return new GenericScript; |
| 207 | break; |
| 208 | case GENERIC_DOOR_SCRIPTID: |
| 209 | return new GenericDoor; |
| 210 | break; |
| 211 | case ID_RAPIDFIRE: |
| 212 | return new RapidFireScript; |
| 213 | break; |
| 214 | case ID_WINGNUT: |
| 215 | return new WingNutScript; |
| 216 | break; |
| 217 | case ID_FORCEWALL: |
| 218 | return new ForceWallScript; |
| 219 | break; |
| 220 | }; |
| 221 | |
| 222 | return NULL; |
| 223 | } |
| 224 | |
| 225 | // DestroyInstance |
| 226 | // Purpose: |
| 227 | // Given an ID, and a pointer to a particular instance of a script, this function will delete and |
| 228 | // destruct all information associated with that script, so it will no longer exist. |
| 229 | void STDCALL DestroyInstance(int id, void *ptr) { |
| 230 | switch (id) { |
| 231 | case GENERIC_POWERUP_SCRIPTID: |
| 232 | delete ((GenericScript *)ptr); |
| 233 | break; |
| 234 | case GENERIC_DOOR_SCRIPTID: |
| 235 | delete ((GenericDoor *)ptr); |
| 236 | break; |
| 237 | case ID_RAPIDFIRE: |
| 238 | delete ((RapidFireScript *)ptr); |
| 239 | break; |
| 240 | case ID_WINGNUT: |
| 241 | delete ((WingNutScript *)ptr); |
| 242 | break; |
| 243 | case ID_FORCEWALL: |
| 244 | delete ((ForceWallScript *)ptr); |
| 245 | break; |
| 246 | }; |
| 247 | } |
| 248 | |
| 249 | // CallInstanceEvent |
| 250 | // Purpose: |
| 251 | // Given an ID, a pointer to a script instance, an event and a pointer to the struct of |
| 252 | // information about the event, this function will translate who this event belongs to and |
| 253 | // passes the event to that instance of the script to be handled. Return a combination of |
| 254 | // CONTINUE_CHAIN and CONTINUE_DEFAULT, to give instructions on what to do based on the |
| 255 | // event. CONTINUE_CHAIN means to continue through the chain of scripts (custom script, level |
| 256 | // script, mission script, and finally default script). If CONTINUE_CHAIN is not specified, |
| 257 | // than the chain is broken and those scripts of lower priority will never get the event. Return |
| 258 | // CONTINUE_DEFAULT in order to tell D3 if you want process the normal action that is built into |
| 259 | // the game for that event. This only pertains to certain events. If the chain continues |
| 260 | // after this script, than the CONTINUE_DEFAULT setting will be overridden by lower priority |
| 261 | // scripts return value. |
| 262 | int16_t STDCALL CallInstanceEvent(int id, void *ptr, int event, tOSIRISEventInfo *data) { |
| 263 | switch (id) { |
| 264 | case GENERIC_POWERUP_SCRIPTID: |
| 265 | case GENERIC_DOOR_SCRIPTID: |
| 266 | case ID_RAPIDFIRE: |
| 267 | case ID_FORCEWALL: |
| 268 | case ID_WINGNUT: |
| 269 | return ((GenericScript *)ptr)->CallEvent(event, data); |
| 270 | break; |
| 271 | }; |
| 272 | |
| 273 | return CONTINUE_CHAIN | CONTINUE_DEFAULT; |
| 274 | } |
| 275 | |
| 276 | // SaveRestoreState |
| 277 | // Purpose: |
| 278 | // This function is called when Descent 3 is saving or restoring the game state. In this function |
| 279 | // you should save/restore any global data that you want preserved through load/save (which includes |
| 280 | // demos). You must be very careful with this function, corrupting the file (reading or writing too |
| 281 | // much or too little) may be hazardous to the game (possibly making it impossible to restore the |
| 282 | // state). It would be best to use version information to keep older versions of saved states still |
| 283 | // able to be used. IT IS VERY IMPORTANT WHEN SAVING THE STATE TO RETURN THE NUMBER OF _BYTES_ WROTE |
| 284 | // TO THE FILE. When restoring the data, the return value is ignored. saving_state is 1 when you should |
| 285 | // write data to the file_ptr, 0 when you should read in the data. |
| 286 | #define VERSION 0x01 |
| 287 | int STDCALL SaveRestoreState(void *file_ptr, uint8_t saving_state) { |
| 288 | int size = 0; |
| 289 | |
| 290 | int i; |
| 291 | int version = VERSION; |
| 292 | |
| 293 | if (saving_state) { |
| 294 | File_WriteByte(VERSION, file_ptr); |
| 295 | size++; |
| 296 | |
| 297 | // save out rapid fire data |
| 298 | for (i = 0; i < MAX_PLAYERS; i++) { |
| 299 | File_WriteInt(RapidFirePlayerTimers[i].timer_handle, file_ptr); |
| 300 | size += sizeof(int); |
| 301 | } |
| 302 | } else { |
| 303 | version = File_ReadByte(file_ptr); |
| 304 | size++; |
| 305 | |
| 306 | // restore rapid fire data |
| 307 | for (i = 0; i < MAX_PLAYERS; i++) { |
| 308 | RapidFirePlayerTimers[i].timer_handle = File_ReadInt(file_ptr); |
| 309 | size += sizeof(int); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | return size; |
| 314 | } |
| 315 | |
| 316 | int16_t GenericScript::CallEvent(int event, tOSIRISEventInfo *data) { |
| 317 | switch (event) { |
| 318 | case EVT_COLLIDE: { |
| 319 | msafe_struct ms; |
| 320 | ms.objhandle = data->evt_collide.it_handle; |
| 321 | MSafe_GetValue(MSAFE_OBJECT_TYPE, &ms); |
| 322 | if (ms.type == OBJ_PLAYER) { |
| 323 | ms.objhandle = data->me_handle; |
| 324 | ms.ithandle = data->evt_collide.it_handle; |
| 325 | ms.playsound = 1; |
| 326 | MSafe_DoPowerup(&ms); |
| 327 | return CONTINUE_CHAIN | CONTINUE_DEFAULT; |
| 328 | } |
| 329 | } break; |
| 330 | } |
| 331 | return CONTINUE_CHAIN | CONTINUE_DEFAULT; |
| 332 | } |
| 333 | |
| 334 | int16_t GenericDoor::CallEvent(int event, tOSIRISEventInfo *data) { |
| 335 | switch (event) { |
| 336 | case EVT_COLLIDE: { |
| 337 | msafe_struct ms; |
| 338 | ms.objhandle = data->me_handle; |
| 339 | MSafe_GetValue(MSAFE_OBJECT_TYPE, &ms); |
| 340 | if (ms.type == OBJ_DOOR) { |
| 341 | msafe_struct mit; |
| 342 | |
| 343 | mit.objhandle = data->evt_collide.it_handle; |
| 344 | MSafe_GetValue(MSAFE_OBJECT_TYPE, &mit); |
| 345 | |
| 346 | if (mit.type == OBJ_CLUTTER) { |
| 347 | break; |
| 348 | } |
| 349 | |
| 350 | // See if door is openable |
| 351 | ms.ithandle = data->evt_collide.it_handle; |
| 352 | MSafe_GetValue(MSAFE_DOOR_OPENABLE, &ms); |
| 353 | |
| 354 | if (ms.state) { |
| 355 | // openable door |
| 356 | MSafe_CallFunction(MSAFE_DOOR_ACTIVATE, &ms); |
| 357 | } else { |
| 358 | // non-openable door. If locked, give a message |
| 359 | MSafe_GetValue(MSAFE_DOOR_LOCK_STATE, &ms); |
| 360 | if (ms.state) { // locked! |
| 361 | ms.objhandle = data->evt_collide.it_handle; |
| 362 | |
| 363 | MSafe_GetValue(MSAFE_OBJECT_PARENT, &ms); |
| 364 | MSafe_GetValue(MSAFE_OBJECT_TYPE, &ms); |
| 365 | |
| 366 | if (ms.type == OBJ_PLAYER) { |
| 367 | strcpy(ms.message, TXT_DOORLOCKED); |
| 368 | ms.color = GR_RGB(0, 255, 0); |
| 369 | MSafe_CallFunction(MSAFE_MISC_FILTERED_HUD_MESSAGE, &ms); |
| 370 | |
| 371 | ms.state = 1; // specific player |
| 372 | ms.index = Scrpt_FindSoundName("DoorIsLocked"); |
| 373 | ms.volume = 0.80f; |
| 374 | if (ms.index != -1) |
| 375 | MSafe_CallFunction(MSAFE_SOUND_2D, &ms); |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | } break; |
| 381 | } |
| 382 | return CONTINUE_CHAIN | CONTINUE_DEFAULT; |
| 383 | } |
| 384 | |
| 385 | RapidFireScript::RapidFireScript() { memory = NULL; } |
| 386 | |
| 387 | #define RAPIDFIRE_RECHARGE 0.7f |
| 388 | int16_t RapidFireScript::CallEvent(int event, tOSIRISEventInfo *data) { |
| 389 | switch (event) { |
| 390 | case EVT_CREATED: { |
| 391 | tOSIRISMEMCHUNK ch; |
| 392 | ch.id = 4; |
| 393 | ch.size = sizeof(rapidfirememory); |
| 394 | ch.my_id.type = OBJECT_SCRIPT; |
| 395 | ch.my_id.objhandle = data->me_handle; |
| 396 | |
| 397 | memory = (rapidfirememory *)Scrpt_MemAlloc(&ch); |
| 398 | memory->collided_pnum = 255; |
| 399 | memory->collided_object = OBJECT_HANDLE_NONE; |
| 400 | |
| 401 | } break; |
| 402 | |
| 403 | case EVT_MEMRESTORE: { |
| 404 | memory = (rapidfirememory *)data->evt_memrestore.memory_ptr; |
| 405 | } break; |
| 406 | |
| 407 | case EVT_COLLIDE: { |
| 408 | if (!memory) |
| 409 | break; |
| 410 | |
| 411 | msafe_struct ms; |
| 412 | ms.objhandle = data->evt_collide.it_handle; |
| 413 | MSafe_GetValue(MSAFE_OBJECT_TYPE, &ms); |
| 414 | |
| 415 | if (ms.type == OBJ_PLAYER) { |
| 416 | |
| 417 | AddHudMessage(data->evt_collide.it_handle, GR_RGB(0, 255, 0), TXT_RAPIDFIRE); |
| 418 | int sound_id = Sound_FindId("RapidFireOn"); |
| 419 | Sound_Play3d(data->me_handle, sound_id); |
| 420 | |
| 421 | MSafe_GetValue(MSAFE_OBJECT_ID, &ms); |
| 422 | memory->collided_pnum = ms.id; |
| 423 | memory->collided_object = data->evt_collide.it_handle; |
| 424 | |
| 425 | if (RapidFirePlayerTimers[memory->collided_pnum].timer_handle != -1) { |
| 426 | |
| 427 | // check to see if the player currently has rapid fire |
| 428 | MSafe_GetValue(MSAFE_OBJECT_RECHARGE_SCALAR, &ms); |
| 429 | if (ms.scalar == RAPIDFIRE_RECHARGE) { |
| 430 | // the player currently has rapid fire |
| 431 | // the player possibly has rapid fire set, check to make sure |
| 432 | if (Scrpt_TimerExists(RapidFirePlayerTimers[memory->collided_pnum].timer_handle)) { |
| 433 | // cancel the old timer so we can create a new timer (give them more time) |
| 434 | Scrpt_CancelTimer(RapidFirePlayerTimers[memory->collided_pnum].timer_handle); |
| 435 | } |
| 436 | |
| 437 | RapidFirePlayerTimers[memory->collided_pnum].timer_handle = -1; |
| 438 | } else { |
| 439 | // rapid fire was stolen from the player |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | memory->collided_pnum = ms.id; |
| 444 | tOSIRISTIMER ti; |
| 445 | ti.flags = OTF_CANCELONDEAD; |
| 446 | ti.id = 31; |
| 447 | ti.object_handle = data->me_handle; |
| 448 | ti.object_handle_detonator = data->evt_collide.it_handle; |
| 449 | ti.timer_interval = 30; |
| 450 | RapidFirePlayerTimers[memory->collided_pnum].timer_handle = Scrpt_CreateTimer(&ti); |
| 451 | |
| 452 | // ghost the object |
| 453 | ms.objhandle = data->me_handle; |
| 454 | MSafe_CallFunction(MSAFE_OBJECT_GHOST, &ms); |
| 455 | |
| 456 | // set player scalar |
| 457 | ms.objhandle = data->evt_collide.it_handle; |
| 458 | ms.scalar = RAPIDFIRE_RECHARGE; |
| 459 | MSafe_CallFunction(MSAFE_OBJECT_RECHARGE_SCALAR, &ms); |
| 460 | } |
| 461 | |
| 462 | } break; |
| 463 | case EVT_TIMER: { |
| 464 | if (!memory) |
| 465 | break; |
| 466 | |
| 467 | msafe_struct ms; |
| 468 | ms.objhandle = memory->collided_object; |
| 469 | MSafe_GetValue(MSAFE_OBJECT_RECHARGE_SCALAR, &ms); |
| 470 | |
| 471 | if (ms.scalar == RAPIDFIRE_RECHARGE) { |
| 472 | // reset recharge scalar |
| 473 | ms.objhandle = memory->collided_object; |
| 474 | ms.scalar = 1.0f; |
| 475 | MSafe_CallFunction(MSAFE_OBJECT_RECHARGE_SCALAR, &ms); |
| 476 | RapidFirePlayerTimers[memory->collided_pnum].timer_handle = -1; |
| 477 | } |
| 478 | |
| 479 | AddHudMessage(memory->collided_object, GR_RGB(0, 255, 0), TXT_RAPIDFIREOFF); |
| 480 | int sound_id = Sound_FindId("RapidFireOff"); |
| 481 | Sound_Play3d(memory->collided_object, sound_id); |
| 482 | |
| 483 | // destroy the powerup |
| 484 | ms.objhandle = data->me_handle; |
| 485 | ms.playsound = 0; |
| 486 | MSafe_CallFunction(MSAFE_OBJECT_REMOVE, &ms); |
| 487 | |
| 488 | } break; |
| 489 | case EVT_TIMERCANCEL: { |
| 490 | msafe_struct ms; |
| 491 | |
| 492 | // destroy the powerup |
| 493 | ms.playsound = 0; |
| 494 | ms.objhandle = data->me_handle; |
| 495 | MSafe_CallFunction(MSAFE_OBJECT_REMOVE, &ms); |
| 496 | } break; |
| 497 | case EVT_INTERVAL: { |
| 498 | // check (if we are ghosted...meaning picked up) if the player |
| 499 | // that picked us up still has rapid fire |
| 500 | if (memory && memory->collided_pnum != 255) { |
| 501 | // check the player to make sure he is still charged up |
| 502 | msafe_struct ms; |
| 503 | ms.objhandle = memory->collided_object; |
| 504 | MSafe_GetValue(MSAFE_OBJECT_RECHARGE_SCALAR, &ms); |
| 505 | |
| 506 | if (ms.scalar != RAPIDFIRE_RECHARGE) { |
| 507 | // player no longer recharged! cancel timer |
| 508 | if (Scrpt_TimerExists(RapidFirePlayerTimers[memory->collided_pnum].timer_handle)) { |
| 509 | // cancel the timer |
| 510 | mprintf(0, "CANCELING RAPID FIRE TIMER FOR PLAYER %d\n", memory->collided_pnum); |
| 511 | Scrpt_CancelTimer(RapidFirePlayerTimers[memory->collided_pnum].timer_handle); |
| 512 | |
| 513 | AddHudMessage(memory->collided_object, GR_RGB(0, 255, 0), TXT_RAPIDFIREOFF); |
| 514 | int sound_id = Sound_FindId("RapidFireOff"); |
| 515 | Sound_Play3d(memory->collided_object, sound_id); |
| 516 | } |
| 517 | |
| 518 | RapidFirePlayerTimers[memory->collided_pnum].timer_handle = -1; |
| 519 | memory->collided_pnum = 255; |
| 520 | memory->collided_object = OBJECT_HANDLE_NONE; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | } break; |
| 525 | } |
| 526 | |
| 527 | return CONTINUE_CHAIN | CONTINUE_DEFAULT; |
| 528 | } |
| 529 | |
| 530 | ForceWallScript::ForceWallScript() { memory = NULL; } |
| 531 | |
| 532 | int16_t ForceWallScript::CallEvent(int event, tOSIRISEventInfo *data) { |
| 533 | switch (event) { |
| 534 | |
| 535 | case EVT_CREATED: { |
| 536 | tOSIRISMEMCHUNK ch; |
| 537 | ch.id = 4; |
| 538 | ch.size = sizeof(forcewallmemory); |
| 539 | ch.my_id.type = OBJECT_SCRIPT; |
| 540 | ch.my_id.objhandle = data->me_handle; |
| 541 | |
| 542 | memory = (forcewallmemory *)Scrpt_MemAlloc(&ch); |
| 543 | memory->object_handle = OBJECT_HANDLE_NONE; |
| 544 | |
| 545 | } break; |
| 546 | |
| 547 | case EVT_MEMRESTORE: { |
| 548 | memory = (forcewallmemory *)data->evt_memrestore.memory_ptr; |
| 549 | } break; |
| 550 | |
| 551 | case EVT_COLLIDE: { |
| 552 | if (!memory) |
| 553 | break; |
| 554 | |
| 555 | msafe_struct ms; |
| 556 | ms.objhandle = data->evt_collide.it_handle; |
| 557 | MSafe_GetValue(MSAFE_OBJECT_TYPE, &ms); |
| 558 | int pnum; |
| 559 | |
| 560 | if (ms.type == OBJ_PLAYER) { |
| 561 | MSafe_GetValue(MSAFE_OBJECT_ID, &ms); |
| 562 | pnum = ms.id; |
| 563 | |
| 564 | AddHudMessage(data->evt_collide.it_handle, GR_RGB(0, 255, 0), TXT_FORCEWALL); |
| 565 | |
| 566 | // add a force wall to the inventory |
| 567 | ms.objhandle = data->me_handle; |
| 568 | MSafe_GetValue(MSAFE_OBJECT_ID, &ms); |
| 569 | ms.slot = pnum; |
| 570 | ms.type = OBJ_POWERUP; |
| 571 | ms.flags = INVAF_TIMEOUTONSPEW; |
| 572 | MSafe_CallFunction(MSAFE_INVEN_ADD_TYPE_ID, &ms); |
| 573 | |
| 574 | // pickit up |
| 575 | ms.playsound = 1; |
| 576 | MSafe_CallFunction(MSAFE_OBJECT_REMOVE, &ms); |
| 577 | } |
| 578 | } break; |
| 579 | case EVT_USE: { |
| 580 | if (!memory) |
| 581 | break; |
| 582 | |
| 583 | msafe_struct ms; |
| 584 | |
| 585 | // Eject a ForceWall and make it last for 60 seconds |
| 586 | memory->object_handle = data->evt_use.it_handle; // save the player for when the timer is up |
| 587 | |
| 588 | AddHudMessage(memory->object_handle, GR_RGB(0, 255, 0), TXT_FORCEWALLEJECT); |
| 589 | |
| 590 | // ghost the object and set a timer |
| 591 | tOSIRISTIMER ti; |
| 592 | ti.flags = 0; // not a volatile object when dropped |
| 593 | ti.id = 32; |
| 594 | ti.object_handle = data->me_handle; |
| 595 | ti.timer_interval = 60; |
| 596 | Scrpt_CreateTimer(&ti); |
| 597 | |
| 598 | // ghost the object |
| 599 | ms.objhandle = data->me_handle; |
| 600 | MSafe_CallFunction(MSAFE_OBJECT_GHOST, &ms); |
| 601 | |
| 602 | // Do the Force Wall stuff here |
| 603 | |
| 604 | } break; |
| 605 | case EVT_TIMER: { |
| 606 | if (!memory) |
| 607 | break; |
| 608 | |
| 609 | msafe_struct ms; |
| 610 | |
| 611 | // End Force Wall stuff here |
| 612 | |
| 613 | AddHudMessage(memory->object_handle, GR_RGB(0, 255, 0), TXT_FORCEWALLDONE); |
| 614 | |
| 615 | // UnGhost the object |
| 616 | ms.objhandle = data->me_handle; |
| 617 | //@@MSafe_CallFunction(MSAFE_OBJECT_UNGHOST,&ms); |
| 618 | |
| 619 | // destroy the powerup |
| 620 | ms.playsound = 0; |
| 621 | MSafe_CallFunction(MSAFE_OBJECT_REMOVE, &ms); |
| 622 | |
| 623 | } break; |
| 624 | } |
| 625 | |
| 626 | return CONTINUE_CHAIN | CONTINUE_DEFAULT; |
| 627 | } |
| 628 | |
| 629 | int16_t WingNutScript::CallEvent(int event, tOSIRISEventInfo *data) { |
| 630 | switch (event) { |
| 631 | case EVT_COLLIDE: { |
| 632 | msafe_struct ms; |
| 633 | ms.objhandle = data->evt_collide.it_handle; |
| 634 | MSafe_GetValue(MSAFE_OBJECT_TYPE, &ms); |
| 635 | int pnum; |
| 636 | |
| 637 | if (ms.type == OBJ_PLAYER) { |
| 638 | MSafe_GetValue(MSAFE_OBJECT_ID, &ms); |
| 639 | pnum = ms.id; |
| 640 | |
| 641 | AddHudMessage(data->evt_collide.it_handle, GR_RGB(0, 255, 0), TXT_WINGNUT); |
| 642 | |
| 643 | // add a wing nut to the inventory |
| 644 | ms.objhandle = data->me_handle; |
| 645 | MSafe_GetValue(MSAFE_OBJECT_ID, &ms); |
| 646 | ms.slot = pnum; |
| 647 | ms.type = OBJ_POWERUP; |
| 648 | ms.flags = INVAF_TIMEOUTONSPEW; |
| 649 | MSafe_CallFunction(MSAFE_INVEN_ADD_TYPE_ID, &ms); |
| 650 | |
| 651 | // pickit up |
| 652 | ms.playsound = 1; |
| 653 | MSafe_CallFunction(MSAFE_OBJECT_REMOVE, &ms); |
| 654 | } |
| 655 | } break; |
| 656 | case EVT_USE: { |
| 657 | } break; |
| 658 | } |
| 659 | |
| 660 | return CONTINUE_CHAIN | CONTINUE_DEFAULT; |
| 661 | } |
| 662 | |
| 663 | void AddHudMessage(int player_objhandle, int color, const char *format, ...) { |
| 664 | msafe_struct ms; |
| 665 | ms.objhandle = player_objhandle; |
| 666 | MSafe_GetValue(MSAFE_OBJECT_TYPE, &ms); |
| 667 | if (ms.type == OBJ_PLAYER) { |
| 668 | MSafe_GetValue(MSAFE_OBJECT_ID, &ms); |
| 669 | ms.slot = ms.id; |
| 670 | ms.color = color; |
| 671 | char big_buffer[1024]; |
| 672 | va_list marker; |
| 673 | va_start(marker, format); |
| 674 | std::vsnprintf(big_buffer, sizeof(big_buffer), format, marker); |
| 675 | va_end(marker); |
| 676 | strncpy(ms.message, big_buffer, MSAFE_MESSAGE_LENGTH - 1); |
| 677 | ms.message[MSAFE_MESSAGE_LENGTH - 1] = '\0'; |
| 678 | |
| 679 | MSafe_CallFunction(MSAFE_MISC_FILTERED_HUD_MESSAGE, &ms); |
| 680 | } |
| 681 | } |