Skip to content

Commit

Permalink
Overhaul gamemode management
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanae6 committed Aug 7, 2022
1 parent 5f1a911 commit 34691af
Show file tree
Hide file tree
Showing 21 changed files with 348 additions and 300 deletions.
4 changes: 2 additions & 2 deletions MakefileNSO
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ include $(DEVKITPRO)/libnx/switch_rules
#---------------------------------------------------------------------------------
TARGET ?= $(notdir $(CURDIR))$(SMOVER)
BUILD ?= build$(SMOVER)
SOURCES := source/sead/time source/sead source/puppets source/server source/layouts source/states source/cameras source/nx source
SOURCES := source/sead/time source/sead source/puppets source/server/hns source/server/gamemode source/server source/layouts source/states source/cameras source/nx source
DATA := data
INCLUDES := include include/sead

Expand All @@ -46,7 +46,7 @@ CFLAGS := -g -Wall -ffunction-sections \

CFLAGS += $(INCLUDE) -D__SWITCH__ -DSMOVER=$(SMOVER) -O3 -DNNSDK -DSWITCH -DBUILDVERSTR=$(BUILDVERSTR) -DBUILDVER=$(BUILDVER) -DDEBUGLOG=$(DEBUGLOG) -DSERVERIP=$(SERVERIP) -DEMU=$(EMU)

CXXFLAGS := $(CFLAGS) -fno-rtti -fomit-frame-pointer -fno-exceptions -fno-asynchronous-unwind-tables -fno-unwind-tables -std=gnu++20
CXXFLAGS := $(CFLAGS) -Wno-invalid-offsetof -Wno-volatile -fno-rtti -fomit-frame-pointer -fno-exceptions -fno-asynchronous-unwind-tables -fno-unwind-tables -std=gnu++20

ASFLAGS := -g $(ARCH)
LDFLAGS = -specs=../switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) -Wl,--version-script=$(TOPDIR)/exported.txt -Wl,-init=__custom_init -Wl,-fini=__custom_fini -nostdlib
Expand Down
3 changes: 3 additions & 0 deletions include/al/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ namespace al
PlayerActorBase *tryGetPlayerActor(al::PlayerHolder const *, int);

sead::Heap *getCurrentHeap(void);
sead::Heap* getStationedHeap();
sead::Heap* getSequenceHeap();
sead::Heap* getSceneHeap();

al::Projection *getProjection(al::IUseCamera const *, int);

Expand Down
15 changes: 10 additions & 5 deletions include/game/StageScene/StageSceneStateServerConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "logger.hpp"
#include "server/gamemode/GameModeConfigMenu.hpp"
#include "server/gamemode/GameModeConfigMenuFactory.hpp"

class FooterParts;

Expand Down Expand Up @@ -65,14 +66,18 @@ class StageSceneStateServerConfig : public al::HostStateBase<al::Scene>, public
// Root Page, contains buttons for gamemode config, server reconnecting, and server ip address changing
SimpleLayoutMenu* mMainOptions = nullptr;
CommonVerticalList *mMainOptionsList = nullptr;
// Sub-Page for Mode configuration, has buttons for selecting current gamemode and configuring currently selected mode (if no mode is chosen, button will not do anything)
SimpleLayoutMenu* mGamemodeConfig = nullptr;
CommonVerticalList* mGameModeConfigList = nullptr;
// Sub-Page of Mode config, used to select a gamemode for the client to use
SimpleLayoutMenu* mModeSelect = nullptr;
CommonVerticalList* mModeSelectList = nullptr;
// Controls what shows up in specific gamemode config page
GameModeConfigMenu *mGamemodeConfigMenu = nullptr;

