Inventory menu (with dragging) improved. Crafting is now handled via a IACTION_CRAFT inventory action.

This commit is contained in:
Kahrl 2012-01-21 21:21:41 +01:00 committed by Perttu Ahola
parent 157a4cf18c
commit 1efdc36b22
9 changed files with 550 additions and 289 deletions

@ -1310,7 +1310,7 @@ void the_game(
"list", inventoryloc, "craft", "list", inventoryloc, "craft",
v2s32(3, 0), v2s32(3, 3))); v2s32(3, 0), v2s32(3, 3)));
draw_spec.push_back(GUIInventoryMenu::DrawSpec( draw_spec.push_back(GUIInventoryMenu::DrawSpec(
"list", inventoryloc, "craftresult", "list", inventoryloc, "craftpreview",
v2s32(7, 1), v2s32(1, 1))); v2s32(7, 1), v2s32(1, 1)));
menu->setDrawSpec(draw_spec); menu->setDrawSpec(draw_spec);

@ -133,6 +133,8 @@ GUIInventoryMenu::GUIInventoryMenu(gui::IGUIEnvironment* env,
m_gamedef(gamedef) m_gamedef(gamedef)
{ {
m_selected_item = NULL; m_selected_item = NULL;
m_selected_amount = 0;
m_selected_dragging = false;
m_tooltip_element = NULL; m_tooltip_element = NULL;
} }
@ -317,29 +319,18 @@ void GUIInventoryMenu::drawList(const ListDrawSpec &s, int phase)
} }
} }
if(phase == 1 && !item.empty()) if(phase == 1)
{ {
// Draw item at the normal position if // Draw item stack
// - the item is not being dragged or if(selected)
// /*- the item is in the crafting result slot*/ {
if(!selected /*|| s.listname == "craftresult"*/) item.takeItem(m_selected_amount);
}
if(!item.empty())
{ {
drawItemStack(driver, font, item, drawItemStack(driver, font, item,
rect, &AbsoluteClippingRect, m_gamedef); rect, &AbsoluteClippingRect, m_gamedef);
} }
}
if(phase ==2 && !item.empty())
{
// Draw dragged item
if(selected)
{
v2s32 offset = m_pointer - rect.getCenter();
rect.UpperLeftCorner += offset;
rect.LowerRightCorner += offset;
drawItemStack(driver, font, item,
rect, NULL, m_gamedef);
}
// Draw tooltip // Draw tooltip
std::string tooltip_text = ""; std::string tooltip_text = "";
@ -359,12 +350,38 @@ void GUIInventoryMenu::drawList(const ListDrawSpec &s, int phase)
core::dimension2d<s32>(tooltip_width, tooltip_height))); core::dimension2d<s32>(tooltip_width, tooltip_height)));
} }
} }
} }
} }
void GUIInventoryMenu::drawSelectedItem()
{
if(!m_selected_item)
return;
video::IVideoDriver* driver = Environment->getVideoDriver();
// Get font
gui::IGUIFont *font = NULL;
gui::IGUISkin* skin = Environment->getSkin();
if (skin)
font = skin->getFont();
Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
assert(inv);
InventoryList *list = inv->getList(m_selected_item->listname);
assert(list);
ItemStack stack = list->getItem(m_selected_item->i);
stack.count = m_selected_amount;
core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
drawItemStack(driver, font, stack, rect, NULL, m_gamedef);
}
void GUIInventoryMenu::drawMenu() void GUIInventoryMenu::drawMenu()
{ {
updateSelectedItem();
gui::IGUISkin* skin = Environment->getSkin(); gui::IGUISkin* skin = Environment->getSkin();
if (!skin) if (!skin)
return; return;
@ -378,22 +395,93 @@ void GUIInventoryMenu::drawMenu()
/* /*
Draw items Draw items
Phase 0: Item slot rectangles Phase 0: Item slot rectangles
Phase 1: Item images Phase 1: Item images; prepare tooltip
Phase 2: Dragged item image; tooltip
*/ */
for(int phase=0; phase<=2; phase++) for(int phase=0; phase<=1; phase++)
for(u32 i=0; i<m_draw_spec.size(); i++) for(u32 i=0; i<m_draw_spec.size(); i++)
{ {
drawList(m_draw_spec[i], phase); drawList(m_draw_spec[i], phase);
} }
/*
Draw dragged item stack
*/
drawSelectedItem();
/* /*
Call base class Call base class
*/ */
gui::IGUIElement::draw(); gui::IGUIElement::draw();
} }
void GUIInventoryMenu::updateSelectedItem()
{
// If the selected stack has become empty for some reason, deselect it.
// If the selected stack has become smaller, adjust m_selected_amount.
if(m_selected_item)
{
bool selection_valid = false;
if(m_selected_item->isValid())
{
Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
if(inv)
{
InventoryList *list = inv->getList(m_selected_item->listname);
if(list && (u32) m_selected_item->i < list->getSize())
{
ItemStack stack = list->getItem(m_selected_item->i);
if(m_selected_amount > stack.count)
m_selected_amount = stack.count;
if(!stack.empty())
selection_valid = true;
}
}
}
if(!selection_valid)
{
delete m_selected_item;
m_selected_item = NULL;
m_selected_amount = 0;
m_selected_dragging = false;
}
}
// If craftresult is nonempty and nothing else is selected, select it now.
if(!m_selected_item)
{
for(u32 i=0; i<m_draw_spec.size(); i++)
{
const ListDrawSpec &s = m_draw_spec[i];
if(s.listname == "craftpreview")
{
Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
InventoryList *list = inv->getList("craftresult");
if(list && list->getSize() >= 1 && !list->getItem(0).empty())
{
m_selected_item = new ItemSpec;
m_selected_item->inventoryloc = s.inventoryloc;
m_selected_item->listname = "craftresult";
m_selected_item->i = 0;
m_selected_amount = 0;
m_selected_dragging = false;
break;
}
}
}
}
// If craftresult is selected, keep the whole stack selected
if(m_selected_item && m_selected_item->listname == "craftresult")
{
Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
assert(inv);
InventoryList *list = inv->getList(m_selected_item->listname);
assert(list);
m_selected_amount = list->getItem(m_selected_item->i).count;
}
}
bool GUIInventoryMenu::OnEvent(const SEvent& event) bool GUIInventoryMenu::OnEvent(const SEvent& event)
{ {
if(event.EventType==EET_KEY_INPUT_EVENT) if(event.EventType==EET_KEY_INPUT_EVENT)
@ -418,133 +506,242 @@ bool GUIInventoryMenu::OnEvent(const SEvent& event)
// Mouse event other than movement // Mouse event other than movement
v2s32 p(event.MouseInput.X, event.MouseInput.Y); v2s32 p(event.MouseInput.X, event.MouseInput.Y);
m_pointer = p;
// Get selected item and hovered/clicked item (s)
updateSelectedItem();
ItemSpec s = getItemAtPos(p); ItemSpec s = getItemAtPos(p);
Inventory *inv_selected = NULL; Inventory *inv_selected = NULL;
Inventory *inv_s = NULL; Inventory *inv_s = NULL;
if(m_selected_item) if(m_selected_item)
{ {
assert(m_selected_item->isValid());
inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc); inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
assert(inv_selected); assert(inv_selected);
assert(inv_selected->getList(m_selected_item->listname) != NULL);
} }
u32 s_count = 0;
if(s.isValid()) if(s.isValid())
{ {
inv_s = m_invmgr->getInventory(s.inventoryloc); inv_s = m_invmgr->getInventory(s.inventoryloc);
assert(inv_s); assert(inv_s);
InventoryList *list = inv_s->getList(s.listname);
if(list != NULL && (u32) s.i < list->getSize())
s_count = list->getItem(s.i).count;
else
s.i = -1; // make it invalid again
} }
bool different_item = m_selected_item
&& ((inv_selected != inv_s)
|| (m_selected_item->listname != s.listname)
|| (m_selected_item->i != s.i));
int amount = -1; // buttons: 0 = left, 1 = right, 2 = middle
// up/down: 0 = down (press), 1 = up (release), 2 = unknown event
int button = 0;
int updown = 2;
if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
amount = 0; { button = 0; updown = 0; }
else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN) else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
amount = 1; { button = 1; updown = 0; }
else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN) else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
amount = 10; { button = 2; updown = 0; }
else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP && different_item) else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
amount = 0; { button = 0; updown = 1; }
//else if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP && different_item) else if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
// amount = 1; { button = 1; updown = 1; }
//else if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP && different_item) else if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
// amount = 10; { button = 2; updown = 1; }
if(amount >= 0) // Set this number to a positive value to generate a move action
// from m_selected_item to s.
u32 move_amount = 0;
// Set this number to a positive value to generate a drop action
// from m_selected_item.
u32 drop_amount = 0;
// Set this number to a positive value to generate a craft action at s.
u32 craft_amount = 0;
if(updown == 0)
{ {
// Indicates whether source slot should be deselected // Some mouse button has been pressed
bool remove_selection = false;
//infostream<<"Mouse action at p=("<<p.X<<","<<p.Y<<")"<<std::endl; //infostream<<"Mouse button "<<button<<" pressed at p=("
if(s.isValid()) // <<p.X<<","<<p.Y<<")"<<std::endl;
m_selected_dragging = false;
if(s.isValid() && s.listname == "craftpreview")
{ {
infostream<<"Mouse action on "<<s.inventoryloc.dump() // Craft preview has been clicked: craft
<<"/"<<s.listname<<" "<<s.i<<std::endl; craft_amount = (button == 2 ? 10 : 1);
if(m_selected_item) }
else if(m_selected_item == NULL)
{
if(s_count != 0)
{ {
Inventory *inv_from = inv_selected; // Non-empty stack has been clicked: select it
Inventory *inv_to = inv_s; m_selected_item = new ItemSpec(s);
assert(inv_from);
assert(inv_to); if(button == 1) // right
InventoryList *list_from = m_selected_amount = (s_count + 1) / 2;
inv_from->getList(m_selected_item->listname); else if(button == 2) // middle
InventoryList *list_to = m_selected_amount = MYMIN(s_count, 10);
inv_to->getList(s.listname); else // left
if(list_from == NULL) m_selected_amount = s_count;
infostream<<"from list doesn't exist"<<std::endl;
if(list_to == NULL) m_selected_dragging = true;
infostream<<"to list doesn't exist"<<std::endl; }
if(list_from && list_to }
&& !list_from->getItem(m_selected_item->i).empty()) else // m_selected_item != NULL
{ {
infostream<<"Handing IACTION_MOVE to manager"<<std::endl; assert(m_selected_amount >= 1);
IMoveAction *a = new IMoveAction();
a->count = amount; if(s.isValid())
a->from_inv = m_selected_item->inventoryloc; {
a->from_list = m_selected_item->listname; // Clicked another slot: move
a->from_i = m_selected_item->i; if(button == 1) // right
a->to_inv = s.inventoryloc; move_amount = 1;
a->to_list = s.listname; else if(button == 2) // middle
a->to_i = s.i; move_amount = MYMIN(m_selected_amount, 10);
m_invmgr->inventoryAction(a); else // left
move_amount = m_selected_amount;
if(amount == 0 || list_from->getItem(m_selected_item->i).count<=amount) }
remove_selection = true; else if(getAbsoluteClippingRect().isPointInside(m_pointer))
} {
// Clicked somewhere else: deselect
m_selected_amount = 0;
} }
else else
{ {
/* // Clicked outside of the window: drop
Select if nonempty if(button == 1) // right
*/ drop_amount = 1;
assert(inv_s); else if(button == 2) // middle
InventoryList *list = inv_s->getList(s.listname); drop_amount = MYMIN(m_selected_amount, 10);
if(list && !list->getItem(s.i).empty()) else // left
{ drop_amount = m_selected_amount;
m_selected_item = new ItemSpec(s);
}
} }
} }
else if(m_selected_item) }
else if(updown == 1)
{
// Some mouse button has been released
//infostream<<"Mouse button "<<button<<" released at p=("
// <<p.X<<","<<p.Y<<")"<<std::endl;
if(m_selected_item != NULL && m_selected_dragging && s.isValid())
{ {
// If moved outside the menu, drop. if((inv_selected != inv_s) ||
// (Otherwise abort inventory action.) (m_selected_item->listname != s.listname) ||
if(getAbsoluteClippingRect().isPointInside(m_pointer)) (m_selected_item->i != s.i))
{ {
// Inside menu // Dragged to different slot: move all selected
remove_selection = true; move_amount = m_selected_amount;
}
else
{
// Outside of menu
Inventory *inv_from = inv_selected;
assert(inv_from);
InventoryList *list_from =
inv_from->getList(m_selected_item->listname);
if(list_from == NULL)
infostream<<"from list doesn't exist"<<std::endl;
if(list_from && !list_from->getItem(m_selected_item->i).empty())
{
infostream<<"Handing IACTION_DROP to manager"<<std::endl;
IDropAction *a = new IDropAction();
a->count = amount;
a->from_inv = m_selected_item->inventoryloc;
a->from_list = m_selected_item->listname;
a->from_i = m_selected_item->i;
m_invmgr->inventoryAction(a);
if(amount == 0 || list_from->getItem(m_selected_item->i).count<=amount)
remove_selection = true;
}
} }
} }
else if(m_selected_item != NULL && m_selected_dragging &&
!(getAbsoluteClippingRect().isPointInside(m_pointer)))
{
// Dragged outside of window: drop all selected
drop_amount = m_selected_amount;
}
if(remove_selection) m_selected_dragging = false;
}
// Possibly send inventory action to server
if(move_amount > 0)
{
// Send IACTION_MOVE
assert(m_selected_item && m_selected_item->isValid());
assert(s.isValid());
assert(inv_selected && inv_s);
InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
InventoryList *list_to = inv_s->getList(s.listname);
assert(list_from && list_to);
ItemStack stack_from = list_from->getItem(m_selected_item->i);
ItemStack stack_to = list_to->getItem(s.i);
// Check how many items can be moved
move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
if(leftover.count == stack_from.count)
{ {
delete m_selected_item; // Swap the stacks
m_selected_item = NULL;
} }
else if(leftover.empty())
{
// Item fits
}
else
{
// Item only fits partially
move_amount -= leftover.count;
}
assert(move_amount > 0 && move_amount <= m_selected_amount);
m_selected_amount -= move_amount;
infostream<<"Handing IACTION_MOVE to manager"<<std::endl;
IMoveAction *a = new IMoveAction();
a->count = move_amount;
a->from_inv = m_selected_item->inventoryloc;
a->from_list = m_selected_item->listname;
a->from_i = m_selected_item->i;
a->to_inv = s.inventoryloc;
a->to_list = s.listname;
a->to_i = s.i;
m_invmgr->inventoryAction(a);
}
else if(drop_amount > 0)
{
// Send IACTION_DROP
assert(m_selected_item && m_selected_item->isValid());
assert(inv_selected);
InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
assert(list_from);
ItemStack stack_from = list_from->getItem(m_selected_item->i);
// Check how many items can be dropped
drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
assert(drop_amount > 0 && drop_amount <= m_selected_amount);
m_selected_amount -= drop_amount;
infostream<<"Handing IACTION_DROP to manager"<<std::endl;
IDropAction *a = new IDropAction();
a->count = drop_amount;
a->from_inv = m_selected_item->inventoryloc;
a->from_list = m_selected_item->listname;
a->from_i = m_selected_item->i;
m_invmgr->inventoryAction(a);
}
else if(craft_amount > 0)
{
// Send IACTION_CRAFT
assert(s.isValid());
assert(inv_s);
infostream<<"Handing IACTION_CRAFT to manager"<<std::endl;
ICraftAction *a = new ICraftAction();
a->count = craft_amount;
a->craft_inv = s.inventoryloc;
m_invmgr->inventoryAction(a);
}
// If m_selected_amount has been decreased to zero, deselect
if(m_selected_amount == 0)
{
delete m_selected_item;
m_selected_item = NULL;
m_selected_dragging = false;
} }
} }
if(event.EventType==EET_GUI_EVENT) if(event.EventType==EET_GUI_EVENT)

