Skip to content

Commit

Permalink
Add OnPlayerDeath callback
Browse files Browse the repository at this point in the history
  • Loading branch information
gijsgroenewegen committed Dec 28, 2021
1 parent 0954035 commit fb754d6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 36 deletions.
2 changes: 1 addition & 1 deletion CubeModLoader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ add_library (CubeModLoader SHARED
crc.cpp
DLL.cpp
main.cpp
mutex.cpp "macros.h" "callbacks/gui/cube__StartMenuWidget__Draw.h" "callbacks/game/cube__Game__MouseUp.h" "callbacks/gui/cube__GUI__Load.h" "ModWidget.h" "ModWidget.cpp" "callbacks/world/cube__World__SpawnCreaturesMaybe.h")
mutex.cpp "macros.h" "callbacks/gui/cube__StartMenuWidget__Draw.h" "callbacks/game/cube__Game__MouseUp.h" "callbacks/gui/cube__GUI__Load.h" "ModWidget.h" "ModWidget.cpp" "callbacks/world/cube__World__SpawnCreaturesMaybe.h" "callbacks/creature/cube__Creature__OnPlayerDeath.h")
target_link_libraries (CubeModLoader LINK_PUBLIC CWSDK)
2 changes: 1 addition & 1 deletion CubeModLoader/CWSDK
Submodule CWSDK updated from 41ab83 to bb96f1
41 changes: 8 additions & 33 deletions CubeModLoader/callbacks/creature/cube__Creature__GetArmor.h
Original file line number Diff line number Diff line change
@@ -1,56 +1,31 @@
#pragma once

bool cube__Item__ClassCanWearItem(cube::Item* item, int classType)
{
return ((bool(*)(cube::Item*, int))Offset(base, 0x1094D0))(item, classType);
}

float cube__Item__GetArmor(cube::Item* item, cube::Creature* creature)
{
return ((float(*)(cube::Item*, cube::Creature*))Offset(base, 0x108D50))(item, creature);
}

bool cube__Item__IsValidEquipment(cube::Item* item, int category, cube::Creature* creature)
{
if (item->category != category)
{
return false;
}

if (creature->entity_data.hostility_type != 0 || cube__Item__ClassCanWearItem(item, creature->entity_data.classType))
{
return true;
}

return false;
}

extern "C" float cube__Creature__GetArmor(cube::Creature * creature)
{
float armor = 0;

cube::Item* chest = &creature->entity_data.equipment.chest;
if (cube__Item__IsValidEquipment(chest, 4, creature))
if (chest->IsValidEquipmentForCreature(creature, 4))
{
armor += cube__Item__GetArmor(chest, creature);
armor += chest->GetArmor(creature);
}

cube::Item* feet = &creature->entity_data.equipment.feet;
if (cube__Item__IsValidEquipment(feet, 6, creature))
if (feet->IsValidEquipmentForCreature(creature, 6))
{
armor += cube__Item__GetArmor(feet, creature);
armor += feet->GetArmor(creature);
}

cube::Item* hands = &creature->entity_data.equipment.hands;
if (cube__Item__IsValidEquipment(hands, 5, creature))
if (hands->IsValidEquipmentForCreature(creature, 5))
{
armor += cube__Item__GetArmor(hands, creature);
armor += hands->GetArmor(creature);
}

cube::Item* shoulder = &creature->entity_data.equipment.shoulder;
if (cube__Item__IsValidEquipment(shoulder, 7, creature))
if (shoulder->IsValidEquipmentForCreature(creature, 7))
{
armor += cube__Item__GetArmor(shoulder, creature);
armor += shoulder->GetArmor(creature);
}

auto flags2 = creature->entity_data.appearance.flags2;
Expand Down
41 changes: 41 additions & 0 deletions CubeModLoader/callbacks/creature/cube__Creature__OnPlayerDeath.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

extern "C" void cube__Creature__OnPlayerDeath(cube::Game* game)
{
cube::Creature* player = game->world->local_creature;
player->entity_data.HP = 0;

for (uint8_t priority = 0; priority <= 4; priority += 1) {
for (DLL* dll : modDLLs) {
if (dll->mod->OnPlayerDeathPriority == (GenericMod::Priority)priority) {
dll->mod->OnPlayerDeath(game, player);
}
}
}
}

extern "C" void SoundPacket__ctor(void* a1)
{
((void (*)(void*))CWOffset(0x80270))(a1);
}

GETTER_VAR(void*, ASM_cube__Creature__OnPlayerDeath_JMPBACK);
__attribute__((naked)) void ASM_cube__Creature__OnPlayerDeath() {
asm(".intel_syntax \n"

// Move current cube::Game* to the first argument.
// This does not have to be restored, because rcx is set to a value afterwards anyways
"mov rcx, r13 \n"
"call cube__Creature__OnPlayerDeath \n"

// Old code
"lea rcx, [rbp+0x0A20] \n"
"call SoundPacket__ctor \n"
DEREF_JMP(ASM_cube__Creature__OnPlayerDeath_JMPBACK)
);
}

void setup_cube__Creature__OnPlayerDeath() {
WriteFarJMP(CWOffset(0xA8EE7), (void*)&ASM_cube__Creature__OnPlayerDeath);
ASM_cube__Creature__OnPlayerDeath_JMPBACK = CWOffset(0xA8EFA);
}
4 changes: 3 additions & 1 deletion CubeModLoader/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "../CubeModLoader/CWSDK/cwsdk.h"

#define MOD_MAJOR_VERSION 7
#define MOD_MINOR_VERSION 2
#define MOD_MINOR_VERSION 3

#define CUBE_VERSION "1.0.0-1"
#define CUBE_PACKED_CRC 0xC7682619
Expand Down Expand Up @@ -55,10 +55,12 @@ GETTER_VAR(void*, initterm_e); // A pointer to that function
#include "callbacks/gui/cube__StartMenuWidget__Draw.h"
#include "callbacks/gui/cube__GUI__Load.h"
#include "callbacks/creature/cube__Creature__GetArmor.h"
#include "callbacks/creature/cube__Creature__OnPlayerDeath.h"
#include "callbacks/game/cube__Game__MouseUp.h"

void SetupHandlers() {
setup_function(cube__Creature__GetArmor);
setup_function(cube__Creature__OnPlayerDeath);
setup_function(cube__StartMenuWidget__Draw);
setup_function(cube__Game__MouseUp);

Expand Down

0 comments on commit fb754d6

Please sign in to comment.