summaryrefslogtreecommitdiff
path: root/src/scriptapi_object.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/scriptapi_object.cpp')
-rw-r--r--src/scriptapi_object.cpp189
1 files changed, 189 insertions, 0 deletions
diff --git a/src/scriptapi_object.cpp b/src/scriptapi_object.cpp
index 05433a598..531bb7a58 100644
--- a/src/scriptapi_object.cpp
+++ b/src/scriptapi_object.cpp
@@ -26,6 +26,30 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#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"},
+ {0, NULL},
+};
+
/*
ObjectRef
@@ -700,6 +724,165 @@ int ObjectRef::l_get_player_control_bits(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, "dir", 0);
+
+ 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 = -1;
+ if (!lua_isnil(L, 2))
+ id = lua_tonumber(L, 2);
+
+ HudElementStat stat = (HudElementStat)getenumfield(L, 3, "stat",
+ es_HudElementStat, HUD_STAT_NUMBER);
+
+ if (id >= player->hud.size())
+ return 0;
+
+ 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;
+ }
+
+ 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;
+}
ObjectRef::ObjectRef(ServerActiveObject *object):
m_object(object)
@@ -807,6 +990,12 @@ const luaL_reg ObjectRef::methods[] = {
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_lock_next_bar),
+ //luamethod(ObjectRef, hud_unlock_bar),
{0,0}
};