@ -137,7 +137,9 @@ public:
ItemSpec getItemAtPos(v2s32 p) const; ItemSpec getItemAtPos(v2s32 p) const;
void drawList(const ListDrawSpec &s, int phase); void drawList(const ListDrawSpec &s, int phase);
void drawSelectedItem();
void drawMenu(); void drawMenu();
void updateSelectedItem();
bool OnEvent(const SEvent& event); bool OnEvent(const SEvent& event);
@ -160,6 +162,9 @@ protected:
core::array<ListDrawSpec> m_draw_spec; core::array<ListDrawSpec> m_draw_spec;
ItemSpec *m_selected_item; ItemSpec *m_selected_item;
u32 m_selected_amount;
bool m_selected_dragging;
v2s32 m_pointer; v2s32 m_pointer;
gui::IGUIStaticText *m_tooltip_element; gui::IGUIStaticText *m_tooltip_element;
}; };

@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "main.h" // for g_settings #include "main.h" // for g_settings
#include "settings.h" #include "settings.h"
#include "utility.h" #include "utility.h"
#include "craftdef.h"
/* /*
InventoryLocation InventoryLocation
@ -124,6 +125,10 @@ InventoryAction * InventoryAction::deSerialize(std::istream &is)
{ {
a = new IDropAction(is); a = new IDropAction(is);
} }
else if(type == "Craft")
{
a = new ICraftAction(is);
}
return a; return a;
} }
@ -152,7 +157,7 @@ IMoveAction::IMoveAction(std::istream &is)
to_i = stoi(ts); to_i = stoi(ts);
} }
void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player) void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
{ {
Inventory *inv_from = mgr->getInventory(from_inv); Inventory *inv_from = mgr->getInventory(from_inv);
Inventory *inv_to = mgr->getInventory(to_inv); Inventory *inv_to = mgr->getInventory(to_inv);
@ -273,7 +278,7 @@ IDropAction::IDropAction(std::istream &is)
from_i = stoi(ts); from_i = stoi(ts);
} }
void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player) void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
{ {
Inventory *inv_from = mgr->getInventory(from_inv); Inventory *inv_from = mgr->getInventory(from_inv);
@ -332,3 +337,120 @@ void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player)
<<" i="<<from_i <<" i="<<from_i
<<std::endl; <<std::endl;
} }
ICraftAction::ICraftAction(std::istream &is)
{
std::string ts;
std::getline(is, ts, ' ');
count = stoi(ts);
std::getline(is, ts, ' ');
craft_inv.deSerialize(ts);
}
void ICraftAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
{
Inventory *inv_craft = mgr->getInventory(craft_inv);
if(!inv_craft){
infostream<<"ICraftAction::apply(): FAIL: inventory not found: "
<<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
return;
}
InventoryList *list_craft = inv_craft->getList("craft");
InventoryList *list_craftresult = inv_craft->getList("craftresult");
/*
If a list doesn't exist or the source item doesn't exist
*/
if(!list_craft){
infostream<<"ICraftAction::apply(): FAIL: craft list not found: "
<<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
return;
}
if(!list_craftresult){
infostream<<"ICraftAction::apply(): FAIL: craftresult list not found: "
<<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
return;
}
if(list_craftresult->getSize() < 1){
infostream<<"ICraftAction::apply(): FAIL: craftresult list too short: "
<<"craft_inv=\""<<craft_inv.dump()<<"\""<<std::endl;
return;
}
ItemStack crafted;
int count_remaining = count;
bool found = getCraftingResult(inv_craft, crafted, false, gamedef);
while(found && list_craftresult->itemFits(0, crafted))
{
// Decrement input and add crafting output
getCraftingResult(inv_craft, crafted, true, gamedef);
list_craftresult->addItem(0, crafted);
mgr->setInventoryModified(craft_inv);
actionstream<<player->getDescription()
<<" crafts "
<<crafted.getItemString()
<<std::endl;
// Decrement counter
if(count_remaining == 1)
break;
else if(count_remaining > 1)
count_remaining--;
// Get next crafting result
found = getCraftingResult(inv_craft, crafted, false, gamedef);
}
infostream<<"ICraftAction::apply(): crafted "
<<" craft_inv=\""<<craft_inv.dump()<<"\""
<<std::endl;
}
// Crafting helper
bool getCraftingResult(Inventory *inv, ItemStack& result,
bool decrementInput, IGameDef *gamedef)
{
DSTACK(__FUNCTION_NAME);
result.clear();
// TODO: Allow different sizes of crafting grids
// Get the InventoryList in which we will operate
InventoryList *clist = inv->getList("craft");
if(!clist || clist->getSize() != 9)
return false;
// Mangle crafting grid to an another format
CraftInput ci;
ci.method = CRAFT_METHOD_NORMAL;
ci.width = 3;
for(u16 i=0; i<9; i++)
ci.items.push_back(clist->getItem(i));
// Find out what is crafted and add it to result item slot
CraftOutput co;
bool found = gamedef->getCraftDefManager()->getCraftResult(
ci, co, decrementInput, gamedef);
if(found)
result.deSerialize(co.item, gamedef->getItemDefManager());
if(found && decrementInput)
{
// CraftInput has been changed, apply changes in clist
for(u16 i=0; i<9; i++)
{
clist->changeItem(i, ci.items[i]);
}
}
return found;
}

