#include "scriptapi_item.h"
#include "scriptapi_entity.h"
#include "scriptapi_common.h"
+#include "hud.h"
+
+
+struct EnumString es_HudElementType[] =
+{
+ {HUD_ELEM_IMAGE, "image"},
+ {HUD_ELEM_TEXT, "text"},
+ {HUD_ELEM_STATBAR, "statbar"},
+ {HUD_ELEM_INVENTORY, "inventory"},
+ {0, NULL},
+};
+
+struct EnumString es_HudElementStat[] =
+{
+ {HUD_STAT_POS, "pos"},
+ {HUD_STAT_NAME, "name"},
+ {HUD_STAT_SCALE, "scale"},
+ {HUD_STAT_TEXT, "text"},
+ {HUD_STAT_NUMBER, "number"},
+ {HUD_STAT_ITEM, "item"},
+ {HUD_STAT_DIR, "direction"},
+ {HUD_STAT_ALIGN, "alignment"},
+ {HUD_STAT_OFFSET, "offset"},
+ {0, NULL},
+};
+
+struct EnumString es_HudBuiltinElement[] =
+{
+ {HUD_FLAG_HOTBAR_VISIBLE, "hotbar"},
+ {HUD_FLAG_HEALTHBAR_VISIBLE, "healthbar"},
+ {HUD_FLAG_CROSSHAIR_VISIBLE, "crosshair"},
+ {HUD_FLAG_WIELDITEM_VISIBLE, "wielditem"},
+ {0, NULL},
+};
+
/*
ObjectRef
return 0;
}
+// set_physics_override(self, physics_override_speed, physics_override_jump, physics_override_gravity)
+int ObjectRef::l_set_physics_override(lua_State *L)
+{
+ ObjectRef *ref = checkobject(L, 1);
+ PlayerSAO *co = (PlayerSAO *) getobject(ref);
+ if(co == NULL) return 0;
+ // Do it
+ if(!lua_isnil(L, 2)){
+ co->m_physics_override_speed = lua_tonumber(L, 2);
+ co->m_physics_override_sent = false;
+ }
+ if(!lua_isnil(L, 3)){
+ co->m_physics_override_jump = lua_tonumber(L, 3);
+ co->m_physics_override_sent = false;
+ }
+ if(!lua_isnil(L, 4)){
+ co->m_physics_override_gravity = lua_tonumber(L, 4);
+ co->m_physics_override_sent = false;
+ }
+ return 0;
+}
+
// set_animation(self, frame_range, frame_speed, frame_blend)
int ObjectRef::l_set_animation(lua_State *L)
{
return 1;
}
+// hud_add(self, form)
+int ObjectRef::l_hud_add(lua_State *L)
+{
+ ObjectRef *ref = checkobject(L, 1);
+ Player *player = getplayer(ref);
+ if (player == NULL)
+ return 0;
+
+ HudElement *elem = new HudElement;
+
+ elem->type = (HudElementType)getenumfield(L, 2, "hud_elem_type",
+ es_HudElementType, HUD_ELEM_TEXT);
+
+ lua_getfield(L, 2, "position");
+ elem->pos = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
+ lua_pop(L, 1);
+
+ lua_getfield(L, 2, "scale");
+ elem->scale = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
+ 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);
+ elem->item = getintfield_default(L, 2, "item", 0);
+ elem->dir = getintfield_default(L, 2, "direction", 0);
+
+ lua_getfield(L, 2, "alignment");
+ elem->align = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
+ lua_pop(L, 1);
+
+ lua_getfield(L, 2, "offset");
+ elem->offset = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
+ lua_pop(L, 1);
+
+ u32 id = get_server(L)->hudAdd(player, elem);
+ if (id == (u32)-1) {
+ delete elem;
+ return 0;
+ }
+
+ lua_pushnumber(L, id);
+ return 1;
+}
+
+// hud_remove(self, id)
+int ObjectRef::l_hud_remove(lua_State *L)
+{
+ ObjectRef *ref = checkobject(L, 1);
+ Player *player = getplayer(ref);
+ if (player == NULL)
+ return 0;
+
+ u32 id = -1;
+ if (!lua_isnil(L, 2))
+ id = lua_tonumber(L, 2);
+
+ if (!get_server(L)->hudRemove(player, id))
+ return 0;
+
+ lua_pushboolean(L, true);
+ return 1;
+}
+
+// hud_change(self, id, stat, data)
+int ObjectRef::l_hud_change(lua_State *L)
+{
+ ObjectRef *ref = checkobject(L, 1);
+ Player *player = getplayer(ref);
+ if (player == NULL)
+ return 0;
+
+ u32 id = !lua_isnil(L, 2) ? lua_tonumber(L, 2) : -1;
+ if (id >= player->hud.size())
+ return 0;
+
+ HudElementStat stat = HUD_STAT_NUMBER;
+ if (!lua_isnil(L, 3)) {
+ int statint;
+ std::string statstr = lua_tostring(L, 3);
+ stat = string_to_enum(es_HudElementStat, statint, statstr) ?
+ (HudElementStat)statint : HUD_STAT_NUMBER;
+ }
+
+ void *value = NULL;
+ HudElement *e = player->hud[id];
+ if (!e)
+ return 0;
+
+ switch (stat) {
+ case HUD_STAT_POS:
+ e->pos = read_v2f(L, 4);
+ value = &e->pos;
+ break;
+ case HUD_STAT_NAME:
+ e->name = lua_tostring(L, 4);
+ value = &e->name;
+ break;
+ case HUD_STAT_SCALE:
+ e->scale = read_v2f(L, 4);
+ value = &e->scale;
+ break;
+ case HUD_STAT_TEXT:
+ e->text = lua_tostring(L, 4);
+ value = &e->text;
+ break;
+ case HUD_STAT_NUMBER:
+ e->number = lua_tonumber(L, 4);
+ value = &e->number;
+ break;
+ case HUD_STAT_ITEM:
+ e->item = lua_tonumber(L, 4);
+ value = &e->item;
+ break;
+ case HUD_STAT_DIR:
+ e->dir = lua_tonumber(L, 4);
+ value = &e->dir;
+ case HUD_STAT_ALIGN:
+ e->align = read_v2f(L, 4);
+ value = &e->align;
+ case HUD_STAT_OFFSET:
+ e->offset = read_v2f(L, 4);
+ value = &e->offset;
+ }
+
+ get_server(L)->hudChange(player, id, stat, value);
+
+ lua_pushboolean(L, true);
+ return 1;
+}
+
+// hud_get(self, id)
+int ObjectRef::l_hud_get(lua_State *L)
+{
+ ObjectRef *ref = checkobject(L, 1);
+ Player *player = getplayer(ref);
+ if (player == NULL)
+ return 0;
+
+ u32 id = lua_tonumber(L, -1);
+ if (id >= player->hud.size())
+ return 0;
+
+ HudElement *e = player->hud[id];
+ if (!e)
+ return 0;
+
+ lua_newtable(L);
+
+ lua_pushstring(L, es_HudElementType[(u8)e->type].str);
+ lua_setfield(L, -2, "type");
+
+ push_v2f(L, e->pos);
+ lua_setfield(L, -2, "position");
+
+ lua_pushstring(L, e->name.c_str());
+ lua_setfield(L, -2, "name");
+
+ push_v2f(L, e->scale);
+ lua_setfield(L, -2, "scale");
+
+ lua_pushstring(L, e->text.c_str());
+ lua_setfield(L, -2, "text");
+
+ lua_pushnumber(L, e->number);
+ lua_setfield(L, -2, "number");
+
+ lua_pushnumber(L, e->item);
+ lua_setfield(L, -2, "item");
+
+ lua_pushnumber(L, e->dir);
+ lua_setfield(L, -2, "dir");
+
+ return 1;
+}
+
+// hud_set_flags(self, flags)
+int ObjectRef::l_hud_set_flags(lua_State *L)
+{
+ ObjectRef *ref = checkobject(L, 1);
+ Player *player = getplayer(ref);
+ if (player == NULL)
+ return 0;
+
+ u32 flags = 0;
+ u32 mask = 0;
+ bool flag;
+
+ const EnumString *esp = es_HudBuiltinElement;
+ for (int i = 0; esp[i].str; i++) {
+ if (getboolfield(L, 2, esp[i].str, flag)) {
+ flags |= esp[i].num * flag;
+ mask |= esp[i].num;
+ }
+ }
+ if (!get_server(L)->hudSetFlags(player, flags, mask))
+ return 0;
+
+ lua_pushboolean(L, true);
+ return 1;
+}
ObjectRef::ObjectRef(ServerActiveObject *object):
m_object(object)
luamethod(ObjectRef, get_wielded_item),
luamethod(ObjectRef, set_wielded_item),
luamethod(ObjectRef, set_armor_groups),
+ luamethod(ObjectRef, set_physics_override),
luamethod(ObjectRef, set_animation),
luamethod(ObjectRef, set_bone_position),
luamethod(ObjectRef, set_attach),
luamethod(ObjectRef, get_inventory_formspec),
luamethod(ObjectRef, get_player_control),
luamethod(ObjectRef, get_player_control_bits),
+ luamethod(ObjectRef, hud_add),
+ luamethod(ObjectRef, hud_remove),
+ luamethod(ObjectRef, hud_change),
+ luamethod(ObjectRef, hud_get),
+ luamethod(ObjectRef, hud_set_flags),
{0,0}
};