2 changes: 2 additions & 0 deletions src/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ struct ClientEvent
v2f *align;
v2f *offset;
v3f *world_pos;
v2s32 * size;
} hudadd;
struct{
u32 id;
Expand All @@ -226,6 +227,7 @@ struct ClientEvent
std::string *sdata;
u32 data;
v3f *v3fdata;
v2s32 * v2s32data;
} hudchange;
struct{
video::SColor *bgcolor;
Expand Down
28 changes: 15 additions & 13 deletions src/clientserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ enum ToClientCommand
u8 keep_metadata // Added in protocol version 22
*/
TOCLIENT_REMOVENODE = 0x22,

TOCLIENT_PLAYERPOS = 0x23, // Obsolete
/*
[0] u16 command
Expand All @@ -171,7 +171,7 @@ enum ToClientCommand
[N] u16 peer_id
[N] char[20] name
*/

TOCLIENT_OPT_BLOCK_NOT_FOUND = 0x25, // Obsolete

TOCLIENT_SECTORMETA = 0x26, // Obsolete
Expand All @@ -186,7 +186,7 @@ enum ToClientCommand
[0] u16 command
[2] serialized inventory
*/

TOCLIENT_OBJECTDATA = 0x28, // Obsolete
/*
Sent as unreliable.
Expand Down Expand Up @@ -237,7 +237,7 @@ enum ToClientCommand
string initialization data
}
*/

TOCLIENT_ACTIVE_OBJECT_MESSAGES = 0x32,
/*
u16 command
Expand Down Expand Up @@ -303,21 +303,21 @@ enum ToClientCommand
u16 length of remote media server url (if applicable)
string url
*/

TOCLIENT_TOOLDEF = 0x39,
/*
u16 command
u32 length of the next item
serialized ToolDefManager
*/

TOCLIENT_NODEDEF = 0x3a,
/*
u16 command
u32 length of the next item
serialized NodeDefManager
*/

TOCLIENT_CRAFTITEMDEF = 0x3b,
/*
u16 command
Expand All @@ -344,7 +344,7 @@ enum ToClientCommand
u32 length of next item
serialized ItemDefManager
*/

TOCLIENT_PLAY_SOUND = 0x3f,
/*
u16 command
Expand Down Expand Up @@ -472,6 +472,8 @@ enum ToClientCommand
u32 dir
v2f1000 align
v2f1000 offset
v3f1000 world_pos
v2s32 size
*/

TOCLIENT_HUDRM = 0x4a,
Expand Down Expand Up @@ -530,7 +532,7 @@ enum ToClientCommand
u8 do_override (boolean)
u16 day-night ratio 0...65535
*/

TOCLIENT_LOCAL_PLAYER_ANIMATIONS = 0x51,
/*
u16 command
Expand Down Expand Up @@ -634,7 +636,7 @@ enum ToServerCommand
2: stop digging (all parameters ignored)
3: digging completed
*/

TOSERVER_RELEASE = 0x29, // Obsolete

// (oops, there is some gap here)
Expand Down Expand Up @@ -676,7 +678,7 @@ enum ToServerCommand
[3] u16 id
[5] u16 item
*/

TOSERVER_DAMAGE = 0x35,
/*
u16 command
Expand All @@ -699,7 +701,7 @@ enum ToServerCommand
[0] u16 TOSERVER_PLAYERITEM
[2] u16 item
*/

TOSERVER_RESPAWN=0x38,
/*
u16 TOSERVER_RESPAWN
Expand All @@ -721,7 +723,7 @@ enum ToServerCommand
(Obsoletes TOSERVER_GROUND_ACTION and TOSERVER_CLICK_ACTIVEOBJECT.)
*/

TOSERVER_REMOVED_SOUNDS = 0x3a,
/*
u16 command
Expand Down
11 changes: 9 additions & 2 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2492,6 +2492,7 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
delete event.hudadd.align;
delete event.hudadd.offset;
delete event.hudadd.world_pos;
delete event.hudadd.size;
continue;
}

Expand All @@ -2507,6 +2508,7 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
e->align = *event.hudadd.align;
e->offset = *event.hudadd.offset;
e->world_pos = *event.hudadd.world_pos;
e->size = *event.hudadd.size;

if (id == nhudelem)
player->hud.push_back(e);
Expand All @@ -2520,6 +2522,7 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
delete event.hudadd.align;
delete event.hudadd.offset;
delete event.hudadd.world_pos;
delete event.hudadd.size;
}
else if (event.type == CE_HUDRM)
{
Expand All @@ -2536,6 +2539,7 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
delete event.hudchange.v3fdata;
delete event.hudchange.v2fdata;
delete event.hudchange.sdata;
delete event.hudchange.v2s32data;
continue;
}

Expand Down Expand Up @@ -2571,11 +2575,15 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
case HUD_STAT_WORLD_POS:
e->world_pos = *event.hudchange.v3fdata;
break;
case HUD_STAT_SIZE:
e->size = *event.hudchange.v2s32data;
break;
}

delete event.hudchange.v3fdata;
delete event.hudchange.v2fdata;
delete event.hudchange.sdata;
delete event.hudchange.v2s32data;
}
else if (event.type == CE_SET_SKY)
{
Expand Down Expand Up @@ -3563,8 +3571,7 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
*/
if (show_hud)
{
hud.drawHotbar(client.getHP(), client.getPlayerItem(),
client.getBreath());
hud.drawHotbar(client.getPlayerItem());
}

/*
Expand Down
43 changes: 27 additions & 16 deletions src/hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ void Hud::drawLuaElements(v3s16 camera_offset) {
if (!e)
continue;

v2s32 pos(e->pos.X * m_screensize.X, e->pos.Y * m_screensize.Y);
v2s32 pos(floor(e->pos.X * (float) m_screensize.X + 0.5),
floor(e->pos.Y * (float) m_screensize.Y + 0.5));
switch (e->type) {
case HUD_ELEM_IMAGE: {
video::ITexture *texture = tsrc->getTexture(e->text);
Expand Down Expand Up @@ -266,7 +267,7 @@ void Hud::drawLuaElements(v3s16 camera_offset) {
break; }
case HUD_ELEM_STATBAR: {
v2s32 offs(e->offset.X, e->offset.Y);
drawStatbar(pos, HUD_CORNER_UPPER, e->dir, e->text, e->number, offs);
drawStatbar(pos, HUD_CORNER_UPPER, e->dir, e->text, e->number, offs, e->size);
break; }
case HUD_ELEM_INVENTORY: {
InventoryList *inv = inventory->getList(e->text);
Expand Down Expand Up @@ -308,7 +309,9 @@ void Hud::drawLuaElements(v3s16 camera_offset) {
}


void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture, s32 count, v2s32 offset) {
void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture,
s32 count, v2s32 offset, v2s32 size)
{
const video::SColor color(255, 255, 255, 255);
const video::SColor colors[] = {color, color, color, color};

Expand All @@ -317,10 +320,25 @@ void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture, s
return;

core::dimension2di srcd(stat_texture->getOriginalSize());
core::dimension2di dstd;
if (size == v2s32()) {
dstd = srcd;
} else {
dstd.Height = size.Y * g_settings->getFloat("gui_scaling") *
porting::getDisplayDensity();
dstd.Width = size.X * g_settings->getFloat("gui_scaling") *
porting::getDisplayDensity();

offset.X *= g_settings->getFloat("gui_scaling") *
porting::getDisplayDensity();

offset.Y *= g_settings->getFloat("gui_scaling") *
porting::getDisplayDensity();
}

v2s32 p = pos;
if (corner & HUD_CORNER_LOWER)
p -= srcd.Height;
p -= dstd.Height;

p += offset;

Expand All @@ -338,13 +356,13 @@ void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture, s
default:
steppos = v2s32(1, 0);
}
steppos.X *= srcd.Width;
steppos.Y *= srcd.Height;
steppos.X *= dstd.Width;
steppos.Y *= dstd.Height;

for (s32 i = 0; i < count / 2; i++)
{
core::rect<s32> srcrect(0, 0, srcd.Width, srcd.Height);
core::rect<s32> dstrect(srcrect);
core::rect<s32> dstrect(0,0, dstd.Width, dstd.Height);

dstrect += p;
driver->draw2DImage(stat_texture, dstrect, srcrect, NULL, colors, true);
Expand All @@ -354,15 +372,15 @@ void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture, s
if (count % 2 == 1)
{
core::rect<s32> srcrect(0, 0, srcd.Width / 2, srcd.Height);
core::rect<s32> dstrect(srcrect);
core::rect<s32> dstrect(0,0, dstd.Width / 2, dstd.Height);

dstrect += p;
driver->draw2DImage(stat_texture, dstrect, srcrect, NULL, colors, true);
}
}


void Hud::drawHotbar(s32 halfheartcount, u16 playeritem, s32 breath) {
void Hud::drawHotbar(u16 playeritem) {

v2s32 centerlowerpos(m_displaycenter.X, m_screensize.Y);

Expand Down Expand Up @@ -393,13 +411,6 @@ void Hud::drawHotbar(s32 halfheartcount, u16 playeritem, s32 breath) {
drawItems(secondpos, hotbar_itemcount, hotbar_itemcount/2, mainlist, playeritem + 1, 0);
}
}

if (player->hud_flags & HUD_FLAG_HEALTHBAR_VISIBLE)
drawStatbar(pos - v2s32(0, 4), HUD_CORNER_LOWER, HUD_DIR_LEFT_RIGHT,
"heart.png", halfheartcount, v2s32(0, 0));
if (player->hud_flags & HUD_FLAG_BREATHBAR_VISIBLE && breath <= 10)
drawStatbar(pos - v2s32(-180, 4), HUD_CORNER_LOWER, HUD_DIR_LEFT_RIGHT,
"bubble.png", breath*2, v2s32(0, 0));
}


Expand Down
10 changes: 6 additions & 4 deletions src/hud.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ enum HudElementStat {
HUD_STAT_DIR,
HUD_STAT_ALIGN,
HUD_STAT_OFFSET,
HUD_STAT_WORLD_POS
HUD_STAT_WORLD_POS,
HUD_STAT_SIZE
};

struct HudElement {
Expand All @@ -81,6 +82,7 @@ struct HudElement {
v2f align;
v2f offset;
v3f world_pos;
v2s32 size;
};

#ifndef SERVER
Expand Down Expand Up @@ -122,14 +124,14 @@ class Hud {
u32 text_height, IGameDef *gamedef,
LocalPlayer *player, Inventory *inventory);

void drawHotbar(s32 halfheartcount, u16 playeritem, s32 breath);
void drawHotbar(u16 playeritem);
void resizeHotbar();
void drawCrosshair();
void drawSelectionBoxes(std::vector<aabb3f> &hilightboxes);
void drawLuaElements(v3s16 camera_offset);
private:
void drawStatbar(v2s32 pos, u16 corner, u16 drawdir,
std::string texture, s32 count, v2s32 offset);
void drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture,
s32 count, v2s32 offset, v2s32 size=v2s32());

void drawItems(v2s32 upperleftpos, s32 itemcount, s32 offset,
InventoryList *mainlist, u16 selectitem, u16 direction);
Expand Down
18 changes: 18 additions & 0 deletions src/script/cpp_api/s_env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ void ScriptApiEnv::environment_Step(float dtime)
}
}

void ScriptApiEnv::player_event(ServerActiveObject* player, std::string type)
{
SCRIPTAPI_PRECHECKHEADER

// Get minetest.registered_playerevents
lua_getglobal(L, "minetest");
lua_getfield(L, -1, "registered_playerevents");

// Call callbacks
objectrefGetOrCreate(player); // player
lua_pushstring(L,type.c_str()); // event type
try {
script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_FIRST);
} catch (LuaError &e) {
getServer()->setAsyncFatalError(e.what());
}
}

void ScriptApiEnv::environment_OnMapgenInit(MapgenParams *mgparams)
{
SCRIPTAPI_PRECHECKHEADER
Expand Down
3 changes: 3 additions & 0 deletions src/script/cpp_api/s_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class ScriptApiEnv
// After initializing mapgens
void environment_OnMapgenInit(MapgenParams *mgparams);

//called on player event
void player_event(ServerActiveObject* player, std::string type);

void initializeEnvironment(ServerEnvironment *env);
};

Expand Down
36 changes: 36 additions & 0 deletions src/script/lua_api/l_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,10 @@ int ObjectRef::l_hud_add(lua_State *L)
elem->scale = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
lua_pop(L, 1);

lua_getfield(L, 2, "size");
elem->size = lua_istable(L, -1) ? read_v2s32(L, -1) : v2s32();
lua_pop(L, 1);

elem->name = getstringfield_default(L, 2, "name", "");
elem->text = getstringfield_default(L, 2, "text", "");
elem->number = getintfield_default(L, 2, "number", 0);
Expand All @@ -924,6 +928,11 @@ int ObjectRef::l_hud_add(lua_State *L)
elem->world_pos = lua_istable(L, -1) ? read_v3f(L, -1) : v3f();
lua_pop(L, 1);

/* check for known deprecated element usage */
if ((elem->type == HUD_ELEM_STATBAR) && (elem->size == v2s32())) {
log_deprecated(L,"Deprecated usage of statbar without size!");
}

u32 id = getServer(L)->hudAdd(player, elem);
if (id == (u32)-1) {
delete elem;
Expand Down Expand Up @@ -1019,6 +1028,10 @@ int ObjectRef::l_hud_change(lua_State *L)
e->world_pos = read_v3f(L, 4);
value = &e->world_pos;
break;
case HUD_STAT_SIZE:
e->size = read_v2s32(L, 4);
value = &e->size;
break;
}

getServer(L)->hudChange(player, id, stat, value);
Expand Down Expand Up @@ -1101,6 +1114,28 @@ int ObjectRef::l_hud_set_flags(lua_State *L)
return 1;
}

