Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #library "ZMRACS"
- #include "zcommon.acs"
- #pragma define raw on
- #pragma block_scope on
- #pragma fixed off
- #define DISABLE_DEBUG
- #ifndef DISABLE_DEBUG
- #define ZMR_DEBUG_LOG Log
- #define ZMR_ASSERT(expr) if(!(expr)) \
- ZMR_DEBUG_LOG(#expr)
- #else
- #define ZMR_DEBUG_LOG(...)
- #define ZMR_ASSERT(expr)
- #endif
- #define tid_t int
- function tid_t zmr_create16KPage(void) {
- tid_t resultTID = UniqueTID();
- Spawn("ZMR_Page16k", 0, 0, 0, resultTID);
- return resultTID;
- }
- function tid_t zmr_createPathfinderMonster(void) {
- tid_t resultTID = UniqueTID();
- Spawn("ZMR_Pathfinder", 0, 0, 0, resultTID);
- return resultTID;
- }
- function tid_t zmr_createPatrolPoint(void) {
- tid_t resultTID = UniqueTID();
- Spawn("PatrolPoint", 0, 0, 0, resultTID);
- return resultTID;
- }
- #define ZMR_KILOBYTE (1024)
- #define ZMR_MEGABYTE (ZMR_KILOBYTE * ZMR_KILOBYTE)
- #define ZMR_PAGE_SIZE (4096*4)
- #define ZMR_PAGE_FOR_PTR(p) ((p) >> 14)
- #define ZMR_PAGE_OFFSET(p) ((p) & (ZMR_PAGE_SIZE - 1))
- #define ZMR_MAX_MEMENTRIES 8192
- #define ZMR_MAX_PAGES 4096
- #define zmr_page_t int
- #define ZMR_STARTUP_MEMORY (ZMR_MEGABYTE * 5)
- #define ZMR_PREALLOC_PAGES ((ZMR_STARTUP_MEMORY / ZMR_PAGE_SIZE) + 1)
- struct zmr_mementry_t {
- int offset;
- int size;
- bool claimed;
- bool in_use;
- };
- zmr_mementry_t g_mementries[ZMR_MAX_MEMENTRIES];
- zmr_page_t g_zmr_pages[ZMR_MAX_PAGES];
- bool g_zmr_didinit = false;
- int g_zmr_current_free_memory = ZMR_STARTUP_MEMORY;
- function void zmr_init_mman(void) {
- g_mementries[0].size = ZMR_STARTUP_MEMORY;
- g_mementries[0].offset = 8;
- g_mementries[0].in_use = true;
- g_mementries[0].claimed = false;
- for(int i = 0; i < ZMR_PREALLOC_PAGES; ++i) {
- g_zmr_pages[i] = zmr_create16KPage();
- }
- g_zmr_didinit = true;
- }
- function void zmr_deinit_mman(void) {
- for(int i = 0; i < ZMR_MAX_PAGES; ++i) {
- g_mementries[i].size = 0;
- g_mementries[i].offset = 0;
- g_mementries[i].in_use = 0;
- g_mementries[i].claimed = 0;
- }
- for(int i = 0; i < ZMR_PREALLOC_PAGES; ++i) {
- g_zmr_pages[i] = 0;
- }
- g_zmr_didinit = false;
- g_zmr_current_free_memory = ZMR_STARTUP_MEMORY;
- }
- function void zmr_ensure_init(void) {
- if(!g_zmr_didinit) {
- zmr_init_mman();
- // ZMR_DEBUG_LOG( s: "mman init");
- }
- }
- function void zmr_free_relocating(int i) {
- if(i > 0 && g_mementries[i-1].claimed == false && g_mementries[i-1].in_use == true) {
- //set current entry to be unused, enlargen previous entry
- g_mementries[i-1].size += g_mementries[i].size;
- g_mementries[i].size = 0;
- g_mementries[i].offset = -1;
- g_mementries[i].in_use = false;
- return;
- }
- else if((i+1) < ZMR_MAX_MEMENTRIES && g_mementries[i+1].in_use == true && g_mementries[i+1].claimed == false) {
- g_mementries[i].size += g_mementries[i+1].size;
- g_mementries[i+1].size = 0;
- g_mementries[i+1].offset = -1;
- g_mementries[i+1].in_use = false;
- }
- else if(i > 0 && g_mementries[i-1].in_use == false) {
- g_mementries[i-1].size = g_mementries[i].size;
- g_mementries[i-1].offset = g_mementries[i].offset;
- g_mementries[i-1].in_use = true;
- g_mementries[i].size = 0;
- g_mementries[i].offset = -1;
- g_mementries[i].in_use = false;
- }
- }
- function void zmr_malloc_relocating(int i, int size) {
- if(g_mementries[i].size == size)
- return;
- int relocate_size = g_mementries[i].size - size;
- if(i > 0 && g_mementries[i-1].claimed == false && g_mementries[i-1].in_use == true) {
- /*
- an entry prior to this one is present,
- the prior entry is a record of memory that is free
- we need to make the previous entry larger by giving it the bytes in our entry
- that we do not need
- prior->size += current->size - size;
- current->offset += current->size - size;
- current->size = size;
- we relocate the unused bytes to prior, advance our offset to be after prior, and set our size to the new size
- */
- g_mementries[i-1].size += relocate_size;
- g_mementries[i].offset += relocate_size;
- g_mementries[i].size = size;
- }
- else if((i+1) < ZMR_MAX_MEMENTRIES && g_mementries[i+1].in_use == true && g_mementries[i+1].claimed == false) {
- /*
- inverse of prior, we relocate bytes to the next entry, moving its offset back
- */
- g_mementries[i+1].size += relocate_size;
- g_mementries[i+1].offset -= relocate_size;
- g_mementries[i].size = size;
- }
- else if(i > 0 && g_mementries[i-1].in_use == false) {
- g_mementries[i-1].in_use = true;
- g_mementries[i-1].size = relocate_size;
- g_mementries[i-1].offset = g_mementries[i].offset;
- g_mementries[i-1].claimed = false;
- g_mementries[i].offset += relocate_size;
- g_mementries[i].size = size;
- }
- else if((i+1) < ZMR_MAX_MEMENTRIES && g_mementries[i+1].in_use == false) {
- g_mementries[i+1].in_use = true;
- g_mementries[i+1].size = relocate_size;
- g_mementries[i+1].offset = g_mementries[i].offset + size;
- g_mementries[i+1].claimed = false;
- g_mementries[i].size = size;
- }
- }
- function int zmr_malloc(int size) {
- ZMR_ASSERT(size < g_zmr_current_free_memory && size > 0);
- for(int i = 0; i < ZMR_MAX_MEMENTRIES;++i) {
- if(g_mementries[i].claimed == false && g_mementries[i].in_use == true && g_mementries[i].size >= size) {
- g_mementries[i].claimed = true;
- zmr_malloc_relocating(i, size);
- g_zmr_current_free_memory -= size;
- int result = g_mementries[i].offset;
- //ZMR_DEBUG_LOG(s:"Allocated memory, offset is ", d:result, s:".");
- return result;
- }
- }
- return 0;
- }
- function bool zmr_free(int offset) {
- for(int i = 0; i < ZMR_MAX_MEMENTRIES;++i) {
- if(g_mementries[i].offset == offset) {
- int sz = g_mementries[i].size;
- g_mementries[i].claimed = false;
- zmr_free_relocating(i);
- g_zmr_current_free_memory += sz;
- return true;
- }
- }
- return false;
- }
- function void zmr_write_i32(int ptr, int value) {
- ZMR_ASSERT(ptr != 0);
- ZMR_ASSERT((ptr & 3) == 0); //must be int aligned
- SetUserArray(g_zmr_pages[ZMR_PAGE_FOR_PTR(ptr)], "user_data", (ZMR_PAGE_OFFSET(ptr)) >> 2, value);
- }
- function int zmr_read_i32(int ptr) {
- ZMR_ASSERT(ptr != 0);
- ZMR_ASSERT((ptr & 3) == 0); //must be int aligned
- return GetUserArray(g_zmr_pages[ZMR_PAGE_FOR_PTR(ptr)], "user_data", ZMR_PAGE_OFFSET(ptr) >> 2);
- }
- #define N_MONSTER_CLASSES 20
- int g_total_monsters = 0;
- str g_monster_classes[N_MONSTER_CLASSES] = {
- "SpiderMastermind",
- "Cyberdemon",
- "Archvile",
- "Arachnotron",
- "StealthArachnotron",
- "BaronOfHell",
- "Revenant",
- "StealthRevenant",
- "PainElemental",
- "Fatso",
- "HellKnight",
- "Cacodemon",
- "Spectre",
- "ChaingunGuy",
- "Demon",
- "ShotgunGuy",
- "DoomImp",
- "LostSoul",
- "WolfensteinSS",
- "ZombieMan"
- };
- str g_weight_cvar_strings[N_MONSTER_CLASSES] = {
- "SpiderMastermind_weight",
- "Cyberdemon_weight",
- "Archvile_weight",
- "Arachnotron_weight",
- "StealthArachnotron_weight",
- "BaronOfHell_weight",
- "Revenant_weight",
- "StealthRevenant_weight",
- "PainElemental_weight",
- "Fatso_weight",
- "HellKnight_weight",
- "Cacodemon_weight",
- "Spectre_weight",
- "ChaingunGuy_weight",
- "Demon_weight",
- "ShotgunGuy_weight",
- "DoomImp_weight",
- "LostSoul_weight",
- "WolfensteinSS_weight",
- "ZombieMan_weight"
- };
- int g_n_monsters_of_class[N_MONSTER_CLASSES] = {0};
- int g_monster_ratios[N_MONSTER_CLASSES] = {0};
- #define zmr_patherptr_t int
- #define zmr_pointvec_t int
- enum {
- ZMR_PATHER_FIELD_startx = 0,
- ZMR_PATHER_FIELD_starty = 4,
- ZMR_PATHER_FIELD_startz = 8,
- ZMR_PATHER_FIELD_goalx = 12,
- ZMR_PATHER_FIELD_goaly = 16,
- ZMR_PATHER_FIELD_goalz = 20,
- ZMR_PATHER_FIELD_tid = 24,
- ZMR_PATHER_FIELD_flags = 28,
- ZMR_PATHER_FIELD_points = 32,
- ZMR_PATHER_FIELD_goaltid = 36,
- ZMR_PATHER_FIELD_tickpoll = 40,
- ZMR_PATHER_FIELD_nextpather = 44,
- ZMR_PATHER_FIELD_lasttick = 48,
- ZMR_PATHER_FIELD_speed = 52,
- ZMR_SIZEOF_PATHER = 56
- }
- enum {
- ZMR_PATHER_FLAGS_in_use = 1,
- ZMR_PATHER_FLAGS_allocated = 2,
- ZMR_PATHER_FLAGS_completed = 4,
- ZMR_PATHER_FLAGS_failed = 8
- }
- #define ZMR_PATHER_SET_START(pather, x, y, z) {\
- zmr_patherptr_t _pather = (pather); \
- int _x = (x);\
- int _y = (y);\
- int _z = (z);\
- Warp(zmr_read_i32(_pather + ZMR_PATHER_FIELD_tid), _x, _y, _z, 0, WARPF_ABSOLUTEPOSITION);\
- zmr_write_i32(_pather + ZMR_PATHER_FIELD_startx, _x);\
- zmr_write_i32(_pather + ZMR_PATHER_FIELD_starty, _y);\
- zmr_write_i32(_pather + ZMR_PATHER_FIELD_startx, _z);\
- }
- #define ZMR_PATHER_SET_GOAL(pather, x, y, z) {\
- zmr_patherptr_t _pather = (pather); \
- int _x = (x);\
- int _y = (y);\
- int _z = (z);\
- tid_t goal_tid = zmr_read_i32(_pather + ZMR_PATHER_FIELD_goaltid);\
- Warp(goal_tid, _x, _y, _z, 0, WARPF_ABSOLUTEPOSITION);\
- zmr_write_i32(_pather + ZMR_PATHER_FIELD_goalx, _x);\
- zmr_write_i32(_pather + ZMR_PATHER_FIELD_goaly, _y);\
- zmr_write_i32(_pather + ZMR_PATHER_FIELD_goalx, _z);\
- Thing_SetGoal(zmr_read_i32(_pather + ZMR_PATHER_FIELD_tid), goal_tid, 0, 1);\
- }
- zmr_patherptr_t g_zmr_first_pather = 0;
- zmr_patherptr_t g_zmr_previous_pather = 0;
- function zmr_patherptr_t zmr_create_pathfinder(void) {
- zmr_patherptr_t pather = zmr_malloc(ZMR_SIZEOF_PATHER);
- ZMR_ASSERT(pather != 0);
- zmr_write_i32(pather + ZMR_PATHER_FIELD_flags, 0);
- zmr_write_i32(pather + ZMR_PATHER_FIELD_tid, zmr_createPathfinderMonster());
- zmr_write_i32(pather + ZMR_PATHER_FIELD_goaltid, zmr_createPatrolPoint());
- zmr_write_i32(pather + ZMR_PATHER_FIELD_nextpather, 0);
- zmr_write_i32(pather + ZMR_PATHER_FIELD_points, 0);
- /*
- save first pather so we can iterate over them
- */
- if(g_zmr_first_pather == 0) {
- g_zmr_first_pather = pather;
- }
- else {
- /*
- link new pather into the global list
- */
- zmr_write_i32(g_zmr_previous_pather + ZMR_PATHER_FIELD_nextpather, pather);
- }
- g_zmr_previous_pather = pather;
- return pather;
- }
- #define ZMR_FOREACH_PATHER(indexvar) for(zmr_patherptr_t indexvar = g_zmr_first_pather; indexvar != 0; indexvar = zmr_read_i32(indexvar + ZMR_PATHER_FIELD_nextpather))
- /*
- find an unused pathfinder in the global list to reuse
- or allocate a new one
- */
- function zmr_patherptr_t zmr_allocate_pathfinder(void) {
- ZMR_FOREACH_PATHER(pather) {
- int pather_flags = zmr_read_i32(pather + ZMR_PATHER_FIELD_flags);
- if((pather_flags & ZMR_PATHER_FLAGS_allocated) == 0) {
- zmr_write_i32(pather + ZMR_PATHER_FIELD_flags, pather_flags | ZMR_PATHER_FLAGS_allocated);
- return pather;
- }
- }
- zmr_patherptr_t new_pather = zmr_create_pathfinder();
- zmr_write_i32(new_pather + ZMR_PATHER_FIELD_flags, ZMR_PATHER_FLAGS_allocated);
- return new_pather;
- }
- function void zmr_release_pathfinder(zmr_patherptr_t pather) {
- ZMR_ASSERT(pather != 0);
- zmr_pointvec_t point_data = zmr_read_i32(pather + ZMR_PATHER_FIELD_points);
- if(point_data != 0) {
- zmr_free(point_data);
- zmr_write_i32(pather + ZMR_PATHER_FIELD_points, 0);
- }
- zmr_write_i32(pather + ZMR_PATHER_FIELD_flags, zmr_read_i32(pather + ZMR_PATHER_FIELD_flags) & (~ZMR_PATHER_FLAGS_allocated));
- }
- enum {
- ZMR_POINTVEC_FIELD_position = 0,
- ZMR_POINTVEC_FIELD_nalloc = 4,
- ZMR_SIZEOF_POINTVEC = 8
- }
- enum {
- ZMR_POINT_FIELD_x = 0,
- ZMR_POINT_FIELD_y = 4,
- ZMR_POINT_FIELD_z = 8,
- ZMR_SIZEOF_POINT = 12
- }
- function zmr_pointvec_t zmr_create_pointvec(int maxpoints) {
- ZMR_ASSERT(maxpoints > 0);
- zmr_pointvec_t points = zmr_malloc(ZMR_SIZEOF_POINTVEC + (ZMR_SIZEOF_POINT * maxpoints));
- ZMR_ASSERT(points != 0);
- zmr_write_i32(points + ZMR_POINTVEC_FIELD_position, 0);
- zmr_write_i32(points + ZMR_POINTVEC_FIELD_nalloc, maxpoints);
- return points;
- }
- function void zmr_release_pointvec(zmr_pointvec_t points) {
- ZMR_ASSERT(points != 0);
- zmr_free(points);
- }
- function void zmr_pointvec_set_at(zmr_pointvec_t points, int x, int y, int z, int index) {
- int start_offs = (points + ZMR_SIZEOF_POINTVEC) + (index * ZMR_SIZEOF_POINT);
- zmr_write_i32(start_offs + ZMR_POINT_FIELD_x, x);
- zmr_write_i32(start_offs + ZMR_POINT_FIELD_y, y);
- zmr_write_i32(start_offs + ZMR_POINT_FIELD_z, z);
- }
- function zmr_patherptr_t zmr_find_path(
- int startx, int starty, int startz,
- int goalx, int goaly, int goalz,
- int movement_speed, int tick_granularity, int maxpoints) {
- zmr_patherptr_t pather = zmr_allocate_pathfinder();
- ZMR_ASSERT(pather != 0);
- ZMR_PATHER_SET_START(pather, startx, starty, startz)
- zmr_write_i32(pather + ZMR_PATHER_FIELD_lasttick, Timer());
- zmr_write_i32(pather + ZMR_PATHER_FIELD_tickpoll, tick_granularity);
- zmr_write_i32(pather + ZMR_PATHER_FIELD_points, zmr_create_pointvec(maxpoints));
- ZMR_PATHER_SET_GOAL(pather, goalx, goaly, goalz)
- zmr_write_i32(pather + ZMR_PATHER_FIELD_speed, movement_speed);
- SetActorProperty( zmr_read_i32(pather + ZMR_PATHER_FIELD_tid), APROP_SPEED, movement_speed);
- zmr_write_i32(pather + ZMR_PATHER_FIELD_flags, zmr_read_i32(pather + ZMR_PATHER_FIELD_flags) | ZMR_PATHER_FLAGS_in_use);
- return pather;
- }
- int
- g_current_unique_id = 1;
- int g_uid_tid_lut = 0;
- int g_uid_tid_mod = 0;
- //i swear to god, this value wasnt randomly chosen. i used the m,n formula for collision calculation and this
- //was the ideal table scale
- #define ZMR_ZEROCOLLIDE_SCALE 666
- #define ZMR_MONSTER_DATA_SIZE 512
- enum {
- ZMR_MONSTER_FIELD_prev = 0,
- ZMR_MONSTER_FIELD_next = 4,
- ZMR_MONSTER_FIELD_thingid = 8,
- ZMR_MONSTER_FIELD_lastx = 12,
- ZMR_MONSTER_FIELD_lasty = 16,
- ZMR_MONSTER_FIELD_lastz = 20,
- ZMR_MONSTER_FIELD_buddy = 24,
- ZMR_MONSTER_FIELD_dead = 28,
- ZMR_MONSTER_FIELD_initial_health = 32,
- ZMR_MONSTER_FIELD_flee_timepoint = 36,
- ZMR_MONSTER_FIELD_is_fleeing = 40,
- ZMR_MONSTER_FIELD_has_missile = 44
- }
- #define zmr_monsterptr_t int
- #define ZMR_M0NSTER_READ(monster, field, type) zmr_read_##type ((monster) + (ZMR_MONSTER_FIELD_##field ))
- #define ZMR_M0NSTER_WRITE(monster, field, type, val) zmr_write_##type ((monster) +ZMR_MONSTER_FIELD_##field , (val))
- zmr_monsterptr_t g_zmr_last_monster_alloc = 0;
- zmr_monsterptr_t g_zmr_first_monster = 0;
- function zmr_monsterptr_t zmr_alloc_monster_data(void) {
- zmr_monsterptr_t p = zmr_malloc(ZMR_MONSTER_DATA_SIZE);
- ZMR_M0NSTER_WRITE(p, prev, i32, g_zmr_last_monster_alloc);
- if(g_zmr_last_monster_alloc != 0)
- ZMR_M0NSTER_WRITE(g_zmr_last_monster_alloc, next, i32, p);
- else
- g_zmr_first_monster = p;
- g_zmr_last_monster_alloc = p;
- return p;
- }
- bool g_zmr_monster_globals_initialized = false;
- function void zmr_monster_global_ensure_init(void) {
- if(!g_zmr_monster_globals_initialized) {
- zmr_ensure_init();
- //WASTE MEMORY
- zmr_malloc(32);
- g_total_monsters = GetLevelInfo(LEVELINFO_TOTAL_MONSTERS);
- g_uid_tid_mod = (g_total_monsters) * ZMR_ZEROCOLLIDE_SCALE;
- g_uid_tid_lut = zmr_malloc(((g_total_monsters) * ZMR_ZEROCOLLIDE_SCALE) * sizeof(int));
- g_zmr_monster_globals_initialized = true;
- ACS_NamedExecute("ZMR_UpdateBuddies", 0);
- ACS_NamedExecute("ZMR_UpdatePaths", 0);
- }
- }
- /*
- zero is not a valid uid
- */
- function bool zmr_monster_has_uid(int tid) {
- return GetUserVariable(tid, "user_ZMR_enemy_uid") != 0;
- }
- function int zmr_monster_get_uid(int tid) {
- return GetUserVariable(tid, "user_ZMR_enemy_uid");
- }
- function void zmr_assign_uid_impl(void) {
- zmr_monster_global_ensure_init();
- g_current_unique_id = g_current_unique_id + 1;
- SetUserVariable(0, "user_ZMR_enemy_uid", g_current_unique_id);
- zmr_monsterptr_t myptr = zmr_alloc_monster_data();
- if(ActivatorTID() ==0)
- Thing_ChangeTID(0, UniqueTID());
- int _tid = ActivatorTID();
- if(_tid == 0) {
- //ZMR_DEBUG_LOG(s:"Got a zero tid in ZMR_AssignUID!");
- }
- ZMR_M0NSTER_WRITE(myptr, thingid, i32, _tid);
- zmr_write_i32(myptr + ZMR_MONSTER_FIELD_initial_health, GetActorProperty(_tid, APROP_HEALTH));
- zmr_set_monster_dataptr(g_current_unique_id, myptr);
- }
- function zmr_monsterptr_t zmr_get_monster_dataptr(int uid) {
- zmr_monster_global_ensure_init();
- if(uid == 0) {
- zmr_assign_uid_impl();
- return 0;
- }
- zmr_monsterptr_t result = zmr_read_i32( g_uid_tid_lut + ((uid % g_uid_tid_mod) << 2));
- ZMR_ASSERT(result != 0);
- return result;
- }
- function void zmr_set_monster_dataptr(int uid, zmr_monsterptr_t dataptr) {
- ZMR_ASSERT(dataptr != 0);
- zmr_write_i32( g_uid_tid_lut + ((uid % g_uid_tid_mod) << 2), dataptr);
- }
- #define ZMR_FOREACH_MONSTER(indexname) for(zmr_monsterptr_t indexname = g_zmr_first_monster; indexname != 0; indexname = zmr_read_i32(indexname + ZMR_MONSTER_FIELD_next))
- #define ZMR_MONSTER_IS_DEAD(monster) (GetActorProperty(zmr_read_i32((monster) + ZMR_MONSTER_FIELD_thingid), APROP_Health) <= 0)
- //(zmr_read_i32((monster) + ZMR_MONSTER_FIELD_dead) != 0
- function zmr_monsterptr_t find_closest_monster(zmr_monsterptr_t self) {
- if(self == 0)
- return 0;
- int mytid = zmr_read_i32(self+ZMR_MONSTER_FIELD_thingid);
- int selfx = GetActorX(mytid),//zmr_read_i32(self + ZMR_MONSTER_FIELD_lastx),
- selfy = GetActorY(mytid),//zmr_read_i32(self + ZMR_MONSTER_FIELD_lasty),
- selfz = GetActorZ(mytid);//zmr_read_i32(self + ZMR_MONSTER_FIELD_lastz);
- int smallest_absdistance = 0x7fffffff;
- zmr_monsterptr_t closest_found = 0;
- ZMR_FOREACH_MONSTER(i) {
- if(i == self || ZMR_MONSTER_IS_DEAD(i) )
- continue;
- int theirtid = zmr_read_i32(i+ZMR_MONSTER_FIELD_thingid);
- int x = GetActorX(theirtid);//zmr_read_i32(i + ZMR_MONSTER_FIELD_lastx);
- int y = GetActorY(theirtid);//zmr_read_i32(i + ZMR_MONSTER_FIELD_lasty);
- //int z = zmr_read_i32(i + ZMR_MONSTER_FIELD_lastz);
- int len = VectorLength(selfx - x, selfy - y);
- //len = VectorLength(selfz - z, len);
- if(len < smallest_absdistance) {
- smallest_absdistance = len;
- closest_found = i;
- }
- }
- //ZMR_DEBUG_LOG(s:"Closest found is ", d: smallest_absdistance);
- return closest_found;
- }
- Script "ZMR_UpdateBuddies" (void) NET {
- zmr_monster_global_ensure_init();
- while(true) {
- Delay(1);
- int counter = 0;
- ZMR_FOREACH_MONSTER(i) {
- if(ZMR_MONSTER_IS_DEAD(i))
- continue;
- zmr_write_i32(i + ZMR_MONSTER_FIELD_buddy, find_closest_monster(i));
- ++counter;
- if((counter & 0xF) == 0)
- Delay(1);
- }
- }
- }
- Script "ZMR_UpdatePaths" (void) NET {
- zmr_monster_global_ensure_init();
- while(true) {
- Delay(10);
- int current_tick = Timer();
- ZMR_FOREACH_PATHER(path) {
- int flags = zmr_read_i32(path + ZMR_PATHER_FIELD_flags);
- if( (flags & (ZMR_PATHER_FLAGS_completed | ZMR_PATHER_FLAGS_failed)) != 0
- ||
- (flags & ZMR_PATHER_FLAGS_in_use) == 0)
- continue;
- int last_tick = zmr_read_i32(path + ZMR_PATHER_FIELD_lasttick);
- int tickpoll = zmr_read_i32(path + ZMR_PATHER_FIELD_tickpoll);
- if( (last_tick + tickpoll) < current_tick) {
- continue;
- }
- zmr_write_i32(path + ZMR_PATHER_FIELD_lasttick, current_tick);
- zmr_pointvec_t points = zmr_read_i32(path + ZMR_PATHER_FIELD_points);
- ZMR_ASSERT(points != 0);
- int current_pointvec_index = zmr_read_i32(points + ZMR_POINTVEC_FIELD_position);
- int pointvec_nalloc = zmr_read_i32(points + ZMR_POINTVEC_FIELD_nalloc);
- int pather_tid = zmr_read_i32(path + ZMR_PATHER_FIELD_tid);
- int currentx = GetActorX(pather_tid),
- currenty = GetActorY(pather_tid),
- currentz = GetActorZ(pather_tid);
- int goalx = zmr_read_i32(path + ZMR_PATHER_FIELD_goalx),
- goaly = zmr_read_i32(path + ZMR_PATHER_FIELD_goaly),
- goalz = zmr_read_i32(path + ZMR_PATHER_FIELD_goalz);
- int len = VectorLength(goalx - currentx, goaly - currenty);
- len = VectorLength(goalz - currentz, len);
- int movement_speed = zmr_read_i32(path + ZMR_PATHER_FIELD_speed);
- if( len < movement_speed) {
- zmr_write_i32(path + ZMR_PATHER_FIELD_flags, flags | ZMR_PATHER_FLAGS_completed);
- SetActorProperty( pather_tid, APROP_SPEED, 0);
- }
- else if((current_pointvec_index + 1) >= pointvec_nalloc) {
- zmr_write_i32(path + ZMR_PATHER_FIELD_flags, flags | ZMR_PATHER_FLAGS_failed);
- SetActorProperty( pather_tid, APROP_SPEED, 0);
- continue;
- }
- zmr_pointvec_set_at(points, currentx, currenty, currentz, current_pointvec_index);
- zmr_write_i32(points + ZMR_POINTVEC_FIELD_position, current_pointvec_index + 1);
- }
- }
- }
- Script 32766 OPEN NET
- {
- zmr_monster_global_ensure_init();
- for( int i = 0; i < N_MONSTER_CLASSES; ++i) {
- g_n_monsters_of_class[i] = ThingCountName(g_monster_classes[i], 0);
- if(g_n_monsters_of_class[i] != 0)
- g_monster_ratios[i] = (g_total_monsters/ g_n_monsters_of_class[i]);
- int x = ((N_MONSTER_CLASSES+1) - i) * g_monster_ratios[i];
- SetCVar(g_weight_cvar_strings[i], x);
- }
- ACS_NamedExecute("ZMR_UpdateBuddies", 0);
- ACS_NamedExecute("ZMR_UpdatePaths", 0);
- }
- #define ZMR_thunk_prologue \
- zmr_monster_global_ensure_init();\
- if(zmr_monster_has_uid(0) == false)\
- terminate;\
- zmr_monsterptr_t self = zmr_get_monster_dataptr(zmr_monster_get_uid(ActivatorTID()) );\
- if(self == 0) \
- terminate;\
- zmr_write_i32(self + ZMR_MONSTER_FIELD_lastx, GetActorX(0));\
- zmr_write_i32(self + ZMR_MONSTER_FIELD_lasty, GetActorY(0));\
- zmr_write_i32(self + ZMR_MONSTER_FIELD_lastz, GetActorZ(0));
- Script "ZMR_OnDeath" KILL NET {
- ZMR_thunk_prologue
- ZMR_DEBUG_LOG(s:"Dead monster had tid ", d: ZMR_M0NSTER_READ( self, thingid, i32), s:".");
- ZMR_DEBUG_LOG(s:"Dead monster had buddy with tid ", d: ZMR_M0NSTER_READ( zmr_read_i32(self + ZMR_MONSTER_FIELD_buddy), thingid, i32), s:".");
- zmr_write_i32(self + ZMR_MONSTER_FIELD_dead, 1);
- SetActivatorToTarget(0);
- int target = ActivatorTID();
- zmr_monsterptr_t buddy = zmr_read_i32(self + ZMR_MONSTER_FIELD_buddy);
- if(buddy == 0 )//|| ZMR_MONSTER_IS_DEAD(buddy))
- terminate;
- //SetActivator(zmr_read_i32(buddy + ZMR_MONSTER_FIELD_thingid));
- //SetPointer(AAPTR_TARGET, target);
- //SetActorState(0, "See");
- int i = 0;
- zmr_monsterptr_t otherbuddy;
- for( otherbuddy = zmr_read_i32(buddy + ZMR_MONSTER_FIELD_buddy); otherbuddy != 0 && otherbuddy != buddy;
- otherbuddy = zmr_read_i32(otherbuddy + ZMR_MONSTER_FIELD_buddy),++i) {
- if(i > 4)
- break;
- if (!ZMR_MONSTER_IS_DEAD(otherbuddy)){
- SetActivator(zmr_read_i32(otherbuddy + ZMR_MONSTER_FIELD_thingid));
- SetPointer(AAPTR_TARGET, target);
- SetActorState(0, "See");
- buddy = otherbuddy;
- }
- }
- if(i != 0) {
- ZMR_DEBUG_LOG(s:"They're coming for you! ", d:i );
- }
- else
- ZMR_DEBUG_LOG(s:"He's coming for you!");
- }
- Script "ZMR_Hierophant_GravManip" (void) {
- }
- Script "ZMR_monster_OnIdle" (void) {
- }
- script "ZMR_monster_OnSee" (void) NET {
- ZMR_thunk_prologue
- }
- script "ZMR_monster_OnMelee" (void) {
- }
- script "ZMR_monster_OnMissile" (void) NET {
- ZMR_thunk_prologue
- zmr_write_i32(self + ZMR_MONSTER_FIELD_has_missile, 1);
- }
- script "ZMR_monster_OnPain" (void) NET {
- ZMR_thunk_prologue
- int curr_tick = Timer();
- if(zmr_read_i32(self + ZMR_MONSTER_FIELD_is_fleeing) == 1) {
- if( curr_tick - zmr_read_i32(self + ZMR_MONSTER_FIELD_flee_timepoint) >= 50) {
- zmr_write_i32(self + ZMR_MONSTER_FIELD_is_fleeing, 0);
- SetActorProperty(0, APROP_Frightened, 0);
- int original_speed = GetActorProperty(0, APROP_Speed);
- original_speed = (original_speed >> 2) * 3;
- SetActorProperty(0, APROP_Speed, original_speed);
- ZMR_DEBUG_LOG(s:"Nah, fuck you.");
- /*
- We put some distance between us and them, so if we have
- a missile attack use it
- */
- if(zmr_read_i32(self + ZMR_MONSTER_FIELD_has_missile) == 1)
- SetActorState(0, "Missile");
- else {
- SetActorState(0, "See");
- }
- }
- else
- terminate;
- }
- int myhealth = GetActorProperty(0, APROP_Health);
- int initial_health = zmr_read_i32(self + ZMR_MONSTER_FIELD_initial_health);
- /*
- panic mode - 50% health - 25%
- */
- if(myhealth < (initial_health >> 1) && myhealth >= (initial_health >> 2) ) {
- SetActorProperty(0, APROP_Frightened, 1);
- int original_speed = GetActorProperty(0, APROP_Speed);
- original_speed += original_speed >> 1;
- SetActorProperty(0, APROP_Speed, original_speed);
- zmr_write_i32(self + ZMR_MONSTER_FIELD_flee_timepoint, curr_tick);
- zmr_write_i32(self + ZMR_MONSTER_FIELD_is_fleeing, 1);
- ZMR_DEBUG_LOG(s:"I'm scared!");
- }
- /*
- desperation - other monsters attacking this monster should leave them alone
- monster attacks slightly faster in a sort of frenzied way
- */
- else if (myhealth < (initial_health >> 2)) {
- int reaction_time = GetActorProperty(0, APROP_ReactionTime);
- SetActorProperty(0, APROP_ReactionTime, reaction_time + (reaction_time >> 3));
- SetActorProperty(0, APROP_NOTARGET, 1);
- ZMR_DEBUG_LOG(s:"I'm desperate");
- }
- }
- script "ZMR_monster_OnResurrected" (void) NET {
- zmr_monster_global_ensure_init();
- }
- script "ZMR_monster_OnSpawn" (void) NET {
- zmr_monster_global_ensure_init();
- zmr_assign_uid_impl();
- }
- Script "ZMR_AssignUID" (void) {
- zmr_monster_global_ensure_init();
- //zmr_monster_global_ensure_init();
- zmr_assign_uid_impl();
- }
- script "ZMR_OnUnload" UNLOADING NET {
- zmr_monster_global_ensure_init();
- ACS_NamedTerminate("ZMR_UpdateBuddies", 0);
- zmr_deinit_mman();
- g_zmr_monster_globals_initialized = false;
- g_current_unique_id = 1;
- g_zmr_last_monster_alloc = 0;
- }
- script 32767 (void) NET
- {
- zmr_monster_global_ensure_init();
- //zmr_monster_global_ensure_init();
- SetActorState(0,"Demonic",true);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement