From e35e29bd0be88ba219956698542aab1779146919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Sat, 14 Nov 2020 12:27:09 +0100 Subject: [PATCH] Attack player only if visible --- game.h | 50 +++++++++++++++++++++++++++----------------------- main_sdl.c | 8 ++++---- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/game.h b/game.h index 95efab1..9fa876f 100755 --- a/game.h +++ b/game.h @@ -2146,6 +2146,24 @@ void SFG_getMonsterWorldPosition(SFG_MonsterRecord *monster, RCL_Unit *x, + RCL_UNITS_PER_SQUARE / 2; } +/** + Checks a 3D point visibility from player's position (WITHOUT considering + facing direction). +*/ +static inline uint8_t SFG_spriteIsVisible(RCL_Vector2D pos, RCL_Unit height) +{ + return + RCL_castRay3D( + SFG_player.camera.position, + SFG_player.camera.height, + pos, + height, + SFG_floorHeightAt, + SFG_ceilingHeightAt, + SFG_game.visibilityRayConstraints + ) == RCL_UNITS_PER_SQUARE; +} + void SFG_monsterPerformAI(SFG_MonsterRecord *monster) { uint8_t state = SFG_MR_STATE(*monster); @@ -2177,7 +2195,15 @@ void SFG_monsterPerformAI(SFG_MonsterRecord *monster) SFG_GET_MONSTER_AGGRESSIVITY(SFG_MONSTER_TYPE_TO_INDEX(type))) ) { - if (SFG_random() % 4 != 0) + RCL_Vector2D pos; + pos.x = SFG_MONSTER_COORD_TO_RCL_UNITS(monster->coords[0]); + pos.y = SFG_MONSTER_COORD_TO_RCL_UNITS(monster->coords[1]); + + if (SFG_random() % 4 != 0 && + SFG_spriteIsVisible(pos,currentHeight + // only if player is visible + SFG_SPRITE_SIZE_TO_HEIGHT_ABOVE_GROUND( + SFG_GET_MONSTER_SPRITE_SIZE( + SFG_MONSTER_TYPE_TO_INDEX(type))))) { // attack @@ -2185,12 +2211,8 @@ void SFG_monsterPerformAI(SFG_MonsterRecord *monster) if (type != SFG_LEVEL_ELEMENT_MONSTER_WARRIOR) { - RCL_Vector2D pos; RCL_Vector2D dir; - pos.x = SFG_MONSTER_COORD_TO_RCL_UNITS(monster->coords[0]); - pos.y = SFG_MONSTER_COORD_TO_RCL_UNITS(monster->coords[1]); - dir.x = SFG_player.camera.position.x - pos.x - 128 * SFG_MONSTER_AIM_RANDOMNESS + SFG_random() * SFG_MONSTER_AIM_RANDOMNESS; @@ -2864,24 +2886,6 @@ static inline uint16_t SFG_getMapRevealBit(uint8_t squareX, uint8_t squareY) return 1 << ((squareY / 16) * 4 + squareX / 16); } -/** - Checks a 3D point visibility from player's position (WITHOUT considering - facing direction). -*/ -static inline uint8_t SFG_spriteIsVisible(RCL_Vector2D pos, RCL_Unit height) -{ - return - RCL_castRay3D( - SFG_player.camera.position, - SFG_player.camera.height, - pos, - height, - SFG_floorHeightAt, - SFG_ceilingHeightAt, - SFG_game.visibilityRayConstraints - ) == RCL_UNITS_PER_SQUARE; -} - /** Returns a tangent in RCL_Unit of vertical autoaim, given current game state. */ diff --git a/main_sdl.c b/main_sdl.c index bdd0fd3..1009d7b 100644 --- a/main_sdl.c +++ b/main_sdl.c @@ -47,7 +47,7 @@ #define SFG_BACKGROUND_BLUR 1 #else // lower quality - #define SFG_FPS 30 + #define SFG_FPS 35 #define SFG_SCREEN_RESOLUTION_X 640 #define SFG_SCREEN_RESOLUTION_Y 480 #define SFG_RAYCASTING_SUBSAMPLE 2 @@ -55,19 +55,19 @@ #define SFG_DIMINISH_SPRITES 0 #define SFG_DITHERED_SHADOW 0 #define SFG_BACKGROUND_BLUR 0 - #define SFG_RAYCASTING_MAX_STEPS 20 + #define SFG_RAYCASTING_MAX_STEPS 18 #define SFG_RAYCASTING_MAX_HITS 8 #endif #else // emscripten - #define SFG_FPS 30 + #define SFG_FPS 35 #define SFG_SCREEN_RESOLUTION_X 512 #define SFG_SCREEN_RESOLUTION_Y 320 #define SFG_CAN_EXIT 0 #define SFG_RESOLUTION_SCALEDOWN 2 #define SFG_DITHERED_SHADOW 1 #define SFG_BACKGROUND_BLUR 0 - #define SFG_RAYCASTING_MAX_STEPS 20 + #define SFG_RAYCASTING_MAX_STEPS 18 #define SFG_RAYCASTING_MAX_HITS 8 #include