summaryrefslogtreecommitdiff
path: root/src/server.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/server.cpp')
-rw-r--r--src/server.cpp180
1 files changed, 55 insertions, 125 deletions
diff --git a/src/server.cpp b/src/server.cpp
index a9632c93c..241826ba2 100644
--- a/src/server.cpp
+++ b/src/server.cpp
@@ -3449,7 +3449,9 @@ void Server::SendChatMessage(u16 peer_id, const std::wstring &message)
// Send as reliable
m_con.Send(peer_id, 0, data, true);
}
-void Server::SendShowFormspecMessage(u16 peer_id, const std::string formspec, const std::string formname)
+
+void Server::SendShowFormspecMessage(u16 peer_id, const std::string formspec,
+ const std::string formname)
{
DSTACK(__FUNCTION_NAME);
@@ -3470,7 +3472,9 @@ void Server::SendShowFormspecMessage(u16 peer_id, const std::string formspec, co
}
// Spawns a particle on peer with peer_id
-void Server::SendSpawnParticle(u16 peer_id, v3f pos, v3f velocity, v3f acceleration, float expirationtime, float size, bool collisiondetection, std::string texture)
+void Server::SendSpawnParticle(u16 peer_id, v3f pos, v3f velocity, v3f acceleration,
+ float expirationtime, float size, bool collisiondetection,
+ std::string texture)
{
DSTACK(__FUNCTION_NAME);
@@ -3492,7 +3496,9 @@ void Server::SendSpawnParticle(u16 peer_id, v3f pos, v3f velocity, v3f accelerat
}
// Spawns a particle on all peers
-void Server::SendSpawnParticleAll(v3f pos, v3f velocity, v3f acceleration, float expirationtime, float size, bool collisiondetection, std::string texture)
+void Server::SendSpawnParticleAll(v3f pos, v3f velocity, v3f acceleration,
+ float expirationtime, float size, bool collisiondetection,
+ std::string texture)
{
for(std::map<u16, RemoteClient*>::iterator
i = m_clients.begin();
@@ -3595,22 +3601,18 @@ void Server::SendDeleteParticleSpawnerAll(u32 id)
}
}
-void Server::SendHUDAdd(u16 peer_id, const u32 id, HudElement* form)
+void Server::SendHUDAdd(u16 peer_id, u32 id, HudElement *form)
{
- DSTACK(__FUNCTION_NAME);
-
std::ostringstream os(std::ios_base::binary);
- u8 buf[12];
// Write command
- writeU16(buf, TOCLIENT_HUDADD);
- os.write((char*)buf, 2);
+ writeU16(os, TOCLIENT_HUDADD);
writeU32(os, id);
- writeU8(os, form->type);
+ writeU8(os, (u8)form->type);
writeV2F1000(os, form->pos);
- os<<serializeString(form->name);
+ os << serializeString(form->name);
writeV2F1000(os, form->scale);
- os<<serializeString(form->text);
+ os << serializeString(form->text);
writeU32(os, form->number);
writeU32(os, form->item);
writeU32(os, form->dir);
@@ -3622,16 +3624,12 @@ void Server::SendHUDAdd(u16 peer_id, const u32 id, HudElement* form)
m_con.Send(peer_id, 0, data, true);
}
-void Server::SendHUDRm(u16 peer_id, const u32 id)
+void Server::SendHUDRemove(u16 peer_id, u32 id)
{
- DSTACK(__FUNCTION_NAME);
-
std::ostringstream os(std::ios_base::binary);
- u8 buf[12];
// Write command
- writeU16(buf, TOCLIENT_HUDRM);
- os.write((char*)buf, 2);
+ writeU16(os, TOCLIENT_HUDRM);
writeU32(os, id);
// Make data buffer
@@ -3641,67 +3639,36 @@ void Server::SendHUDRm(u16 peer_id, const u32 id)
m_con.Send(peer_id, 0, data, true);
}
-void Server::SendHUDChange(u16 peer_id, const u32 id, const u8 stat, v2f data)
+void Server::SendHUDChange(u16 peer_id, u32 id, HudElementStat stat, void *value)
{
- DSTACK(__FUNCTION_NAME);
-
std::ostringstream os(std::ios_base::binary);
- u8 buf[12];
// Write command
- writeU16(buf, TOCLIENT_HUDCHANGE);
- os.write((char*)buf, 2);
+ writeU16(os, TOCLIENT_HUDCHANGE);
writeU32(os, id);
- writeU8(os, stat);
- writeV2F1000(os, data);
-
- // Make data buffer
- std::string s = os.str();
- SharedBuffer<u8> ddata((u8*)s.c_str(), s.size());
- // Send as reliable
- m_con.Send(peer_id, 0, ddata, true);
-}
-
-void Server::SendHUDChange(u16 peer_id, const u32 id, const u8 stat, std::string data)
-{
- DSTACK(__FUNCTION_NAME);
-
- std::ostringstream os(std::ios_base::binary);
- u8 buf[12];
-
- // Write command
- writeU16(buf, TOCLIENT_HUDCHANGE);
- os.write((char*)buf, 2);
- writeU32(os, id);
- writeU8(os, stat);
- os<<serializeString(data);
-
- // Make data buffer
- std::string s = os.str();
- SharedBuffer<u8> ddata((u8*)s.c_str(), s.size());
- // Send as reliable
- m_con.Send(peer_id, 0, ddata, true);
-}
-
-void Server::SendHUDChange(u16 peer_id, const u32 id, const u8 stat, u32 data)
-{
- DSTACK(__FUNCTION_NAME);
-
- std::ostringstream os(std::ios_base::binary);
- u8 buf[12];
-
- // Write command
- writeU16(buf, TOCLIENT_HUDCHANGE);
- os.write((char*)buf, 2);
- writeU32(os, id);
- writeU8(os, stat);
- writeU32(os, data);
+ writeU8(os, (u8)stat);
+ switch (stat) {
+ case HUD_STAT_POS:
+ case HUD_STAT_SCALE:
+ writeV2F1000(os, *(v2f *)value);
+ break;
+ case HUD_STAT_NAME:
+ case HUD_STAT_TEXT:
+ os << serializeString(*(std::string *)value);
+ break;
+ case HUD_STAT_NUMBER:
+ case HUD_STAT_ITEM:
+ case HUD_STAT_DIR:
+ default:
+ writeU32(os, *(u32 *)value);
+ break;
+ }
// Make data buffer
std::string s = os.str();
- SharedBuffer<u8> ddata((u8*)s.c_str(), s.size());
+ SharedBuffer<u8> data((u8 *)s.c_str(), s.size());
// Send as reliable
- m_con.Send(peer_id, 0, ddata, true);
+ m_con.Send(peer_id, 0, data, true);
}
void Server::BroadcastChatMessage(const std::wstring &message)
@@ -4657,71 +4624,34 @@ bool Server::showFormspec(const char *playername, const std::string &formspec, c
return true;
}
-bool Server::hudadd(const char *playername, const u32 &id, HudElement* form)
-{
- Player *player = m_env->getPlayer(playername);
-
- if(!player)
- {
- infostream<<"hudadd: couldn't find player:"<<playername<<std::endl;
- return false;
- }
+u32 Server::hudAdd(Player *player, HudElement *form) {
+ if (!player)
+ return -1;
+ u32 id = hud_get_free_id(player);
+ if (id < player->hud.size())
+ player->hud[id] = form;
+ else
+ player->hud.push_back(form);
+
SendHUDAdd(player->peer_id, id, form);
- return true;
-}
-
-bool Server::hudrm(const char *playername, const u32 &id)
-{
- Player *player = m_env->getPlayer(playername);
-
- if(!player)
- {
- infostream<<"hudrm: couldn't find player:"<<playername<<std::endl;
- return false;
- }
-
- SendHUDRm(player->peer_id, id);
- return true;
-}
-
-bool Server::hudchange(const char *playername, const u32 &id, const u8 &stat, v2f data)
-{
- Player *player = m_env->getPlayer(playername);
-
- if(!player)
- {
- infostream<<"hudchange: couldn't find player:"<<playername<<std::endl;
- return false;
- }
-
- SendHUDChange(player->peer_id, id, stat, data);
- return true;
+ return id;
}
-bool Server::hudchange(const char *playername, const u32 &id, const u8 &stat, std::string data)
-{
- Player *player = m_env->getPlayer(playername);
-
- if(!player)
- {
- infostream<<"hudchange: couldn't find player:"<<playername<<std::endl;
+bool Server::hudRemove(Player *player, u32 id) {
+ if (!player || id >= player->hud.size() || !player->hud[id])
return false;
- }
- SendHUDChange(player->peer_id, id, stat, data);
+ delete player->hud[id];
+ player->hud[id] = NULL;
+
+ SendHUDRemove(player->peer_id, id);
return true;
}
-bool Server::hudchange(const char *playername, const u32 &id, const u8 &stat, u32 data)
-{
- Player *player = m_env->getPlayer(playername);
-
- if(!player)
- {
- infostream<<"hudchange: couldn't find player:"<<playername<<std::endl;
+bool Server::hudChange(Player *player, u32 id, HudElementStat stat, void *data) {
+ if (!player)
return false;
- }
SendHUDChange(player->peer_id, id, stat, data);
return true;