@ -92,6 +92,7 @@ public:
#define IACTION_MOVE 0 #define IACTION_MOVE 0
#define IACTION_DROP 1 #define IACTION_DROP 1
#define IACTION_CRAFT 2
struct InventoryAction struct InventoryAction
{ {
@ -99,7 +100,8 @@ struct InventoryAction
virtual u16 getType() const = 0; virtual u16 getType() const = 0;
virtual void serialize(std::ostream &os) const = 0; virtual void serialize(std::ostream &os) const = 0;
virtual void apply(InventoryManager *mgr, ServerActiveObject *player) = 0; virtual void apply(InventoryManager *mgr, ServerActiveObject *player,
IGameDef *gamedef) = 0;
}; };
struct IMoveAction : public InventoryAction struct IMoveAction : public InventoryAction
@ -139,7 +141,7 @@ struct IMoveAction : public InventoryAction
os<<to_i; os<<to_i;
} }
void apply(InventoryManager *mgr, ServerActiveObject *player); void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
}; };
struct IDropAction : public InventoryAction struct IDropAction : public InventoryAction
@ -172,8 +174,40 @@ struct IDropAction : public InventoryAction
os<<from_i; os<<from_i;
} }
void apply(InventoryManager *mgr, ServerActiveObject *player); void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
}; };
struct ICraftAction : public InventoryAction
{
// count=0 means "everything"
u16 count;
InventoryLocation craft_inv;
ICraftAction()
{
count = 0;
}
ICraftAction(std::istream &is);
u16 getType() const
{
return IACTION_CRAFT;
}
void serialize(std::ostream &os) const
{
os<<"Craft ";
os<<count<<" ";
os<<craft_inv.dump()<<" ";
}
void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
};
// Crafting helper
bool getCraftingResult(Inventory *inv, ItemStack& result,
bool decrementInput, IGameDef *gamedef);
#endif #endif

