Skip to content

Commit

Permalink
[+] Some GridItem functions and fixes
Browse files Browse the repository at this point in the history
[+] ChallengeDefinition
[+] Ability to add brand new minigames
[*] Inclusion fix in Lunacy.hpp
  • Loading branch information
0xVB committed Sep 7, 2024
1 parent 62026c6 commit 68611fb
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 3 deletions.
32 changes: 32 additions & 0 deletions include/Lunacy/Challange.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,36 @@ class Challenge
int mScaryPotterPots;
int mRainCounter;
int mTreeOfWisdomTalkIndex;
};

class ChallengeDefinition
{
private:
static ChallengeDefinition* _gDefArray;
static ChallengeDefinition* _gDefArrayEnd;
static unsigned int _gUsedDefs;
static unsigned int _gMaxDefs;
static size_t _gDefSize;

static unsigned int _aEndRefCount;
static unsigned int _aEndRefs[];

static unsigned int _aRefCount;
static unsigned int _aRefs[];

ChallengeDefinition();
ChallengeDefinition(GameMode, ChallengePage, int IconIndex, int Row, int Col, const char* Name);

public:
GameMode mChallengeMode;
int mChallengeIconIndex;
ChallengePage mPage;
int mRow;
int mCol;
const char* mChallengeName;

static ChallengeDefinition* Reallocate(unsigned int NewCapacity);
static ChallengeDefinition* AddChallenge(const char* Name, ChallengePage, int Row, int Col);
static ChallengeDefinition* GetDefinition(GameMode);
static ChallengeDefinition* GetDefinitions();
};
4 changes: 3 additions & 1 deletion include/Lunacy/ConstEnums.h
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,9 @@ enum GameMode
GAMEMODE_PUZZLE_I_ZOMBIE_ENDLESS,
GAMEMODE_UPSELL,
GAMEMODE_INTRO,
NUM_GAME_MODES
NUM_GAME_MODES,

