From b4cc671cd8f67bd3559e6f4e3fa69c71a533e785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Sat, 11 Jan 2020 18:05:27 +0100 Subject: [PATCH] Fix active distance --- constants.h | 6 +++--- levels.h | 2 +- main.c | 35 ++++++++++++++++++++--------------- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/constants.h b/constants.h index 36b7e75..fbd0b62 100644 --- a/constants.h +++ b/constants.h @@ -58,10 +58,10 @@ #define SFG_DOOR_OPEN_SPEED 2048 /** - Says the (Chebyshev) distance in game squares at which level elements - (items, monsters etc.) become active. + Says the distance in RCL_Units at which level elements (items, monsters etc.) + are active. */ -#define SFG_LEVEL_ELEMENT_ACTIVE_DISTANCE 10 +#define SFG_LEVEL_ELEMENT_ACTIVE_DISTANCE (12 * 1024) /** Rate at which AI will be updated, which also affects how fast enemies will diff --git a/levels.h b/levels.h index 5413614..5083cc5 100644 --- a/levels.h +++ b/levels.h @@ -255,7 +255,7 @@ SFG_PROGRAM_MEMORY SFG_Level SFG_level0 = 13, // doorTextureIndex 10, // floorColor 32, // ceilingColor - {10,10,0}, // player start: x, y, direction + {8,19,0}, // player start: x, y, direction 0, // backgroundImage { // elements {SFG_LEVEL_ELEMENT_BARREL, {9, 1}}, {SFG_LEVEL_ELEMENT_BARREL, {9, 13}}, diff --git a/main.c b/main.c index 7f40e3a..27aea11 100755 --- a/main.c +++ b/main.c @@ -351,6 +351,13 @@ RCL_Unit SFG_taxicabDistance( return (RCL_absVal(x0 - x1) + RCL_absVal(y0 - y1) + RCL_absVal(z0 - z1)); } +uint8_t SFG_isInActiveDistanceFromPlayer(RCL_Unit x, RCL_Unit y, RCL_Unit z) +{ + return SFG_taxicabDistance( + x,y,z,SFG_player.camera.position.x,SFG_player.camera.position.y, + SFG_player.camera.height) <= SFG_LEVEL_ELEMENT_ACTIVE_DISTANCE; +} + static inline uint8_t SFG_RCLUnitToZBuffer(RCL_Unit x) { x /= RCL_UNITS_PER_SQUARE; @@ -2181,7 +2188,6 @@ void SFG_gameStep() } // handle door: - if (SFG_currentLevel.doorRecordCount > 0) // has to be here { /* Check one door on whether a player is standing nearby. For performance @@ -2226,7 +2232,6 @@ void SFG_gameStep() } // handle items, in a similar manner to door: - if (SFG_currentLevel.itemRecordCount > 0) // has to be here { // check item distances: @@ -2245,11 +2250,10 @@ void SFG_gameStep() SFG_currentLevel.levelPointer->elements[item]; if ( - RCL_absVal(SFG_player.squarePosition[0] - e.coords[0]) - <= SFG_LEVEL_ELEMENT_ACTIVE_DISTANCE - && - RCL_absVal(SFG_player.squarePosition[1] - e.coords[1]) - <= SFG_LEVEL_ELEMENT_ACTIVE_DISTANCE + SFG_isInActiveDistanceFromPlayer( + e.coords[0] * RCL_UNITS_PER_SQUARE + RCL_UNITS_PER_SQUARE / 2, + e.coords[1] * RCL_UNITS_PER_SQUARE + RCL_UNITS_PER_SQUARE / 2, + SFG_floorHeightAt(e.coords[0],e.coords[1]) + RCL_UNITS_PER_SQUARE / 2) ) item |= SFG_ITEM_RECORD_ACTIVE_MASK; @@ -2263,7 +2267,6 @@ void SFG_gameStep() } // similarly handle monsters: - if (SFG_currentLevel.monsterRecordCount > 0) // has to be here { // check monster distances: @@ -2277,11 +2280,14 @@ void SFG_gameStep() &(SFG_currentLevel.monsterRecords[SFG_currentLevel.checkedMonsterIndex]); if ( // far away from the player? - RCL_absVal(SFG_player.squarePosition[0] - monster->coords[0] / 4) - > SFG_LEVEL_ELEMENT_ACTIVE_DISTANCE - || - RCL_absVal(SFG_player.squarePosition[1] - monster->coords[1] / 4) - > SFG_LEVEL_ELEMENT_ACTIVE_DISTANCE + !SFG_isInActiveDistanceFromPlayer( + SFG_MONSTER_COORD_TO_RCL_UNITS(monster->coords[0]), + SFG_MONSTER_COORD_TO_RCL_UNITS(monster->coords[1]), + SFG_floorHeightAt( + SFG_MONSTER_COORD_TO_SQUARES(monster->coords[0]), + SFG_MONSTER_COORD_TO_SQUARES(monster->coords[1])) + + RCL_UNITS_PER_SQUARE / 2 + ) ) { monster->stateType = @@ -2303,8 +2309,7 @@ void SFG_gameStep() } } - // update AI and handle dead monsters - + // update AI and handle dead monsters: if ((SFG_gameFrame - SFG_currentLevel.frameStart) % SFG_AI_UPDATE_FRAME_INTERVAL == 0) {