// Sub-Pages for Mode configuration, has buttons for selecting current gamemode and configuring currently selected mode (if no mode is chosen, button will not do anything)
struct GameModeEntry {
GameModeConfigMenu* mMenu;
SimpleLayoutMenu* mLayout = nullptr;
CommonVerticalList* mList = nullptr;
};
sead::SafeArray<GameModeEntry, GameModeConfigMenuFactory::getMenuCount()> mGamemodeConfigMenus;
GameModeEntry *mGamemodeConfigMenu = nullptr;

bool mIsDecideConfig = false;
};
Expand Down
58 changes: 7 additions & 51 deletions include/server/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ class Client {
static bool isNeedUpdateShines();
bool isShineCollected(int shineId);

static void initMode(GameModeInitInfo const &initInfo);

static void sendHackCapInfPacket(const HackCap *hackCap);
static void sendPlayerInfPacket(const PlayerActorHakoniwa *player);
static void sendGameInfPacket(const PlayerActorHakoniwa *player, GameDataHolderAccessor holder);
Expand All @@ -110,8 +108,6 @@ class Client {
int getCollectedShinesCount() { return curCollectedShines.size(); }
int getShineID(int index) { if (index < curCollectedShines.size()) { return curCollectedShines[index]; } return -1; }

static void setGameActive(bool state);

static void setStageInfo(GameDataHolderAccessor holder);

static bool tryAddPuppet(PuppetActor *puppet);
Expand All @@ -130,27 +126,8 @@ class Client {

static PuppetActor* getDebugPuppet();

static GameMode getServerMode() {
return sInstance ? sInstance->mServerMode : GameMode::NONE;
}

static void setServerMode(GameMode mode) {
if (sInstance) sInstance->mServerMode = mode;
}

static GameMode getCurrentMode();

static GameModeBase* getModeBase() { return sInstance ? sInstance->mCurMode : nullptr; }

template <typename T>
static T* getMode() {return sInstance ? (T*)sInstance->mCurMode : nullptr;}

static GameModeConfigMenu* tryCreateModeMenu();

static int getMaxPlayerCount() { return sInstance ? sInstance->maxPuppets + 1 : 10;}

static void toggleCurrentMode();

static void updateStates();

static void clearArrays();
Expand All @@ -173,6 +150,12 @@ class Client {
return 0;
}

static PuppetHolder* getPuppetHolder() {
if (sInstance)
return sInstance->mPuppetHolder;
return nullptr;
}

static void setSceneInfo(const al::ActorInitInfo& initInfo, const StageScene *stageScene);

static bool tryRegisterShine(Shine* shine);
Expand All @@ -184,23 +167,6 @@ class Client {
static bool openKeyboardIP();
static bool openKeyboardPort();

static GameModeInfoBase* getModeInfo() {
return sInstance ? sInstance->mModeInfo : nullptr;
}

// should only be called during mode init
static void setModeInfo(GameModeInfoBase* info) {
if(sInstance) sInstance->mModeInfo = info;
}

static void tryRestartCurrentMode();

static bool isModeActive() { return sInstance ? sInstance->mIsModeActive : false; }

static bool isSelectedMode(GameMode mode) {
return sInstance ? sInstance->mCurMode->getMode() == mode : false;
}

static void showConnect();

static void showConnectError(const char16_t* msg);
Expand Down Expand Up @@ -295,21 +261,11 @@ class Client {

sead::FrameHeap *mHeap = nullptr; // Custom FrameHeap used for all Client related memory

// --- Mode Info ---

GameModeBase* mCurMode = nullptr;

GameMode mServerMode = GameMode::NONE; // current mode set by server, will sometimes not match up with current game mode (until scene re-init) if server switches gamemodes

GameModeInfoBase *mModeInfo = nullptr;

bool mIsModeActive = false;

// --- Puppet Info ---

int maxPuppets = 9; // default max player count is 10, so default max puppets will be 9

PuppetInfo *mPuppetInfoArr[MAXPUPINDEX];
PuppetInfo *mPuppetInfoArr[MAXPUPINDEX] = {};

PuppetHolder *mPuppetHolder = nullptr;

Expand Down
4 changes: 2 additions & 2 deletions include/server/gamemode/GameModeBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ enum GameMode : s8 {

// struct containing info about the games state for use in gamemodes
struct GameModeInitInfo {

GameModeInitInfo(al::ActorInitInfo* info, al::Scene *scene){
mLayoutInitInfo = info->mLayoutInitInfo;
mPlayerHolder = info->mActorSceneInfo.mPlayerHolder;
Expand All @@ -47,6 +46,7 @@ struct GameModeInitInfo {
class GameModeBase : public al::IUseName, public al::IUseSceneObjHolder {
public:
GameModeBase(const char* name) { mName = name; }
virtual ~GameModeBase() = default;
const char* getName() const override { return mName.cstr(); }
al::SceneObjHolder* getSceneObjHolder() const override { return mSceneObjHolder; }

Expand All @@ -56,7 +56,7 @@ class GameModeBase : public al::IUseName, public al::IUseSceneObjHolder {

virtual void init(GameModeInitInfo const &info);

virtual void begin() { mIsActive = true;}
virtual void begin() { mIsActive = true; }
virtual void update();
virtual void end() { mIsActive = false; }

Expand Down
38 changes: 38 additions & 0 deletions include/server/gamemode/GameModeConfigMenuFactory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

#include "al/factory/Factory.h"
#include "server/hns/HideAndSeekConfigMenu.hpp"
#include "server/gamemode/GameModeConfigMenu.hpp"

typedef GameModeConfigMenu* (*createMenu)(const char* name);

template <class T>
GameModeConfigMenu* createGameModeConfigMenu(const char* name) {
return new T();
};

__attribute((used)) constexpr al::NameToCreator<createMenu> menuTable[] = {
{"HideAndSeek", &createGameModeConfigMenu<HideAndSeekConfigMenu>},
};

class GameModeConfigMenuFactory : public al::Factory<createMenu> {
public:
GameModeConfigMenuFactory(const char* fName) {
this->factoryName = fName;
this->actorTable = menuTable;
this->factoryCount = sizeof(menuTable) / sizeof(menuTable[0]);
};

constexpr static const char* getMenuName(int idx);
constexpr static int getMenuCount();
};

constexpr const char* GameModeConfigMenuFactory::getMenuName(int idx) {
if (idx >= 0 && idx < sizeof(menuTable) / sizeof(menuTable[0]))
return menuTable[idx].creatorName;
return nullptr;
}

constexpr int GameModeConfigMenuFactory::getMenuCount() {
return sizeof(menuTable) / sizeof(menuTable[0]);
}
9 changes: 4 additions & 5 deletions include/server/gamemode/GameModeFactory.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#pragma once

#include "al/factory/Factory.h"
#include "layouts/HideAndSeekIcon.h"
#include "server/gamemode/GameModeBase.hpp"
#include "server/HideAndSeekMode.hpp"
#include "server/hns/HideAndSeekMode.hpp"

typedef GameModeBase* (*createMode)(const char* name);

Expand Down Expand Up @@ -37,19 +36,19 @@ class GameModeFactory : public al::Factory<createMode> {

// TODO: possibly use shadows' crc32 hash algorithm for this
constexpr const char* GameModeFactory::getModeString(GameMode mode) {
if(mode >= 0 && mode < sizeof(modeTable)/sizeof(modeTable[0]))
if(mode >= 0 && (size_t)mode < sizeof(modeTable)/sizeof(modeTable[0]))
return modeTable[mode].creatorName;
return nullptr;
}

constexpr const char* GameModeFactory::getModeName(GameMode mode) {
if(mode >= 0 && mode < sizeof(modeNames)/sizeof(modeNames[0]))
if(mode >= 0 && (size_t)mode < sizeof(modeNames)/sizeof(modeNames[0]))
return modeNames[mode];
return nullptr;
}

constexpr const char* GameModeFactory::getModeName(int idx) {
if(idx >= 0 && idx < sizeof(modeNames)/sizeof(modeNames[0]))
if(idx >= 0 && (size_t)idx < sizeof(modeNames)/sizeof(modeNames[0]))
return modeNames[idx];
return nullptr;
}
Expand Down
14 changes: 2 additions & 12 deletions include/server/gamemode/GameModeInfoBase.hpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
#pragma once

#include "server/gamemode/GameModeBase.hpp"
#include <heap/seadHeap.h>

// base struct containing info about the current gamemode
struct GameModeInfoBase {
GameMode mMode;
};

template<class T>
T *createModeInfo() {
// using sequence heap to create mode info should allow for mode info to persist between scenes
sead::Heap* seqHeap = sead::HeapMgr::instance()->findHeapByName("SequenceHeap", 0);

if (seqHeap) {
return new (seqHeap) T();
} else {
// if failed to get sequence heap, fall back to current heap (will return null if current heap is also null)
return new T();
}
}
#include "server/gamemode/GameModeManager.hpp"
59 changes: 59 additions & 0 deletions include/server/gamemode/GameModeManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#pragma once

#include <heap/seadDisposer.h>
#include <heap/seadHeap.h>
#include <container/seadSafeArray.h>
#include "server/gamemode/GameModeBase.hpp"
#include "server/gamemode/GameModeInfoBase.hpp"

class GameModeManager {
SEAD_SINGLETON_DISPOSER(GameModeManager)
GameModeManager();
~GameModeManager();

public:
void setMode(GameMode mode);
void initScene(const GameModeInitInfo& info);
void begin();
void end();
void update();

GameMode getGameMode() const { return mCurMode; }
template<class T> T* getMode() const { return static_cast<T*>(mCurModeBase); }
template<class T> T* getInfo() const { return static_cast<T*>(mModeInfo); }
void setInfo(GameModeInfoBase* info) {
mModeInfo = info;
}

template<class T>
T* createModeInfo();

sead::Heap* getHeap() { return mHeap; }
void toggleActive();
void setActive(bool active) { mActive = active; }
void setPaused(bool paused);
bool isMode(GameMode mode) const { return mCurMode == mode; }
bool isActive() const { return mActive; }
bool isModeAndActive(GameMode mode) const { return isMode(mode) && isActive(); }
bool isPaused() const { return mPaused; }
private:
sead::Heap* mHeap = nullptr;

bool mActive = false;
bool mPaused = false;
bool mWasSceneTrans = false;
bool mWasSetMode = false;
GameMode mCurMode = GameMode::NONE;
GameModeBase* mCurModeBase = nullptr;
GameModeInfoBase *mModeInfo = nullptr;
GameModeInitInfo *mLastInitInfo = nullptr;
};

template<class T>
T* GameModeManager::createModeInfo() {
sead::ScopedCurrentHeapSetter heapSetter(mHeap);

T* info = new T();
mModeInfo = info;
return info;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "gamemode/GameModeConfigMenu.hpp"
#include "server/gamemode/GameModeConfigMenu.hpp"
#include "game/Layouts/CommonVerticalList.h"
#include "server/gamemode/GameModeBase.hpp"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

#include <math.h>
#include "al/camera/CameraTicket.h"
#include "gamemode/GameModeBase.hpp"
#include "gamemode/GameModeInfoBase.hpp"
#include "server/gamemode/GameModeBase.hpp"
#include "server/gamemode/GameModeInfoBase.hpp"
#include "server/gamemode/GameModeConfigMenu.hpp"
#include "server/gamemode/GameModeTimer.hpp"
#include "server/HideAndSeekConfigMenu.hpp"
#include "server/hns/HideAndSeekConfigMenu.hpp"

struct HideAndSeekInfo : GameModeInfoBase {
HideAndSeekInfo() { mMode = GameMode::HIDEANDSEEK; }
Expand Down
Loading

0 comments on commit 34691af

Please sign in to comment.