Skip to content

Commit fb754d6

Browse files
Add OnPlayerDeath callback
1 parent 0954035 commit fb754d6

File tree

5 files changed

+54
-36
lines changed

5 files changed

+54
-36
lines changed

CubeModLoader/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ add_library (CubeModLoader SHARED
55
crc.cpp
66
DLL.cpp
77
main.cpp
8-
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")
8+
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")
99
target_link_libraries (CubeModLoader LINK_PUBLIC CWSDK)

CubeModLoader/CWSDK

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Subproject commit 41ab83dade059778e1ac7409df63ad5d5175173e
1+
Subproject commit bb96f14cc92e5241ac6fa70c8cd0b9de14657b56

CubeModLoader/callbacks/creature/cube__Creature__GetArmor.h

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,31 @@
11
#pragma once
22

3-
bool cube__Item__ClassCanWearItem(cube::Item* item, int classType)
4-
{
5-
return ((bool(*)(cube::Item*, int))Offset(base, 0x1094D0))(item, classType);
6-
}
7-
8-
float cube__Item__GetArmor(cube::Item* item, cube::Creature* creature)
9-
{
10-
return ((float(*)(cube::Item*, cube::Creature*))Offset(base, 0x108D50))(item, creature);
11-
}
12-
13-
bool cube__Item__IsValidEquipment(cube::Item* item, int category, cube::Creature* creature)
14-
{
15-
if (item->category != category)
16-
{
17-
return false;
18-
}
19-
20-
if (creature->entity_data.hostility_type != 0 || cube__Item__ClassCanWearItem(item, creature->entity_data.classType))
21-
{
22-
return true;
23-
}
24-
25-
return false;
26-
}
27-
283
extern "C" float cube__Creature__GetArmor(cube::Creature * creature)
294
{
305
float armor = 0;
316

327
cube::Item* chest = &creature->entity_data.equipment.chest;
33-
if (cube__Item__IsValidEquipment(chest, 4, creature))
8+
if (chest->IsValidEquipmentForCreature(creature, 4))
349
{
35-
armor += cube__Item__GetArmor(chest, creature);
10+
armor += chest->GetArmor(creature);
3611
}
3712

3813
cube::Item* feet = &creature->entity_data.equipment.feet;
39-
if (cube__Item__IsValidEquipment(feet, 6, creature))
14+
if (feet->IsValidEquipmentForCreature(creature, 6))
4015
{
41-
armor += cube__Item__GetArmor(feet, creature);
16+
armor += feet->GetArmor(creature);
4217
}
4318

4419
cube::Item* hands = &creature->entity_data.equipment.hands;
45-
if (cube__Item__IsValidEquipment(hands, 5, creature))
20+
if (hands->IsValidEquipmentForCreature(creature, 5))
4621
{
47-
armor += cube__Item__GetArmor(hands, creature);
22+
armor += hands->GetArmor(creature);
4823
}
4924

5025
cube::Item* shoulder = &creature->entity_data.equipment.shoulder;
51-
if (cube__Item__IsValidEquipment(shoulder, 7, creature))
26+
if (shoulder->IsValidEquipmentForCreature(creature, 7))
5227
{
53-
armor += cube__Item__GetArmor(shoulder, creature);
28+
armor += shoulder->GetArmor(creature);
5429
}
5530

5631
auto flags2 = creature->entity_data.appearance.flags2;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#pragma once
2+
3+
extern "C" void cube__Creature__OnPlayerDeath(cube::Game* game)
4+
{
5+
cube::Creature* player = game->world->local_creature;
6+
player->entity_data.HP = 0;
7+
8+
for (uint8_t priority = 0; priority <= 4; priority += 1) {
9+
for (DLL* dll : modDLLs) {
10+
if (dll->mod->OnPlayerDeathPriority == (GenericMod::Priority)priority) {
11+
dll->mod->OnPlayerDeath(game, player);
12+
}
13+
}
14+
}
15+
}
16+
17+
extern "C" void SoundPacket__ctor(void* a1)
18+
{
19+
((void (*)(void*))CWOffset(0x80270))(a1);
20+
}
21+
22+
GETTER_VAR(void*, ASM_cube__Creature__OnPlayerDeath_JMPBACK);
23+
__attribute__((naked)) void ASM_cube__Creature__OnPlayerDeath() {
24+
asm(".intel_syntax \n"
25+
26+
// Move current cube::Game* to the first argument.
27+
// This does not have to be restored, because rcx is set to a value afterwards anyways
28+
"mov rcx, r13 \n"
29+
"call cube__Creature__OnPlayerDeath \n"
30+
31+
// Old code
32+
"lea rcx, [rbp+0x0A20] \n"
33+
"call SoundPacket__ctor \n"
34+
DEREF_JMP(ASM_cube__Creature__OnPlayerDeath_JMPBACK)
35+
);
36+
}
37+
38+
void setup_cube__Creature__OnPlayerDeath() {
39+
WriteFarJMP(CWOffset(0xA8EE7), (void*)&ASM_cube__Creature__OnPlayerDeath);
40+
ASM_cube__Creature__OnPlayerDeath_JMPBACK = CWOffset(0xA8EFA);
41+
}

CubeModLoader/main.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "../CubeModLoader/CWSDK/cwsdk.h"
1212

1313
#define MOD_MAJOR_VERSION 7
14-
#define MOD_MINOR_VERSION 2
14+
#define MOD_MINOR_VERSION 3
1515

1616
#define CUBE_VERSION "1.0.0-1"
1717
#define CUBE_PACKED_CRC 0xC7682619
@@ -55,10 +55,12 @@ GETTER_VAR(void*, initterm_e); // A pointer to that function
5555
#include "callbacks/gui/cube__StartMenuWidget__Draw.h"
5656
#include "callbacks/gui/cube__GUI__Load.h"
5757
#include "callbacks/creature/cube__Creature__GetArmor.h"
58+
#include "callbacks/creature/cube__Creature__OnPlayerDeath.h"
5859
#include "callbacks/game/cube__Game__MouseUp.h"
5960

6061
void SetupHandlers() {
6162
setup_function(cube__Creature__GetArmor);
63+
setup_function(cube__Creature__OnPlayerDeath);
6264
setup_function(cube__StartMenuWidget__Draw);
6365
setup_function(cube__Game__MouseUp);
6466

0 commit comments

Comments
 (0)