@ -40,7 +40,6 @@ Player::Player(IGameDef *gamedef):
swimming_up(false), swimming_up(false),
inventory(gamedef->idef()), inventory(gamedef->idef()),
inventory_backup(NULL), inventory_backup(NULL),
craftresult_is_preview(true),
hp(20), hp(20),
peer_id(PEER_ID_INEXISTENT), peer_id(PEER_ID_INEXISTENT),
// protected // protected
@ -64,6 +63,7 @@ void Player::resetInventory()
inventory.clear(); inventory.clear();
inventory.addList("main", PLAYER_INVENTORY_SIZE); inventory.addList("main", PLAYER_INVENTORY_SIZE);
inventory.addList("craft", 9); inventory.addList("craft", 9);
inventory.addList("craftpreview", 1);
inventory.addList("craftresult", 1); inventory.addList("craftresult", 1);
} }
@ -119,7 +119,6 @@ void Player::serialize(std::ostream &os)
args.setFloat("pitch", m_pitch); args.setFloat("pitch", m_pitch);
args.setFloat("yaw", m_yaw); args.setFloat("yaw", m_yaw);
args.setV3F("position", m_position); args.setV3F("position", m_position);
args.setBool("craftresult_is_preview", craftresult_is_preview);
args.setS32("hp", hp); args.setS32("hp", hp);
args.writeLines(os); args.writeLines(os);
@ -157,11 +156,10 @@ void Player::deSerialize(std::istream &is)
setPitch(args.getFloat("pitch")); setPitch(args.getFloat("pitch"));
setYaw(args.getFloat("yaw")); setYaw(args.getFloat("yaw"));
setPosition(args.getV3F("position")); setPosition(args.getV3F("position"));
bool craftresult_is_preview = true;
try{ try{
craftresult_is_preview = args.getBool("craftresult_is_preview"); craftresult_is_preview = args.getBool("craftresult_is_preview");
}catch(SettingNotFoundException &e){ }catch(SettingNotFoundException &e){}
craftresult_is_preview = true;
}
try{ try{
hp = args.getS32("hp"); hp = args.getS32("hp");
}catch(SettingNotFoundException &e){ }catch(SettingNotFoundException &e){
@ -169,6 +167,18 @@ void Player::deSerialize(std::istream &is)
} }
inventory.deSerialize(is); inventory.deSerialize(is);
if(inventory.getList("craftpreview") == NULL)
{
// Convert players without craftpreview
inventory.addList("craftpreview", 1);
if(craftresult_is_preview)
{
// Clear craftresult
inventory.getList("craftresult")->changeItem(0, ItemStack());
}
}
} }
#ifndef SERVER #ifndef SERVER

