Add Food Overlay #86

Merged
TheBrokenRail merged 2 commits from bigjango13/minecraft-pi-reborn:apple-skin into master 2024-02-07 00:02:32 +00:00
6 changed files with 78 additions and 2 deletions

View File

@ -53,3 +53,4 @@ TRUE 3D Chest Model
TRUE Replace Block Highlight With Outline
TRUE Use Java Beta 1.3 Light Ramp
TRUE Send Full Level When Hosting Game
FALSE Food Overlay

View File

@ -3,6 +3,7 @@
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <math.h>
#ifndef MCPI_HEADLESS_MODE
#include <GLES/gl.h>
@ -19,6 +20,60 @@
#include "misc-internal.h"
#include <mods/misc/misc.h>
// Heart food overlay
static int heal_amount = 0, heal_amount_drawing = 0;
void Gui_renderHearts_injection(Gui *gui) {
// Get heal_amount
heal_amount = heal_amount_drawing = 0;
Inventory *inventory = gui->minecraft->player->inventory;
ItemInstance *held_ii = Inventory_getSelected(inventory);
if (held_ii) {
Item *held = Item_items[held_ii->id];
if (held->vtable->isFood(held) && held_ii->id) {
int nutrition = ((FoodItem *) held)->nutrition;
int cur_health = gui->minecraft->player->health;
int heal_num = fmin(cur_health + nutrition, 20) - cur_health;
heal_amount = heal_amount_drawing = heal_num;
}
}
// Call original
Gui_renderHearts(gui);
}
#define PINK_HEART_FULL 70
bigjango13 marked this conversation as resolved Outdated

Maybe call this Gui_blit_renderHearts_original? _injection is a bit unclear IMO. Also, does it not let you do = Gui_blit in this line?

Maybe call this `Gui_blit_renderHearts_original`? `_injection` is a bit unclear IMO. Also, does it not let you do ` = Gui_blit` in this line?

Great minds think alike it seems 😉
I tried to do Gui_blit_t Gui_blit_renderHearts_injection = Gui_blit;, however C does not allow this, it requires global initializers to be constant, and the symbol split makes it no longer constant. As for the rename, it's been done.

