| 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 | // testscript.cpp |
| 20 | // 0.1 |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include "osiris_import.h" |
| 25 | #include "osiris_common.h" |
| 26 | |
| 27 | #include "module.h" |
| 28 | |
| 29 | #ifdef __cplusplus |
| 30 | extern "C" { |
| 31 | #endif |
| 32 | DLLEXPORT char STDCALL InitializeDLL(tOSIRISModuleInit *func_list); |
| 33 | DLLEXPORT void STDCALL ShutdownDLL(void); |
| 34 | DLLEXPORT int STDCALL GetGOScriptID(const char *name, uint8_t isdoor); |
| 35 | DLLEXPORT void STDCALLPTR CreateInstance(int id); |
| 36 | DLLEXPORT void STDCALL DestroyInstance(int id, void *ptr); |
| 37 | DLLEXPORT int16_t STDCALL CallInstanceEvent(int id, void *ptr, int event, tOSIRISEventInfo *data); |
| 38 | DLLEXPORT int STDCALL SaveRestoreState(void *file_ptr, uint8_t saving_state); |
| 39 | #ifdef __cplusplus |
| 40 | } |
| 41 | #endif |
| 42 | |
| 43 | // ========================== |
| 44 | // Script class definitions// |
| 45 | // ========================== |
| 46 | #define ID_SHIELD_ORB 0x00 // shield id |
| 47 | #define ID_ENERGY_ORB 0x01 // energy id |
| 48 | #define MAX_IDS 0x02 // maximum number of IDs |
| 49 | struct tScriptInfo { |
| 50 | int id; |
| 51 | const char *name; |
| 52 | }; |
| 53 | |
| 54 | tScriptInfo ScriptInfo[MAX_IDS] = {{ID_SHIELD_ORB, "Shield"}, {ID_ENERGY_ORB, "Energy"}}; |
| 55 | |
| 56 | class BaseObjScript { |
| 57 | public: |
| 58 | BaseObjScript(); |
| 59 | ~BaseObjScript(); |
| 60 | virtual int16_t CallEvent(int event, tOSIRISEventInfo *data); |
| 61 | |
| 62 | protected: |
| 63 | bool called; |
| 64 | }; |
| 65 | |
| 66 | struct tShieldOrbInfo { |
| 67 | int hitcount; |
| 68 | }; |
| 69 | //------------------ |
| 70 | // Shield orb script |
| 71 | //------------------ |
| 72 | class ShieldOrb : public BaseObjScript { |
| 73 | public: |
| 74 | ShieldOrb() { info = NULL; } |
| 75 | int16_t CallEvent(int event, tOSIRISEventInfo *data); |
| 76 | |
| 77 | private: |
| 78 | tShieldOrbInfo *info; |
| 79 | }; |
| 80 | |
| 81 | //------------------ |
| 82 | // Energy orb script |
| 83 | //------------------ |
| 84 | class EnergyOrb : public BaseObjScript { |
| 85 | public: |
| 86 | int16_t CallEvent(int event, tOSIRISEventInfo *data); |
| 87 | }; |
| 88 | |
| 89 | char STDCALL InitializeDLL(tOSIRISModuleInit *func_list) { |
| 90 | osicommon_Initialize((tOSIRISModuleInit *)func_list); |
| 91 | |
| 92 | return 1; |
| 93 | } |
| 94 | |
| 95 | void STDCALL ShutdownDLL(void) {} |
| 96 | |
| 97 | int STDCALL GetGOScriptID(const char *name, uint8_t isdoor) { |
| 98 | for (int i = 0; i < MAX_IDS; i++) { |
| 99 | if (!stricmp(name, ScriptInfo[i].name)) { |
| 100 | return ScriptInfo[i].id; |
| 101 | } |
| 102 | } |
| 103 | return -1; |
| 104 | } |
| 105 | |
| 106 | void STDCALLPTR CreateInstance(int id) { |
| 107 | switch (id) { |
| 108 | case ID_SHIELD_ORB: |
| 109 | return new ShieldOrb; |
| 110 | break; |
| 111 | case ID_ENERGY_ORB: |
| 112 | return new EnergyOrb; |
| 113 | break; |
| 114 | default: |
| 115 | mprintf(0, "SCRIPT: Illegal ID (%d)\n", id); |
| 116 | break; |
| 117 | }; |
| 118 | return NULL; |
| 119 | } |
| 120 | |
| 121 | void STDCALL DestroyInstance(int id, void *ptr) { |
| 122 | switch (id) { |
| 123 | case ID_SHIELD_ORB: |
| 124 | delete ((ShieldOrb *)ptr); |
| 125 | break; |
| 126 | case ID_ENERGY_ORB: |
| 127 | delete ((EnergyOrb *)ptr); |
| 128 | break; |
| 129 | default: |
| 130 | mprintf(0, "SCRIPT: Illegal ID (%d)\n", id); |
| 131 | break; |
| 132 | }; |
| 133 | } |
| 134 | |
| 135 | int16_t STDCALL CallInstanceEvent(int id, void *ptr, int event, tOSIRISEventInfo *data) { |
| 136 | switch (id) { |
| 137 | case ID_SHIELD_ORB: |
| 138 | case ID_ENERGY_ORB: |
| 139 | return ((BaseObjScript *)ptr)->CallEvent(event, data); |
| 140 | break; |
| 141 | default: |
| 142 | mprintf(0, "SCRIPT: Illegal ID (%d)\n", id); |
| 143 | break; |
| 144 | }; |
| 145 | |
| 146 | return CONTINUE_CHAIN | CONTINUE_DEFAULT; |
| 147 | } |
| 148 | |
| 149 | // SaveRestoreState |
| 150 | // Purpose: |
| 151 | // This function is called when Descent 3 is saving or restoring the game state. In this function |
| 152 | // you should save/restore any global data that you want preserved through load/save (which includes |
| 153 | // demos). You must be very careful with this function, corrupting the file (reading or writing too |
| 154 | // much or too little) may be hazardous to the game (possibly making it impossible to restore the |
| 155 | // state). It would be best to use version information to keep older versions of saved states still |
| 156 | // able to be used. IT IS VERY IMPORTANT WHEN SAVING THE STATE TO RETURN THE NUMBER OF _BYTES_ WROTE |
| 157 | // TO THE FILE. When restoring the data, the return value is ignored. saving_state is 1 when you should |
| 158 | // write data to the file_ptr, 0 when you should read in the data. |
| 159 | int STDCALL SaveRestoreState(void *file_ptr, uint8_t saving_state) { return 0; } |
| 160 | |
| 161 | //============================================ |
| 162 | // Script Implementation |
| 163 | //============================================ |
| 164 | BaseObjScript::BaseObjScript() { called = false; } |
| 165 | |
| 166 | BaseObjScript::~BaseObjScript() {} |
| 167 | |
| 168 | int16_t BaseObjScript::CallEvent(int event, tOSIRISEventInfo *data) { |
| 169 | switch (event) { |
| 170 | case EVT_INTERVAL: |
| 171 | mprintf(0, "."); |
| 172 | break; |
| 173 | case EVT_AI_FRAME: |
| 174 | mprintf(0, "*"); |
| 175 | break; |
| 176 | case EVT_DAMAGED: |
| 177 | mprintf(0, "EVT_DAMAGED "); |
| 178 | break; |
| 179 | case EVT_COLLIDE: |
| 180 | mprintf(0, "EVT_COLLIDE "); |
| 181 | break; |
| 182 | case EVT_CREATED: |
| 183 | mprintf(0, "EVT_CREATED "); |
| 184 | break; |
| 185 | case EVT_DESTROY: |
| 186 | mprintf(0, "EVT_DESTROY "); |
| 187 | break; |
| 188 | case EVT_TIMER: |
| 189 | mprintf(0, "EVT_TIMER "); |
| 190 | break; |
| 191 | case EVT_USE: |
| 192 | mprintf(0, "EVT_USE "); |
| 193 | break; |
| 194 | case EVT_AI_NOTIFY: |
| 195 | mprintf(0, "EVT_AI_NOTIFY "); |
| 196 | break; |
| 197 | case EVT_AI_INIT: |
| 198 | mprintf(0, "EVT_AI_INIT "); |
| 199 | break; |
| 200 | case EVT_CHANGESEG: |
| 201 | mprintf(0, "EVT_CHANGESEG "); |
| 202 | break; |
| 203 | }; |
| 204 | |
| 205 | return CONTINUE_CHAIN | CONTINUE_DEFAULT; |
| 206 | } |
| 207 | |
| 208 | // -------------- |
| 209 | // Shield orb |
| 210 | // -------------- |
| 211 | int16_t ShieldOrb::CallEvent(int event, tOSIRISEventInfo *data) { |
| 212 | switch (event) { |
| 213 | case EVT_COLLIDE: |
| 214 | if (!called) { |
| 215 | mprintf(0, "Collide with shield\n"); |
| 216 | msafe_struct mo; |
| 217 | |
| 218 | mo.objhandle = data->evt_collide.it_handle; |
| 219 | MSafe_GetValue(MSAFE_OBJECT_TYPE, &mo); |
| 220 | MSafe_GetValue(MSAFE_OBJECT_ID, &mo); |
| 221 | |
| 222 | if (mo.type == OBJ_PLAYER) { |
| 223 | |
| 224 | MSafe_GetValue(MSAFE_OBJECT_SHIELDS, &mo); |
| 225 | if (mo.shields < 200) { |
| 226 | mo.shields += 15; |
| 227 | if (mo.shields > 200) |
| 228 | mo.shields = 200; |
| 229 | MSafe_CallFunction(MSAFE_OBJECT_SHIELDS, &mo); |
| 230 | |
| 231 | mo.slot = mo.id; |
| 232 | mo.color = GR_RGB(0, 255, 0); |
| 233 | snprintf(mo.message, sizeof(mo.message), "Shields boosted to %d", (int)mo.shields); |
| 234 | MSafe_CallFunction(MSAFE_MISC_HUD_MESSAGE, &mo); |
| 235 | called = true; |
| 236 | } |
| 237 | |
| 238 | tOSIRISTIMER ti; |
| 239 | ti.flags = 0; |
| 240 | ti.id = 0; |
| 241 | ti.repeat_count = -1; |
| 242 | ti.object_handle = data->me_handle; |
| 243 | ti.timer_interval = 10.0f; |
| 244 | Scrpt_CreateTimer(&ti); |
| 245 | } |
| 246 | |
| 247 | tOSIRISMEMCHUNK ch; |
| 248 | ch.id = 4; |
| 249 | ch.size = sizeof(tShieldOrbInfo); |
| 250 | ch.my_id.type = OBJECT_SCRIPT; |
| 251 | ch.my_id.objhandle = data->me_handle; |
| 252 | info = (tShieldOrbInfo *)Scrpt_MemAlloc(&ch); |
| 253 | if (info) { |
| 254 | info->hitcount = 1; |
| 255 | } |
| 256 | } else { |
| 257 | if (info) { |
| 258 | info->hitcount++; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | if (info) { |
| 263 | mprintf(0, "HIT(%d)\n", info->hitcount); |
| 264 | } |
| 265 | break; |
| 266 | |
| 267 | case EVT_MEMRESTORE: { |
| 268 | info = (tShieldOrbInfo *)data->evt_memrestore.memory_ptr; |
| 269 | mprintf(0, "MEM RESTORE!!! -- %d(%d)\n", data->evt_memrestore.id, info->hitcount); |
| 270 | } break; |
| 271 | |
| 272 | case EVT_TIMER: { |
| 273 | msafe_struct mo; |
| 274 | mo.objhandle = data->me_handle; |
| 275 | MSafe_GetValue(MSAFE_OBJECT_ID, &mo); |
| 276 | mo.slot = mo.id; |
| 277 | mo.color = GR_RGB(0, 255, 0); |
| 278 | snprintf(mo.message, sizeof(mo.message), "TIMER: HIT COUNT %d", (info) ? info->hitcount : -1); |
| 279 | MSafe_CallFunction(MSAFE_MISC_HUD_MESSAGE, &mo); |
| 280 | } break; |
| 281 | } |
| 282 | return CONTINUE_CHAIN | CONTINUE_DEFAULT; // BaseObjScript::CallEvent(event,data); |
| 283 | } |
| 284 | |
| 285 | // -------------- |
| 286 | // Energy orb |
| 287 | // -------------- |
| 288 | int16_t EnergyOrb::CallEvent(int event, tOSIRISEventInfo *data) { |
| 289 | switch (event) { |
| 290 | case EVT_COLLIDE: |
| 291 | if (!called) { |
| 292 | mprintf(0, "Collide with energy\n"); |
| 293 | msafe_struct mo; |
| 294 | |
| 295 | mo.objhandle = data->evt_collide.it_handle; |
| 296 | MSafe_GetValue(MSAFE_OBJECT_TYPE, &mo); |
| 297 | MSafe_GetValue(MSAFE_OBJECT_ID, &mo); |
| 298 | |
| 299 | if (mo.type == OBJ_PLAYER) { |
| 300 | |
| 301 | MSafe_GetValue(MSAFE_OBJECT_ENERGY, &mo); |
| 302 | if (mo.energy < 200) { |
| 303 | mo.energy += 15; |
| 304 | if (mo.energy > 200) |
| 305 | mo.energy = 200; |
| 306 | MSafe_CallFunction(MSAFE_OBJECT_ENERGY, &mo); |
| 307 | |
| 308 | mo.slot = mo.id; |
| 309 | mo.color = GR_RGB(0, 255, 0); |
| 310 | snprintf(mo.message, sizeof(mo.message), "Energy boosted to %d", (int)mo.energy); |
| 311 | MSafe_CallFunction(MSAFE_MISC_HUD_MESSAGE, &mo); |
| 312 | called = true; |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | break; |
| 317 | } |
| 318 | return CONTINUE_CHAIN | CONTINUE_DEFAULT; |
| 319 | } |