@ -155,8 +155,6 @@ public:
// Actual inventory is backed up here when creative mode is used // Actual inventory is backed up here when creative mode is used
Inventory *inventory_backup; Inventory *inventory_backup;
bool craftresult_is_preview;
u16 hp; u16 hp;
u16 peer_id; u16 peer_id;

@ -2377,13 +2377,6 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
*/ */
if(a->getType() == IACTION_MOVE) if(a->getType() == IACTION_MOVE)
{ {
InventoryList *rlist = player->inventory.getList("craftresult");
assert(rlist);
InventoryList *clist = player->inventory.getList("craft");
assert(clist);
InventoryList *mlist = player->inventory.getList("main");
assert(mlist);
IMoveAction *ma = (IMoveAction*)a; IMoveAction *ma = (IMoveAction*)a;
ma->from_inv.applyCurrentPlayer(player->getName()); ma->from_inv.applyCurrentPlayer(player->getName());
@ -2398,98 +2391,36 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
(ma->to_inv.name == player->getName()); (ma->to_inv.name == player->getName());
/* /*
Disable moving items into craftresult from elsewhere Disable moving items out of craftpreview
*/ */
if(to_inv_is_current_player if(ma->from_list == "craftpreview")
&& ma->to_list == "craftresult"
&& (!from_inv_is_current_player
|| ma->from_list != "craftresult"))
{ {
infostream<<"Ignoring IMoveAction from " infostream<<"Ignoring IMoveAction from "
<<(ma->from_inv.dump())<<":"<<ma->from_list <<(ma->from_inv.dump())<<":"<<ma->from_list
<<" to "<<(ma->to_inv.dump())<<":"<<ma->to_list <<" to "<<(ma->to_inv.dump())<<":"<<ma->to_list
<<" because dst is craftresult" <<" because src is "<<ma->from_list<<std::endl;
<<" and src isn't craftresult"<<std::endl;
delete a; delete a;
return; return;
} }
/* /*
Handle crafting (source is craftresult, which is preview) Disable moving items into craftresult and craftpreview
*/ */
if(from_inv_is_current_player if(ma->to_list == "craftpreview" || ma->to_list == "craftresult")
&& ma->from_list == "craftresult"
&& player->craftresult_is_preview
&& g_settings->getBool("creative_mode") == false)
{ {
ItemStack crafting_result; infostream<<"Ignoring IMoveAction from "
bool crafting_possible = GetCraftingResult(peer_id, <<(ma->from_inv.dump())<<":"<<ma->from_list
crafting_result, false); <<" to "<<(ma->to_inv.dump())<<":"<<ma->to_list
<<" because dst is "<<ma->to_list<<std::endl;
/*
If the craftresult is placed on itself,
crafting takes place and result is moved
into main list.
*/
if(crafting_possible
&& to_inv_is_current_player
&& ma->to_list == "craftresult")
{
if(mlist->roomForItem(crafting_result))
{
actionstream<<player->getName()
<<" crafts "
<<crafting_result.getItemString()
<<std::endl;
// Decrement crafting materials
GetCraftingResult(peer_id, crafting_result, true);
mlist->addItem(crafting_result);
rlist->clearItems();
player->craftresult_is_preview = true;
srp->m_inventory_not_sent = true;
}
}
/*
Otherwise, if the destination is part of
the same player's inventory, crafting
takes place normally.
*/
else if(crafting_possible
&& to_inv_is_current_player)
{
InventoryList *list = player->inventory.getList(ma->to_list);
if(list && list->itemFits(ma->to_i, crafting_result))
{
actionstream<<player->getName()
<<" crafts "
<<crafting_result.getItemString()
<<std::endl;
// Decrement crafting materials
GetCraftingResult(peer_id, crafting_result, true);
list->addItem(ma->to_i, crafting_result);
rlist->clearItems();
player->craftresult_is_preview = true;
srp->m_inventory_not_sent = true;
}
}
// Do not apply the action normally.
delete a; delete a;
return; return;
} }
/*
Non-crafting move
*/
// Disallow moving items in elsewhere than player's inventory // Disallow moving items in elsewhere than player's inventory
// if not allowed to interact // if not allowed to interact
if((getPlayerPrivs(player) & PRIV_INTERACT) == 0 if((getPlayerPrivs(player) & PRIV_INTERACT) == 0
&& (from_inv_is_current_player && (!from_inv_is_current_player
|| to_inv_is_current_player)) || !to_inv_is_current_player))
{ {
infostream<<"Cannot move outside of player's inventory: " infostream<<"Cannot move outside of player's inventory: "
<<"No interact privilege"<<std::endl; <<"No interact privilege"<<std::endl;
@ -2550,9 +2481,45 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
} }
} }
} }
/*
Handle restrictions and special cases of the craft action
*/
else if(a->getType() == IACTION_CRAFT)
{
ICraftAction *ca = (ICraftAction*)a;
ca->craft_inv.applyCurrentPlayer(player->getName());
//bool craft_inv_is_current_player =
// (ca->craft_inv.type == InventoryLocation::PLAYER) &&
// (ca->craft_inv.name == player->getName());
// Disallow crafting if not allowed to interact
if((getPlayerPrivs(player) & PRIV_INTERACT) == 0)
{
infostream<<"Cannot craft: "
<<"No interact privilege"<<std::endl;
delete a;
return;
}
// If player is not an admin, check for ownership of inventory
if((getPlayerPrivs(player) & PRIV_SERVER) == 0)
{
std::string owner_craft = getInventoryOwner(ca->craft_inv);
if(owner_craft != "" && owner_craft != player->getName())
{
infostream<<"WARNING: "<<player->getName()
<<" tried to access an inventory that"
<<" belongs to "<<owner_craft<<std::endl;
delete a;
return;
}
}
}
// Do the action // Do the action
a->apply(this, srp); a->apply(this, srp, this);
// Eat the action // Eat the action
delete a; delete a;
} }
@ -4049,95 +4016,24 @@ void Server::RespawnPlayer(Player *player)
SendPlayerHP(player); SendPlayerHP(player);
} }
bool Server::GetCraftingResult(u16 peer_id, ItemStack &result, bool decrementInput)
{
DSTACK(__FUNCTION_NAME);
Player* player = m_env->getPlayer(peer_id);
assert(player);
// Get the crafting InventoryList of the player in which we will operate
InventoryList *clist = player->inventory.getList("craft");
assert(clist);
// Mangle crafting grid to an another format
CraftInput ci;
ci.method = CRAFT_METHOD_NORMAL;
ci.width = 3;
for(u16 i=0; i<9; i++)
{
ci.items.push_back(clist->getItem(i));
}
// Find out what is crafted and add it to result item slot
CraftOutput co;
bool found = m_craftdef->getCraftResult(ci, co, decrementInput, this);
if(found)
result.deSerialize(co.item, m_itemdef);
if(decrementInput)
{
// CraftInput has been changed, apply changes in clist
for(u16 i=0; i<9; i++)
{
clist->changeItem(i, ci.items[i]);
}
}
return found;
}
void Server::UpdateCrafting(u16 peer_id) void Server::UpdateCrafting(u16 peer_id)
{ {
DSTACK(__FUNCTION_NAME); DSTACK(__FUNCTION_NAME);
Player* player = m_env->getPlayer(peer_id); Player* player = m_env->getPlayer(peer_id);
assert(player); assert(player);
ServerRemotePlayer *srp = static_cast<ServerRemotePlayer*>(player);
// Get a preview for crafting
ItemStack preview;
// No crafting in creative mode // No crafting in creative mode
if(g_settings->getBool("creative_mode")) if(g_settings->getBool("creative_mode") == false)
return; getCraftingResult(&player->inventory, preview, false, this);
// Get the InventoryLists of the player in which we will operate
InventoryList *clist = player->inventory.getList("craft");
assert(clist);
InventoryList *rlist = player->inventory.getList("craftresult");
assert(rlist);
InventoryList *mlist = player->inventory.getList("main");
assert(mlist);
// If the result list is not a preview and is not empty, try to // Put the new preview in
// throw the item into main list InventoryList *plist = player->inventory.getList("craftpreview");
if(!player->craftresult_is_preview && rlist->getUsedSlots() != 0) assert(plist);
{ assert(plist->getSize() >= 1);
// Grab item out of craftresult plist->changeItem(0, preview);
ItemStack item = rlist->changeItem(0, ItemStack());
// Try to put in main
ItemStack leftover = mlist->addItem(item);
// If there are leftovers, put them back to craftresult
rlist->addItem(leftover);
// Inventory was modified
srp->m_inventory_not_sent = true;
}
// If result list is empty, we will make it preview what would be
// crafted
if(rlist->getUsedSlots() == 0)
player->craftresult_is_preview = true;
// If it is a preview, find out what is the crafting result
// and put it in
if(player->craftresult_is_preview)
{
// Clear the possible old preview in it
rlist->clearItems();
// Put the new preview in
ItemStack crafting_result;
if(GetCraftingResult(peer_id, crafting_result, false))
rlist->addItem(crafting_result);
}
} }
RemoteClient* Server::getClient(u16 peer_id) RemoteClient* Server::getClient(u16 peer_id)

@ -595,7 +595,6 @@ private:
void HandlePlayerHP(Player *player, s16 damage); void HandlePlayerHP(Player *player, s16 damage);
void RespawnPlayer(Player *player); void RespawnPlayer(Player *player);
bool GetCraftingResult(u16 peer_id, ItemStack &result, bool decrementInput);
void UpdateCrafting(u16 peer_id); void UpdateCrafting(u16 peer_id);
// When called, connection mutex should be locked // When called, connection mutex should be locked