/*
Minetest
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "inventorymanager.h"
#include "log.h"
#include "environment.h"
#include "scripting_game.h"
#include "serverobject.h"
#include "main.h" // for g_settings
#include "settings.h"
#include "craftdef.h"
#include "rollback_interface.h"
#include "strfnd.h"
#define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
#define PLAYER_TO_SA(p) p->getEnv()->getScriptIface()
/*
InventoryLocation
*/
std::string InventoryLocation::dump() const
{
std::ostringstream os(std::ios::binary);
serialize(os);
return os.str();
}
void InventoryLocation::serialize(std::ostream &os) const
{
switch(type){
case InventoryLocation::UNDEFINED:
os<<"undefined";
break;
case InventoryLocation::CURRENT_PLAYER:
os<<"current_player";
break;
case InventoryLocation::PLAYER:
os<<"player:"<<name;
break;
case InventoryLocation::NODEMETA:
os<<"nodemeta:"<<p.X<<","<<p.Y<<","<<p.Z;
break;
case InventoryLocation::DETACHED:
os<<"detached:"<<name;
break;
default:
assert(0);
}
}
void InventoryLocation::deSerialize(std::istream &is)
{
std::string tname;
std::getline(is, tname, ':');
if(tname == "undefined")
{
type = InventoryLocation::UNDEFINED;
}
else if(tname == "current_player")
{
type = InventoryLocation::CURRENT_PLAYER;
}
else if(tname == "player")
{
type = InventoryLocation::PLAYER;
std::getline(is, name, '\n');
}
else if(tname == "nodemeta")
{
type = InventoryLocation::NODEMETA;
std::string pos;
std::getline(is, pos, '\n');
Strfnd fn(pos);
p.X = stoi(fn.next(","));
p.Y = stoi(fn.next(","));
p.Z = stoi(fn.next(","));
}
else if(tname == "detached")
{
type = InventoryLocation::DETACHED;
std::getline(is, name, '\n');
}
else
{
infostream<<"Unknown InventoryLocation type=\""<<tname<<"\""<<std::endl;
throw SerializationError("Unknown InventoryLocation type");
}
}
void InventoryLocation::deSerialize(std::string s)
{
std::istringstream is(s, std::ios::binary);
deSerialize(is);
}
/*
InventoryAction
*/
InventoryAction * InventoryAction::deSerialize(std::istream &is)
{
std::string type;
std::getline(is, type, ' ');
InventoryAction *a = NULL;
if(type == "Move")
{
a = new IMoveAction(is);
}
else if(type == "Drop")
{
a = new IDropAction(is);
}
else if(type == "Craft")
{
a = new ICraftAction(is);
}
return a;
}
/*
IMoveAction
*/
IMoveAction::IMoveAction(std::istream &is)
{
std::string ts;
std::getline(is, ts, ' ');
count = stoi(ts);
|