#include "server.h"
#include <iostream>
#include <queue>
+#include <algorithm>
#include "clientserver.h"
#include "map.h"
#include "jmutexautolock.h"
#include "settings.h"
#include "profiler.h"
#include "log.h"
-#include "script.h"
-#include "scriptapi.h"
+#include "script/cpp_api/scriptapi.h"
#include "nodedef.h"
#include "itemdef.h"
#include "craftdef.h"
#include "util/mathconstants.h"
#include "rollback.h"
#include "util/serialize.h"
+#include "defaultsettings.h"
void * ServerThread::Thread()
{
}
void RemoteClient::GetNextBlocks(Server *server, float dtime,
- core::array<PrioritySortedBlockTransfer> &dest)
+ std::vector<PrioritySortedBlockTransfer> &dest)
{
DSTACK(__FUNCTION_NAME);
Get the border/face dot coordinates of a "d-radiused"
box
*/
- core::list<v3s16> list;
+ std::list<v3s16> list;
getFacePositions(list, d);
- core::list<v3s16>::Iterator li;
- for(li=list.begin(); li!=list.end(); li++)
+ std::list<v3s16>::iterator li;
+ for(li=list.begin(); li!=list.end(); ++li)
{
v3s16 p = *li + center;
}
// Don't send blocks that are currently being transferred
- if(m_blocks_sending.find(p) != NULL)
+ if(m_blocks_sending.find(p) != m_blocks_sending.end())
continue;
/*
Don't send already sent blocks
*/
{
- if(m_blocks_sent.find(p) != NULL)
+ if(m_blocks_sent.find(p) != m_blocks_sent.end())
{
continue;
}
void RemoteClient::GotBlock(v3s16 p)
{
- if(m_blocks_sending.find(p) != NULL)
- m_blocks_sending.remove(p);
+ if(m_blocks_sending.find(p) != m_blocks_sending.end())
+ m_blocks_sending.erase(p);
else
{
/*infostream<<"RemoteClient::GotBlock(): Didn't find in"
" m_blocks_sending"<<std::endl;*/
m_excess_gotblocks++;
}
- m_blocks_sent.insert(p, true);
+ m_blocks_sent.insert(p);
}
void RemoteClient::SentBlock(v3s16 p)
{
- if(m_blocks_sending.find(p) == NULL)
- m_blocks_sending.insert(p, 0.0);
+ if(m_blocks_sending.find(p) == m_blocks_sending.end())
+ m_blocks_sending[p] = 0.0;
else
infostream<<"RemoteClient::SentBlock(): Sent block"
" already in m_blocks_sending"<<std::endl;
{
m_nearest_unsent_d = 0;
- if(m_blocks_sending.find(p) != NULL)
- m_blocks_sending.remove(p);
- if(m_blocks_sent.find(p) != NULL)
- m_blocks_sent.remove(p);
+ if(m_blocks_sending.find(p) != m_blocks_sending.end())
+ m_blocks_sending.erase(p);
+ if(m_blocks_sent.find(p) != m_blocks_sent.end())
+ m_blocks_sent.erase(p);
}
-void RemoteClient::SetBlocksNotSent(core::map<v3s16, MapBlock*> &blocks)
+void RemoteClient::SetBlocksNotSent(std::map<v3s16, MapBlock*> &blocks)
{
m_nearest_unsent_d = 0;
- for(core::map<v3s16, MapBlock*>::Iterator
- i = blocks.getIterator();
- i.atEnd()==false; i++)
+ for(std::map<v3s16, MapBlock*>::iterator
+ i = blocks.begin();
+ i != blocks.end(); ++i)
{
- v3s16 p = i.getNode()->getKey();
+ v3s16 p = i->first;
- if(m_blocks_sending.find(p) != NULL)
- m_blocks_sending.remove(p);
- if(m_blocks_sent.find(p) != NULL)
- m_blocks_sent.remove(p);
+ if(m_blocks_sending.find(p) != m_blocks_sending.end())
+ m_blocks_sending.erase(p);
+ if(m_blocks_sent.find(p) != m_blocks_sent.end())
+ m_blocks_sent.erase(p);
}
}
m_simple_singleplayer_mode(simple_singleplayer_mode),
m_async_fatal_error(""),
m_env(NULL),
- m_con(PROTOCOL_ID, 512, CONNECTION_TIMEOUT, this),
+ m_con(PROTOCOL_ID, 512, CONNECTION_TIMEOUT,
+ g_settings->getBool("enable_ipv6") && g_settings->getBool("ipv6_server"), this),
m_banmanager(path_world+DIR_DELIM+"ipban.txt"),
m_rollback(NULL),
m_rollback_sink_enabled(true),
m_enable_rollback_recording(false),
m_emerge(NULL),
- m_biomedef(NULL),
- m_lua(NULL),
+ m_script(NULL),
m_itemdef(createItemDefManager()),
m_nodedef(createNodeDefManager()),
m_craftdef(createCraftDefManager()),
m_event(new EventManager()),
m_thread(this),
- //m_emergethread(this),
m_time_of_day_send_timer(0),
m_uptime(0),
m_shutdown_requested(false),
infostream<<"- config: "<<m_path_config<<std::endl;
infostream<<"- game: "<<m_gamespec.path<<std::endl;
- // Create biome definition manager
- m_biomedef = new BiomeDefManager(this);
-
+ // Initialize default settings and override defaults with those provided
+ // by the game
+ set_default_settings(g_settings);
+ Settings gamedefaults;
+ getGameMinetestConfig(gamespec.path, gamedefaults);
+ override_default_settings(g_settings, &gamedefaults);
+
+ // Create emerge manager
+ m_emerge = new EmergeManager(this);
+
// Create rollback manager
std::string rollback_path = m_path_world+DIR_DELIM+"rollback.txt";
m_rollback = createRollbackManager(rollback_path, this);
ModConfiguration modconf(m_path_world);
m_mods = modconf.getMods();
- std::list<ModSpec> unsatisfied_mods = modconf.getUnsatisfiedMods();
+ std::vector<ModSpec> unsatisfied_mods = modconf.getUnsatisfiedMods();
// complain about mods with unsatisfied dependencies
if(!modconf.isConsistent())
{
- for(std::list<ModSpec>::iterator it = unsatisfied_mods.begin();
+ for(std::vector<ModSpec>::iterator it = unsatisfied_mods.begin();
it != unsatisfied_mods.end(); ++it)
{
ModSpec mod = *it;
std::string worldmt = m_path_world + DIR_DELIM + "world.mt";
worldmt_settings.readConfigFile(worldmt.c_str());
std::vector<std::string> names = worldmt_settings.getNames();
- std::set<std::string> exclude_mod_names;
std::set<std::string> load_mod_names;
for(std::vector<std::string>::iterator it = names.begin();
it != names.end(); ++it)
{
std::string name = *it;
- if (name.compare(0,9,"load_mod_")==0)
- {
- if(worldmt_settings.getBool(name))
- load_mod_names.insert(name.substr(9));
- else
- exclude_mod_names.insert(name.substr(9));
- }
+ if(name.compare(0,9,"load_mod_")==0 && worldmt_settings.getBool(name))
+ load_mod_names.insert(name.substr(9));
}
// complain about mods declared to be loaded, but not found
for(std::vector<ModSpec>::iterator it = m_mods.begin();
- it != m_mods.end(); ++it)
+ it != m_mods.end(); ++it)
load_mod_names.erase((*it).name);
- for(std::list<ModSpec>::iterator it = unsatisfied_mods.begin();
- it != unsatisfied_mods.end(); ++it)
+ for(std::vector<ModSpec>::iterator it = unsatisfied_mods.begin();
+ it != unsatisfied_mods.end(); ++it)
load_mod_names.erase((*it).name);
if(!load_mod_names.empty())
{
// Initialize scripting
infostream<<"Server: Initializing Lua"<<std::endl;
- m_lua = script_init();
- assert(m_lua);
- // Export API
- scriptapi_export(m_lua, this);
+
+ m_script = new ScriptApi(this);
+
+
// Load and run builtin.lua
infostream<<"Server: Loading builtin.lua [\""
<<builtinpath<<"\"]"<<std::endl;
- bool success = scriptapi_loadmod(m_lua, builtinpath, "__builtin");
+ bool success = m_script->loadMod(builtinpath, "__builtin");
if(!success){
errorstream<<"Server: Failed to load and run "
<<builtinpath<<std::endl;
std::string scriptpath = mod.path + DIR_DELIM + "init.lua";
infostream<<" ["<<padStringRight(mod.name, 12)<<"] [\""
<<scriptpath<<"\"]"<<std::endl;
- bool success = scriptapi_loadmod(m_lua, scriptpath, mod.name);
+ bool success = m_script->loadMod(scriptpath, mod.name);
if(!success){
errorstream<<"Server: Failed to load and run "
<<scriptpath<<std::endl;
// Apply item aliases in the node definition manager
m_nodedef->updateAliases(m_itemdef);
- // Add default biomes after nodedef had its aliases added
- m_biomedef->addDefaultBiomes();
-
- // Create emerge manager
- m_emerge = new EmergeManager(this, m_biomedef);
-
// Initialize Environment
ServerMap *servermap = new ServerMap(path_world, this, m_emerge);
- m_env = new ServerEnvironment(servermap, m_lua, this, this);
+ m_env = new ServerEnvironment(servermap, m_script, this, this);
+
+ // Run some callbacks after the MG params have been set up but before activation
+ MapgenParams *mgparams = servermap->getMapgenParams();
+ m_script->environment_OnMapgenInit(mgparams);
- m_emerge->initMapgens(servermap->getMapgenParams());
+ // Initialize mapgens
+ m_emerge->initMapgens(mgparams);
// Give environment reference to scripting api
- scriptapi_add_environment(m_lua, m_env);
+ m_script->initializeEnvironment(m_env);
// Register us to receive map edit events
servermap->addEventReceiver(this);
/*
Send the message to clients
*/
- for(core::map<u16, RemoteClient*>::Iterator
- i = m_clients.getIterator();
- i.atEnd() == false; i++)
+ for(std::map<u16, RemoteClient*>::iterator
+ i = m_clients.begin();
+ i != m_clients.end(); ++i)
{
// Get client and check that it is valid
- RemoteClient *client = i.getNode()->getValue();
- assert(client->peer_id == i.getNode()->getKey());
+ RemoteClient *client = i->second;
+ assert(client->peer_id == i->first);
if(client->serialization_version == SER_FMT_VER_INVALID)
continue;
/*
Execute script shutdown hooks
*/
- scriptapi_on_shutdown(m_lua);
+ m_script->on_shutdown();
}
{
*/
stop();
+ //shutdown all emerge threads first!
+ delete m_emerge;
+
/*
Delete clients
*/
{
JMutexAutoLock clientslock(m_con_mutex);
- for(core::map<u16, RemoteClient*>::Iterator
- i = m_clients.getIterator();
- i.atEnd() == false; i++)
+ for(std::map<u16, RemoteClient*>::iterator
+ i = m_clients.begin();
+ i != m_clients.end(); ++i)
{
// Delete client
- delete i.getNode()->getValue();
+ delete i->second;
}
}
// Delete things in the reverse order of creation
delete m_env;
delete m_rollback;
- delete m_emerge;
delete m_event;
delete m_itemdef;
delete m_nodedef;
// Deinitialize scripting
infostream<<"Server: Deinitializing scripting"<<std::endl;
- script_deinit(m_lua);
+ delete m_script;
// Delete detached inventories
{
//JMutexAutoLock envlock(m_env_mutex);
JMutexAutoLock conlock(m_con_mutex);
- for(core::map<u16, RemoteClient*>::Iterator
- i = m_clients.getIterator();
- i.atEnd() == false; i++)
+ for(std::map<u16, RemoteClient*>::iterator
+ i = m_clients.begin();
+ i != m_clients.end(); ++i)
{
- RemoteClient *client = i.getNode()->getValue();
+ RemoteClient *client = i->second;
SharedBuffer<u8> data = makePacket_TOCLIENT_TIME_OF_DAY(
m_env->getTimeOfDay(), g_settings->getFloat("time_speed"));
// Send as reliable
ScopeProfiler sp(g_profiler, "Server: handle players");
- for(core::map<u16, RemoteClient*>::Iterator
- i = m_clients.getIterator();
- i.atEnd() == false; i++)
+ for(std::map<u16, RemoteClient*>::iterator
+ i = m_clients.begin();
+ i != m_clients.end(); ++i)
{
- RemoteClient *client = i.getNode()->getValue();
+ RemoteClient *client = i->second;
PlayerSAO *playersao = getPlayerSAO(client->peer_id);
if(playersao == NULL)
continue;
SendPlayerHP(client->peer_id);
}
+ /*
+ Send player breath if changed
+ */
+ if(playersao->m_breath_not_sent){
+ SendPlayerBreath(client->peer_id);
+ }
+
/*
Send player inventories if necessary
*/
ScopeProfiler sp(g_profiler, "Server: liquid transform");
- core::map<v3s16, MapBlock*> modified_blocks;
+ std::map<v3s16, MapBlock*> modified_blocks;
m_env->getMap().transformLiquids(modified_blocks);
#if 0
/*
JMutexAutoLock lock2(m_con_mutex);
- for(core::map<u16, RemoteClient*>::Iterator
- i = m_clients.getIterator();
- i.atEnd() == false; i++)
+ for(std::map<u16, RemoteClient*>::iterator
+ i = m_clients.begin();
+ i != m_clients.end(); ++i)
{
- RemoteClient *client = i.getNode()->getValue();
+ RemoteClient *client = i->second;
if(modified_blocks.size() > 0)
{
m_clients_number = 0;
if(m_clients.size() != 0)
infostream<<"Players:"<<std::endl;
- for(core::map<u16, RemoteClient*>::Iterator
- i = m_clients.getIterator();
- i.atEnd() == false; i++)
+ for(std::map<u16, RemoteClient*>::iterator
+ i = m_clients.begin();
+ i != m_clients.end(); ++i)
{
//u16 peer_id = i.getNode()->getKey();
- RemoteClient *client = i.getNode()->getValue();
+ RemoteClient *client = i->second;
Player *player = m_env->getPlayer(client->peer_id);
if(player==NULL)
continue;
float &counter = m_masterserver_timer;
if((!counter || counter >= 300.0) && g_settings->getBool("server_announce") == true)
{
- ServerList::sendAnnounce(!counter ? "start" : "update", m_clients_number);
+ ServerList::sendAnnounce(!counter ? "start" : "update", m_clients_number, m_uptime.get(), m_gamespec.id, m_mods);
counter = 0.01;
}
counter += dtime;
s16 radius = g_settings->getS16("active_object_send_range_blocks");
radius *= MAP_BLOCKSIZE;
- for(core::map<u16, RemoteClient*>::Iterator
- i = m_clients.getIterator();
- i.atEnd() == false; i++)
+ for(std::map<u16, RemoteClient*>::iterator
+ i = m_clients.begin();
+ i != m_clients.end(); ++i)
{
- RemoteClient *client = i.getNode()->getValue();
+ RemoteClient *client = i->second;
// If definitions and textures have not been sent, don't
// send objects either
}
v3s16 pos = floatToInt(player->getPosition(), BS);
- core::map<u16, bool> removed_objects;
- core::map<u16, bool> added_objects;
+ std::set<u16> removed_objects;
+ std::set<u16> added_objects;
m_env->getRemovedActiveObjects(pos, radius,
client->m_known_objects, removed_objects);
m_env->getAddedActiveObjects(pos, radius,
// Handle removed objects
writeU16((u8*)buf, removed_objects.size());
data_buffer.append(buf, 2);
- for(core::map<u16, bool>::Iterator
- i = removed_objects.getIterator();
- i.atEnd()==false; i++)
+ for(std::set<u16>::iterator
+ i = removed_objects.begin();
+ i != removed_objects.end(); ++i)
{
// Get object
- u16 id = i.getNode()->getKey();
+ u16 id = *i;
ServerActiveObject* obj = m_env->getActiveObject(id);
// Add to data buffer for sending
- writeU16((u8*)buf, i.getNode()->getKey());
+ writeU16((u8*)buf, id);
data_buffer.append(buf, 2);
// Remove from known objects
- client->m_known_objects.remove(i.getNode()->getKey());
+ client->m_known_objects.erase(id);
if(obj && obj->m_known_by_count > 0)
obj->m_known_by_count--;
// Handle added objects
writeU16((u8*)buf, added_objects.size());
data_buffer.append(buf, 2);
- for(core::map<u16, bool>::Iterator
- i = added_objects.getIterator();
- i.atEnd()==false; i++)
+ for(std::set<u16>::iterator
+ i = added_objects.begin();
+ i != added_objects.end(); ++i)
{
// Get object
- u16 id = i.getNode()->getKey();
+ u16 id = *i;
ServerActiveObject* obj = m_env->getActiveObject(id);
// Get object type
data_buffer.append(serializeLongString(""));
// Add to known objects
- client->m_known_objects.insert(i.getNode()->getKey(), false);
+ client->m_known_objects.insert(id);
if(obj)
obj->m_known_by_count++;
// Key = object id
// Value = data sent by object
- core::map<u16, core::list<ActiveObjectMessage>* > buffered_messages;
+ std::map<u16, std::list<ActiveObjectMessage>* > buffered_messages;
// Get active object messages from environment
for(;;)
if(aom.id == 0)
break;
- core::list<ActiveObjectMessage>* message_list = NULL;
- core::map<u16, core::list<ActiveObjectMessage>* >::Node *n;
+ std::list<ActiveObjectMessage>* message_list = NULL;
+ std::map<u16, std::list<ActiveObjectMessage>* >::iterator n;
n = buffered_messages.find(aom.id);
- if(n == NULL)
+ if(n == buffered_messages.end())
{
- message_list = new core::list<ActiveObjectMessage>;
- buffered_messages.insert(aom.id, message_list);
+ message_list = new std::list<ActiveObjectMessage>;
+ buffered_messages[aom.id] = message_list;
}
else
{
- message_list = n->getValue();
+ message_list = n->second;
}
message_list->push_back(aom);
}
// Route data to every client
- for(core::map<u16, RemoteClient*>::Iterator
- i = m_clients.getIterator();
- i.atEnd()==false; i++)
+ for(std::map<u16, RemoteClient*>::iterator
+ i = m_clients.begin();
+ i != m_clients.end(); ++i)
{
- RemoteClient *client = i.getNode()->getValue();
+ RemoteClient *client = i->second;
std::string reliable_data;
std::string unreliable_data;
// Go through all objects in message buffer
- for(core::map<u16, core::list<ActiveObjectMessage>* >::Iterator
- j = buffered_messages.getIterator();
- j.atEnd()==false; j++)
+ for(std::map<u16, std::list<ActiveObjectMessage>* >::iterator
+ j = buffered_messages.begin();
+ j != buffered_messages.end(); ++j)
{
// If object is not known by client, skip it
- u16 id = j.getNode()->getKey();
- if(client->m_known_objects.find(id) == NULL)
+ u16 id = j->first;
+ if(client->m_known_objects.find(id) == client->m_known_objects.end())
continue;
// Get message list of object
- core::list<ActiveObjectMessage>* list = j.getNode()->getValue();
+ std::list<ActiveObjectMessage>* list = j->second;
// Go through every message
- for(core::list<ActiveObjectMessage>::Iterator
- k = list->begin(); k != list->end(); k++)
+ for(std::list<ActiveObjectMessage>::iterator
+ k = list->begin(); k != list->end(); ++k)
{
// Compose the full new data with header
ActiveObjectMessage aom = *k;
}
// Clear buffered_messages
- for(core::map<u16, core::list<ActiveObjectMessage>* >::Iterator
- i = buffered_messages.getIterator();
- i.atEnd()==false; i++)
+ for(std::map<u16, std::list<ActiveObjectMessage>* >::iterator
+ i = buffered_messages.begin();
+ i != buffered_messages.end(); ++i)
{
- delete i.getNode()->getValue();
+ delete i->second;
}
}
// Players far away from the change are stored here.
// Instead of sending the changes, MapBlocks are set not sent
// for them.
- core::list<u16> far_players;
+ std::list<u16> far_players;
if(event->type == MEET_ADDNODE)
{
{
infostream<<"Server: MEET_OTHER"<<std::endl;
prof.add("MEET_OTHER", 1);
- for(core::map<v3s16, bool>::Iterator
- i = event->modified_blocks.getIterator();
- i.atEnd()==false; i++)
+ for(std::set<v3s16>::iterator
+ i = event->modified_blocks.begin();
+ i != event->modified_blocks.end(); ++i)
{
- v3s16 p = i.getNode()->getKey();
- setBlockNotSent(p);
+ setBlockNotSent(*i);
}
}
else
if(far_players.size() > 0)
{
// Convert list format to that wanted by SetBlocksNotSent
- core::map<v3s16, MapBlock*> modified_blocks2;
- for(core::map<v3s16, bool>::Iterator
- i = event->modified_blocks.getIterator();
- i.atEnd()==false; i++)
+ std::map<v3s16, MapBlock*> modified_blocks2;
+ for(std::set<v3s16>::iterator
+ i = event->modified_blocks.begin();
+ i != event->modified_blocks.end(); ++i)
{
- v3s16 p = i.getNode()->getKey();
- modified_blocks2.insert(p,
- m_env->getMap().getBlockNoCreateNoEx(p));
+ modified_blocks2[*i] =
+ m_env->getMap().getBlockNoCreateNoEx(*i);
}
// Set blocks not sent
- for(core::list<u16>::Iterator
+ for(std::list<u16>::iterator
i = far_players.begin();
- i != far_players.end(); i++)
+ i != far_players.end(); ++i)
{
u16 peer_id = *i;
RemoteClient *client = getClient(peer_id);
{
counter = 0.0;
- for (int i = 0; i != m_emerge->emergethread.size(); i++)
+ for (unsigned int i = 0; i != m_emerge->emergethread.size(); i++)
m_emerge->emergethread[i]->trigger();
// Update m_enable_rollback_recording here too
u8 client_max = data[2];
u8 our_max = SER_FMT_VER_HIGHEST;
// Use the highest version supported by both
- u8 deployed = core::min_(client_max, our_max);
+ u8 deployed = std::min(client_max, our_max);
// If it's lower than the lowest supported, give up.
if(deployed < SER_FMT_VER_LOWEST)
deployed = SER_FMT_VER_INVALID;
return;
}
+ if(!isSingleplayer() && strcasecmp(playername, "singleplayer") == 0)
+ {
+ actionstream<<"Server: Player with an invalid name "
+ <<"tried to connect from "<<addr_s<<std::endl;
+ SendAccessDenied(m_con, peer_id,
+ L"Name is not allowed");
+ return;
+ }
+
infostream<<"Server: New connection: \""<<playername<<"\" from "
<<m_con.GetPeerAddress(peer_id).serializeString()<<std::endl;
}
std::string checkpwd; // Password hash to check against
- bool has_auth = scriptapi_get_auth(m_lua, playername, &checkpwd, NULL);
+ bool has_auth = m_script->getAuth(playername, &checkpwd, NULL);
// If no authentication info exists for user, create it
if(!has_auth){
if (raw_default_password.length() == 0)
initial_password = given_password;
- scriptapi_create_auth(m_lua, playername, initial_password);
+ m_script->createAuth(playername, initial_password);
}
- has_auth = scriptapi_get_auth(m_lua, playername, &checkpwd, NULL);
+ has_auth = m_script->getAuth(playername, &checkpwd, NULL);
if(!has_auth){
SendAccessDenied(m_con, peer_id, L"Not allowed to login");
SendMovement(m_con, peer_id);
// Send item definitions
- SendItemDef(m_con, peer_id, m_itemdef);
+ SendItemDef(m_con, peer_id, m_itemdef, client->net_proto_version);
// Send node definitions
SendNodeDef(m_con, peer_id, m_nodedef, client->net_proto_version);
if(g_settings->getBool("enable_damage"))
SendPlayerHP(peer_id);
+ // Send Breath
+ SendPlayerBreath(peer_id);
+
// Send detached inventories
sendDetachedInventories(peer_id);
*/
{
std::ostringstream os(std::ios_base::binary);
- for(core::map<u16, RemoteClient*>::Iterator
- i = m_clients.getIterator();
- i.atEnd() == false; i++)
+ for(std::map<u16, RemoteClient*>::iterator
+ i = m_clients.begin();
+ i != m_clients.end(); ++i)
{
- RemoteClient *client = i.getNode()->getValue();
- assert(client->peer_id == i.getNode()->getKey());
+ RemoteClient *client = i->second;
+ assert(client->peer_id == i->first);
if(client->serialization_version == SER_FMT_VER_INVALID)
continue;
// Get player
os<<player->getName()<<" ";
}
- actionstream<<player->getName()<<" joins game. List of players: "
+ actionstream<<player->getName()<<" ["<<addr_s<<"] "<<" joins game. List of players: "
<<os.str()<<std::endl;
}
setInventoryModified(da->from_inv);
+ /*
+ Disable dropping items out of craftpreview
+ */
+ if(da->from_list == "craftpreview")
+ {
+ infostream<<"Ignoring IDropAction from "
+ <<(da->from_inv.dump())<<":"<<da->from_list
+ <<" because src is "<<da->from_list<<std::endl;
+ delete a;
+ return;
+ }
+
// Disallow dropping items if not allowed to interact
if(!checkPriv(player->getName(), "interact"))
{
std::wstring name = narrow_to_wide(player->getName());
// Run script hook
- bool ate = scriptapi_on_chat_message(m_lua, player->getName(),
+ bool ate = m_script->on_chat_message(player->getName(),
wide_to_narrow(message));
// If script ate the message, don't proceed
if(ate)
/*
Send the message to clients
*/
- for(core::map<u16, RemoteClient*>::Iterator
- i = m_clients.getIterator();
- i.atEnd() == false; i++)
+ for(std::map<u16, RemoteClient*>::iterator
+ i = m_clients.begin();
+ i != m_clients.end(); ++i)
{
// Get client and check that it is valid
- RemoteClient *client = i.getNode()->getValue();
- assert(client->peer_id == i.getNode()->getKey());
+ RemoteClient *client = i->second;
+ assert(client->peer_id == i->first);
if(client->serialization_version == SER_FMT_VER_INVALID)
continue;
SendPlayerHP(peer_id);
}
}
+ else if(command == TOSERVER_BREATH)
+ {
+ std::string datastring((char*)&data[2], datasize-2);
+ std::istringstream is(datastring, std::ios_base::binary);
+ u16 breath = readU16(is);
+ playersao->setBreath(breath);
+ }
else if(command == TOSERVER_PASSWORD)
{
/*
std::string playername = player->getName();
std::string checkpwd;
- scriptapi_get_auth(m_lua, playername, &checkpwd, NULL);
+ m_script->getAuth(playername, &checkpwd, NULL);
if(oldpwd != checkpwd)
{
return;
}
- bool success = scriptapi_set_password(m_lua, playername, newpwd);
+ bool success = m_script->setPassword(playername, newpwd);
if(success){
actionstream<<player->getName()<<" changes password"<<std::endl;
SendChatMessage(peer_id, L"Password change successful.");
std::string datastring((char*)&data[2], datasize-2);
std::istringstream is(datastring, std::ios_base::binary);
- core::list<MediaRequest> tosend;
+ std::list<MediaRequest> tosend;
u16 numfiles = readU16(is);
infostream<<"Sending "<<numfiles<<" files to "
m_emerge->enqueueBlockEmerge(peer_id, getNodeBlockPos(p_above), false);
}
if(n.getContent() != CONTENT_IGNORE)
- scriptapi_node_on_punch(m_lua, p_under, n, playersao);
+ m_script->node_on_punch(p_under, n, playersao);
// Cheat prevention
playersao->noCheatDigStart(p_under);
}
/* Actually dig node */
if(is_valid_dig && n.getContent() != CONTENT_IGNORE)
- scriptapi_node_on_dig(m_lua, p_under, n, playersao);
+ m_script->node_on_dig(p_under, n, playersao);
// Send unusual result (that is, node not being removed)
if(m_env->getMap().getNodeNoEx(p_under).getContent() != CONTENT_AIR)
// Do stuff
pointed_object->rightClick(playersao);
}
- else if(scriptapi_item_on_place(m_lua,
+ else if(m_script->item_OnPlace(
item, playersao, pointed))
{
// Placement was handled in lua
playersao->setWieldedItem(item);
}
- // If item has node placement prediction, always send the above
- // node to make sure the client knows what exactly happened
+ // If item has node placement prediction, always send the
+ // blocks to make sure the client knows what exactly happened
if(item.getDefinition(m_itemdef).node_placement_prediction != ""){
RemoteClient *client = getClient(peer_id);
v3s16 blockpos = getNodeBlockPos(floatToInt(pointed_pos_above, BS));
client->SetBlockNotSent(blockpos);
+ v3s16 blockpos2 = getNodeBlockPos(floatToInt(pointed_pos_under, BS));
+ if(blockpos2 != blockpos){
+ client->SetBlockNotSent(blockpos2);
+ }
}
} // action == 3
actionstream<<player->getName()<<" uses "<<item.name
<<", pointing at "<<pointed.dump()<<std::endl;
- if(scriptapi_item_on_use(m_lua,
+ if(m_script->item_OnUse(
item, playersao, pointed))
{
// Apply returned ItemStack
// Check the target node for rollback data; leave others unnoticed
RollbackNode rn_old(&m_env->getMap(), p, this);
- scriptapi_node_on_receive_fields(m_lua, p, formname, fields,
- playersao);
+ m_script->node_on_receive_fields(p, formname, fields,playersao);
// Report rollback data
RollbackNode rn_new(&m_env->getMap(), p, this);
fields[fieldname] = fieldvalue;
}
- scriptapi_on_player_receive_fields(m_lua, playersao, formname, fields);
+ m_script->on_playerReceiveFields(playersao, formname, fields);
}
else
{
}
}
-core::list<PlayerInfo> Server::getPlayerInfo()
-{
- DSTACK(__FUNCTION_NAME);
- JMutexAutoLock envlock(m_env_mutex);
- JMutexAutoLock conlock(m_con_mutex);
-
- core::list<PlayerInfo> list;
-
- core::list<Player*> players = m_env->getPlayers();
-
- core::list<Player*>::Iterator i;
- for(i = players.begin();
- i != players.end(); i++)
- {
- PlayerInfo info;
-
- Player *player = *i;
-
- try{
- // Copy info from connection to info struct
- info.id = player->peer_id;
- info.address = m_con.GetPeerAddress(player->peer_id);
- info.avg_rtt = m_con.GetPeerAvgRTT(player->peer_id);
- }
- catch(con::PeerNotFoundException &e)
- {
- // Set dummy peer info
- info.id = 0;
- info.address = Address(0,0,0,0,0);
- info.avg_rtt = 0.0;
- }
-
- snprintf(info.name, PLAYERNAME_SIZE, "%s", player->getName());
- info.position = player->getPosition();
-
- list.push_back(info);
- }
-
- return list;
-}
+//std::list<PlayerInfo> Server::getPlayerInfo()
+//{
+// DSTACK(__FUNCTION_NAME);
+// JMutexAutoLock envlock(m_env_mutex);
+// JMutexAutoLock conlock(m_con_mutex);
+//
+// std::list<PlayerInfo> list;
+//
+// std::list<Player*> players = m_env->getPlayers();
+//
+// std::list<Player*>::iterator i;
+// for(i = players.begin();
+// i != players.end(); ++i)
+// {
+// PlayerInfo info;
+//
+// Player *player = *i;
+//
+// try{
+// // Copy info from connection to info struct
+// info.id = player->peer_id;
+// info.address = m_con.GetPeerAddress(player->peer_id);
+// info.avg_rtt = m_con.GetPeerAvgRTT(player->peer_id);
+// }
+// catch(con::PeerNotFoundException &e)
+// {
+// // Set dummy peer info
+// info.id = 0;
+// info.address = Address(0,0,0,0,0);
+// info.avg_rtt = 0.0;
+// }
+//
+// snprintf(info.name, PLAYERNAME_SIZE, "%s", player->getName());
+// info.position = player->getPosition();
+//
+// list.push_back(info);
+// }
+//
+// return list;
+//}
void Server::peerAdded(con::Peer *peer)
con.Send(peer_id, 0, data, true);
}
+void Server::SendBreath(con::Connection &con, u16 peer_id, u16 breath)
+{
+ DSTACK(__FUNCTION_NAME);
+ std::ostringstream os(std::ios_base::binary);
+
+ writeU16(os, TOCLIENT_BREATH);
+ writeU16(os, breath);
+
+ // Make data buffer
+ std::string s = os.str();
+ SharedBuffer<u8> data((u8*)s.c_str(), s.size());
+ // Send as reliable
+ con.Send(peer_id, 0, data, true);
+}
+
void Server::SendAccessDenied(con::Connection &con, u16 peer_id,
const std::wstring &reason)
{
}
void Server::SendItemDef(con::Connection &con, u16 peer_id,
- IItemDefManager *itemdef)
+ IItemDefManager *itemdef, u16 protocol_version)
{
DSTACK(__FUNCTION_NAME);
std::ostringstream os(std::ios_base::binary);
*/
writeU16(os, TOCLIENT_ITEMDEF);
std::ostringstream tmp_os(std::ios::binary);
- itemdef->serialize(tmp_os);
+ itemdef->serialize(tmp_os, protocol_version);
std::ostringstream tmp_os2(std::ios::binary);
compressZlib(tmp_os.str(), tmp_os2);
os<<serializeLongString(tmp_os2.str());
// 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);
m_con.Send(peer_id, 0, data, true);
}
+// 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)
+{
+ DSTACK(__FUNCTION_NAME);
+
+ std::ostringstream os(std::ios_base::binary);
+ writeU16(os, TOCLIENT_SPAWN_PARTICLE);
+ writeV3F1000(os, pos);
+ writeV3F1000(os, velocity);
+ writeV3F1000(os, acceleration);
+ writeF1000(os, expirationtime);
+ writeF1000(os, size);
+ writeU8(os, collisiondetection);
+ os<<serializeLongString(texture);
+
+ // Make data buffer
+ std::string s = os.str();
+ SharedBuffer<u8> data((u8*)s.c_str(), s.size());
+ // Send as reliable
+ m_con.Send(peer_id, 0, data, true);
+}
+
+// Spawns a particle on all peers
+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();
+ i != m_clients.end(); i++)
+ {
+ // Get client and check that it is valid
+ RemoteClient *client = i->second;
+ assert(client->peer_id == i->first);
+ if(client->serialization_version == SER_FMT_VER_INVALID)
+ continue;
+
+ SendSpawnParticle(client->peer_id, pos, velocity, acceleration,
+ expirationtime, size, collisiondetection, texture);
+ }
+}
+
+// Adds a ParticleSpawner on peer with peer_id
+void Server::SendAddParticleSpawner(u16 peer_id, u16 amount, float spawntime, v3f minpos, v3f maxpos,
+ v3f minvel, v3f maxvel, v3f minacc, v3f maxacc, float minexptime, float maxexptime,
+ float minsize, float maxsize, bool collisiondetection, std::string texture, u32 id)
+{
+ DSTACK(__FUNCTION_NAME);
+
+ std::ostringstream os(std::ios_base::binary);
+ writeU16(os, TOCLIENT_ADD_PARTICLESPAWNER);
+
+ writeU16(os, amount);
+ writeF1000(os, spawntime);
+ writeV3F1000(os, minpos);
+ writeV3F1000(os, maxpos);
+ writeV3F1000(os, minvel);
+ writeV3F1000(os, maxvel);
+ writeV3F1000(os, minacc);
+ writeV3F1000(os, maxacc);
+ writeF1000(os, minexptime);
+ writeF1000(os, maxexptime);
+ writeF1000(os, minsize);
+ writeF1000(os, maxsize);
+ writeU8(os, collisiondetection);
+ os<<serializeLongString(texture);
+ writeU32(os, id);
+
+ // Make data buffer
+ std::string s = os.str();
+ SharedBuffer<u8> data((u8*)s.c_str(), s.size());
+ // Send as reliable
+ m_con.Send(peer_id, 0, data, true);
+}
+
+// Adds a ParticleSpawner on all peers
+void Server::SendAddParticleSpawnerAll(u16 amount, float spawntime, v3f minpos, v3f maxpos,
+ v3f minvel, v3f maxvel, v3f minacc, v3f maxacc, float minexptime, float maxexptime,
+ float minsize, float maxsize, bool collisiondetection, std::string texture, u32 id)
+{
+ for(std::map<u16, RemoteClient*>::iterator
+ i = m_clients.begin();
+ i != m_clients.end(); i++)
+ {
+ // Get client and check that it is valid
+ RemoteClient *client = i->second;
+ assert(client->peer_id == i->first);
+ if(client->serialization_version == SER_FMT_VER_INVALID)
+ continue;
+
+ SendAddParticleSpawner(client->peer_id, amount, spawntime,
+ minpos, maxpos, minvel, maxvel, minacc, maxacc,
+ minexptime, maxexptime, minsize, maxsize, collisiondetection, texture, id);
+ }
+}
+
+void Server::SendDeleteParticleSpawner(u16 peer_id, u32 id)
+{
+ DSTACK(__FUNCTION_NAME);
+
+ std::ostringstream os(std::ios_base::binary);
+ writeU16(os, TOCLIENT_DELETE_PARTICLESPAWNER);
+
+ writeU16(os, id);
+
+ // Make data buffer
+ std::string s = os.str();
+ SharedBuffer<u8> data((u8*)s.c_str(), s.size());
+ // Send as reliable
+ m_con.Send(peer_id, 0, data, true);
+}
+
+void Server::SendDeleteParticleSpawnerAll(u32 id)
+{
+ for(std::map<u16, RemoteClient*>::iterator
+ i = m_clients.begin();
+ i != m_clients.end(); i++)
+ {
+ // Get client and check that it is valid
+ RemoteClient *client = i->second;
+ assert(client->peer_id == i->first);
+ if(client->serialization_version == SER_FMT_VER_INVALID)
+ continue;
+
+ SendDeleteParticleSpawner(client->peer_id, id);
+ }
+}
+
+void Server::SendHUDAdd(u16 peer_id, u32 id, HudElement *form)
+{
+ std::ostringstream os(std::ios_base::binary);
+
+ // Write command
+ writeU16(os, TOCLIENT_HUDADD);
+ writeU32(os, id);
+ writeU8(os, (u8)form->type);
+ writeV2F1000(os, form->pos);
+ os << serializeString(form->name);
+ writeV2F1000(os, form->scale);
+ os << serializeString(form->text);
+ writeU32(os, form->number);
+ writeU32(os, form->item);
+ writeU32(os, form->dir);
+ writeV2F1000(os, form->align);
+ writeV2F1000(os, form->offset);
+
+ // Make data buffer
+ std::string s = os.str();
+ SharedBuffer<u8> data((u8*)s.c_str(), s.size());
+ // Send as reliable
+ m_con.Send(peer_id, 0, data, true);
+}
+
+void Server::SendHUDRemove(u16 peer_id, u32 id)
+{
+ std::ostringstream os(std::ios_base::binary);
+
+ // Write command
+ writeU16(os, TOCLIENT_HUDRM);
+ writeU32(os, id);
+
+ // Make data buffer
+ std::string s = os.str();
+ SharedBuffer<u8> data((u8*)s.c_str(), s.size());
+ // Send as reliable
+ m_con.Send(peer_id, 0, data, true);
+}
+
+void Server::SendHUDChange(u16 peer_id, u32 id, HudElementStat stat, void *value)
+{
+ std::ostringstream os(std::ios_base::binary);
+
+ // Write command
+ writeU16(os, TOCLIENT_HUDCHANGE);
+ writeU32(os, id);
+ writeU8(os, (u8)stat);
+ switch (stat) {
+ case HUD_STAT_POS:
+ case HUD_STAT_SCALE:
+ case HUD_STAT_ALIGN:
+ case HUD_STAT_OFFSET:
+ 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> data((u8 *)s.c_str(), s.size());
+ // Send as reliable
+ m_con.Send(peer_id, 0, data, true);
+}
+
+void Server::SendHUDSetFlags(u16 peer_id, u32 flags, u32 mask)
+{
+ std::ostringstream os(std::ios_base::binary);
+
+ // Write command
+ writeU16(os, TOCLIENT_HUD_SET_FLAGS);
+ writeU32(os, flags);
+ writeU32(os, mask);
+
+ // Make data buffer
+ std::string s = os.str();
+ SharedBuffer<u8> data((u8 *)s.c_str(), s.size());
+ // Send as reliable
+ m_con.Send(peer_id, 0, data, true);
+}
+
+void Server::SendHUDSetParam(u16 peer_id, u16 param, const std::string &value)
+{
+ std::ostringstream os(std::ios_base::binary);
+
+ // Write command
+ writeU16(os, TOCLIENT_HUD_SET_PARAM);
+ writeU16(os, param);
+ os<<serializeString(value);
+
+ // Make data buffer
+ std::string s = os.str();
+ SharedBuffer<u8> data((u8 *)s.c_str(), s.size());
+ // Send as reliable
+ m_con.Send(peer_id, 0, data, true);
+}
+
void Server::BroadcastChatMessage(const std::wstring &message)
{
- for(core::map<u16, RemoteClient*>::Iterator
- i = m_clients.getIterator();
- i.atEnd() == false; i++)
+ for(std::map<u16, RemoteClient*>::iterator
+ i = m_clients.begin();
+ i != m_clients.end(); ++i)
{
// Get client and check that it is valid
- RemoteClient *client = i.getNode()->getValue();
- assert(client->peer_id == i.getNode()->getKey());
+ RemoteClient *client = i->second;
+ assert(client->peer_id == i->first);
if(client->serialization_version == SER_FMT_VER_INVALID)
continue;
SendHP(m_con, peer_id, playersao->getHP());
}
+void Server::SendPlayerBreath(u16 peer_id)
+{
+ DSTACK(__FUNCTION_NAME);
+ PlayerSAO *playersao = getPlayerSAO(peer_id);
+ assert(playersao);
+ playersao->m_breath_not_sent = false;
+ SendBreath(m_con, peer_id, playersao->getBreath());
+}
+
void Server::SendMovePlayer(u16 peer_id)
{
DSTACK(__FUNCTION_NAME);
return;
std::set<std::string> privs;
- scriptapi_get_auth(m_lua, player->getName(), NULL, &privs);
+ m_script->getAuth(player->getName(), NULL, &privs);
std::ostringstream os(std::ios_base::binary);
writeU16(os, TOCLIENT_PRIVILEGES);
}
else
{
- for(core::map<u16, RemoteClient*>::Iterator
- i = m_clients.getIterator(); i.atEnd() == false; i++)
+ for(std::map<u16, RemoteClient*>::iterator
+ i = m_clients.begin(); i != m_clients.end(); ++i)
{
- RemoteClient *client = i.getNode()->getValue();
+ RemoteClient *client = i->second;
Player *player = m_env->getPlayer(client->peer_id);
if(!player)
continue;
}
void Server::sendRemoveNode(v3s16 p, u16 ignore_id,
- core::list<u16> *far_players, float far_d_nodes)
+ std::list<u16> *far_players, float far_d_nodes)
{
float maxd = far_d_nodes*BS;
v3f p_f = intToFloat(p, BS);
writeS16(&reply[4], p.Y);
writeS16(&reply[6], p.Z);
- for(core::map<u16, RemoteClient*>::Iterator
- i = m_clients.getIterator();
- i.atEnd() == false; i++)
+ for(std::map<u16, RemoteClient*>::iterator
+ i = m_clients.begin();
+ i != m_clients.end(); ++i)
{
// Get client and check that it is valid
- RemoteClient *client = i.getNode()->getValue();
- assert(client->peer_id == i.getNode()->getKey());
+ RemoteClient *client = i->second;
+ assert(client->peer_id == i->first);
if(client->serialization_version == SER_FMT_VER_INVALID)
continue;
}
void Server::sendAddNode(v3s16 p, MapNode n, u16 ignore_id,
- core::list<u16> *far_players, float far_d_nodes)
+ std::list<u16> *far_players, float far_d_nodes)
{
float maxd = far_d_nodes*BS;
v3f p_f = intToFloat(p, BS);
- for(core::map<u16, RemoteClient*>::Iterator
- i = m_clients.getIterator();
- i.atEnd() == false; i++)
+ for(std::map<u16, RemoteClient*>::iterator
+ i = m_clients.begin();
+ i != m_clients.end(); ++i)
{
// Get client and check that it is valid
- RemoteClient *client = i.getNode()->getValue();
- assert(client->peer_id == i.getNode()->getKey());
+ RemoteClient *client = i->second;
+ assert(client->peer_id == i->first);
if(client->serialization_version == SER_FMT_VER_INVALID)
continue;
void Server::setBlockNotSent(v3s16 p)
{
- for(core::map<u16, RemoteClient*>::Iterator
- i = m_clients.getIterator();
- i.atEnd()==false; i++)
+ for(std::map<u16, RemoteClient*>::iterator
+ i = m_clients.begin();
+ i != m_clients.end(); ++i)
{
- RemoteClient *client = i.getNode()->getValue();
+ RemoteClient *client = i->second;
client->SetBlockNotSent(p);
}
}
ScopeProfiler sp(g_profiler, "Server: sel and send blocks to clients");
- core::array<PrioritySortedBlockTransfer> queue;
+ std::vector<PrioritySortedBlockTransfer> queue;
s32 total_sending = 0;
{
ScopeProfiler sp(g_profiler, "Server: selecting blocks for sending");
- for(core::map<u16, RemoteClient*>::Iterator
- i = m_clients.getIterator();
- i.atEnd() == false; i++)
+ for(std::map<u16, RemoteClient*>::iterator
+ i = m_clients.begin();
+ i != m_clients.end(); ++i)
{
- RemoteClient *client = i.getNode()->getValue();
- assert(client->peer_id == i.getNode()->getKey());
+ RemoteClient *client = i->second;
+ assert(client->peer_id == i->first);
// If definitions and textures have not been sent, don't
// send MapBlocks either
// Sort.
// Lowest priority number comes first.
// Lowest is most important.
- queue.sort();
+ std::sort(queue.begin(), queue.end());
for(u32 i=0; i<queue.size(); i++)
{
verbosestream<<"Server: Announcing files to id("<<peer_id<<")"
<<std::endl;
- core::list<SendableMediaAnnouncement> file_announcements;
+ std::list<SendableMediaAnnouncement> file_announcements;
for(std::map<std::string, MediaInfo>::iterator i = m_media.begin();
i != m_media.end(); i++){
writeU16(os, TOCLIENT_ANNOUNCE_MEDIA);
writeU16(os, file_announcements.size());
- for(core::list<SendableMediaAnnouncement>::Iterator
+ for(std::list<SendableMediaAnnouncement>::iterator
j = file_announcements.begin();
- j != file_announcements.end(); j++){
+ j != file_announcements.end(); ++j){
os<<serializeString(j->name);
os<<serializeString(j->sha1_digest);
}
};
void Server::sendRequestedMedia(u16 peer_id,
- const core::list<MediaRequest> &tosend)
+ const std::list<MediaRequest> &tosend)
{
DSTACK(__FUNCTION_NAME);
// Put 5kB in one bunch (this is not accurate)
u32 bytes_per_bunch = 5000;
- core::array< core::list<SendableMedia> > file_bunches;
- file_bunches.push_back(core::list<SendableMedia>());
+ std::vector< std::list<SendableMedia> > file_bunches;
+ file_bunches.push_back(std::list<SendableMedia>());
u32 file_size_bunch_total = 0;
- for(core::list<MediaRequest>::ConstIterator i = tosend.begin();
- i != tosend.end(); i++)
+ for(std::list<MediaRequest>::const_iterator i = tosend.begin();
+ i != tosend.end(); ++i)
{
if(m_media.find(i->name) == m_media.end()){
errorstream<<"Server::sendRequestedMedia(): Client asked for "
// Start next bunch if got enough data
if(file_size_bunch_total >= bytes_per_bunch){
- file_bunches.push_back(core::list<SendableMedia>());
+ file_bunches.push_back(std::list<SendableMedia>());
file_size_bunch_total = 0;
}
writeU16(os, i);
writeU32(os, file_bunches[i].size());
- for(core::list<SendableMedia>::Iterator
+ for(std::list<SendableMedia>::iterator
j = file_bunches[i].begin();
- j != file_bunches[i].end(); j++){
+ j != file_bunches[i].end(); ++j){
os<<serializeString(j->name);
os<<serializeLongString(j->data);
}
{
DSTACK(__FUNCTION_NAME);
- for(core::map<u16, RemoteClient*>::Iterator
- i = m_clients.getIterator();
- i.atEnd() == false; i++){
- RemoteClient *client = i.getNode()->getValue();
+ for(std::map<u16, RemoteClient*>::iterator
+ i = m_clients.begin();
+ i != m_clients.end(); ++i){
+ RemoteClient *client = i->second;
sendDetachedInventory(name, client->peer_id);
}
}
playersao->setHP(0);
// Trigger scripted stuff
- scriptapi_on_dieplayer(m_lua, playersao);
+ m_script->on_dieplayer(playersao);
SendPlayerHP(peer_id);
SendDeathscreen(m_con, peer_id, false, v3f(0,0,0));
playersao->setHP(PLAYER_MAX_HP);
- bool repositioned = scriptapi_on_respawnplayer(m_lua, playersao);
+ bool repositioned = m_script->on_respawnplayer(playersao);
if(!repositioned){
v3f pos = findSpawnPos(m_env->getServerMap());
playersao->setPos(pos);
{
DSTACK(__FUNCTION_NAME);
//JMutexAutoLock lock(m_con_mutex);
- core::map<u16, RemoteClient*>::Node *n;
+ std::map<u16, RemoteClient*>::iterator n;
n = m_clients.find(peer_id);
// A client should exist for all peers
- assert(n != NULL);
- return n->getValue();
+ assert(n != m_clients.end());
+ return n->second;
}
std::wstring Server::getStatusString()
// Uptime
os<<L", uptime="<<m_uptime.get();
// Information about clients
- core::map<u16, RemoteClient*>::Iterator i;
+ std::map<u16, RemoteClient*>::iterator i;
bool first;
os<<L", clients={";
- for(i = m_clients.getIterator(), first = true;
- i.atEnd() == false; i++)
+ for(i = m_clients.begin(), first = true;
+ i != m_clients.end(); ++i)
{
// Get client and check that it is valid
- RemoteClient *client = i.getNode()->getValue();
- assert(client->peer_id == i.getNode()->getKey());
+ RemoteClient *client = i->second;
+ assert(client->peer_id == i->first);
if(client->serialization_version == SER_FMT_VER_INVALID)
continue;
// Get player
std::set<std::string> Server::getPlayerEffectivePrivs(const std::string &name)
{
std::set<std::string> privs;
- scriptapi_get_auth(m_lua, name, NULL, &privs);
+ m_script->getAuth(name, NULL, &privs);
return privs;
}
void Server::reportPrivsModified(const std::string &name)
{
if(name == ""){
- for(core::map<u16, RemoteClient*>::Iterator
- i = m_clients.getIterator();
- i.atEnd() == false; i++){
- RemoteClient *client = i.getNode()->getValue();
+ for(std::map<u16, RemoteClient*>::iterator
+ i = m_clients.begin();
+ i != m_clients.end(); ++i){
+ RemoteClient *client = i->second;
Player *player = m_env->getPlayer(client->peer_id);
reportPrivsModified(player->getName());
}
g_settings->updateConfigFile(m_path_config.c_str());
}
-void Server::notifyPlayer(const char *name, const std::wstring msg)
+void Server::notifyPlayer(const char *name, const std::wstring msg, const bool prepend = true)
{
Player *player = m_env->getPlayer(name);
if(!player)
return;
- SendChatMessage(player->peer_id, std::wstring(L"Server: -!- ")+msg);
+ if (prepend)
+ SendChatMessage(player->peer_id, std::wstring(L"Server -!- ")+msg);
+ else
+ SendChatMessage(player->peer_id, msg);
}
bool Server::showFormspec(const char *playername, const std::string &formspec, const std::string &formname)
return true;
}
+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 id;
+}
+
+bool Server::hudRemove(Player *player, u32 id) {
+ if (!player || id >= player->hud.size() || !player->hud[id])
+ return false;
+
+ delete player->hud[id];
+ player->hud[id] = NULL;
+
+ SendHUDRemove(player->peer_id, id);
+ return true;
+}
+
+bool Server::hudChange(Player *player, u32 id, HudElementStat stat, void *data) {
+ if (!player)
+ return false;
+
+ SendHUDChange(player->peer_id, id, stat, data);
+ return true;
+}
+
+bool Server::hudSetFlags(Player *player, u32 flags, u32 mask) {
+ if (!player)
+ return false;
+
+ SendHUDSetFlags(player->peer_id, flags, mask);
+ return true;
+}
+
+bool Server::hudSetHotbarItemcount(Player *player, s32 hotbar_itemcount) {
+ if (!player)
+ return false;
+ if (hotbar_itemcount <= 0 || hotbar_itemcount > HUD_HOTBAR_ITEMCOUNT_MAX)
+ return false;
+
+ std::ostringstream os(std::ios::binary);
+ writeS32(os, hotbar_itemcount);
+ SendHUDSetParam(player->peer_id, HUD_PARAM_HOTBAR_ITEMCOUNT, os.str());
+ return true;
+}
+
void Server::notifyPlayers(const std::wstring msg)
{
BroadcastChatMessage(msg);
}
+void Server::spawnParticle(const char *playername, v3f pos,
+ v3f velocity, v3f acceleration,
+ float expirationtime, float size, bool
+ collisiondetection, std::string texture)
+{
+ Player *player = m_env->getPlayer(playername);
+ if(!player)
+ return;
+ SendSpawnParticle(player->peer_id, pos, velocity, acceleration,
+ expirationtime, size, collisiondetection, texture);
+}
+
+void Server::spawnParticleAll(v3f pos, v3f velocity, v3f acceleration,
+ float expirationtime, float size,
+ bool collisiondetection, std::string texture)
+{
+ SendSpawnParticleAll(pos, velocity, acceleration,
+ expirationtime, size, collisiondetection, texture);
+}
+
+u32 Server::addParticleSpawner(const char *playername,
+ u16 amount, float spawntime,
+ v3f minpos, v3f maxpos,
+ v3f minvel, v3f maxvel,
+ v3f minacc, v3f maxacc,
+ float minexptime, float maxexptime,
+ float minsize, float maxsize,
+ bool collisiondetection, std::string texture)
+{
+ Player *player = m_env->getPlayer(playername);
+ if(!player)
+ return -1;
+
+ u32 id = 0;
+ for(;;) // look for unused particlespawner id
+ {
+ id++;
+ if (std::find(m_particlespawner_ids.begin(),
+ m_particlespawner_ids.end(), id)
+ == m_particlespawner_ids.end())
+ {
+ m_particlespawner_ids.push_back(id);
+ break;
+ }
+ }
+
+ SendAddParticleSpawner(player->peer_id, amount, spawntime,
+ minpos, maxpos, minvel, maxvel, minacc, maxacc,
+ minexptime, maxexptime, minsize, maxsize,
+ collisiondetection, texture, id);
+
+ return id;
+}
+
+u32 Server::addParticleSpawnerAll(u16 amount, float spawntime,
+ v3f minpos, v3f maxpos,
+ v3f minvel, v3f maxvel,
+ v3f minacc, v3f maxacc,
+ float minexptime, float maxexptime,
+ float minsize, float maxsize,
+ bool collisiondetection, std::string texture)
+{
+ u32 id = 0;
+ for(;;) // look for unused particlespawner id
+ {
+ id++;
+ if (std::find(m_particlespawner_ids.begin(),
+ m_particlespawner_ids.end(), id)
+ == m_particlespawner_ids.end())
+ {
+ m_particlespawner_ids.push_back(id);
+ break;
+ }
+ }
+
+ SendAddParticleSpawnerAll(amount, spawntime,
+ minpos, maxpos, minvel, maxvel, minacc, maxacc,
+ minexptime, maxexptime, minsize, maxsize,
+ collisiondetection, texture, id);
+
+ return id;
+}
+
+void Server::deleteParticleSpawner(const char *playername, u32 id)
+{
+ Player *player = m_env->getPlayer(playername);
+ if(!player)
+ return;
+
+ m_particlespawner_ids.erase(
+ std::remove(m_particlespawner_ids.begin(),
+ m_particlespawner_ids.end(), id),
+ m_particlespawner_ids.end());
+ SendDeleteParticleSpawner(player->peer_id, id);
+}
+
+void Server::deleteParticleSpawnerAll(u32 id)
+{
+ m_particlespawner_ids.erase(
+ std::remove(m_particlespawner_ids.begin(),
+ m_particlespawner_ids.end(), id),
+ m_particlespawner_ids.end());
+ SendDeleteParticleSpawnerAll(id);
+}
+
void Server::queueBlockEmerge(v3s16 blockpos, bool allow_generate)
{
m_emerge->enqueueBlockEmerge(PEER_ID_INEXISTENT, blockpos, allow_generate);
}
return NULL;
}
-void Server::getModNames(core::list<std::string> &modlist)
+void Server::getModNames(std::list<std::string> &modlist)
{
for(std::vector<ModSpec>::iterator i = m_mods.begin(); i != m_mods.end(); i++)
{
- modlist.push_back((*i).name);
+ modlist.push_back(i->name);
}
}
std::string Server::getBuiltinLuaPath()
{
s32 range = 1 + i;
// We're going to try to throw the player to this position
- v2s16 nodepos2d = v2s16(-range + (myrand()%(range*2)),
- -range + (myrand()%(range*2)));
- //v2s16 sectorpos = getNodeSectorPos(nodepos2d);
- // Get ground height at point (fallbacks to heightmap function)
+ v2s16 nodepos2d = v2s16(
+ -range + (myrand() % (range * 2)),
+ -range + (myrand() % (range * 2)));
+
+ // Get ground height at point
s16 groundheight = map.findGroundLevel(nodepos2d);
- // Don't go underwater
- if(groundheight <= water_level)
- {
- //infostream<<"-> Underwater"<<std::endl;
+ if (groundheight <= water_level) // Don't go underwater
continue;
- }
- // Don't go to high places
- if(groundheight > water_level + 6)
- {
- //infostream<<"-> Underwater"<<std::endl;
+ if (groundheight > water_level + 6) // Don't go to high places
continue;
- }
- nodepos = v3s16(nodepos2d.X, groundheight-2, nodepos2d.Y);
+ nodepos = v3s16(nodepos2d.X, groundheight, nodepos2d.Y);
bool is_good = false;
s32 air_count = 0;
- for(s32 i=0; i<10; i++){
+ for (s32 i = 0; i < 10; i++) {
v3s16 blockpos = getNodeBlockPos(nodepos);
map.emergeBlock(blockpos, true);
- MapNode n = map.getNodeNoEx(nodepos);
- if(n.getContent() == CONTENT_AIR){
+ content_t c = map.getNodeNoEx(nodepos).getContent();
+ if (c == CONTENT_AIR || c == CONTENT_IGNORE) {
air_count++;
- if(air_count >= 2){
+ if (air_count >= 2){
is_good = true;
- nodepos.Y -= 1;
break;
}
}
getPlayerEffectivePrivs(player->getName()),
isSingleplayer());
+ /* Clean up old HUD elements from previous sessions */
+ player->hud.clear();
+
/* Add object to environment */
m_env->addActiveObject(playersao);
/* Run scripts */
if(newplayer)
- scriptapi_on_newplayer(m_lua, playersao);
+ m_script->on_newplayer(playersao);
- scriptapi_on_joinplayer(m_lua, playersao);
+ m_script->on_joinplayer(playersao);
return playersao;
}
*/
// Error check
- core::map<u16, RemoteClient*>::Node *n;
+ std::map<u16, RemoteClient*>::iterator n;
n = m_clients.find(c.peer_id);
// The client shouldn't already exist
- assert(n == NULL);
+ assert(n == m_clients.end());
// Create client
RemoteClient *client = new RemoteClient();
client->peer_id = c.peer_id;
- m_clients.insert(client->peer_id, client);
+ m_clients[client->peer_id] = client;
} // PEER_ADDED
else if(c.type == PEER_REMOVED)
*/
// Error check
- core::map<u16, RemoteClient*>::Node *n;
+ std::map<u16, RemoteClient*>::iterator n;
n = m_clients.find(c.peer_id);
// The client should exist
- assert(n != NULL);
+ assert(n != m_clients.end());
/*
Mark objects to be not known by the client
*/
- RemoteClient *client = n->getValue();
+ RemoteClient *client = n->second;
// Handle objects
- for(core::map<u16, bool>::Iterator
- i = client->m_known_objects.getIterator();
- i.atEnd()==false; i++)
+ for(std::set<u16>::iterator
+ i = client->m_known_objects.begin();
+ i != client->m_known_objects.end(); ++i)
{
// Get object
- u16 id = i.getNode()->getKey();
+ u16 id = *i;
ServerActiveObject* obj = m_env->getActiveObject(id);
if(obj && obj->m_known_by_count > 0)
PlayerSAO *playersao = player->getPlayerSAO();
assert(playersao);
- scriptapi_on_leaveplayer(m_lua, playersao);
+ m_script->on_leaveplayer(playersao);
playersao->disconnected();
}
if(player != NULL)
{
std::ostringstream os(std::ios_base::binary);
- for(core::map<u16, RemoteClient*>::Iterator
- i = m_clients.getIterator();
- i.atEnd() == false; i++)
+ for(std::map<u16, RemoteClient*>::iterator
+ i = m_clients.begin();
+ i != m_clients.end(); ++i)
{
- RemoteClient *client = i.getNode()->getValue();
- assert(client->peer_id == i.getNode()->getKey());
+ RemoteClient *client = i->second;
+ assert(client->peer_id == i->first);
if(client->serialization_version == SER_FMT_VER_INVALID)
continue;
// Get player
// Delete client
delete m_clients[c.peer_id];
- m_clients.remove(c.peer_id);
+ m_clients.erase(c.peer_id);
// Send player info to all remaining clients
//SendPlayerInfos();