31 changes: 13 additions & 18 deletions src/script/cpp_api/s_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ bool ScriptApiBase::loadScript(const std::string &script_path, std::string *erro
if (!ok) {
std::string error_msg = lua_tostring(L, -1);
if (error)
(*error) = error_msg;
*error = error_msg;
errorstream << "========== ERROR FROM LUA ===========" << std::endl
<< "Failed to load and run script from " << std::endl
<< script_path << ":" << std::endl << std::endl
Expand All @@ -157,8 +157,8 @@ bool ScriptApiBase::loadScript(const std::string &script_path, std::string *erro
void ScriptApiBase::realityCheck()
{
int top = lua_gettop(m_luastack);
if(top >= 30){
dstream<<"Stack is over 30:"<<std::endl;
if (top >= 30) {
dstream << "Stack is over 30:" << std::endl;
stackDump(dstream);
std::string traceback = script_get_backtrace(m_luastack);
throw LuaError("Stack is over 30 (reality check)\n" + traceback);
Expand All @@ -172,34 +172,29 @@ void ScriptApiBase::scriptError()

void ScriptApiBase::stackDump(std::ostream &o)
{
int i;
int top = lua_gettop(m_luastack);
for (i = 1; i <= top; i++) { /* repeat for each level */
for (int i = 1; i <= top; i++) { /* repeat for each level */
int t = lua_type(m_luastack, i);
switch (t) {

case LUA_TSTRING: /* strings */
o<<"\""<<lua_tostring(m_luastack, i)<<"\"";
o << "\"" << lua_tostring(m_luastack, i) << "\"";
break;

case LUA_TBOOLEAN: /* booleans */
o<<(lua_toboolean(m_luastack, i) ? "true" : "false");
o << (lua_toboolean(m_luastack, i) ? "true" : "false");
break;

case LUA_TNUMBER: /* numbers */ {
char buf[10];
snprintf(buf, 10, "%g", lua_tonumber(m_luastack, i));
o<<buf;
break; }

o << buf;
break;
}
default: /* other values */
o<<lua_typename(m_luastack, t);
o << lua_typename(m_luastack, t);
break;

}
o<<" ";
o << " ";
}
o<<std::endl;
o << std::endl;
}

void ScriptApiBase::addObjectReference(ServerActiveObject *cobj)
Expand Down Expand Up @@ -251,7 +246,7 @@ void ScriptApiBase::removeObjectReference(ServerActiveObject *cobj)
void ScriptApiBase::objectrefGetOrCreate(lua_State *L,
ServerActiveObject *cobj)
{
if(cobj == NULL || cobj->getId() == 0){
if (cobj == NULL || cobj->getId() == 0) {
ObjectRef::create(L, cobj);
} else {
objectrefGet(L, cobj->getId());
Expand Down
3 changes: 2 additions & 1 deletion src/script/cpp_api/s_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ class ScriptApiBase {
ScriptApiBase();
virtual ~ScriptApiBase();

bool loadMod(const std::string &script_path, const std::string &mod_name, std::string *error=NULL);
bool loadMod(const std::string &script_path, const std::string &mod_name,
std::string *error=NULL);
bool loadScript(const std::string &script_path, std::string *error=NULL);

/* object */
Expand Down
143 changes: 68 additions & 75 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2838,7 +2838,7 @@ std::string Server::getBanDescription(const std::string &ip_or_name)
void Server::notifyPlayer(const char *name, const std::wstring &msg)
{
Player *player = m_env->getPlayer(name);
if(!player)
if (!player)
return;

if (player->peer_id == PEER_ID_INEXISTENT)
Expand All @@ -2847,21 +2847,19 @@ void Server::notifyPlayer(const char *name, const std::wstring &msg)
SendChatMessage(player->peer_id, msg);
}

bool Server::showFormspec(const char *playername, const std::string &formspec, const std::string &formname)
bool Server::showFormspec(const char *playername, const std::string &formspec,
const std::string &formname)
{
Player *player = m_env->getPlayer(playername);

if(!player)
{
infostream<<"showFormspec: couldn't find player:"<<playername<<std::endl;
if (!player)
return false;
}

SendShowFormspecMessage(player->peer_id, formspec, formname);
return true;
}

u32 Server::hudAdd(Player *player, HudElement *form) {
u32 Server::hudAdd(Player *player, HudElement *form)
{
if (!player)
return -1;

Expand All @@ -2887,15 +2885,17 @@ bool Server::hudRemove(Player *player, u32 id) {
return true;
}

bool Server::hudChange(Player *player, u32 id, HudElementStat stat, void *data) {
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) {
bool Server::hudSetFlags(Player *player, u32 flags, u32 mask)
{
if (!player)
return false;

Expand All @@ -2911,7 +2911,8 @@ bool Server::hudSetFlags(Player *player, u32 flags, u32 mask) {
return true;
}

bool Server::hudSetHotbarItemcount(Player *player, s32 hotbar_itemcount) {
bool Server::hudSetHotbarItemcount(Player *player, s32 hotbar_itemcount)
{
if (!player)
return false;
if (hotbar_itemcount <= 0 || hotbar_itemcount > HUD_HOTBAR_ITEMCOUNT_MAX)
Expand All @@ -2924,42 +2925,48 @@ bool Server::hudSetHotbarItemcount(Player *player, s32 hotbar_itemcount) {
return true;
}

s32 Server::hudGetHotbarItemcount(Player *player) {
s32 Server::hudGetHotbarItemcount(Player *player)
{
if (!player)
return 0;
return player->getHotbarItemcount();
}

void Server::hudSetHotbarImage(Player *player, std::string name) {
void Server::hudSetHotbarImage(Player *player, std::string name)
{
if (!player)
return;

player->setHotbarImage(name);
SendHUDSetParam(player->peer_id, HUD_PARAM_HOTBAR_IMAGE, name);
}

std::string Server::hudGetHotbarImage(Player *player) {
std::string Server::hudGetHotbarImage(Player *player)
{
if (!player)
return "";
return player->getHotbarImage();
}

void Server::hudSetHotbarSelectedImage(Player *player, std::string name) {
void Server::hudSetHotbarSelectedImage(Player *player, std::string name)
{
if (!player)
return;

player->setHotbarSelectedImage(name);
SendHUDSetParam(player->peer_id, HUD_PARAM_HOTBAR_SELECTED_IMAGE, name);
}

std::string Server::hudGetHotbarSelectedImage(Player *player) {
std::string Server::hudGetHotbarSelectedImage(Player *player)
{
if (!player)
return "";

return player->getHotbarSelectedImage();
}

bool Server::setLocalPlayerAnimations(Player *player, v2s32 animation_frames[4], f32 frame_speed)
bool Server::setLocalPlayerAnimations(Player *player,
v2s32 animation_frames[4], f32 frame_speed)
{
if (!player)
return false;
Expand All @@ -2981,7 +2988,7 @@ bool Server::setPlayerEyeOffset(Player *player, v3f first, v3f third)
}

bool Server::setSky(Player *player, const video::SColor &bgcolor,
const std::string &type, const std::vector<std::string> &params)
const std::string &type, const std::vector<std::string> &params)
{
if (!player)
return false;
Expand All @@ -2992,7 +2999,7 @@ bool Server::setSky(Player *player, const video::SColor &bgcolor,
}

bool Server::overrideDayNightRatio(Player *player, bool do_override,
float ratio)
float ratio)
{
if (!player)
return false;
Expand All @@ -3008,9 +3015,9 @@ void Server::notifyPlayers(const std::wstring &msg)
}

void Server::spawnParticle(const char *playername, v3f pos,
v3f velocity, v3f acceleration,
float expirationtime, float size, bool
collisiondetection, bool vertical, std::string texture)
v3f velocity, v3f acceleration,
float expirationtime, float size, bool
collisiondetection, bool vertical, const std::string &texture)
{
Player *player = m_env->getPlayer(playername);
if(!player)
Expand All @@ -3020,21 +3027,17 @@ void Server::spawnParticle(const char *playername, v3f pos,
}

void Server::spawnParticleAll(v3f pos, v3f velocity, v3f acceleration,
float expirationtime, float size,
bool collisiondetection, bool vertical, std::string texture)
float expirationtime, float size,
bool collisiondetection, bool vertical, const std::string &texture)
{
SendSpawnParticle(PEER_ID_INEXISTENT,pos, velocity, acceleration,
expirationtime, size, collisiondetection, vertical, 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, bool vertical, std::string 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, bool vertical, const std::string &texture)
{
Player *player = m_env->getPlayer(playername);
if(!player)
Expand Down Expand Up @@ -3062,12 +3065,12 @@ u32 Server::addParticleSpawner(const char *playername,
}

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, bool vertical, std::string texture)
v3f minpos, v3f maxpos,
v3f minvel, v3f maxvel,
v3f minacc, v3f maxacc,
float minexptime, float maxexptime,
float minsize, float maxsize,
bool collisiondetection, bool vertical, const std::string &texture)
{
u32 id = 0;
for(;;) // look for unused particlespawner id
Expand Down Expand Up @@ -3128,24 +3131,6 @@ Inventory* Server::createDetachedInventory(const std::string &name)
return inv;
}

class BoolScopeSet
{
public:
BoolScopeSet(bool *dst, bool val):
m_dst(dst)
{
m_orig_state = *m_dst;
*m_dst = val;
}
~BoolScopeSet()
{
*m_dst = m_orig_state;
}
private:
bool *m_dst;
bool m_orig_state;
};

// actions: time-reversed list
// Return value: success/failure
bool Server::rollbackRevertActions(const std::list<RollbackAction> &actions,
Expand Down Expand Up @@ -3195,27 +3180,29 @@ bool Server::rollbackRevertActions(const std::list<RollbackAction> &actions,

// IGameDef interface
// Under envlock
IItemDefManager* Server::getItemDefManager()
IItemDefManager *Server::getItemDefManager()
{
return m_itemdef;
}
INodeDefManager* Server::getNodeDefManager()

INodeDefManager *Server::getNodeDefManager()
{
return m_nodedef;
}
ICraftDefManager* Server::getCraftDefManager()

ICraftDefManager *Server::getCraftDefManager()
{
return m_craftdef;
}
ITextureSource* Server::getTextureSource()
ITextureSource *Server::getTextureSource()
{
return NULL;
}
IShaderSource* Server::getShaderSource()
IShaderSource *Server::getShaderSource()
{
return NULL;
}
scene::ISceneManager* Server::getSceneManager()
scene::ISceneManager *Server::getSceneManager()
{
return NULL;
}
Expand All @@ -3224,44 +3211,50 @@ u16 Server::allocateUnknownNodeId(const std::string &name)
{
return m_nodedef->allocateDummy(name);
}
ISoundManager* Server::getSoundManager()

ISoundManager *Server::getSoundManager()
{
return &dummySoundManager;
}
MtEventManager* Server::getEventManager()

MtEventManager *Server::getEventManager()
{
return m_event;
}

IWritableItemDefManager* Server::getWritableItemDefManager()
IWritableItemDefManager *Server::getWritableItemDefManager()
{
return m_itemdef;
}
IWritableNodeDefManager* Server::getWritableNodeDefManager()

IWritableNodeDefManager *Server::getWritableNodeDefManager()
{
return m_nodedef;
}
IWritableCraftDefManager* Server::getWritableCraftDefManager()

IWritableCraftDefManager *Server::getWritableCraftDefManager()
{
return m_craftdef;
}

const ModSpec* Server::getModSpec(const std::string &modname) const
const ModSpec *Server::getModSpec(const std::string &modname) const
{
for(std::vector<ModSpec>::const_iterator i = m_mods.begin();
i != m_mods.end(); i++){
const ModSpec &mod = *i;
if(mod.name == modname)
std::vector<ModSpec>::const_iterator it;
for (it = m_mods.begin(); it != m_mods.end(); ++it) {
const ModSpec &mod = *it;
if (mod.name == modname)
return &mod;
}
return NULL;
}

void Server::getModNames(std::vector<std::string> &modlist)
{
for(std::vector<ModSpec>::iterator i = m_mods.begin(); i != m_mods.end(); i++) {
modlist.push_back(i->name);
}
std::vector<ModSpec>::iterator it;
for (it = m_mods.begin(); it != m_mods.end(); ++it)
modlist.push_back(it->name);
}

std::string Server::getBuiltinLuaPath()
{
return porting::path_share + DIR_DELIM + "builtin";
Expand Down
8 changes: 4 additions & 4 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,11 @@ class Server : public con::PeerHandler, public MapEventReceiver,
void spawnParticle(const char *playername,
v3f pos, v3f velocity, v3f acceleration,
float expirationtime, float size,
bool collisiondetection, bool vertical, std::string texture);
bool collisiondetection, bool vertical, const std::string &texture);

void spawnParticleAll(v3f pos, v3f velocity, v3f acceleration,
float expirationtime, float size,
bool collisiondetection, bool vertical, std::string texture);
bool collisiondetection, bool vertical, const std::string &texture);

u32 addParticleSpawner(const char *playername,
u16 amount, float spawntime,
Expand All @@ -280,15 +280,15 @@ class Server : public con::PeerHandler, public MapEventReceiver,
v3f minacc, v3f maxacc,
float minexptime, float maxexptime,
float minsize, float maxsize,
bool collisiondetection, bool vertical, std::string texture);
bool collisiondetection, bool vertical, const std::string &texture);

u32 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, bool vertical, std::string texture);
bool collisiondetection, bool vertical, const std::string &texture);

void deleteParticleSpawner(const char *playername, u32 id);
void deleteParticleSpawnerAll(u32 id);
Expand Down