BASE_NUM_GAMEMODES = GAMEMODE_INTRO + 1
};
enum GameObjectType
{
Expand Down
3 changes: 2 additions & 1 deletion include/Lunacy/GridItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ class GridItem
void OpenPortal();
void ClosePortal();

Sexy::IVector2 GetHitbox();
Sexy::IRect GetHitbox();
Sexy::IVector2 GetCenter();

std::list<GridItem*> GetGridItemsAround(int ColRange, int RowRange, GridItemType = GRIDITEM_NONE, bool IsBlacklist = false);
std::list<Zombie*> GetZombiesAround(int ColRange, int RowRange, ZombieType = ZOMBIE_NONE, bool IsBlacklist = false);
Expand Down
1 change: 1 addition & 0 deletions include/Lunacy/Lunacy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "UIRoot.h"
#include "Image.h"

#include "SeedBank.h"
#include "Challange.h"
#include "Projectile.h"
#include "LawnMower.h"
Expand Down
114 changes: 114 additions & 0 deletions src/Lunacy/ChallengeDefinition.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include "Lunacy/Challange.h"

ChallengeDefinition* const GLOBAL_DEFS = (ChallengeDefinition*)0x6A2BA0;
ChallengeDefinition* ChallengeDefinition::_gDefArray = GLOBAL_DEFS;
ChallengeDefinition* ChallengeDefinition::_gDefArrayEnd = (ChallengeDefinition*)0x6A3278;
unsigned int ChallengeDefinition::_gUsedDefs = BASE_NUM_GAMEMODES;
unsigned int ChallengeDefinition::_gMaxDefs = BASE_NUM_GAMEMODES;
size_t ChallengeDefinition::_gDefSize = 0x6A3278 - 0x6A2BA0;

unsigned int ChallengeDefinition::_aRefCount = 10;
unsigned int ChallengeDefinition::_aRefs[] =
{
0x42DF79,
0x42E3E4,
0x42E459,
0x42E515,
0x42E5AA,
0x42E720,
0x42E8B6,
0x42E965,
0x42F834,
0x455BA5
};

unsigned int ChallengeDefinition::_aEndRefCount = 9;
unsigned int ChallengeDefinition::_aEndRefs[] =
{
0x424A0A,
0x424CEE,
0x424E46,
0x42E066,
0x42E425,
0x42E541,
0x42E5D1,
0x42F963,
0x455C08
};

ChallengeDefinition* ChallengeDefinition::AddChallenge(const char* Name, ChallengePage Page, int Row, int Col)
{
if (_gMaxDefs >= _gUsedDefs)
Reallocate(_gUsedDefs * 2);

ChallengeDefinition* NewChallenge = _gDefArray + _gUsedDefs;

NewChallenge->mChallengeMode = (GameMode)_gUsedDefs;
NewChallenge->mChallengeIconIndex = 0;
NewChallenge->mChallengeName = Name;
NewChallenge->mPage = Page;
NewChallenge->mRow = Row;
NewChallenge->mCol = Col;

_gUsedDefs++;
return NewChallenge;
}

ChallengeDefinition* ChallengeDefinition::GetDefinitions()
{
return _gDefArray;
}

ChallengeDefinition* ChallengeDefinition::GetDefinition(GameMode Mode)
{
return _gDefArray + Mode;
}

ChallengeDefinition* ChallengeDefinition::Reallocate(unsigned int NewCapacity)
{
ChallengeDefinition* NewArray = (ChallengeDefinition*)operator new(sizeof(ChallengeDefinition) * NewCapacity);
memset(NewArray, 0, NewCapacity * sizeof(ChallengeDefinition));
memcpy(NewArray, _gDefArray, _gDefSize);

unsigned int NewDefAddress = (unsigned int)NewArray;
for (int i = 0; i < _aRefCount; i++)
{
auto RefP = (unsigned int*)_aRefs[i];
unsigned int Ref;

DWORD OldProt;
VirtualProtect(RefP, 4, PAGE_EXECUTE_READWRITE, &OldProt);
Ref = *RefP;

unsigned int Offset = Ref - (unsigned int)_gDefArray;
Ref = NewDefAddress + Offset;
*RefP = Ref;
VirtualProtect(RefP, 4, OldProt, &OldProt);
}

NewDefAddress = (unsigned int)NewArray + sizeof(ChallengeDefinition) * NewCapacity;
for (int i = 0; i < _aEndRefCount; i++)
{
auto RefP = (unsigned int*)_aEndRefs[i];
unsigned int Ref;

DWORD OldProt;
VirtualProtect(RefP, 4, PAGE_EXECUTE_READWRITE, &OldProt);
Ref = *RefP;

unsigned int Offset = Ref - (unsigned int)_gDefArrayEnd;
Ref = NewDefAddress + Offset;
*RefP = Ref;
VirtualProtect(RefP, 4, OldProt, &OldProt);
}

if (_gDefArray != GLOBAL_DEFS)
delete _gDefArray;

_gDefArray = NewArray;
_gDefArrayEnd = NewArray + NewCapacity;
_gDefSize = (unsigned int)_gDefArrayEnd - (unsigned int)_gDefArray;
_gMaxDefs = NewCapacity;

return NewArray;
}
6 changes: 5 additions & 1 deletion src/Lunacy/GridItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ GridItem::GridItem(GridItemType Type)
mMotionTrailCount = 0;
}

Sexy::IVector2 GridItem::GetHitbox()
Sexy::IRect GridItem::GetHitbox()
{
return mLawn->GridToPixelArea(mCol, mRow, 1, 1);
}
Sexy::IVector2 GridItem::GetCenter()
{
return GetHitbox().GetCenter();
}

std::list<GridItem*> GridItem::GetGridItemsAround(int ColRange, int RowRange, GridItemType Type, bool Blacklist)
{
Expand Down

0 comments on commit 68611fb

Please sign in to comment.