Great minds think alike it seems 😉 I tried to do `Gui_blit_t Gui_blit_renderHearts_injection = Gui_blit;`, however C does not allow this, it requires global initializers to be constant, and the symbol split makes it no longer constant. As for the rename, it's been done.
#define PINK_HEART_HALF 79
Gui_blit_t Gui_blit_renderHearts_original = NULL;
void Gui_renderHearts_GuiComponent_blit_overlay_empty_injection(Gui *gui, int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t w1, int32_t h1, int32_t w2, int32_t h2) {
// Call original
Gui_blit_renderHearts_original(gui, x1, y1, x2, y2, w1, h1, w2, h2);
// Render the overlay
if (heal_amount_drawing == 1) {
// Half heart
Gui_blit_renderHearts_original(gui, x1, y1, PINK_HEART_HALF, 0, w1, h1, w2, h2);
heal_amount_drawing = 0;
} else if (heal_amount_drawing > 0) {
bigjango13 marked this conversation as resolved
Review

Should these constants (79, 70) be put in a macro so they're not repeated?

Should these constants (79, 70) be put in a macro so they're not repeated?
// Full heart
Gui_blit_renderHearts_original(gui, x1, y1, PINK_HEART_FULL, 0, w1, h1, w2, h2);
heal_amount_drawing -= 2;
}
}
void Gui_renderHearts_GuiComponent_blit_overlay_hearts_injection(Gui *gui, int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t w1, int32_t h1, int32_t w2, int32_t h2) {
// Offset the overlay
if (x2 == 52) {
heal_amount_drawing += 2;
} else if (x2 == 61 && heal_amount) {
// Half heart, flipped
Gui_blit_renderHearts_original(gui, x1, y1, PINK_HEART_FULL, 0, w1, h1, w2, h2);
heal_amount_drawing += 1;
};
// Call original
Gui_blit_renderHearts_original(gui, x1, y1, x2, y2, w1, h1, w2, h2);
heal_amount_drawing = fmin(heal_amount_drawing, heal_amount);
}
// Classic HUD
#define DEFAULT_HUD_PADDING 2
#define NEW_HUD_PADDING 1
@ -568,14 +623,24 @@ void init_misc() {
patch((void *) 0x63c98, invalid_item_background_patch);
}
// Classic HUD
Gui_blit_renderHearts_original = Gui_blit;
if (feature_has("Classic HUD", server_disabled)) {
use_classic_hud = 1;
overwrite_call((void *) 0x266f8, (void *) Gui_renderHearts_GuiComponent_blit_hearts_injection);
overwrite_call((void *) 0x26758, (void *) Gui_renderHearts_GuiComponent_blit_hearts_injection);
overwrite_call((void *) 0x267c8, (void *) Gui_renderHearts_GuiComponent_blit_hearts_injection);
overwrite_call((void *) 0x2656c, (void *) Gui_renderHearts_GuiComponent_blit_armor_injection);
overwrite_call((void *) 0x268c4, (void *) Gui_renderBubbles_GuiComponent_blit_injection);
overwrite_call((void *) 0x266f8, (void *) Gui_renderHearts_GuiComponent_blit_hearts_injection);
overwrite_call((void *) 0x267c8, (void *) Gui_renderHearts_GuiComponent_blit_hearts_injection);
Gui_blit_renderHearts_original = Gui_renderHearts_GuiComponent_blit_hearts_injection;
}
// Food overlay
if (feature_has("Food Overlay", server_disabled)) {
bigjango13 marked this conversation as resolved Outdated

I'd suggest re-ordering this so that Classic HUD is setup first, then Food Overlay. That way, Food Overlay's overwrites can just replace Classic HUD's rather than Classic HUD's having to explicitly check.

I'd suggest re-ordering this so that `Classic HUD` is setup first, then `Food Overlay`. That way, `Food Overlay`'s overwrites can just replace `Classic HUD`'s rather than `Classic HUD`'s having to explicitly check.
overwrite_calls((void *) Gui_renderHearts, Gui_renderHearts_injection);
overwrite_call((void *) 0x266f8, (void *) Gui_renderHearts_GuiComponent_blit_overlay_empty_injection);
overwrite_call((void *) 0x267c8, (void *) Gui_renderHearts_GuiComponent_blit_overlay_hearts_injection);
}
// Render Selected Item Text + Hide Chat Messages

View File

@ -82,6 +82,7 @@ set(SRC
src/item/AuxDataTileItem.def
src/item/ItemInstance.def
src/item/Item.def
src/item/FoodItem.def
src/item/ArmorMaterial.def
src/item/ArmorItem.def
src/item/TileItem.def

View File

@ -13,6 +13,7 @@ method void getSlotPos(int slot, int *x, int *y) = 0x25548;
method void renderSlot(int slot, int x, int y, float alpha) = 0x25cc0;
method void renderSlotText(ItemInstance *item, float x, float y, bool finite, bool shadow) = 0x25df8;
method void handleKeyPressed(int key) = 0x25a08;
method void renderHearts() = 0x2641c;
property Minecraft *minecraft = 0x9f4;
property float selected_item_text_timer = 0x9fc;

View File

@ -0,0 +1,7 @@
extends Item;
size 0x30;
vtable 0x10e7b0;
vtable-size 0x98;
property int nutrition = 0x24;

View File

@ -10,6 +10,7 @@ virtual-method void setIcon(int texture_x, int texture_y) = 0x18;
virtual-method int getIcon(int auxiliary) = 0x14;
virtual-method int useOn(ItemInstance *item_instance, Player *player, Level *level, int x, int y, int z, int hit_side, float hit_x, float hit_y, float hit_z) = 0x20;
virtual-method ItemInstance *use(ItemInstance *item_instance, Level *level, Player *player) = 0x30;
virtual-method bool isFood() = 0x64;
virtual-method void setDescriptionId(std::string *name) = 0x6c;
virtual-method std::string getDescriptionId(ItemInstance *item_instance) = 0x7c;