Skip to content

Commit

Permalink
add util function for enum class masks, fix get_liquids_at OOB check
Browse files Browse the repository at this point in the history
  • Loading branch information
estebanfer committed Dec 9, 2024
1 parent 5f3eed6 commit 1ca5c18
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/game_api/aliases.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ enum class PAUSE_SCREEN : int64_t
};
ENUM_CLASS_FLAGS(PAUSE_SCREEN);

enum class ENTITY_MASK
enum class ENTITY_MASK : uint32_t
{
PLAYER = 0x1,
MOUNT = 0x2,
Expand All @@ -212,3 +212,11 @@ enum class ENTITY_MASK
ANY = 0x0,
};
ENUM_CLASS_FLAGS(ENTITY_MASK)

// Returns true if any of the set bits in `mask` are in `flags`
template <class T>
requires std::is_enum_v<T>
bool test_mask(T flags, T mask)
{
return static_cast<std::underlying_type_t<T>>(flags & mask) != 0;
}
7 changes: 4 additions & 3 deletions src/game_api/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <unordered_set> // for _Uset_traits<>::allocator_type, _Use...
#include <utility> // for min, max, pair, find

#include "aliases.hpp"
#include "bucket.hpp"
#include "containers/custom_vector.hpp" //
#include "custom_types.hpp" // for get_custom_entity_types, CUSTOM_TYPE
Expand Down Expand Up @@ -1360,7 +1361,7 @@ uint8_t get_liquids_at(ENTITY_MASK liquid_mask, float x, float y, LAYER layer)
LiquidPhysics* liquid_physics = State::get().ptr()->liquid_physics;
if (actual_layer != get_liquid_layer())
return 0;
if (y > 125.5 || y < 0 || x > 85.5 || x < 0)
if (y >= 125.5 || y < 0 || x >= 85.5 || x < 0) // Game uses `>` instead of `>=`, resulting in it being able to read out of bounds of the array, but it never happens without OL because of the border blocks
return 0;

uint32_t ix = static_cast<int>((x + 0.5) / 0.3333333);
Expand All @@ -1373,9 +1374,9 @@ uint8_t get_liquids_at(ENTITY_MASK liquid_mask, float x, float y, LAYER layer)
else
{
uint8_t liquids_num = 0;
if ((liquid_mask & ENTITY_MASK::WATER) == ENTITY_MASK::WATER)
if (test_mask(liquid_mask, ENTITY_MASK::WATER))
liquids_num += liquids_at.water;
if ((liquid_mask & ENTITY_MASK::LAVA) == ENTITY_MASK::LAVA)
if (test_mask(liquid_mask, ENTITY_MASK::LAVA))
liquids_num += liquids_at.lava;
return liquids_num;
}
Expand Down

0 comments on commit 1ca5c18

Please sign in to comment.