| 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 | // aigame2.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 | #include "osiris_vector.h" |
| 27 | #include "DallasFuncs.cpp" |
| 28 | |
| 29 | #include "module.h" |
| 30 | |
| 31 | #ifdef __cplusplus |
| 32 | extern "C" { |
| 33 | #endif |
| 34 | DLLEXPORT char STDCALL InitializeDLL(tOSIRISModuleInit *func_list); |
| 35 | DLLEXPORT void STDCALL ShutdownDLL(void); |
| 36 | DLLEXPORT int STDCALL GetGOScriptID(const char *name, uint8_t isdoor); |
| 37 | DLLEXPORT void STDCALLPTR CreateInstance(int id); |
| 38 | DLLEXPORT void STDCALL DestroyInstance(int id, void *ptr); |
| 39 | DLLEXPORT int16_t STDCALL CallInstanceEvent(int id, void *ptr, int event, tOSIRISEventInfo *data); |
| 40 | DLLEXPORT int STDCALL SaveRestoreState(void *file_ptr, uint8_t saving_state); |
| 41 | #ifdef __cplusplus |
| 42 | } |
| 43 | #endif |
| 44 | |
| 45 | static int String_table_size = 0; |
| 46 | static char **String_table = NULL; |
| 47 | static const char *_Error_string = "!!ERROR MISSING STRING!!"; |
| 48 | static const char *_Empty_string = ""; |
| 49 | const char *GetStringFromTable(int index) { |
| 50 | if ((index < 0) || (index >= String_table_size)) |
| 51 | return _Error_string; |
| 52 | if (!String_table[index]) |
| 53 | return _Empty_string; |
| 54 | return String_table[index]; |
| 55 | } |
| 56 | #define TXT(x) GetStringFromTable(x) |
| 57 | |
| 58 | /////////////////////////////////////////////////////////////////////////////// |
| 59 | // Script IDs |
| 60 | |
| 61 | #define NUM_IDS 5 // maximum number of IDs |
| 62 | #define FIRST_ID 50 |
| 63 | |
| 64 | #define ID_SAMIRPEST 50 // Samir's test pest. |
| 65 | #define ID_BLACKSTORMTROOP 51 // The CED Black Stormtrooper |
| 66 | #define ID_CREEPER 52 // The CREEPER |
| 67 | #define ID_LUKETURRET 53 // Luke's nifty security turret |
| 68 | #define ID_STBLACKBARREL 54 // The Black Stormtrooper's barrel pack |
| 69 | |
| 70 | /////////////////////////////////////////////////////////////////////////////// |
| 71 | // Script names |
| 72 | |
| 73 | static const char *const Script_names[NUM_IDS] = {"Samir's Pest", "StormTrooperBlack", "Creeper", "LukeTurret", |
| 74 | "STBlackBarrel"}; |
| 75 | |
| 76 | // Class definitions |
| 77 | |
| 78 | #define N_SNIPE_POINTS 3 |
| 79 | |
| 80 | #define GOAL_UID_GETTOPOS 0x100 |
| 81 | |
| 82 | // ai base class |
| 83 | class aiObjScript { |
| 84 | public: |
| 85 | int16_t CallEvent(int event, tOSIRISEventInfo *data); |
| 86 | |
| 87 | protected: |
| 88 | // Handles all possible OSIRIS events. |
| 89 | virtual void OnInit(int me_handle){}; // EVT_AI_INIT |
| 90 | virtual void OnDestroy(int me_handle, tOSIRISEVTDESTROY *evt){}; // EVT_DESTROY |
| 91 | virtual void OnInterval(tOSIRISEventInfo *data){}; // EVT_AI_FRAME |
| 92 | virtual void OnCollide(int me_handle, int it_handle){}; // EVT_COLLIDE |
| 93 | virtual bool OnNotify(int me_handle, tOSIRISEVTAINOTIFY *data) { return true; }; // EVT_AI_NOTIFY |
| 94 | virtual void OnDamaged(int me_handle, tOSIRISEVTDAMAGED *evt){}; // EVT_DAMAGED |
| 95 | virtual void OnMemRestore(void *ptr){}; // EVT_MEMRESTORE |
| 96 | }; |
| 97 | |
| 98 | // ai base class |
| 99 | #define PEST_STATE_ROAM 0 |
| 100 | #define PEST_STATE_HIVE 1 |
| 101 | |
| 102 | class aiSamirPest : public aiObjScript { |
| 103 | public: |
| 104 | aiSamirPest(); |
| 105 | ~aiSamirPest(); |
| 106 | |
| 107 | private: |
| 108 | struct t_pest_memory { |
| 109 | int tail_handle; |
| 110 | float last_anim_frame; |
| 111 | int foot_sounds[3]; |
| 112 | int state; |
| 113 | }; |
| 114 | |
| 115 | t_pest_memory *memory; |
| 116 | |
| 117 | protected: |
| 118 | // Handles all possible OSIRIS events. |
| 119 | virtual void OnInit(int me_handle); |
| 120 | virtual void OnDestroy(int me_handle, tOSIRISEVTDESTROY *evt); |
| 121 | virtual void OnInterval(tOSIRISEventInfo *data); |
| 122 | virtual void OnCollide(int me_handle, int it_handle); |
| 123 | virtual bool OnNotify(int me_handle, tOSIRISEVTAINOTIFY *data); |
| 124 | virtual void OnMemRestore(void *ptr) { memory = (t_pest_memory *)ptr; }; |
| 125 | }; |
| 126 | |
| 127 | #define STATE_IDLE 0 // waiting state |
| 128 | #define STATE_INIT 1 // initial state |
| 129 | #define STATE_BASE 2 // sentry state. |
| 130 | #define STATE_HIDE 3 // hiding, waiting... |
| 131 | #define STATE_SNIPE 4 // will attack player briefly. |
| 132 | #define STATE_ATTACK 5 // regular attack |
| 133 | #define STATE_STALK 6 // stalks player |
| 134 | |
| 135 | class aiBlackStormTrooper : public aiObjScript { |
| 136 | public: |
| 137 | aiBlackStormTrooper(); |
| 138 | ~aiBlackStormTrooper(); |
| 139 | |
| 140 | private: |
| 141 | struct t_bst_memory { |
| 142 | float timer; |
| 143 | float snipe_timer; |
| 144 | float last_frame; |
| 145 | float laser_timer; |
| 146 | int target_handle; |
| 147 | int ammo_barrel; |
| 148 | int gun_sight_obj; |
| 149 | int camera_obj; |
| 150 | int8_t state; |
| 151 | int8_t snipe_points; |
| 152 | int8_t cur_snipe_point; |
| 153 | int8_t laser_targeted; |
| 154 | int16_t backpack_hp; // points before destroy backpack. |
| 155 | int16_t flee_hp; |
| 156 | |
| 157 | // a Nx(N-1) array with a extra int for number of VALID connecting snipe points per snipe point. |
| 158 | // note: obvious cases ignored (ptA->ptA) |
| 159 | // used to create a quick map of snipe points for sniper to use. |
| 160 | int snipe_point_handles[N_SNIPE_POINTS]; |
| 161 | int8_t access_array[N_SNIPE_POINTS][N_SNIPE_POINTS]; |
| 162 | |
| 163 | // used to keep track of targets black storm trooper knows about. storm trooper must |
| 164 | // SEE the target first before storing the target in memory. |
| 165 | int snipe_target_handles[N_SNIPE_POINTS]; |
| 166 | }; |
| 167 | |
| 168 | t_bst_memory *memory; |
| 169 | |
| 170 | void set_state(int me_handle, int state); |
| 171 | |
| 172 | protected: |
| 173 | // Handles all possible OSIRIS events. |
| 174 | virtual void OnInit(int me_handle); |
| 175 | virtual void OnDestroy(int me_handle, tOSIRISEVTDESTROY *evt); |
| 176 | virtual void OnInterval(tOSIRISEventInfo *data); |
| 177 | virtual bool OnNotify(int me_handle, tOSIRISEVTAINOTIFY *data); |
| 178 | virtual void OnDamaged(int me_handle, tOSIRISEVTDAMAGED *evt); |
| 179 | virtual void OnMemRestore(void *ptr) { memory = (t_bst_memory *)ptr; }; |
| 180 | |
| 181 | private: |
| 182 | void find_targets(int me_handle, bool new_snipe_point); |
| 183 | }; |
| 184 | |
| 185 | class aiCreeper : public aiObjScript { |
| 186 | public: |
| 187 | aiCreeper(); |
| 188 | ~aiCreeper(); |
| 189 | |
| 190 | private: |
| 191 | struct t_creep_memory { |
| 192 | float timer; |
| 193 | float eye_timer; |
| 194 | int8_t state; |
| 195 | uint8_t melee_flags; |
| 196 | int16_t energy; |
| 197 | int16_t hits; |
| 198 | int eye_obj; |
| 199 | }; |
| 200 | |
| 201 | t_creep_memory *memory; |
| 202 | |
| 203 | void set_state(int me_handle, int state); |
| 204 | |
| 205 | protected: |
| 206 | // Handles all possible OSIRIS events. |
| 207 | virtual void OnInit(int me_handle); |
| 208 | virtual void OnDestroy(int me_handle, tOSIRISEVTDESTROY *evt); |
| 209 | virtual void OnInterval(tOSIRISEventInfo *data); |
| 210 | virtual bool OnNotify(int me_handle, tOSIRISEVTAINOTIFY *data); |
| 211 | virtual void OnDamaged(int me_handle, tOSIRISEVTDAMAGED *evt); |
| 212 | virtual void OnMemRestore(void *ptr) { memory = (t_creep_memory *)ptr; }; |
| 213 | }; |
| 214 | |
| 215 | class aiLukeTurret : public aiObjScript { |
| 216 | struct t_lturret_memory { |
| 217 | float timer; |
| 218 | }; |
| 219 | |
| 220 | t_lturret_memory *memory; |
| 221 | |
| 222 | public: |
| 223 | aiLukeTurret(){}; |
| 224 | ~aiLukeTurret(){}; |
| 225 | |
| 226 | protected: |
| 227 | virtual void OnInit(int me_handle); |
| 228 | virtual bool OnNotify(int me_handle, tOSIRISEVTAINOTIFY *data); |
| 229 | virtual void OnInterval(tOSIRISEventInfo *data); |
| 230 | virtual void OnMemRestore(void *ptr) { memory = (t_lturret_memory *)ptr; }; |
| 231 | }; |
| 232 | |
| 233 | class aiSTBlackBarrel : public aiObjScript { |
| 234 | struct t_stblackbarrel_memory { |
| 235 | matrix orient; |
| 236 | }; |
| 237 | |
| 238 | t_stblackbarrel_memory *memory; |
| 239 | |
| 240 | public: |
| 241 | aiSTBlackBarrel(){}; |
| 242 | ~aiSTBlackBarrel(){}; |
| 243 | |
| 244 | protected: |
| 245 | virtual void OnInit(int me_handle); |
| 246 | virtual void OnDestroy(int me_handle, tOSIRISEVTDESTROY *evt); |
| 247 | virtual void OnDamaged(int me_handle, tOSIRISEVTDAMAGED *evt); |
| 248 | virtual void OnMemRestore(void *ptr) { memory = (t_stblackbarrel_memory *)ptr; }; |
| 249 | }; |
| 250 | |
| 251 | /////////////////////////////////////////////////////////////////////////////// |
| 252 | // InitializeDLL |
| 253 | // Purpose: |
| 254 | // This function gets called when the DLL first gets loaded. It will only be called once (until the |
| 255 | // DLL is unloaded). Passed in is a struct of data passed from the game needed for the DLL to interact |
| 256 | // with D3. Usually this function will just call osicommon_Initialize(), which sets up the imported |
| 257 | // functions. However, you can alloc some memory or whatever in this function, and free it in ShutdownDLL(). |
| 258 | // Note: You cannot call any imported functions until osicommon_Initialize() is called. |
| 259 | // Returns 1 if initialization went ok, 0 if there was an error and the DLL should not be loaded. |
| 260 | char STDCALL InitializeDLL(tOSIRISModuleInit *func_list) { |
| 261 | osicommon_Initialize((tOSIRISModuleInit *)func_list); |
| 262 | String_table_size = func_list->string_count; |
| 263 | String_table = func_list->string_table; |
| 264 | if (func_list->game_checksum != CHECKSUM) { |
| 265 | mprintf(0, "Game-Checksum FAIL!!! (%ul!=%ul)\n", func_list->game_checksum, CHECKSUM); |
| 266 | mprintf(0, "RECOMPILE YOUR SCRIPTS!!!\n"); |
| 267 | return 0; |
| 268 | } |
| 269 | return 1; |
| 270 | } |
| 271 | |
| 272 | // ShutdownDLL |
| 273 | // Purpose: |
| 274 | // This function is called right before a DLL is about to be unloaded from memory. You can free |
| 275 | // any unfree'd memory, or anything else you need to do. Don't worry about destroying any instances |
| 276 | // of scripts, as they will all be automatically destroyed before this function is called. The |
| 277 | // same goes for any memory allocated with Scrpt_MemAlloc(), as this will automatically be freed |
| 278 | // when a scripts instance is destroyed. |
| 279 | void STDCALL ShutdownDLL(void) {} |
| 280 | |
| 281 | // GetGOScriptID |
| 282 | // Purpose: |
| 283 | // Given the name of the object (from it's pagename), this function will search through it's |
| 284 | // list of General Object Scripts for a script with a matching name (to see if there is a script |
| 285 | // for that type/id of object within this DLL). If a matching scriptname is found, a UNIQUE ID |
| 286 | // is to be returned back to Descent 3. This ID will be used from here on out for all future |
| 287 | // interaction with the DLL. Since doors are not part of the generic object's, it's possible |
| 288 | // for a door to have the same name as a generic object (OBJ_POWERUP, OBJ_BUILDING, OBJ_CLUTTER |
| 289 | // or OBJ_ROBOT), therefore, a 1 is passed in for isdoor if the given object name refers to a |
| 290 | // door, else it is a 0. The return value is the unique identifier, else -1 if the script |
| 291 | // does not exist in the DLL. |
| 292 | int STDCALL GetGOScriptID(const char *name, uint8_t isdoor) { |
| 293 | int i; |
| 294 | for (i = 0; i < NUM_IDS; i++) { |
| 295 | if (strcmp(name, Script_names[i]) == 0) { |
| 296 | return (FIRST_ID + i); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | return -1; |
| 301 | } |
| 302 | |
| 303 | // CreateInstance |
| 304 | // Purpose: |
| 305 | // Given an ID from a call to GetGOScriptID(), this function will create a new instance for that |
| 306 | // particular script (by allocating and initializing memory, etc.). A pointer to this instance |
| 307 | // is to be returned back to Descent 3. This pointer will be passed around, along with the ID |
| 308 | // for CallInstanceEvent() and DestroyInstance(). Return NULL if there was an error. |
| 309 | void STDCALLPTR CreateInstance(int id) { |
| 310 | switch (id) { |
| 311 | case ID_SAMIRPEST: |
| 312 | return new aiSamirPest; |
| 313 | case ID_BLACKSTORMTROOP: |
| 314 | return new aiBlackStormTrooper; |
| 315 | case ID_CREEPER: |
| 316 | return new aiCreeper; |
| 317 | case ID_LUKETURRET: |
| 318 | return new aiLukeTurret; |
| 319 | case ID_STBLACKBARREL: |
| 320 | return new aiSTBlackBarrel; |
| 321 | } |
| 322 | |
| 323 | return NULL; |
| 324 | } |
| 325 | |
| 326 | // DestroyInstance |
| 327 | // Purpose: |
| 328 | // Given an ID, and a pointer to a particular instance of a script, this function will delete and |
| 329 | // destruct all information associated with that script, so it will no longer exist. |
| 330 | void STDCALL DestroyInstance(int id, void *ptr) { |
| 331 | switch (id) { |
| 332 | case ID_SAMIRPEST: |
| 333 | delete ((aiSamirPest *)ptr); |
| 334 | break; |
| 335 | case ID_BLACKSTORMTROOP: |
| 336 | delete ((aiBlackStormTrooper *)ptr); |
| 337 | break; |
| 338 | case ID_CREEPER: |
| 339 | delete ((aiCreeper *)ptr); |
| 340 | break; |
| 341 | case ID_LUKETURRET: |
| 342 | delete ((aiLukeTurret *)ptr); |
| 343 | break; |
| 344 | case ID_STBLACKBARREL: |
| 345 | delete ((aiSTBlackBarrel *)ptr); |
| 346 | break; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | // CallInstanceEvent |
| 351 | // Purpose: |
| 352 | // Given an ID, a pointer to a script instance, an event and a pointer to the struct of |
| 353 | // information about the event, this function will translate who this event belongs to and |
| 354 | // passes the event to that instance of the script to be handled. Return a combination of |
| 355 | // CONTINUE_CHAIN and CONTINUE_DEFAULT, to give instructions on what to do based on the |
| 356 | // event. CONTINUE_CHAIN means to continue through the chain of scripts (custom script, level |
| 357 | // script, mission script, and finally default script). If CONTINUE_CHAIN is not specified, |
| 358 | // than the chain is broken and those scripts of lower priority will never get the event. Return |
| 359 | // CONTINUE_DEFAULT in order to tell D3 if you want process the normal action that is built into |
| 360 | // the game for that event. This only pertains to certain events. If the chain continues |
| 361 | // after this script, than the CONTINUE_DEFAULT setting will be overridden by lower priority |
| 362 | // scripts return value. |
| 363 | int16_t STDCALL CallInstanceEvent(int id, void *ptr, int event, tOSIRISEventInfo *data) { |
| 364 | return ((aiObjScript *)ptr)->CallEvent(event, data); |
| 365 | } |
| 366 | |
| 367 | // SaveRestoreState |
| 368 | // Purpose: |
| 369 | // This function is called when Descent 3 is saving or restoring the game state. In this function |
| 370 | // you should save/restore any global data that you want preserved through load/save (which includes |
| 371 | // demos). You must be very careful with this function, corrupting the file (reading or writing too |
| 372 | // much or too little) may be hazardous to the game (possibly making it impossible to restore the |
| 373 | // state). It would be best to use version information to keep older versions of saved states still |
| 374 | // able to be used. IT IS VERY IMPORTANT WHEN SAVING THE STATE TO RETURN THE NUMBER OF _BYTES_ WROTE |
| 375 | // TO THE FILE. When restoring the data, the return value is ignored. saving_state is 1 when you should |
| 376 | // write data to the file_ptr, 0 when you should read in the data. |
| 377 | int STDCALL SaveRestoreState(void *file_ptr, uint8_t saving_state) { return 0; } |
| 378 | |
| 379 | static int CreateAndAttach(int me, const char *child_name, uint8_t child_type, char parent_ap, char child_ap, |
| 380 | bool f_aligned = true, bool f_set_parent = false); |
| 381 | static void FlushGoal(int me_handle, int goal_priority); |
| 382 | static void SafeGoalClearAll(int obj_handle); |
| 383 | static void AI_SafeSetType(int obj_handle, int ai_type); |
| 384 | static int TurnOnSpew(int objref, int gunpoint, int effect_type, float mass, float drag, int gravity_type, uint8_t isreal, |
| 385 | float lifetime, float interval, float longevity, float size, float speed, uint8_t random); |
| 386 | |
| 387 | ////////////////////////////////////////////////////////////////////////////// |
| 388 | // HELPER FUNCTIONS |
| 389 | |
| 390 | // Returns the new child's handle |
| 391 | int CreateAndAttach(int me, const char *child_name, uint8_t child_type, char parent_ap, char child_ap, bool f_aligned, |
| 392 | bool f_set_parent) { |
| 393 | int child_handle = OBJECT_HANDLE_NONE; |
| 394 | int child_id = Obj_FindID(child_name); |
| 395 | msafe_struct m; |
| 396 | m.objhandle = me; |
| 397 | MSafe_GetValue(MSAFE_OBJECT_POS, &m); |
| 398 | MSafe_GetValue(MSAFE_OBJECT_ROOMNUM, &m); |
| 399 | |
| 400 | if (child_id >= 0) { |
| 401 | int parent; |
| 402 | if (f_set_parent) |
| 403 | parent = me; |
| 404 | else |
| 405 | parent = OBJECT_HANDLE_NONE; |
| 406 | |
| 407 | child_handle = Obj_Create(child_type, child_id, m.roomnum, &m.pos, NULL, parent); |
| 408 | if (child_handle != OBJECT_HANDLE_NONE) { |
| 409 | if (!Obj_AttachObjectAP(me, parent_ap, child_handle, child_ap, f_aligned)) { |
| 410 | // chrishack (we need a way to instantly kill scripted objects) |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | return child_handle; |
| 416 | } |
| 417 | |
| 418 | void FlushGoal(int me_handle, int goal_priority) { |
| 419 | bool used; |
| 420 | |
| 421 | AI_GoalValue(me_handle, goal_priority, VF_GET, AIGV_B_USED, &used); |
| 422 | if (used) { |
| 423 | int flags; |
| 424 | AI_GoalValue(me_handle, 3, VF_GET, AIGV_I_FLAGS, &flags); |
| 425 | |
| 426 | if (!(flags & GF_NONFLUSHABLE)) { |
| 427 | AI_ClearGoal(me_handle, goal_priority); |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | #define DALLAS_LOW_PRIORITY_GOAL_SLOT 0 |
| 433 | #define DALLAS_HIGH_PRIORITY_GOAL_SLOT 3 |
| 434 | |
| 435 | // Wipes out all goals except slots 0 and 3 (used by Dallas) |
| 436 | void SafeGoalClearAll(int obj_handle) { |
| 437 | int i; |
| 438 | |
| 439 | for (i = 0; i < MAX_GOALS; i++) { |
| 440 | if (i != DALLAS_LOW_PRIORITY_GOAL_SLOT && i != DALLAS_HIGH_PRIORITY_GOAL_SLOT) { |
| 441 | AI_ClearGoal(obj_handle, i); |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | // Works like AI_SetType(), but won't clear out Dallas' goal slots 0 or 3 |
| 447 | void AI_SafeSetType(int obj_handle, int ai_type) { |
| 448 | float aware = AWARE_BARELY; |
| 449 | float time = 25.0f; |
| 450 | |
| 451 | SafeGoalClearAll(obj_handle); |
| 452 | |
| 453 | switch (ai_type) { |
| 454 | case AIT_STALKER: { |
| 455 | vector goal_pos; |
| 456 | int goal_room; |
| 457 | |
| 458 | Obj_Value(obj_handle, VF_GET, OBJV_V_POS, &goal_pos); |
| 459 | Obj_Value(obj_handle, VF_GET, OBJV_I_ROOMNUM, &goal_room); |
| 460 | |
| 461 | AI_AddGoal(obj_handle, AIG_WANDER_AROUND, 1, 1.0f, -1, GF_NONFLUSHABLE | GF_KEEP_AT_COMPLETION, &goal_pos, |
| 462 | goal_room); |
| 463 | } break; |
| 464 | |
| 465 | case AIT_EVADER2: { |
| 466 | vector goal_pos; |
| 467 | int goal_room; |
| 468 | float size, circle_distance; |
| 469 | |
| 470 | Obj_Value(obj_handle, VF_GET, OBJV_V_POS, &goal_pos); |
| 471 | Obj_Value(obj_handle, VF_GET, OBJV_I_ROOMNUM, &goal_room); |
| 472 | Obj_Value(obj_handle, VF_GET, OBJV_F_SIZE, &size); |
| 473 | AI_Value(obj_handle, VF_GET, AIV_F_CIRCLE_DIST, &circle_distance); |
| 474 | |
| 475 | AI_AddGoal(obj_handle, AIG_GUARD_AREA, 1, 1.0f, -1, GF_NONFLUSHABLE, &goal_pos, goal_room); |
| 476 | AI_SetGoalCircleDist(obj_handle, 1, size * 2.0f); |
| 477 | AI_GoalAddEnabler(obj_handle, 1, AIE_GT_AWARENESS, 1.0, 0.0, &aware); |
| 478 | AI_GoalAddEnabler(obj_handle, 1, AIE_LTE_LAST_SEE_TARGET_INTERVAL, 1.0, 0.0, &time); |
| 479 | |
| 480 | int gi = AI_AddGoal(obj_handle, AIG_GET_AROUND_OBJ, ACTIVATION_BLEND_LEVEL, 3.0f, -1, |
| 481 | GF_NONFLUSHABLE | GF_OBJ_IS_TARGET, OBJECT_HANDLE_NONE); |
| 482 | AI_GoalAddEnabler(obj_handle, gi, AIE_NEAR, 1.0, 0.0, &circle_distance); |
| 483 | AI_SetGoalCircleDist(obj_handle, gi, circle_distance); |
| 484 | } break; |
| 485 | |
| 486 | case AIT_EVADER1: { |
| 487 | float circle_distance; |
| 488 | AI_Value(obj_handle, VF_GET, AIV_F_CIRCLE_DIST, &circle_distance); |
| 489 | |
| 490 | AI_AddGoal(obj_handle, AIG_MOVE_RELATIVE_OBJ, 1, 1.0f, -1, GF_OBJ_IS_TARGET | GF_SPEED_ATTACK | GF_NONFLUSHABLE, |
| 491 | OBJECT_HANDLE_NONE); |
| 492 | AI_GoalAddEnabler(obj_handle, 1, AIE_GT_AWARENESS, 1.0, 0.0, &aware); |
| 493 | AI_GoalAddEnabler(obj_handle, 1, AIE_LTE_LAST_SEE_TARGET_INTERVAL, 1.0, 0.0, &time); |
| 494 | |
| 495 | if (circle_distance > 0.0f) { |
| 496 | int gi = AI_AddGoal(obj_handle, AIG_GET_AROUND_OBJ, ACTIVATION_BLEND_LEVEL, 3.0f, -1, |
| 497 | GF_NONFLUSHABLE | GF_OBJ_IS_TARGET, OBJECT_HANDLE_NONE); |
| 498 | |
| 499 | float temp = circle_distance * 0.55f; |
| 500 | AI_GoalAddEnabler(obj_handle, gi, AIE_NEAR, 1.0, 0.0, &temp); |
| 501 | AI_SetGoalCircleDist(obj_handle, gi, circle_distance * 0.55f); |
| 502 | } |
| 503 | } break; |
| 504 | |
| 505 | case AIT_MELEE1: { |
| 506 | int flags = AISR_MELEE; |
| 507 | int m_goal; |
| 508 | |
| 509 | AI_AddGoal(obj_handle, AIG_MOVE_RELATIVE_OBJ, 1, 1.0f, -1, |
| 510 | GF_NONFLUSHABLE | GF_OBJ_IS_TARGET | GF_KEEP_AT_COMPLETION, OBJECT_HANDLE_NONE); |
| 511 | AI_GoalAddEnabler(obj_handle, 1, AIE_GT_AWARENESS, 1.0, 0.0, &aware); |
| 512 | AI_GoalAddEnabler(obj_handle, 1, AIE_LTE_LAST_SEE_TARGET_INTERVAL, 1.0, 0.0, &time); |
| 513 | |
| 514 | AI_AddGoal(obj_handle, AIG_GET_TO_OBJ, 2, 1.0f, -1, |
| 515 | GF_SPEED_ATTACK | GF_NONFLUSHABLE | GF_OBJ_IS_TARGET | GF_USE_BLINE_IF_SEES_GOAL | GF_KEEP_AT_COMPLETION, |
| 516 | OBJECT_HANDLE_NONE); |
| 517 | AI_GoalAddEnabler(obj_handle, 2, AIE_AI_STATUS_FLAG, 1.0, 0.0, &flags); |
| 518 | AI_SetGoalCircleDist(obj_handle, 2, -100.0f); |
| 519 | AI_GoalAddEnabler(obj_handle, 2, AIE_GT_AWARENESS, 1.0, 0.0, &aware); |
| 520 | AI_GoalAddEnabler(obj_handle, 2, AIE_LTE_LAST_SEE_TARGET_INTERVAL, 1.0, 0.0, &time); |
| 521 | |
| 522 | m_goal = AI_AddGoal(obj_handle, AIG_MELEE_TARGET, ACTIVATION_BLEND_LEVEL, 1.0f, -1, |
| 523 | GF_NONFLUSHABLE | GF_OBJ_IS_TARGET | GF_KEEP_AT_COMPLETION, OBJECT_HANDLE_NONE); |
| 524 | AI_GoalAddEnabler(obj_handle, m_goal, AIE_GT_AWARENESS, 1.0, 0.0, &aware); |
| 525 | AI_GoalAddEnabler(obj_handle, m_goal, AIE_LTE_LAST_SEE_TARGET_INTERVAL, 1.0, 0.0, &time); |
| 526 | } break; |
| 527 | |
| 528 | case AIT_BIRD_FLOCK1: { |
| 529 | } break; |
| 530 | |
| 531 | case AIT_HERD1: { |
| 532 | } break; |
| 533 | |
| 534 | case AIT_STATIONARY_TURRET: |
| 535 | break; |
| 536 | |
| 537 | case AIT_AIS: |
| 538 | break; |
| 539 | |
| 540 | case AIT_FLYLANDER: |
| 541 | break; |
| 542 | |
| 543 | default: { |
| 544 | mprintf(0, "Invalid AI type passed into AI_SafeSetType()\n"); |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | int TurnOnSpew(int objref, int gunpoint, int effect_type, float mass, float drag, int gravity_type, uint8_t isreal, |
| 550 | float lifetime, float interval, float longevity, float size, float speed, uint8_t random) { |
| 551 | msafe_struct mstruct; |
| 552 | |
| 553 | mstruct.objhandle = objref; |
| 554 | mstruct.gunpoint = gunpoint; |
| 555 | mstruct.effect_type = effect_type; |
| 556 | mstruct.mass = mass; |
| 557 | mstruct.drag = drag; |
| 558 | mstruct.phys_info = gravity_type; |
| 559 | mstruct.is_real = isreal; |
| 560 | mstruct.lifetime = lifetime; |
| 561 | mstruct.interval = interval; |
| 562 | mstruct.longevity = longevity; |
| 563 | mstruct.size = size; |
| 564 | mstruct.speed = speed; |
| 565 | mstruct.random = (random) ? SPEW_RAND_SIZE | SPEW_RAND_LIFETIME | SPEW_RAND_SPEED : 0; |
| 566 | |
| 567 | MSafe_CallFunction(MSAFE_OBJECT_START_SPEW, &mstruct); |
| 568 | |
| 569 | return mstruct.id; |
| 570 | } |
| 571 | |
| 572 | ////////////////////////////////////////////////////////////////////////////// |
| 573 | // aiObjScript |
| 574 | // all ai scripts will have this as a base function. |
| 575 | int16_t aiObjScript::CallEvent(int event, tOSIRISEventInfo *data) { |
| 576 | switch (event) { |
| 577 | case EVT_AI_INIT: |
| 578 | OnInit(data->me_handle); |
| 579 | break; |
| 580 | case EVT_AI_FRAME: |
| 581 | OnInterval(data); |
| 582 | break; |
| 583 | case EVT_AI_NOTIFY: |
| 584 | if (!OnNotify(data->me_handle, &data->evt_ai_notify)) |
| 585 | return 0; |
| 586 | break; |
| 587 | case EVT_DESTROY: |
| 588 | OnDestroy(data->me_handle, &data->evt_destroy); |
| 589 | break; |
| 590 | case EVT_COLLIDE: |
| 591 | OnCollide(data->me_handle, data->evt_collide.it_handle); |
| 592 | break; |
| 593 | case EVT_DAMAGED: |
| 594 | OnDamaged(data->me_handle, &data->evt_damaged); |
| 595 | break; |
| 596 | case EVT_MEMRESTORE: |
| 597 | OnMemRestore(data->evt_memrestore.memory_ptr); |
| 598 | break; |
| 599 | } |
| 600 | |
| 601 | return CONTINUE_CHAIN | CONTINUE_DEFAULT; |
| 602 | }; |
| 603 | |
| 604 | ////////////////////////////////////////////////////////////////////////////// |
| 605 | // aiSamirPest |
| 606 | // The Samir Pest |
| 607 | |
| 608 | // ai base class |
| 609 | aiSamirPest::aiSamirPest() { memory = NULL; } |
| 610 | |
| 611 | aiSamirPest::~aiSamirPest() {} |
| 612 | |
| 613 | void aiSamirPest::OnInit(int me_handle) { |
| 614 | tOSIRISMEMCHUNK ch; |
| 615 | |
| 616 | mprintf(0, "OnInit\n"); |
| 617 | |
| 618 | ch.id = 4; |
| 619 | ch.size = sizeof(aiSamirPest::t_pest_memory); |
| 620 | ch.my_id.type = OBJECT_SCRIPT; |
| 621 | ch.my_id.objhandle = me_handle; |
| 622 | |
| 623 | memory = (aiSamirPest::t_pest_memory *)Scrpt_MemAlloc(&ch); |
| 624 | |
| 625 | memory->tail_handle = CreateAndAttach(me_handle, "Pesttail", OBJ_ROBOT, 0, 0); |
| 626 | memory->last_anim_frame = 0; |
| 627 | |
| 628 | memory->foot_sounds[0] = Sound_FindId("RbtPestFoot1"); |
| 629 | memory->foot_sounds[1] = Sound_FindId("RbtPestFoot2"); |
| 630 | memory->foot_sounds[2] = Sound_FindId("RbtPestFoot3"); |
| 631 | |
| 632 | memory->state = PEST_STATE_ROAM; |
| 633 | |
| 634 | AI_SetType(me_handle, AIT_EVADER1); |
| 635 | } |
| 636 | |
| 637 | void aiSamirPest::OnDestroy(int me_handle, tOSIRISEVTDESTROY *evt) { |
| 638 | mprintf(0, "OnDestroy\n"); |
| 639 | |
| 640 | msafe_struct mstruct; |
| 641 | |
| 642 | mstruct.objhandle = memory->tail_handle; |
| 643 | mstruct.killer_handle = OBJECT_HANDLE_NONE; |
| 644 | mstruct.damage_type = GD_SCRIPTED; |
| 645 | mstruct.amount = 1000.0f; |
| 646 | |
| 647 | MSafe_CallFunction(MSAFE_OBJECT_DAMAGE_OBJECT, &mstruct); |
| 648 | } |
| 649 | |
| 650 | void aiSamirPest::OnInterval(tOSIRISEventInfo *data) { |
| 651 | float frame; |
| 652 | Obj_Value(data->me_handle, VF_GET, OBJV_F_ANIM_FRAME, &frame); |
| 653 | if ((frame > 9 && memory->last_anim_frame <= 9) || (frame > 10 && memory->last_anim_frame <= 10) || |
| 654 | (frame > 13 && memory->last_anim_frame <= 13)) { |
| 655 | int index = rand() % 3; |
| 656 | |
| 657 | Sound_Play3d(data->me_handle, memory->foot_sounds[index]); |
| 658 | } |
| 659 | |
| 660 | memory->last_anim_frame = frame; |
| 661 | } |
| 662 | |
| 663 | void aiSamirPest::OnCollide(int me_handle, int it_handle) { |
| 664 | int type; |
| 665 | |
| 666 | Obj_Value(it_handle, VF_GET, OBJV_I_TYPE, &type); |
| 667 | |
| 668 | if (type == OBJ_PLAYER && memory->state != PEST_STATE_HIVE) { |
| 669 | // tell pest to return to hive. |
| 670 | int id = Scrpt_FindObjectName("Hive"); |
| 671 | int room_index; |
| 672 | vector pos; |
| 673 | |
| 674 | // force scripting for this. |
| 675 | AI_SetType(me_handle, AIT_AIS); |
| 676 | |
| 677 | // go to hive location! |
| 678 | Obj_Value(id, VF_GET, OBJV_V_POS, &pos); |
| 679 | Obj_Value(id, VF_GET, OBJV_I_ROOMNUM, &room_index); |
| 680 | AI_AddGoal(me_handle, AIG_GET_TO_POS, 1, 1.0, -1, GF_KEEP_AT_COMPLETION | GF_NOTIFIES, &pos, room_index); |
| 681 | AI_SetGoalCircleDist(me_handle, 1, 10.0f); |
| 682 | |
| 683 | memory->state = PEST_STATE_HIVE; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | bool aiSamirPest::OnNotify(int me_handle, tOSIRISEVTAINOTIFY *data) { return true; } |
| 688 | |
| 689 | ////////////////////////////////////////////////////////////////////////////// |
| 690 | // aiBlackStormTrooper |
| 691 | // CED Black Stormtrooper |
| 692 | |
| 693 | // ai base class |
| 694 | aiBlackStormTrooper::aiBlackStormTrooper() { memory = NULL; } |
| 695 | |
| 696 | aiBlackStormTrooper::~aiBlackStormTrooper() {} |
| 697 | |
| 698 | void aiBlackStormTrooper::OnInit(int me_handle) { |
| 699 | tOSIRISMEMCHUNK ch; |
| 700 | int n_pts, j, i; |
| 701 | |
| 702 | ch.id = 4; |
| 703 | ch.size = sizeof(aiBlackStormTrooper::t_bst_memory); |
| 704 | ch.my_id.type = OBJECT_SCRIPT; |
| 705 | ch.my_id.objhandle = me_handle; |
| 706 | |
| 707 | memory = (aiBlackStormTrooper::t_bst_memory *)Scrpt_MemAlloc(&ch); |
| 708 | |
| 709 | // reset timer for robot. |
| 710 | memory->timer = 0; |
| 711 | |
| 712 | for (i = 0; i < N_SNIPE_POINTS; i++) { |
| 713 | memory->snipe_target_handles[i] = OBJECT_HANDLE_NONE; |
| 714 | } |
| 715 | |
| 716 | // get sniper points available for trooper. |
| 717 | memory->cur_snipe_point = -1; |
| 718 | for (memory->snipe_points = 0; memory->snipe_points < N_SNIPE_POINTS; memory->snipe_points++) { |
| 719 | char buf[64]; |
| 720 | char name[48]; |
| 721 | int id; |
| 722 | Obj_Value(me_handle, VF_GET, OBJV_S_NAME, name); |
| 723 | snprintf(buf, sizeof(buf), "Snipe%s-%c", name, 'A' + memory->snipe_points); |
| 724 | id = Scrpt_FindObjectName(buf); |
| 725 | if (id == OBJECT_HANDLE_NONE) { |
| 726 | break; |
| 727 | } |
| 728 | memory->snipe_point_handles[memory->snipe_points] = id; |
| 729 | } |
| 730 | |
| 731 | // determine reachable snipe points for each snipe point! |
| 732 | for (i = 0; i < memory->snipe_points; i++) { |
| 733 | for (j = 0, n_pts = 0; j < memory->snipe_points; j++) { |
| 734 | if (i != j) { |
| 735 | if (AI_IsObjReachable(memory->snipe_point_handles[i], memory->snipe_point_handles[j])) { |
| 736 | memory->access_array[i][n_pts++] = (int8_t)j; |
| 737 | } |
| 738 | } |
| 739 | } |
| 740 | memory->access_array[i][N_SNIPE_POINTS - 1] = (int8_t)n_pts; |
| 741 | } |
| 742 | |
| 743 | // initialize miscellaneous stuff |
| 744 | memory->backpack_hp = 200; |
| 745 | memory->flee_hp = 50; |
| 746 | memory->gun_sight_obj = CreateAndAttach(me_handle, "STEmitter", OBJ_ROBOT, 0, 0, true, true); |
| 747 | memory->ammo_barrel = CreateAndAttach(me_handle, "StormTrooperBlackBarrel", OBJ_ROBOT, 1, 0, true, true); |
| 748 | memory->camera_obj = OBJECT_HANDLE_NONE; |
| 749 | |
| 750 | memory->last_frame = 0.0f; |
| 751 | memory->target_handle = OBJECT_HANDLE_NONE; |
| 752 | memory->laser_targeted = false; |
| 753 | |
| 754 | // just wait for a target. |
| 755 | set_state(me_handle, STATE_INIT); |
| 756 | } |
| 757 | |
| 758 | void aiBlackStormTrooper::find_targets(int me_handle, bool new_snipe_point) { |
| 759 | vector pos; // position of trooper |
| 760 | int room; // room of trooper |
| 761 | |
| 762 | int nearby_objs[32]; // objhandles for nearby players (or robots) |
| 763 | int player_objs[8]; // nearby players. |
| 764 | int nobjs, np; // number of objects and players |
| 765 | int closest_player = OBJECT_HANDLE_NONE; // handle of closest player |
| 766 | float closest_player_dist = 10000.0f; // distance of closest player from trooper. |
| 767 | float current_player_dist = 10000.0f; // used to store distance of current targeted player |
| 768 | int i, j; |
| 769 | |
| 770 | // now determine target, closest player? |
| 771 | // get nearby objects within 1000 units of trooper. our substitution for FOV check, since troopers |
| 772 | // are usually behind walls. |
| 773 | Obj_Value(me_handle, VF_GET, OBJV_V_POS, &pos); |
| 774 | Obj_Value(me_handle, VF_GET, OBJV_I_ROOMNUM, &room); |
| 775 | |
| 776 | nobjs = AI_GetNearbyObjs(&pos, room, 500.0f, nearby_objs, 32, |
| 777 | false, // lightmaps, no. |
| 778 | true, // players and robots |
| 779 | false, // don't include non collide objects |
| 780 | true); // stop at doors, don't check behind them. |
| 781 | |
| 782 | // get only players. |
| 783 | for (i = 0, np = 0; i < nobjs && np < 8; i++) { |
| 784 | int objtype = -1; |
| 785 | |
| 786 | Obj_Value(nearby_objs[i], VF_GET, OBJV_I_TYPE, &objtype); |
| 787 | if (objtype == OBJ_PLAYER) { |
| 788 | // if a player, check if it's in the target list |
| 789 | // (the trooper must have seen the target at least once before to keep track of it) |
| 790 | for (j = 0; j < N_SNIPE_POINTS; j++) { |
| 791 | if (nearby_objs[i] == memory->snipe_target_handles[j]) { |
| 792 | float dist; |
| 793 | player_objs[np++] = nearby_objs[i]; |
| 794 | dist = AI_GetDistToObj(me_handle, nearby_objs[i]); |
| 795 | if (memory->target_handle == nearby_objs[i]) { |
| 796 | current_player_dist = dist; |
| 797 | } |
| 798 | if (closest_player_dist > dist) { |
| 799 | closest_player_dist = dist; |
| 800 | closest_player = nearby_objs[i]; |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | //// lost all targets, go back into idle state. |
| 808 | if (closest_player == OBJECT_HANDLE_NONE) { |
| 809 | memory->target_handle = OBJECT_HANDLE_NONE; |
| 810 | return; |
| 811 | } |
| 812 | |
| 813 | // if we don't have a target yet, make this player a target. |
| 814 | if (np && (memory->target_handle == OBJECT_HANDLE_NONE || |
| 815 | (current_player_dist > closest_player_dist && memory->target_handle != closest_player))) { |
| 816 | memory->target_handle = closest_player; |
| 817 | AI_Value(me_handle, VF_SET, AIV_I_TARGET_HANDLE, &memory->target_handle); |
| 818 | mprintf(0, "BStorm: acquired new target through clairvoyance.\n"); |
| 819 | } |
| 820 | |
| 821 | // do something with snipe point if new_snipe_point is true (like find an ideal one) |
| 822 | } |
| 823 | |
| 824 | void aiBlackStormTrooper::OnDestroy(int me_handle, tOSIRISEVTDESTROY *evt) { |
| 825 | msafe_struct mstruct; |
| 826 | |
| 827 | mstruct.objhandle = memory->ammo_barrel; |
| 828 | mstruct.killer_handle = OBJECT_HANDLE_NONE; |
| 829 | mstruct.damage_type = GD_SCRIPTED; |
| 830 | mstruct.amount = 1000.0f; |
| 831 | |
| 832 | MSafe_CallFunction(MSAFE_OBJECT_DAMAGE_OBJECT, &mstruct); |
| 833 | |
| 834 | if (memory->camera_obj != OBJECT_HANDLE_NONE) { |
| 835 | mstruct.objhandle = memory->camera_obj; |
| 836 | MSafe_CallFunction(MSAFE_OBJECT_REMOVE, &mstruct); |
| 837 | } |
| 838 | |
| 839 | mstruct.objhandle = memory->gun_sight_obj; |
| 840 | MSafe_CallFunction(MSAFE_OBJECT_REMOVE, &mstruct); |
| 841 | } |
| 842 | |
| 843 | void aiBlackStormTrooper::OnInterval(tOSIRISEventInfo *data) { |
| 844 | msafe_struct mstruct; |
| 845 | int me_handle = data->me_handle; |
| 846 | float anim_frame; |
| 847 | vector pos; |
| 848 | int room; |
| 849 | |
| 850 | Obj_Value(me_handle, VF_GET, OBJV_F_ANIM_FRAME, &anim_frame); |
| 851 | Obj_Value(me_handle, VF_GET, OBJV_I_ROOMNUM, &room); |
| 852 | Obj_Value(me_handle, VF_GET, OBJV_V_POS, &pos); |
| 853 | |
| 854 | // laser gunsight |
| 855 | if (memory->laser_targeted) { |
| 856 | ray_info ray; |
| 857 | matrix orient; |
| 858 | |
| 859 | Obj_Value(me_handle, VF_GET, OBJV_M_ORIENT, &orient); |
| 860 | |
| 861 | // Determine real start pos - room |
| 862 | vector end_pos = pos; |
| 863 | end_pos += orient.fvec * 2000.0f; |
| 864 | |
| 865 | int fvi_flags = FQ_CHECK_OBJS | FQ_IGNORE_POWERUPS | FQ_IGNORE_WEAPONS | FQ_IGNORE_MOVING_OBJECTS | |
| 866 | FQ_IGNORE_NON_LIGHTMAP_OBJECTS; |
| 867 | int fate = FVI_RayCast(me_handle, &pos, &end_pos, room, 0.0f, fvi_flags, &ray); |
| 868 | |
| 869 | Obj_Value(memory->camera_obj, VF_SET, OBJV_I_ROOMNUM, &ray.hit_room); |
| 870 | Obj_Value(memory->camera_obj, VF_SET, OBJV_V_POS, &ray.hit_point); |
| 871 | |
| 872 | if (Game_GetTime() >= memory->laser_timer) { |
| 873 | mstruct.objhandle = memory->camera_obj; |
| 874 | MSafe_CallFunction(MSAFE_OBJECT_REMOVE, &mstruct); |
| 875 | |
| 876 | memory->camera_obj = OBJECT_HANDLE_NONE; |
| 877 | memory->laser_targeted = false; |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | // check if the pack is gone |
| 882 | int attached_handle = Obj_GetAttachChildHandle(me_handle, 1); |
| 883 | if (attached_handle == OBJECT_HANDLE_NONE) { |
| 884 | aObjDestroy(me_handle); |
| 885 | return; |
| 886 | } |
| 887 | |
| 888 | // state code. |
| 889 | switch (memory->state) { |
| 890 | case STATE_HIDE: |
| 891 | // we didn't find a valid snipe point? try to find one after a certain amount of time. |
| 892 | // this will ensure that every so often, if there is no valid snipe point specified, |
| 893 | // we will try to find one. |
| 894 | if (memory->snipe_points == 0) { |
| 895 | if (Game_GetTime() > memory->timer) { |
| 896 | set_state(data->me_handle, STATE_SNIPE); |
| 897 | } |
| 898 | } else if (memory->cur_snipe_point == -1) { |
| 899 | if (Game_GetTime() > memory->timer) { |
| 900 | set_state(data->me_handle, STATE_HIDE); |
| 901 | } |
| 902 | } |
| 903 | break; |
| 904 | |
| 905 | case STATE_SNIPE: |
| 906 | // snipe for a certain amount of time, then return to base. |
| 907 | if (memory->laser_targeted == false) { |
| 908 | int type; |
| 909 | |
| 910 | memory->camera_obj = Obj_Create(OBJ_POWERUP, Obj_FindID("Invisiblepowerup"), room, &pos, NULL, me_handle); |
| 911 | mstruct.objhandle = memory->camera_obj; |
| 912 | MSafe_CallFunction(MSAFE_OBJECT_NO_RENDER, &mstruct); |
| 913 | |
| 914 | Obj_Value(me_handle, VF_GET, OBJV_I_ROOMNUM, &mstruct.roomnum); |
| 915 | Obj_Value(me_handle, VF_GET, OBJV_V_POS, &mstruct.pos); |
| 916 | Obj_Value(memory->camera_obj, VF_GET, OBJV_V_POS, &mstruct.pos2); |
| 917 | |
| 918 | mstruct.objhandle = memory->gun_sight_obj; |
| 919 | mstruct.ithandle = memory->camera_obj; |
| 920 | mstruct.lifetime = 5.0f; |
| 921 | mstruct.size = 1.0f; |
| 922 | |
| 923 | mstruct.interval = 1.0f; |
| 924 | mstruct.count = 2; |
| 925 | mstruct.index = 1; |
| 926 | mstruct.texnum = Scrpt_FindTextureName("Stlaser"); |
| 927 | mstruct.color = ((128 >> 3) << 10) | ((128 >> 3) << 5) | (128 >> 3); |
| 928 | |
| 929 | mstruct.state = 0; |
| 930 | mstruct.flags = 0; |
| 931 | |
| 932 | memory->laser_timer = Game_GetTime() + mstruct.lifetime; |
| 933 | memory->laser_targeted = true; |
| 934 | |
| 935 | MSafe_CallFunction(MSAFE_WEATHER_LIGHTNING_BOLT, &mstruct); |
| 936 | // mprintf(0, "BStorm: TARGETING!!!\n"); |
| 937 | } |
| 938 | |
| 939 | if (Game_GetTime() > memory->timer) { |
| 940 | set_state(data->me_handle, STATE_HIDE); |
| 941 | } |
| 942 | break; |
| 943 | |
| 944 | case STATE_BASE: |
| 945 | if (Game_GetTime() > memory->timer) { |
| 946 | if (memory->target_handle == OBJECT_HANDLE_NONE) { |
| 947 | set_state(data->me_handle, STATE_IDLE); |
| 948 | } else { |
| 949 | int roll = (rand() % 100); |
| 950 | if (roll <= 50) { |
| 951 | find_targets(data->me_handle, false); |
| 952 | memory->timer = Game_GetTime() + ((memory->target_handle == OBJECT_HANDLE_NONE) ? 5.0f : 2.0f); |
| 953 | } |
| 954 | } |
| 955 | } |
| 956 | if (Game_GetTime() > memory->snipe_timer) { |
| 957 | set_state(data->me_handle, STATE_SNIPE); |
| 958 | memory->snipe_timer = Game_GetTime() + 3.0f + (rand() % 7) * 0.5f; |
| 959 | } |
| 960 | break; |
| 961 | } |
| 962 | |
| 963 | memory->last_frame = anim_frame; |
| 964 | } |
| 965 | |
| 966 | void aiBlackStormTrooper::OnDamaged(int me_handle, tOSIRISEVTDAMAGED *evt) { |
| 967 | if (memory->state == STATE_BASE || memory->state == STATE_SNIPE) { |
| 968 | memory->flee_hp -= (int16_t)evt->damage; |
| 969 | if (memory->flee_hp <= 0) { |
| 970 | memory->flee_hp = 50; |
| 971 | set_state(me_handle, STATE_HIDE); |
| 972 | } |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | bool aiBlackStormTrooper::OnNotify(int me_handle, tOSIRISEVTAINOTIFY *data) { |
| 977 | switch (data->notify_type) { |
| 978 | case AIN_FIRED_WEAPON: |
| 979 | // Create gun barrel smoke |
| 980 | TurnOnSpew(me_handle, 0, 7, 0.0f, 0.0f, 65536, false, 0.5f, 0.1f, 0.7f, 1.0f, 15.0f, true); |
| 981 | break; |
| 982 | |
| 983 | case AIN_SEE_TARGET: |
| 984 | memory->snipe_target_handles[0] = data->it_handle; |
| 985 | // mprintf(0, "BStorm: See target!!\n"); |
| 986 | if (memory->state == STATE_IDLE) { |
| 987 | // mprintf(0, "BStorm: Found target!!\n"); |
| 988 | memory->target_handle = memory->snipe_target_handles[0]; |
| 989 | set_state(me_handle, STATE_HIDE); |
| 990 | |
| 991 | mprintf(0, "Black Stormtrooper Saw target!\n"); |
| 992 | } |
| 993 | break; |
| 994 | |
| 995 | //@@ case AIN_SEE_TARGET: |
| 996 | //@@ // snipe!! |
| 997 | //@@ if (memory->state == STATE_IDLE) { |
| 998 | //@@ mprintf(0, "Acquired!\n"); |
| 999 | //@@ set_state(me_handle, STATE_HIDE); |
| 1000 | //@@ } |
| 1001 | //@@ else if (memory->state == STATE_BASE && memory->timer < Game_GetTime()) { |
| 1002 | //@@ if ((rand() % 100) < 60) { |
| 1003 | //@@ set_state(me_handle, STATE_SNIPE); |
| 1004 | //@@ } |
| 1005 | //@@ memory->timer = Game_GetTime() + 2.0f + (rand() % 6)*0.5f; |
| 1006 | //// use timer to switch states. |
| 1007 | //@@ } |
| 1008 | break; |
| 1009 | |
| 1010 | case AIN_NEAR_TARGET: |
| 1011 | if (memory->state == STATE_BASE) { |
| 1012 | // mprintf(0, "BStorm: Near target, must flee.\n"); |
| 1013 | set_state(me_handle, STATE_HIDE); |
| 1014 | } |
| 1015 | break; |
| 1016 | |
| 1017 | case AIN_GOAL_COMPLETE: |
| 1018 | if (data->goal_uid == GOAL_UID_GETTOPOS) { |
| 1019 | // mprintf(0, "BStorm: At hiding position %d.\n", memory->cur_snipe_point); |
| 1020 | set_state(me_handle, STATE_BASE); |
| 1021 | } |
| 1022 | break; |
| 1023 | |
| 1024 | case AIN_HIT_BY_WEAPON: |
| 1025 | // check if backpack hit. if so, deduct hitpoints, if 0, frag it. |
| 1026 | // also, if hit enough, force another hide action. |
| 1027 | break; |
| 1028 | } |
| 1029 | |
| 1030 | return true; |
| 1031 | } |
| 1032 | |
| 1033 | void aiBlackStormTrooper::set_state(int me_handle, int state) { |
| 1034 | int room_number, id, i; |
| 1035 | vector pos; |
| 1036 | char buf[64]; |
| 1037 | char name[48]; |
| 1038 | float dist; |
| 1039 | |
| 1040 | // SafeGoalClearAll(me_handle); |
| 1041 | |
| 1042 | retry_set_state: |
| 1043 | switch (state) { |
| 1044 | case STATE_INIT: |
| 1045 | mprintf(0, "Black Stormtrooper set to init mode.\n"); |
| 1046 | state = STATE_IDLE; |
| 1047 | goto retry_set_state; |
| 1048 | |
| 1049 | case STATE_IDLE: |
| 1050 | mprintf(0, "Black Stormtrooper set to idle mode.\n"); |
| 1051 | |
| 1052 | // AI_SafeSetType(me_handle, AIT_EVADER2); |
| 1053 | ///* |
| 1054 | FlushGoal(me_handle, 1); |
| 1055 | FlushGoal(me_handle, 2); |
| 1056 | Obj_Value(me_handle, VF_GET, OBJV_V_POS, &pos); |
| 1057 | Obj_Value(me_handle, VF_GET, OBJV_I_ROOMNUM, &room_number); |
| 1058 | |
| 1059 | dist = 10.0f; |
| 1060 | AI_AddGoal(me_handle, AIG_GUARD_AREA, 0, 1.0f, -1, GF_NOTIFIES | GF_KEEP_AT_COMPLETION, &pos, room_number); |
| 1061 | AI_GoalValue(me_handle, 0, VF_SET, AIGV_F_CIRCLE_DIST, &dist); |
| 1062 | //*/ |
| 1063 | |
| 1064 | // mprintf(0, "BStorm: Idling around.\n"); |
| 1065 | break; |
| 1066 | |
| 1067 | case STATE_BASE: |
| 1068 | mprintf(0, "Black Stormtrooper set to base mode.\n"); |
| 1069 | |
| 1070 | // AI_SafeSetType(me_handle, AIT_EVADER2); |
| 1071 | ///* |
| 1072 | // okay, we will stand still, wait to snipe, face target, try to acquire targets, etc. |
| 1073 | FlushGoal(me_handle, 1); |
| 1074 | FlushGoal(me_handle, 2); |
| 1075 | if (memory->cur_snipe_point == -1) { |
| 1076 | // mprintf(0, "BStorm: Eek! We should be hiding, not basing!"); |
| 1077 | state = STATE_HIDE; |
| 1078 | goto retry_set_state; |
| 1079 | } |
| 1080 | id = memory->snipe_point_handles[memory->cur_snipe_point]; |
| 1081 | Obj_Value(id, VF_GET, OBJV_V_POS, &pos); |
| 1082 | Obj_Value(id, VF_GET, OBJV_I_ROOMNUM, &room_number); |
| 1083 | |
| 1084 | dist = 0.0f; |
| 1085 | AI_AddGoal(me_handle, AIG_GUARD_AREA, 2, 1.0f, -1, GF_KEEP_AT_COMPLETION, &pos, room_number); |
| 1086 | AI_GoalValue(me_handle, 2, VF_SET, AIGV_F_CIRCLE_DIST, &dist); |
| 1087 | //*/ |
| 1088 | |
| 1089 | /* |
| 1090 | AI_AddGoal(me_handle, AIG_FIRE_AT_OBJ, ACTIVATION_BLEND_LEVEL, 1.0f, -1, GF_ORIENT_TARGET | |
| 1091 | GF_SPEED_ATTACK | |
| 1092 | GF_KEEP_AT_COMPLETION | |
| 1093 | GF_OBJ_IS_TARGET, |
| 1094 | NULL, GST_NEG_FVEC); |
| 1095 | */ |
| 1096 | |
| 1097 | memory->timer = Game_GetTime() + 2.0f; // use timer to reacquire targets. |
| 1098 | memory->snipe_timer = Game_GetTime() + 2.0f + (rand() % 7) * 0.5f; |
| 1099 | |
| 1100 | find_targets(me_handle, false); |
| 1101 | // mprintf(0, "BStorm: Base\n"); |
| 1102 | break; |
| 1103 | |
| 1104 | case STATE_HIDE: |
| 1105 | mprintf(0, "Black Stormtrooper set to hide mode.\n"); |
| 1106 | // find a snipe point that's different from current one. |
| 1107 | FlushGoal(me_handle, 1); |
| 1108 | FlushGoal(me_handle, 2); |
| 1109 | if (memory->snipe_points) { |
| 1110 | if (memory->cur_snipe_point == -1) { |
| 1111 | // find a snipe point now! |
| 1112 | // bot is either fresh or has been wandering, looking for a place to snipe |
| 1113 | for (i = 0; i < memory->snipe_points; i++) { |
| 1114 | if (AI_IsObjReachable(me_handle, memory->snipe_point_handles[i])) { |
| 1115 | break; |
| 1116 | } |
| 1117 | } |
| 1118 | if (i == memory->snipe_points) { |
| 1119 | // hmm, the bot is not in a good place. have him wander until he finds a snipe point. |
| 1120 | memory->timer = Game_GetTime() + 4.0f; |
| 1121 | Obj_Value(me_handle, VF_GET, OBJV_V_POS, &pos); |
| 1122 | Obj_Value(me_handle, VF_GET, OBJV_I_ROOMNUM, &room_number); |
| 1123 | AI_AddGoal(me_handle, AIG_WANDER_AROUND, 1, 1.0f, -1, GF_SPEED_FLEE | GF_NOTIFIES | GF_KEEP_AT_COMPLETION, |
| 1124 | &pos, room_number); |
| 1125 | mprintf(0, "BStorm: WARNING-0 no valid snipe point nearby. Design problem?\n"); |
| 1126 | } else { |
| 1127 | memory->cur_snipe_point = i; |
| 1128 | } |
| 1129 | } else { |
| 1130 | // this will get an index into the reachable snipe points. |
| 1131 | int n_valid_pts = memory->access_array[memory->cur_snipe_point][N_SNIPE_POINTS - 1]; |
| 1132 | |
| 1133 | if (n_valid_pts > 0) { |
| 1134 | i = rand() % n_valid_pts; |
| 1135 | memory->cur_snipe_point = memory->access_array[memory->cur_snipe_point][i]; |
| 1136 | } else { |
| 1137 | memory->cur_snipe_point = -1; // force a periodic check for a snipe point. |
| 1138 | memory->timer = Game_GetTime() + 4.0f; |
| 1139 | mprintf(0, "BStorm: WARNING-1 no valid snipe point nearby. Design problem?\n"); |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | // by now, we either have a new valid snipe point, or no snipe point. |
| 1144 | if (memory->cur_snipe_point != -1) { |
| 1145 | id = memory->snipe_point_handles[memory->cur_snipe_point]; |
| 1146 | Obj_Value(id, VF_GET, OBJV_V_POS, &pos); |
| 1147 | Obj_Value(id, VF_GET, OBJV_I_ROOMNUM, &room_number); |
| 1148 | AI_AddGoal(me_handle, AIG_GET_TO_POS, 2, 1.0, GOAL_UID_GETTOPOS, GF_NOTIFIES | GF_SPEED_FLEE | GF_NONFLUSHABLE, |
| 1149 | &pos, room_number); |
| 1150 | /* |
| 1151 | AI_AddGoal(me_handle, AIG_FIRE_AT_OBJ, ACTIVATION_BLEND_LEVEL, 1.0f, -1, GF_ORIENT_TARGET | |
| 1152 | GF_SPEED_ATTACK | |
| 1153 | GF_KEEP_AT_COMPLETION | |
| 1154 | GF_OBJ_IS_TARGET, |
| 1155 | NULL, GST_NEG_FVEC); |
| 1156 | */ |
| 1157 | |
| 1158 | // mprintf(0, "BStorm: Hide to point %d\n", memory->cur_snipe_point+1); |
| 1159 | } |
| 1160 | } else { |
| 1161 | // bot will track you with no snipe points.. |
| 1162 | memory->timer = Game_GetTime() + 3.0f; |
| 1163 | dist = 80.0f + ((float)rand() / (float)RAND_MAX) * 10.0f; |
| 1164 | AI_AddGoal(me_handle, AIG_MOVE_RELATIVE_OBJ, 1, 1.0f, -1, GF_OBJ_IS_TARGET | GF_KEEP_AT_COMPLETION, |
| 1165 | OBJECT_HANDLE_NONE); |
| 1166 | AI_GoalValue(me_handle, 1, VF_SET, AIGV_F_CIRCLE_DIST, &dist); |
| 1167 | } |
| 1168 | break; |
| 1169 | |
| 1170 | case STATE_SNIPE: |
| 1171 | mprintf(0, "Black Stormtrooper set to snipe mode.\n"); |
| 1172 | |
| 1173 | // AI_SafeSetType(me_handle, AIT_EVADER1); |
| 1174 | ///* |
| 1175 | FlushGoal(me_handle, 2); |
| 1176 | |
| 1177 | // dist = 80.0f + ((float)rand()/(float)RAND_MAX)*10.0f; |
| 1178 | AI_AddGoal(me_handle, AIG_MOVE_RELATIVE_OBJ, 1, 1.0f, -1, GF_OBJ_IS_TARGET | GF_KEEP_AT_COMPLETION, |
| 1179 | OBJECT_HANDLE_NONE); |
| 1180 | // AI_GoalValue(me_handle, 1, VF_SET, AIGV_F_CIRCLE_DIST, &dist); |
| 1181 | //*/ |
| 1182 | |
| 1183 | /* |
| 1184 | AI_AddGoal(me_handle, AIG_FIRE_AT_OBJ, ACTIVATION_BLEND_LEVEL, 1.0f, -1, GF_ORIENT_TARGET | |
| 1185 | GF_SPEED_ATTACK | |
| 1186 | GF_KEEP_AT_COMPLETION | |
| 1187 | GF_OBJ_IS_TARGET, |
| 1188 | NULL, GST_NEG_FVEC); |
| 1189 | */ |
| 1190 | |
| 1191 | memory->timer = Game_GetTime() + 8.0f; |
| 1192 | // mprintf(0, "BStorm: Snipe!\n"); |
| 1193 | break; |
| 1194 | } |
| 1195 | |
| 1196 | memory->state = state; |
| 1197 | } |
| 1198 | |
| 1199 | ////////////////////////////////////////////////////////////////////////////// |
| 1200 | // aiCreeper |
| 1201 | |
| 1202 | // ai base class |
| 1203 | aiCreeper::aiCreeper() { memory = NULL; } |
| 1204 | |
| 1205 | aiCreeper::~aiCreeper() {} |
| 1206 | |
| 1207 | void aiCreeper::OnInit(int me_handle) { |
| 1208 | tOSIRISMEMCHUNK ch; |
| 1209 | int flags; |
| 1210 | |
| 1211 | ch.id = 4; |
| 1212 | ch.size = sizeof(aiCreeper::t_creep_memory); |
| 1213 | ch.my_id.type = OBJECT_SCRIPT; |
| 1214 | ch.my_id.objhandle = me_handle; |
| 1215 | |
| 1216 | memory = (aiCreeper::t_creep_memory *)Scrpt_MemAlloc(&ch); |
| 1217 | |
| 1218 | // reset timer for robot. |
| 1219 | memory->timer = 0; |
| 1220 | |
| 1221 | // initialize the creeper eye |
| 1222 | memory->eye_obj = CreateAndAttach(me_handle, "CreeperEye", OBJ_ROBOT, 0, 0); |
| 1223 | memory->eye_timer = 0.0f; |
| 1224 | |
| 1225 | // get sniper points available for trooper. n |
| 1226 | memory->state = -1; |
| 1227 | memory->energy = 10; |
| 1228 | memory->hits = 50; |
| 1229 | |
| 1230 | AI_Value(me_handle, VF_GET, AIV_I_FLAGS, &flags); |
| 1231 | memory->melee_flags = (uint8_t)(flags & (AIF_MELEE1 | AIF_MELEE2)); |
| 1232 | |
| 1233 | // set initial state of creeper |
| 1234 | set_state(me_handle, STATE_IDLE); |
| 1235 | } |
| 1236 | |
| 1237 | void aiCreeper::OnDamaged(int me_handle, tOSIRISEVTDAMAGED *evt) { |
| 1238 | if (memory->state == STATE_ATTACK) { |
| 1239 | if (memory->hits > -25) { |
| 1240 | memory->hits = memory->hits - (int8_t)evt->damage; |
| 1241 | // mprintf(0, "[Creeper] hits = %d\n", memory->hits); |
| 1242 | } |
| 1243 | if (memory->hits < 0) { |
| 1244 | set_state(me_handle, STATE_STALK); |
| 1245 | } |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | void aiCreeper::OnDestroy(int me_handle, tOSIRISEVTDESTROY *evt) { |
| 1250 | msafe_struct mstruct; |
| 1251 | |
| 1252 | mstruct.objhandle = memory->eye_obj; |
| 1253 | mstruct.killer_handle = OBJECT_HANDLE_NONE; |
| 1254 | mstruct.damage_type = GD_SCRIPTED; |
| 1255 | mstruct.amount = 1000.0f; |
| 1256 | |
| 1257 | MSafe_CallFunction(MSAFE_OBJECT_DAMAGE_OBJECT, &mstruct); |
| 1258 | } |
| 1259 | |
| 1260 | void aiCreeper::OnInterval(tOSIRISEventInfo *data) { |
| 1261 | int flags; |
| 1262 | |
| 1263 | switch (memory->state) { |
| 1264 | case STATE_IDLE: |
| 1265 | case STATE_STALK: |
| 1266 | if (memory->timer < Game_GetTime()) { |
| 1267 | if (memory->energy < 10) { |
| 1268 | memory->energy += (memory->state == STATE_IDLE) ? 2 : 1; |
| 1269 | memory->timer = Game_GetTime() + 1.0f; |
| 1270 | if (memory->energy > 10) { |
| 1271 | memory->energy = 10; |
| 1272 | } |
| 1273 | } |
| 1274 | if (memory->hits < 25) { |
| 1275 | memory->hits += (memory->state == STATE_IDLE) ? 10 : 7; |
| 1276 | } |
| 1277 | |
| 1278 | if (memory->state == STATE_STALK && memory->eye_timer != 0.0f) { |
| 1279 | // now cloak rest of body 1 sec after eye cloaked. |
| 1280 | msafe_struct m; |
| 1281 | m.objhandle = data->me_handle; |
| 1282 | m.lifetime = memory->eye_timer; |
| 1283 | m.state = 1; |
| 1284 | |
| 1285 | memory->eye_timer = 0.0f; |
| 1286 | |
| 1287 | MSafe_CallFunction(MSAFE_OBJECT_CLOAK, &m); |
| 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | if (memory->state == STATE_STALK && memory->eye_timer == 0.0f) { |
| 1292 | if (!Obj_IsEffect(data->me_handle, EF_FADING_OUT | EF_CLOAKED)) { |
| 1293 | set_state(data->me_handle, STATE_ATTACK); |
| 1294 | } |
| 1295 | } |
| 1296 | break; |
| 1297 | |
| 1298 | case STATE_ATTACK: |
| 1299 | if (memory->timer < Game_GetTime()) { |
| 1300 | // recloak and recharge. |
| 1301 | set_state(data->me_handle, STATE_STALK); |
| 1302 | } else { |
| 1303 | flags = AIF_MELEE2; |
| 1304 | if (memory->energy <= 5 && (memory->melee_flags & AIF_MELEE2)) { |
| 1305 | AI_Value(data->me_handle, VF_CLEAR_FLAGS, AIV_I_FLAGS, &flags); |
| 1306 | memory->melee_flags &= (~AIF_MELEE2); |
| 1307 | } else if (memory->energy > 5 && !(memory->melee_flags & AIF_MELEE2)) { |
| 1308 | AI_Value(data->me_handle, VF_SET_FLAGS, AIV_I_FLAGS, &flags); |
| 1309 | memory->melee_flags |= AIF_MELEE2; |
| 1310 | } |
| 1311 | } |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | bool aiCreeper::OnNotify(int me_handle, tOSIRISEVTAINOTIFY *data) { |
| 1316 | vector pos; |
| 1317 | int room_number; |
| 1318 | |
| 1319 | switch (data->notify_type) { |
| 1320 | case AIN_SEE_TARGET: |
| 1321 | case AIN_NEAR_TARGET: |
| 1322 | // rules: leave's base state if recharged enough-->cloaks |
| 1323 | if (memory->state == STATE_IDLE) { |
| 1324 | set_state(me_handle, STATE_STALK); |
| 1325 | } |
| 1326 | // mprintf(0, "[Creeper] see notify %d\n", data->notify_type); |
| 1327 | break; |
| 1328 | |
| 1329 | case AIN_GOAL_COMPLETE: |
| 1330 | case AIN_PLAYER_SEES_YOU: |
| 1331 | case AIN_ANIM_COMPLETE: |
| 1332 | case AIN_BUMPED_OBJ: |
| 1333 | break; |
| 1334 | |
| 1335 | case AIN_MELEE_ATTACK_FRAME: |
| 1336 | // reduce energy accordingly depending on attack. |
| 1337 | if (data->attack_num == 1) { |
| 1338 | // blast ring attack |
| 1339 | int wpnid; |
| 1340 | |
| 1341 | Obj_Value(me_handle, VF_GET, OBJV_I_ROOMNUM, &room_number); |
| 1342 | Obj_GetGunPos(me_handle, 0, &pos); |
| 1343 | |
| 1344 | wpnid = Wpn_FindID("CreeperBlastRing"); |
| 1345 | Obj_Create(OBJ_WEAPON, wpnid, room_number, &pos, NULL, me_handle); |
| 1346 | |
| 1347 | memory->energy -= 5; |
| 1348 | // mprintf(0, "[Creeper] Special attack.\n"); |
| 1349 | } else { |
| 1350 | // standard attack |
| 1351 | memory->energy -= 3; |
| 1352 | } |
| 1353 | |
| 1354 | if (memory->energy < 3) { |
| 1355 | set_state(me_handle, STATE_STALK); |
| 1356 | } |
| 1357 | break; |
| 1358 | |
| 1359 | // default: |
| 1360 | // mprintf(0, "[Creeper] notify %d\n", data->notify_type); |
| 1361 | } |
| 1362 | |
| 1363 | return true; |
| 1364 | } |
| 1365 | |
| 1366 | void aiCreeper::set_state(int me_handle, int state) { |
| 1367 | int room_number = 0, id, i, flags; |
| 1368 | vector pos; |
| 1369 | char buf[64]; |
| 1370 | char name[48]; |
| 1371 | msafe_struct m; |
| 1372 | float circle_dist; |
| 1373 | char cenable; |
| 1374 | |
| 1375 | retry_set_state: |
| 1376 | switch (state) { |
| 1377 | case STATE_IDLE: |
| 1378 | // stay at one position until ready. note that if attacked , it will flee in this state. |
| 1379 | FlushGoal(me_handle, 2); |
| 1380 | |
| 1381 | memory->timer = Game_GetTime() + 1.0f; |
| 1382 | |
| 1383 | Obj_Value(me_handle, VF_GET, OBJV_V_POS, &pos); |
| 1384 | Obj_Value(me_handle, VF_GET, OBJV_I_ROOMNUM, &room_number); |
| 1385 | |
| 1386 | AI_AddGoal(me_handle, AIG_GET_TO_POS, 1, 1.0f, -1, GF_KEEP_AT_COMPLETION, &pos, room_number); |
| 1387 | // mprintf(0, "[Creeper] Idle\n"); |
| 1388 | break; |
| 1389 | |
| 1390 | case STATE_STALK: |
| 1391 | // cloak and stalk player. |
| 1392 | // FlushGoal(me_handle, 3); // take out any priority goals set. |
| 1393 | FlushGoal(me_handle, 2); // take out any goals previously set |
| 1394 | |
| 1395 | memory->timer = Game_GetTime() + 1.0f; // set timer for recharge! |
| 1396 | // memory->timer = Game_GetTime() + 1.0f + (rand()%4) * 0.5f; |
| 1397 | |
| 1398 | m.objhandle = memory->eye_obj; |
| 1399 | m.lifetime = 5.0f + (rand() % 20) * 0.5f; |
| 1400 | m.state = 1; |
| 1401 | |
| 1402 | memory->eye_timer = m.lifetime; |
| 1403 | |
| 1404 | MSafe_CallFunction(MSAFE_OBJECT_CLOAK, &m); |
| 1405 | |
| 1406 | circle_dist = 40.0f; |
| 1407 | AI_Value(me_handle, VF_SET, AIV_F_CIRCLE_DIST, &circle_dist); |
| 1408 | // AI_AddGoal(me_handle, AIG_WANDER_AROUND, 3, 1.0f, -1, GF_ORIENT_VELOCITY | GF_NOTIFIES | |
| 1409 | // GF_KEEP_AT_COMPLETION, &pos, room_number); |
| 1410 | AI_AddGoal(me_handle, AIG_MOVE_RELATIVE_OBJ, 2, 1.0f, -1, GF_KEEP_AT_COMPLETION | GF_OBJ_IS_TARGET, NULL, 0); |
| 1411 | |
| 1412 | flags = AIF_DISABLE_MELEE; |
| 1413 | AI_Value(me_handle, VF_SET_FLAGS, AIV_I_FLAGS, &flags); |
| 1414 | |
| 1415 | cenable = 1; |
| 1416 | Obj_Value(me_handle, VF_SET, OBJV_C_NEGATIVE_LIGHT, &cenable); |
| 1417 | // mprintf(0, "[Creeper] Stalk\n"); |
| 1418 | break; |
| 1419 | |
| 1420 | case STATE_ATTACK: |
| 1421 | // FlushGoal(me_handle, 3); // we've decloaked, attack. |
| 1422 | FlushGoal(me_handle, 2); |
| 1423 | |
| 1424 | memory->timer = Game_GetTime() + 4.0f + (float)(rand() % 3); |
| 1425 | flags = AIF_DISABLE_MELEE; |
| 1426 | AI_Value(me_handle, VF_CLEAR_FLAGS, AIV_I_FLAGS, &flags); |
| 1427 | |
| 1428 | cenable = 1; |
| 1429 | Obj_Value(me_handle, VF_SET, OBJV_C_NEGATIVE_LIGHT, &cenable); |
| 1430 | // mprintf(0, "[Creeper] Attack\n"); |
| 1431 | break; |
| 1432 | } |
| 1433 | |
| 1434 | memory->state = state; |
| 1435 | } |
| 1436 | |
| 1437 | // aiLukeTurret |
| 1438 | // Luke's funky turret |
| 1439 | |
| 1440 | void aiLukeTurret::OnInit(int me_handle) { |
| 1441 | tOSIRISMEMCHUNK ch; |
| 1442 | |
| 1443 | ch.id = 4; |
| 1444 | ch.size = sizeof(aiLukeTurret::t_lturret_memory); |
| 1445 | ch.my_id.type = OBJECT_SCRIPT; |
| 1446 | ch.my_id.objhandle = me_handle; |
| 1447 | |
| 1448 | memory = (aiLukeTurret::t_lturret_memory *)Scrpt_MemAlloc(&ch); |
| 1449 | memory->timer = -1.0f; |
| 1450 | |
| 1451 | // turrets only activate when they 'hear' a player fire a weapon. |
| 1452 | int flags = AIF_DISABLE_FIRING; |
| 1453 | |
| 1454 | AI_SetType(me_handle, AIT_AIS); |
| 1455 | AI_Value(me_handle, VF_SET_FLAGS, AIV_I_FLAGS, &flags); |
| 1456 | } |
| 1457 | |
| 1458 | bool aiLukeTurret::OnNotify(int me_handle, tOSIRISEVTAINOTIFY *data) { |
| 1459 | int flags; |
| 1460 | |
| 1461 | switch (data->notify_type) { |
| 1462 | case AIN_OBJ_FIRED: |
| 1463 | // set 5 seconds for turret awareness before it becomes inert. |
| 1464 | memory->timer = Game_GetTime() + 12.0f; |
| 1465 | AI_SetType(me_handle, AIT_STATIONARY_TURRET); |
| 1466 | flags = AIF_DISABLE_FIRING; |
| 1467 | AI_Value(me_handle, VF_CLEAR_FLAGS, AIV_I_FLAGS, &flags); |
| 1468 | break; |
| 1469 | } |
| 1470 | |
| 1471 | return true; |
| 1472 | } |
| 1473 | |
| 1474 | void aiLukeTurret::OnInterval(tOSIRISEventInfo *data) { |
| 1475 | if (memory->timer == -1.0f) |
| 1476 | return; |
| 1477 | |
| 1478 | if (memory->timer < Game_GetTime()) { |
| 1479 | int flags = AIF_DISABLE_FIRING; |
| 1480 | AI_SetType(data->me_handle, AIT_AIS); |
| 1481 | AI_Value(data->me_handle, VF_SET_FLAGS, AIV_I_FLAGS, &flags); |
| 1482 | memory->timer = -1.0f; |
| 1483 | } |
| 1484 | } |
| 1485 | |
| 1486 | // aiSTBlackBarrel |
| 1487 | // Black Stormtrooper's ammo pack |
| 1488 | |
| 1489 | void aiSTBlackBarrel::OnInit(int me_handle) { |
| 1490 | tOSIRISMEMCHUNK ch; |
| 1491 | |
| 1492 | ch.id = 4; |
| 1493 | ch.size = sizeof(aiSTBlackBarrel::t_stblackbarrel_memory); |
| 1494 | ch.my_id.type = OBJECT_SCRIPT; |
| 1495 | ch.my_id.objhandle = me_handle; |
| 1496 | |
| 1497 | memory = (aiSTBlackBarrel::t_stblackbarrel_memory *)Scrpt_MemAlloc(&ch); |
| 1498 | |
| 1499 | Obj_Value(me_handle, VF_GET, OBJV_M_ORIENT, &memory->orient); |
| 1500 | } |
| 1501 | |
| 1502 | void aiSTBlackBarrel::OnDestroy(int me_handle, tOSIRISEVTDESTROY *evt) { |
| 1503 | // Create a frag burst effect |
| 1504 | vector pos, new_dir; |
| 1505 | int room; |
| 1506 | matrix orient, new_orient; |
| 1507 | |
| 1508 | Obj_Value(me_handle, VF_GET, OBJV_V_POS, &pos); |
| 1509 | Obj_Value(me_handle, VF_GET, OBJV_I_ROOMNUM, &room); |
| 1510 | Obj_Value(me_handle, VF_GET, OBJV_M_ORIENT, &orient); |
| 1511 | |
| 1512 | new_dir = -orient.fvec; |
| 1513 | vm_VectorToMatrix(&new_orient, &new_dir, NULL, NULL); |
| 1514 | |
| 1515 | Obj_Create(OBJ_WEAPON, Wpn_FindID("AlienBossFragBurst"), room, &pos, &new_orient, me_handle); |
| 1516 | |
| 1517 | // mprintf(0,"Barrel destroyed.\n"); |
| 1518 | } |
| 1519 | |
| 1520 | void aiSTBlackBarrel::OnDamaged(int me_handle, tOSIRISEVTDAMAGED *evt) { |
| 1521 | // mprintf(0,"Barrel damaged...\n"); |
| 1522 | } |