Small cleanup of hud add/remove code

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

@ -2333,16 +2333,13 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
{
// Read client events
for(;;)
{
for(;;) {
ClientEvent event = client.getClientEvent();
if(event.type == CE_NONE)
{
if(event.type == CE_NONE) {
break;
}
else if(event.type == CE_PLAYER_DAMAGE &&
client.getHP() != 0)
{
client.getHP() != 0) {
//u16 damage = event.player_damage.amount;
//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");
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_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,
device, &client);
@ -2376,8 +2371,7 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
player->hurt_tilt_strength = 0;
}
else if (event.type == CE_SHOW_FORMSPEC)
{
else if (event.type == CE_SHOW_FORMSPEC) {
FormspecFormSource* fs_src =
new FormspecFormSource(*(event.show_formspec.formspec));
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.formname);
}
else if(event.type == CE_SPAWN_PARTICLE)
{
else if(event.type == CE_SPAWN_PARTICLE) {
LocalPlayer* player = client.getEnv().getLocalPlayer();
video::ITexture *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(1.0, 1.0));
}
else if(event.type == CE_ADD_PARTICLESPAWNER)
{
else if(event.type == CE_ADD_PARTICLESPAWNER) {
LocalPlayer* player = client.getEnv().getLocalPlayer();
video::ITexture *texture =
gamedef->tsrc()->getTexture(*(event.add_particlespawner.texture));
@ -2431,15 +2423,15 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
texture,
event.add_particlespawner.id);
}
else if(event.type == CE_DELETE_PARTICLESPAWNER)
{
else if(event.type == CE_DELETE_PARTICLESPAWNER) {
delete_particlespawner (event.delete_particlespawner.id);
}
else if (event.type == CE_HUDADD)
{
else if (event.type == CE_HUDADD) {
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.name;
delete event.hudadd.scale;
@ -2451,7 +2443,7 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
continue;
}
HudElement *e = new HudElement;
e = new HudElement;
e->type = (HudElementType)event.hudadd.type;
e->pos = *event.hudadd.pos;
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->size = *event.hudadd.size;
if (id == nhudelem)
player->hud.push_back(e);
else
player->hud[id] = e;
u32 new_id = player->addHud(e);
//if this isn't true our huds aren't consistent
assert(new_id == id);
delete event.hudadd.pos;
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.size;
}
else if (event.type == CE_HUDRM)
{
u32 id = event.hudrm.id;
if (id < player->hud.size() && player->hud[id]) {
delete player->hud[id];
player->hud[id] = NULL;
}
else if (event.type == CE_HUDRM) {
HudElement* e = player->removeHud(event.hudrm.id);
if (e != NULL)
delete (e);
}
else if (event.type == CE_HUDCHANGE)
{
else if (event.type == CE_HUDCHANGE) {
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.v2fdata;
delete event.hudchange.sdata;
@ -2498,7 +2488,6 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
continue;
}
HudElement* e = player->hud[id];
switch (event.hudchange.stat) {
case HUD_STAT_POS:
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.v2s32data;
}
else if (event.type == CE_SET_SKY)
{
else if (event.type == CE_SET_SKY) {
sky->setVisible(false);
if(skybox){
skybox->drop();
skybox = NULL;
}
// Handle according to type
if(*event.set_sky.type == "regular"){
if(*event.set_sky.type == "regular") {
sky->setVisible(true);
}
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);
skybox = smgr->addSkyBoxSceneNode(
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.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;
u32 value = event.override_day_night_ratio.ratio_f * 1000;
client.getEnv().setDayNightRatioOverride(enable, value);

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

@ -216,19 +216,18 @@ void Player::deSerialize(std::istream &is, std::string playername)
setPosition(args.getV3F("position"));
try{
hp = args.getS32("hp");
}catch(SettingNotFoundException &e){
}catch(SettingNotFoundException &e) {
hp = 20;
}
try{
m_breath = args.getS32("breath");
}catch(SettingNotFoundException &e){
}catch(SettingNotFoundException &e) {
m_breath = 11;
}
inventory.deSerialize(is);
if(inventory.getList("craftpreview") == NULL)
{
if(inventory.getList("craftpreview") == NULL) {
// Convert players without craftpreview
inventory.addList("craftpreview", 1);
@ -246,14 +245,47 @@ void Player::deSerialize(std::istream &is, std::string playername)
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
*/
void RemotePlayer::setPosition(const v3f &position)
{
Player::setPosition(position);

@ -294,10 +294,17 @@ public:
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;
s32 hud_hotbar_itemcount;
protected:
IGameDef *m_gamedef;
@ -314,6 +321,8 @@ protected:
v3f m_last_pos;
u16 m_last_hp;
Inventory m_last_inventory;
std::vector<HudElement *> hud;
};

@ -971,7 +971,9 @@ int ObjectRef::l_hud_change(lua_State *L)
return 0;
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;
HudElementStat stat = HUD_STAT_NUMBER;
@ -983,10 +985,6 @@ int ObjectRef::l_hud_change(lua_State *L)
}
void *value = NULL;
HudElement *e = player->hud[id];
if (!e)
return 0;
switch (stat) {
case HUD_STAT_POS:
e->pos = read_v2f(L, 4);
@ -1049,10 +1047,8 @@ int ObjectRef::l_hud_get(lua_State *L)
return 0;
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)
return 0;

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