int ObjectRef::l_hud_get_flags(lua_State *L)
{
ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref);
if (player == NULL)
return 0;

lua_newtable(L);
lua_pushboolean(L, player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE);
lua_setfield(L, -2, "hotbar");
lua_pushboolean(L, player->hud_flags & HUD_FLAG_HEALTHBAR_VISIBLE);
lua_setfield(L, -2, "healthbar");
lua_pushboolean(L, player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE);
lua_setfield(L, -2, "crosshair");
lua_pushboolean(L, player->hud_flags & HUD_FLAG_WIELDITEM_VISIBLE);
lua_setfield(L, -2, "wielditem");
lua_pushboolean(L, player->hud_flags & HUD_FLAG_BREATHBAR_VISIBLE);
lua_setfield(L, -2, "breathbar");

return 1;
}

// hud_set_hotbar_itemcount(self, hotbar_itemcount)
int ObjectRef::l_hud_set_hotbar_itemcount(lua_State *L)
{
Expand Down Expand Up @@ -1321,6 +1356,7 @@ const luaL_reg ObjectRef::methods[] = {
luamethod(ObjectRef, hud_change),
luamethod(ObjectRef, hud_get),
luamethod(ObjectRef, hud_set_flags),
luamethod(ObjectRef, hud_get_flags),
luamethod(ObjectRef, hud_set_hotbar_itemcount),
luamethod(ObjectRef, hud_set_hotbar_image),
luamethod(ObjectRef, hud_set_hotbar_selected_image),
Expand Down
3 changes: 3 additions & 0 deletions src/script/lua_api/l_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ class ObjectRef : public ModApiBase {
// hud_set_flags(self, flags)
static int l_hud_set_flags(lua_State *L);

// hud_get_flags()
static int l_hud_get_flags(lua_State *L);

// hud_set_hotbar_itemcount(self, hotbar_itemcount)
static int l_hud_set_hotbar_itemcount(lua_State *L);

Expand Down
11 changes: 10 additions & 1 deletion src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ void Server::AsyncRunStep(bool initial_step)
/*
Send player breath if changed
*/
if(playersao->m_breath_not_sent){
if(playersao->m_breath_not_sent) {
SendPlayerBreath(*i);
}

Expand Down Expand Up @@ -2202,6 +2202,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
std::istringstream is(datastring, std::ios_base::binary);
u16 breath = readU16(is);
playersao->setBreath(breath);
m_script->player_event(playersao,"breath_changed");
}
else if(command == TOSERVER_PASSWORD)
{
Expand Down Expand Up @@ -3289,6 +3290,7 @@ void Server::SendHUDAdd(u16 peer_id, u32 id, HudElement *form)
writeV2F1000(os, form->align);
writeV2F1000(os, form->offset);
writeV3F1000(os, form->world_pos);
writeV2S32(os,form->size);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spaces after commas! Especially next to code that has spaces after commas.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder what your profession is, something like teacher for some language would be a perfect match. ;-)
I'm gonna fix it in case this is really to be merged.


// Make data buffer
std::string s = os.str();
Expand Down Expand Up @@ -3335,6 +3337,9 @@ void Server::SendHUDChange(u16 peer_id, u32 id, HudElementStat stat, void *value
case HUD_STAT_WORLD_POS:
writeV3F1000(os, *(v3f *)value);
break;
case HUD_STAT_SIZE:
writeV2S32(os,*(v2s32 *)value);
break;
case HUD_STAT_NUMBER:
case HUD_STAT_ITEM:
case HUD_STAT_DIR:
Expand Down Expand Up @@ -3445,6 +3450,7 @@ void Server::SendPlayerHP(u16 peer_id)
assert(playersao);
playersao->m_hp_not_sent = false;
SendHP(peer_id, playersao->getHP());
m_script->player_event(playersao,"health_changed");

// Send to other clients
std::string str = gob_cmd_punched(playersao->readDamage(), playersao->getHP());
Expand All @@ -3458,6 +3464,7 @@ void Server::SendPlayerBreath(u16 peer_id)
PlayerSAO *playersao = getPlayerSAO(peer_id);
assert(playersao);
playersao->m_breath_not_sent = false;
m_script->player_event(playersao,"breath_changed");
SendBreath(peer_id, playersao->getBreath());
}

Expand Down Expand Up @@ -4588,6 +4595,8 @@ bool Server::hudSetFlags(Player *player, u32 flags, u32 mask) {
return false;

SendHUDSetFlags(player->peer_id, flags, mask);

m_script->player_event(player->getPlayerSAO(),"hud_changed");
return true;
}

Expand Down