Skip to content

Commit

Permalink
Merge pull request #58 from MikeIsAStar/avoid-depending-on-the-item-d…
Browse files Browse the repository at this point in the history
…irector-class

[MKW] Avoid depending on the 'ItemDirector' class to assess the behaviour of items
  • Loading branch information
mkwcat authored Mar 5, 2024
2 parents 9f93d02 + 5b522dd commit e0892ae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 44 deletions.
57 changes: 23 additions & 34 deletions payload/import/mkw/item.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <wwfcUtil.h>
#include <wwfcCommon.h>

namespace mkw::Item
{
Expand Down Expand Up @@ -49,40 +49,30 @@ enum class ItemObject {
NoObject = 0x10,
};

class ItemDirector
{
public:
static ItemDirector* Instance()
{
return s_instance;
}

private:
/* 0x000 */ u8 _000[0x430 - 0x000];

static ItemDirector* s_instance
AT(RMCXD_PORT(0x809C3618, 0x809BEE20, 0x809C2678, 0x809B1C58));
};

static_assert(sizeof(ItemDirector) == 0x430);

// https://github.com/SeekyCt/mkw-structures/blob/master/itembehaviour.h
struct ItemBehaviourEntry {
struct ItemBehaviour {
enum class UseType {
Use = 0,
Throw = 1,
Trail = 2,
Circle = 3,
};

/* 0x00 */ u8 _00[0x14 - 0x00];
/* 0x14 */ UseType useType;
/* 0x18 */ void (*useFunction)(void* kartItem);
};

static_assert(sizeof(ItemBehaviourEntry) == 0x1C);
static constexpr UseType s_useType[0x13] = {
UseType::Throw, UseType::Throw, UseType::Throw, UseType::Throw,
UseType::Use, UseType::Use, UseType::Throw, UseType::Throw,
UseType::Use, UseType::Use, UseType::Use, UseType::Use,
UseType::Use, UseType::Use, UseType::Use, UseType::Use,
UseType::Circle, UseType::Circle, UseType::Trail,
};

extern ItemBehaviourEntry itemBehaviourTable[0x13] AT(
RMCXD_PORT(0x809C36A0, 0x809BEE98, 0x809C2700, 0x809B1CE0)
);
static constexpr bool s_hasUseFunction[0x13] = {
false, false, false, false, //
true, true, false, true, //
true, true, true, true, //
true, true, true, true, //
false, false, false,
};
};

static bool IsItemValid(ItemBox item)
{
Expand Down Expand Up @@ -132,8 +122,7 @@ static bool CanUseItem(ItemBox item)

u8 itemToUse = static_cast<u8>(item);

return itemBehaviourTable[itemToUse].useType ==
ItemBehaviourEntry::UseType::Use;
return ItemBehaviour::s_useType[itemToUse] == ItemBehaviour::UseType::Use;
}

static bool CanThrowItem(ItemBox item)
Expand All @@ -144,8 +133,8 @@ static bool CanThrowItem(ItemBox item)

u8 itemToThrow = static_cast<u8>(item);

return itemBehaviourTable[itemToThrow].useType ==
ItemBehaviourEntry::UseType::Throw;
return ItemBehaviour::s_useType[itemToThrow] ==
ItemBehaviour::UseType::Throw;
}

static bool CanTrailItem(ItemBox item)
Expand All @@ -156,7 +145,7 @@ static bool CanTrailItem(ItemBox item)

u8 itemToTrail = static_cast<u8>(item);

return !itemBehaviourTable[itemToTrail].useFunction;
return !ItemBehaviour::s_hasUseFunction[itemToTrail];
}

static bool CanHitItemObject(ItemObject itemObject)
Expand Down
10 changes: 0 additions & 10 deletions payload/wwfcSecurity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,11 +441,6 @@ IsItemPacketDataValid(const void* packet, u8 packetSize, u8 /* playerAid */)
if (!NetController::Instance()->inVanillaRaceScene()) {
return true;
}
// Ensure that the table which controls the behaviour of items is loaded
// into memory before attempting to use it.
if (!mkw::Item::ItemDirector::Instance()) {
return true;
}

RaceConfig::Scenario* scenario = &RaceConfig::Instance()->raceScenario();

Expand Down Expand Up @@ -511,11 +506,6 @@ static bool IsEventPacketDataValid(
) != RKScene::SceneID::Race) {
return true;
}
// Ensure that the table which controls the behaviour of items is loaded
// into memory before attempting to use it.
if (!mkw::Item::ItemDirector::Instance()) {
return true;
}

const EventHandler::Packet* eventPacket =
reinterpret_cast<const EventHandler::Packet*>(packet);
Expand Down

0 comments on commit e0892ae

Please sign in to comment.