Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MKW] Avoid depending on the 'ItemDirector' class to assess the behaviour of items #58

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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