Small cleanup of hud add/remove code

master
sapier 2014-05-25 14:34:32 +02:00
parent 5bd2aea663
commit d76b8c6e7c
6 changed files with 100 additions and 75 deletions

View File

@ -2333,16 +2333,13 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
{ {
// Read client events // Read client events
for(;;) for(;;) {
{
ClientEvent event = client.getClientEvent(); ClientEvent event = client.getClientEvent();
if(event.type == CE_NONE) if(event.type == CE_NONE) {
{
break; break;
} }
else if(event.type == CE_PLAYER_DAMAGE && else if(event.type == CE_PLAYER_DAMAGE &&
client.getHP() != 0) client.getHP() != 0) {
{
//u16 damage = event.player_damage.amount; //u16 damage = event.player_damage.amount;
//infostream<<"Player damage: "<<damage<<std::endl; //infostream<<"Player damage: "<<damage<<std::endl;
@ -2356,13 +2353,11 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
MtEvent *e = new SimpleTriggerEvent("PlayerDamage"); MtEvent *e = new SimpleTriggerEvent("PlayerDamage");
gamedef->event()->put(e); gamedef->event()->put(e);
} }
else if(event.type == CE_PLAYER_FORCE_MOVE) else if(event.type == CE_PLAYER_FORCE_MOVE) {
{
camera_yaw = event.player_force_move.yaw; camera_yaw = event.player_force_move.yaw;
camera_pitch = event.player_force_move.pitch; camera_pitch = event.player_force_move.pitch;
} }
else if(event.type == CE_DEATHSCREEN) else if(event.type == CE_DEATHSCREEN) {
{
show_deathscreen(&current_formspec, &client, gamedef, tsrc, show_deathscreen(&current_formspec, &client, gamedef, tsrc,
device, &client); device, &client);
@ -2376,8 +2371,7 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
player->hurt_tilt_strength = 0; player->hurt_tilt_strength = 0;
} }
else if (event.type == CE_SHOW_FORMSPEC) else if (event.type == CE_SHOW_FORMSPEC) {
{
FormspecFormSource* fs_src = FormspecFormSource* fs_src =
new FormspecFormSource(*(event.show_formspec.formspec)); new FormspecFormSource(*(event.show_formspec.formspec));
TextDestPlayerInventory* txt_dst = TextDestPlayerInventory* txt_dst =
@ -2389,8 +2383,7 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
delete(event.show_formspec.formspec); delete(event.show_formspec.formspec);
delete(event.show_formspec.formname); delete(event.show_formspec.formname);
} }
else if(event.type == CE_SPAWN_PARTICLE) else if(event.type == CE_SPAWN_PARTICLE) {
{
LocalPlayer* player = client.getEnv().getLocalPlayer(); LocalPlayer* player = client.getEnv().getLocalPlayer();
video::ITexture *texture = video::ITexture *texture =
gamedef->tsrc()->getTexture(*(event.spawn_particle.texture)); gamedef->tsrc()->getTexture(*(event.spawn_particle.texture));
@ -2407,8 +2400,7 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
v2f(0.0, 0.0), v2f(0.0, 0.0),
v2f(1.0, 1.0)); v2f(1.0, 1.0));
} }
else if(event.type == CE_ADD_PARTICLESPAWNER) else if(event.type == CE_ADD_PARTICLESPAWNER) {
{
LocalPlayer* player = client.getEnv().getLocalPlayer(); LocalPlayer* player = client.getEnv().getLocalPlayer();
video::ITexture *texture = video::ITexture *texture =
gamedef->tsrc()->getTexture(*(event.add_particlespawner.texture)); gamedef->tsrc()->getTexture(*(event.add_particlespawner.texture));
@ -2431,15 +2423,15 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
texture, texture,
event.add_particlespawner.id); event.add_particlespawner.id);
} }
else if(event.type == CE_DELETE_PARTICLESPAWNER) else if(event.type == CE_DELETE_PARTICLESPAWNER) {
{
delete_particlespawner (event.delete_particlespawner.id); delete_particlespawner (event.delete_particlespawner.id);
} }
else if (event.type == CE_HUDADD) else if (event.type == CE_HUDADD) {
{
u32 id = event.hudadd.id; u32 id = event.hudadd.id;
size_t nhudelem = player->hud.size();
if (id > nhudelem || (id < nhudelem && player->hud[id])) { HudElement *e = player->getHud(id);
if (e != NULL) {
delete event.hudadd.pos; delete event.hudadd.pos;
delete event.hudadd.name; delete event.hudadd.name;
delete event.hudadd.scale; delete event.hudadd.scale;
@ -2451,7 +2443,7 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
continue; continue;
} }
HudElement *e = new HudElement; e = new HudElement;
e->type = (HudElementType)event.hudadd.type; e->type = (HudElementType)event.hudadd.type;
e->pos = *event.hudadd.pos; e->pos = *event.hudadd.pos;
e->name = *event.hudadd.name; e->name = *event.hudadd.name;
@ -2465,10 +2457,9 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
e->world_pos = *event.hudadd.world_pos; e->world_pos = *event.hudadd.world_pos;
e->size = *event.hudadd.size; e->size = *event.hudadd.size;
if (id == nhudelem) u32 new_id = player->addHud(e);
player->hud.push_back(e); //if this isn't true our huds aren't consistent
else assert(new_id == id);
player->hud[id] = e;
delete event.hudadd.pos; delete event.hudadd.pos;
delete event.hudadd.name; delete event.hudadd.name;
@ -2479,18 +2470,17 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
delete event.hudadd.world_pos; delete event.hudadd.world_pos;
delete event.hudadd.size; delete event.hudadd.size;
} }
else if (event.type == CE_HUDRM) else if (event.type == CE_HUDRM) {
{ HudElement* e = player->removeHud(event.hudrm.id);
u32 id = event.hudrm.id;
if (id < player->hud.size() && player->hud[id]) { if (e != NULL)
delete player->hud[id]; delete (e);
player->hud[id] = NULL;
}
} }
else if (event.type == CE_HUDCHANGE) else if (event.type == CE_HUDCHANGE) {
{
u32 id = event.hudchange.id; u32 id = event.hudchange.id;
if (id >= player->hud.size() || !player->hud[id]) { HudElement* e = player->getHud(id);
if (e == NULL)
{
delete event.hudchange.v3fdata; delete event.hudchange.v3fdata;
delete event.hudchange.v2fdata; delete event.hudchange.v2fdata;
delete event.hudchange.sdata; delete event.hudchange.sdata;
@ -2498,7 +2488,6 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
continue; continue;
} }
HudElement* e = player->hud[id];
switch (event.hudchange.stat) { switch (event.hudchange.stat) {
case HUD_STAT_POS: case HUD_STAT_POS:
e->pos = *event.hudchange.v2fdata; e->pos = *event.hudchange.v2fdata;
@ -2540,19 +2529,18 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
delete event.hudchange.sdata; delete event.hudchange.sdata;
delete event.hudchange.v2s32data; delete event.hudchange.v2s32data;
} }
else if (event.type == CE_SET_SKY) else if (event.type == CE_SET_SKY) {
{
sky->setVisible(false); sky->setVisible(false);
if(skybox){ if(skybox){
skybox->drop(); skybox->drop();
skybox = NULL; skybox = NULL;
} }
// Handle according to type // Handle according to type
if(*event.set_sky.type == "regular"){ if(*event.set_sky.type == "regular") {
sky->setVisible(true); sky->setVisible(true);
} }
else if(*event.set_sky.type == "skybox" && else if(*event.set_sky.type == "skybox" &&
event.set_sky.params->size() == 6){ event.set_sky.params->size() == 6) {
sky->setFallbackBgColor(*event.set_sky.bgcolor); sky->setFallbackBgColor(*event.set_sky.bgcolor);
skybox = smgr->addSkyBoxSceneNode( skybox = smgr->addSkyBoxSceneNode(
tsrc->getTexture((*event.set_sky.params)[0]), tsrc->getTexture((*event.set_sky.params)[0]),
@ -2574,8 +2562,7 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
delete event.set_sky.type; delete event.set_sky.type;
delete event.set_sky.params; delete event.set_sky.params;
} }
else if (event.type == CE_OVERRIDE_DAY_NIGHT_RATIO) else if (event.type == CE_OVERRIDE_DAY_NIGHT_RATIO) {
{
bool enable = event.override_day_night_ratio.do_override; bool enable = event.override_day_night_ratio.do_override;
u32 value = event.override_day_night_ratio.ratio_f * 1000; u32 value = event.override_day_night_ratio.ratio_f * 1000;
client.getEnv().setDayNightRatioOverride(enable, value); client.getEnv().setDayNightRatioOverride(enable, value);

View File

@ -226,8 +226,8 @@ void Hud::drawItems(v2s32 upperleftpos, s32 itemcount, s32 offset,
void Hud::drawLuaElements(v3s16 camera_offset) { void Hud::drawLuaElements(v3s16 camera_offset) {
for (size_t i = 0; i != player->hud.size(); i++) { for (size_t i = 0; i != player->maxHudId(); i++) {
HudElement *e = player->hud[i]; HudElement *e = player->getHud(i);
if (!e) if (!e)
continue; continue;

View File

@ -216,19 +216,18 @@ void Player::deSerialize(std::istream &is, std::string playername)
setPosition(args.getV3F("position")); setPosition(args.getV3F("position"));
try{ try{
hp = args.getS32("hp"); hp = args.getS32("hp");
}catch(SettingNotFoundException &e){ }catch(SettingNotFoundException &e) {
hp = 20; hp = 20;
} }
try{ try{
m_breath = args.getS32("breath"); m_breath = args.getS32("breath");
}catch(SettingNotFoundException &e){ }catch(SettingNotFoundException &e) {
m_breath = 11; m_breath = 11;
} }
inventory.deSerialize(is); inventory.deSerialize(is);
if(inventory.getList("craftpreview") == NULL) if(inventory.getList("craftpreview") == NULL) {
{
// Convert players without craftpreview // Convert players without craftpreview
inventory.addList("craftpreview", 1); inventory.addList("craftpreview", 1);
@ -246,14 +245,47 @@ void Player::deSerialize(std::istream &is, std::string playername)
checkModified(); checkModified();
} }
u32 Player::addHud(HudElement *toadd)
{
u32 id = getFreeHudID();
if (id < hud.size())
hud[id] = toadd;
else
hud.push_back(toadd);
return id;
}
HudElement* Player::getHud(u32 id)
{
if (id < hud.size())
return hud[id];
return NULL;
}
HudElement* Player::removeHud(u32 id)
{
HudElement* retval = NULL;
if (id < hud.size()) {
retval = hud[id];
hud[id] = NULL;
}
return retval;
}
void Player::clearHud()
{
while(!hud.empty()) {
delete hud.back();
hud.pop_back();
}
}
/* /*
RemotePlayer RemotePlayer
*/ */
void RemotePlayer::setPosition(const v3f &position) void RemotePlayer::setPosition(const v3f &position)
{ {
Player::setPosition(position); Player::setPosition(position);

View File

@ -294,10 +294,17 @@ public:
u32 keyPressed; u32 keyPressed;
std::vector<HudElement *> hud;
HudElement* getHud(u32 id);
u32 addHud(HudElement* hud);
HudElement* removeHud(u32 id);
void clearHud();
u32 maxHudId() {
return hud.size();
}
u32 hud_flags; u32 hud_flags;
s32 hud_hotbar_itemcount; s32 hud_hotbar_itemcount;
protected: protected:
IGameDef *m_gamedef; IGameDef *m_gamedef;
@ -314,6 +321,8 @@ protected:
v3f m_last_pos; v3f m_last_pos;
u16 m_last_hp; u16 m_last_hp;
Inventory m_last_inventory; Inventory m_last_inventory;
std::vector<HudElement *> hud;
}; };

View File

@ -971,7 +971,9 @@ int ObjectRef::l_hud_change(lua_State *L)
return 0; return 0;
u32 id = !lua_isnil(L, 2) ? lua_tonumber(L, 2) : -1; u32 id = !lua_isnil(L, 2) ? lua_tonumber(L, 2) : -1;
if (id >= player->hud.size())
HudElement *e = player->getHud(id);
if (!e)
return 0; return 0;
HudElementStat stat = HUD_STAT_NUMBER; HudElementStat stat = HUD_STAT_NUMBER;
@ -983,10 +985,6 @@ int ObjectRef::l_hud_change(lua_State *L)
} }
void *value = NULL; void *value = NULL;
HudElement *e = player->hud[id];
if (!e)
return 0;
switch (stat) { switch (stat) {
case HUD_STAT_POS: case HUD_STAT_POS:
e->pos = read_v2f(L, 4); e->pos = read_v2f(L, 4);
@ -1049,10 +1047,8 @@ int ObjectRef::l_hud_get(lua_State *L)
return 0; return 0;
u32 id = lua_tonumber(L, -1); u32 id = lua_tonumber(L, -1);
if (id >= player->hud.size())
return 0;
HudElement *e = player->hud[id]; HudElement *e = player->getHud(id);
if (!e) if (!e)
return 0; return 0;

View File

@ -4560,24 +4560,25 @@ bool Server::showFormspec(const char *playername, const std::string &formspec, c
u32 Server::hudAdd(Player *player, HudElement *form) { u32 Server::hudAdd(Player *player, HudElement *form) {
if (!player) if (!player)
return -1; return -1;
u32 id = player->getFreeHudID();
if (id < player->hud.size())
player->hud[id] = form;
else
player->hud.push_back(form);
u32 id = player->addHud(form);
SendHUDAdd(player->peer_id, id, form); SendHUDAdd(player->peer_id, id, form);
return id; return id;
} }
bool Server::hudRemove(Player *player, u32 id) { bool Server::hudRemove(Player *player, u32 id) {
if (!player || id >= player->hud.size() || !player->hud[id]) if (!player)
return false; return false;
delete player->hud[id]; HudElement* todel = player->removeHud(id);
player->hud[id] = NULL;
if (!todel)
return false;
delete todel;
SendHUDRemove(player->peer_id, id); SendHUDRemove(player->peer_id, id);
return true; return true;
} }
@ -5047,7 +5048,7 @@ PlayerSAO* Server::emergePlayer(const char *name, u16 peer_id)
isSingleplayer()); isSingleplayer());
/* Clean up old HUD elements from previous sessions */ /* Clean up old HUD elements from previous sessions */
player->hud.clear(); player->clearHud();
/* Add object to environment */ /* Add object to environment */
m_env->addActiveObject(playersao); m_env->addActiveObject(playersao);