aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/game.cpp2
-rw-r--r--src/guiInventoryMenu.cpp425
-rw-r--r--src/guiInventoryMenu.h5
-rw-r--r--src/inventorymanager.cpp126
-rw-r--r--src/inventorymanager.h40
-rw-r--r--src/player.cpp20
-rw-r--r--src/player.h2
-rw-r--r--src/server.cpp220
-rw-r--r--src/server.h1
9 files changed, 551 insertions, 290 deletions
diff --git a/src/game.cpp b/src/game.cpp
index 055320a8e..47a0e6afe 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -1310,7 +1310,7 @@ void the_game(
"list", inventoryloc, "craft",
v2s32(3, 0), v2s32(3, 3)));
draw_spec.push_back(GUIInventoryMenu::DrawSpec(
- "list", inventoryloc, "craftresult",
+ "list", inventoryloc, "craftpreview",
v2s32(7, 1), v2s32(1, 1)));
menu->setDrawSpec(draw_spec);
diff --git a/src/guiInventoryMenu.cpp b/src/guiInventoryMenu.cpp
index 6e46bd49c..a4b16b251 100644
--- a/src/guiInventoryMenu.cpp
+++ b/src/guiInventoryMenu.cpp
@@ -133,6 +133,8 @@ GUIInventoryMenu::GUIInventoryMenu(gui::IGUIEnvironment* env,
m_gamedef(gamedef)
{
m_selected_item = NULL;
+ m_selected_amount = 0;
+ m_selected_dragging = false;
m_tooltip_element = NULL;
}
@@ -317,28 +319,17 @@ void GUIInventoryMenu::drawList(const ListDrawSpec &s, int phase)
}
}
- if(phase == 1 && !item.empty())
+ if(phase == 1)
{
- // Draw item at the normal position if
- // - the item is not being dragged or
- // /*- the item is in the crafting result slot*/
- if(!selected /*|| s.listname == "craftresult"*/)
+ // Draw item stack
+ if(selected)
{
- drawItemStack(driver, font, item,
- rect, &AbsoluteClippingRect, m_gamedef);
+ item.takeItem(m_selected_amount);
}
- }
-
- if(phase ==2 && !item.empty())
- {
- // Draw dragged item
- if(selected)
+ if(!item.empty())
{
- v2s32 offset = m_pointer - rect.getCenter();
- rect.UpperLeftCorner += offset;
- rect.LowerRightCorner += offset;
drawItemStack(driver, font, item,
- rect, NULL, m_gamedef);
+ rect, &AbsoluteClippingRect, m_gamedef);
}
// Draw tooltip
@@ -359,12 +350,38 @@ void GUIInventoryMenu::drawList(const ListDrawSpec &s, int phase)
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()
{
+ updateSelectedItem();
+
gui::IGUISkin* skin = Environment->getSkin();
if (!skin)
return;
@@ -378,22 +395,93 @@ void GUIInventoryMenu::drawMenu()
/*
Draw items
Phase 0: Item slot rectangles
- Phase 1: Item images
- Phase 2: Dragged item image; tooltip
+ Phase 1: Item images; prepare 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++)
{
drawList(m_draw_spec[i], phase);
}
/*
+ Draw dragged item stack
+ */
+ drawSelectedItem();
+
+ /*
Call base class
*/
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)
{
if(event.EventType==EET_KEY_INPUT_EVENT)
@@ -418,133 +506,242 @@ bool GUIInventoryMenu::OnEvent(const SEvent& event)
// Mouse event other than movement
v2s32 p(event.MouseInput.X, event.MouseInput.Y);
+ m_pointer = p;
+
+ // Get selected item and hovered/clicked item (s)
+
+ updateSelectedItem();
ItemSpec s = getItemAtPos(p);
Inventory *inv_selected = NULL;
Inventory *inv_s = NULL;
+
if(m_selected_item)
{
- assert(m_selected_item->isValid());
inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
assert(inv_selected);
+ assert(inv_selected->getList(m_selected_item->listname) != NULL);
}
+
+ u32 s_count = 0;
+
if(s.isValid())
{
inv_s = m_invmgr->getInventory(s.inventoryloc);
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)
- amount = 0;
+ { button = 0; updown = 0; }
else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
- amount = 1;
+ { button = 1; updown = 0; }
else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
- amount = 10;
- else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP && different_item)
- amount = 0;
- //else if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP && different_item)
- // amount = 1;
- //else if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP && different_item)
- // amount = 10;
-
- if(amount >= 0)
+ { button = 2; updown = 0; }
+ else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
+ { button = 0; updown = 1; }
+ else if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
+ { button = 1; updown = 1; }
+ else if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
+ { button = 2; updown = 1; }
+
+ // 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
- bool remove_selection = false;
+ // Some mouse button has been pressed
- //infostream<<"Mouse action at p=("<<p.X<<","<<p.Y<<")"<<std::endl;
- if(s.isValid())
+ //infostream<<"Mouse button "<<button<<" pressed at p=("
+ // <<p.X<<","<<p.Y<<")"<<std::endl;
+
+ m_selected_dragging = false;
+
+ if(s.isValid() && s.listname == "craftpreview")
{
- infostream<<"Mouse action on "<<s.inventoryloc.dump()
- <<"/"<<s.listname<<" "<<s.i<<std::endl;
- if(m_selected_item)
- {
- Inventory *inv_from = inv_selected;
- Inventory *inv_to = inv_s;
- assert(inv_from);
- assert(inv_to);
- InventoryList *list_from =
- inv_from->getList(m_selected_item->listname);
- InventoryList *list_to =
- inv_to->getList(s.listname);
- if(list_from == NULL)
- infostream<<"from list doesn't exist"<<std::endl;
- if(list_to == NULL)
- infostream<<"to list doesn't exist"<<std::endl;
- if(list_from && list_to
- && !list_from->getItem(m_selected_item->i).empty())
- {
- infostream<<"Handing IACTION_MOVE to manager"<<std::endl;
- IMoveAction *a = new IMoveAction();
- a->count = 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);
-
- if(amount == 0 || list_from->getItem(m_selected_item->i).count<=amount)
- remove_selection = true;
- }
- }
- else
+ // Craft preview has been clicked: craft
+ craft_amount = (button == 2 ? 10 : 1);
+ }
+ else if(m_selected_item == NULL)
+ {
+ if(s_count != 0)
{
- /*
- Select if nonempty
- */
- assert(inv_s);
- InventoryList *list = inv_s->getList(s.listname);
- if(list && !list->getItem(s.i).empty())
- {
- m_selected_item = new ItemSpec(s);
- }
+ // Non-empty stack has been clicked: select it
+ m_selected_item = new ItemSpec(s);
+
+ if(button == 1) // right
+ m_selected_amount = (s_count + 1) / 2;
+ else if(button == 2) // middle
+ m_selected_amount = MYMIN(s_count, 10);
+ else // left
+ m_selected_amount = s_count;
+
+ m_selected_dragging = true;
}
}
- else if(m_selected_item)
+ else // m_selected_item != NULL
{
- // If moved outside the menu, drop.
- // (Otherwise abort inventory action.)
- if(getAbsoluteClippingRect().isPointInside(m_pointer))
+ assert(m_selected_amount >= 1);
+
+ if(s.isValid())
{
- // Inside menu
- remove_selection = true;
+ // Clicked another slot: move
+ if(button == 1) // right
+ move_amount = 1;
+ else if(button == 2) // middle
+ move_amount = MYMIN(m_selected_amount, 10);
+ else // left
+ move_amount = m_selected_amount;
+ }
+ else if(getAbsoluteClippingRect().isPointInside(m_pointer))
+ {
+ // Clicked somewhere else: deselect
+ m_selected_amount = 0;
}
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;
- }
+ // Clicked outside of the window: drop
+ if(button == 1) // right
+ drop_amount = 1;
+ else if(button == 2) // middle
+ drop_amount = MYMIN(m_selected_amount, 10);
+ else // left
+ drop_amount = m_selected_amount;
+ }
+ }
+ }
+ 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((inv_selected != inv_s) ||
+ (m_selected_item->listname != s.listname) ||
+ (m_selected_item->i != s.i))
+ {
+ // Dragged to different slot: move all selected
+ move_amount = m_selected_amount;
}
}
+ 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)
+ {
+ // Swap the stacks
+ }
+ else if(leftover.empty())
+ {
+ // Item fits
+ }
+ else
{
- delete m_selected_item;
- m_selected_item = NULL;
+ // 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)
diff --git a/src/guiInventoryMenu.h b/src/guiInventoryMenu.h
index 903a8b137..ed8df88fc 100644
--- a/src/guiInventoryMenu.h
+++ b/src/guiInventoryMenu.h
@@ -137,7 +137,9 @@ public:
ItemSpec getItemAtPos(v2s32 p) const;
void drawList(const ListDrawSpec &s, int phase);
+ void drawSelectedItem();
void drawMenu();
+ void updateSelectedItem();
bool OnEvent(const SEvent& event);
@@ -160,6 +162,9 @@ protected:
core::array<ListDrawSpec> m_draw_spec;
ItemSpec *m_selected_item;
+ u32 m_selected_amount;
+ bool m_selected_dragging;
+
v2s32 m_pointer;
gui::IGUIStaticText *m_tooltip_element;
};
diff --git a/src/inventorymanager.cpp b/src/inventorymanager.cpp
index eeb293cb1..b42a80673 100644
--- a/src/inventorymanager.cpp
+++ b/src/inventorymanager.cpp
@@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "main.h" // for g_settings
#include "settings.h"
#include "utility.h"
+#include "craftdef.h"
/*
InventoryLocation
@@ -124,6 +125,10 @@ InventoryAction * InventoryAction::deSerialize(std::istream &is)
{
a = new IDropAction(is);
}
+ else if(type == "Craft")
+ {
+ a = new ICraftAction(is);
+ }
return a;
}
@@ -152,7 +157,7 @@ IMoveAction::IMoveAction(std::istream &is)
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_to = mgr->getInventory(to_inv);
@@ -273,7 +278,7 @@ IDropAction::IDropAction(std::istream &is)
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);
@@ -332,3 +337,120 @@ void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player)
<<" i="<<from_i
<<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;
+}
+
diff --git a/src/inventorymanager.h b/src/inventorymanager.h
index f6ae4feba..890d05168 100644
--- a/src/inventorymanager.h
+++ b/src/inventorymanager.h
@@ -92,6 +92,7 @@ public:
#define IACTION_MOVE 0
#define IACTION_DROP 1
+#define IACTION_CRAFT 2
struct InventoryAction
{
@@ -99,7 +100,8 @@ struct InventoryAction
virtual u16 getType() 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
@@ -139,7 +141,7 @@ struct IMoveAction : public InventoryAction
os<<to_i;
}
- void apply(InventoryManager *mgr, ServerActiveObject *player);
+ void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
};
struct IDropAction : public InventoryAction
@@ -172,8 +174,40 @@ struct IDropAction : public InventoryAction
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
diff --git a/src/player.cpp b/src/player.cpp
index b072746c6..688be5d98 100644
--- a/src/player.cpp
+++ b/src/player.cpp
@@ -40,7 +40,6 @@ Player::Player(IGameDef *gamedef):
swimming_up(false),
inventory(gamedef->idef()),
inventory_backup(NULL),
- craftresult_is_preview(true),
hp(20),
peer_id(PEER_ID_INEXISTENT),
// protected
@@ -64,6 +63,7 @@ void Player::resetInventory()
inventory.clear();
inventory.addList("main", PLAYER_INVENTORY_SIZE);
inventory.addList("craft", 9);
+ inventory.addList("craftpreview", 1);
inventory.addList("craftresult", 1);
}
@@ -119,7 +119,6 @@ void Player::serialize(std::ostream &os)
args.setFloat("pitch", m_pitch);
args.setFloat("yaw", m_yaw);
args.setV3F("position", m_position);
- args.setBool("craftresult_is_preview", craftresult_is_preview);
args.setS32("hp", hp);
args.writeLines(os);
@@ -157,11 +156,10 @@ void Player::deSerialize(std::istream &is)
setPitch(args.getFloat("pitch"));
setYaw(args.getFloat("yaw"));
setPosition(args.getV3F("position"));
+ bool craftresult_is_preview = true;
try{
craftresult_is_preview = args.getBool("craftresult_is_preview");
- }catch(SettingNotFoundException &e){
- craftresult_is_preview = true;
- }
+ }catch(SettingNotFoundException &e){}
try{
hp = args.getS32("hp");
}catch(SettingNotFoundException &e){
@@ -169,6 +167,18 @@ void Player::deSerialize(std::istream &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
diff --git a/src/player.h b/src/player.h
index 43f6ee5ec..085a4a15a 100644
--- a/src/player.h
+++ b/src/player.h
@@ -155,8 +155,6 @@ public:
// Actual inventory is backed up here when creative mode is used
Inventory *inventory_backup;
- bool craftresult_is_preview;
-
u16 hp;
u16 peer_id;
diff --git a/src/server.cpp b/src/server.cpp
index 9f3db34d9..177551881 100644
--- a/src/server.cpp
+++ b/src/server.cpp
@@ -2377,13 +2377,6 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
*/
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;
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());
/*
- Disable moving items into craftresult from elsewhere
+ Disable moving items out of craftpreview
*/
- if(to_inv_is_current_player
- && ma->to_list == "craftresult"
- && (!from_inv_is_current_player
- || ma->from_list != "craftresult"))
+ if(ma->from_list == "craftpreview")
{
infostream<<"Ignoring IMoveAction from "
<<(ma->from_inv.dump())<<":"<<ma->from_list
<<" to "<<(ma->to_inv.dump())<<":"<<ma->to_list
- <<" because dst is craftresult"
- <<" and src isn't craftresult"<<std::endl;
+ <<" because src is "<<ma->from_list<<std::endl;
delete a;
return;
}
/*
- Handle crafting (source is craftresult, which is preview)
+ Disable moving items into craftresult and craftpreview
*/
- if(from_inv_is_current_player
- && ma->from_list == "craftresult"
- && player->craftresult_is_preview
- && g_settings->getBool("creative_mode") == false)
+ if(ma->to_list == "craftpreview" || ma->to_list == "craftresult")
{
- ItemStack crafting_result;
- bool crafting_possible = GetCraftingResult(peer_id,
- crafting_result, false);
-
- /*
- 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.
+ infostream<<"Ignoring IMoveAction from "
+ <<(ma->from_inv.dump())<<":"<<ma->from_list
+ <<" to "<<(ma->to_inv.dump())<<":"<<ma->to_list
+ <<" because dst is "<<ma->to_list<<std::endl;
delete a;
return;
}
- /*
- Non-crafting move
- */
-
// Disallow moving items in elsewhere than player's inventory
// if not allowed to interact
if((getPlayerPrivs(player) & PRIV_INTERACT) == 0
- && (from_inv_is_current_player
- || to_inv_is_current_player))
+ && (!from_inv_is_current_player
+ || !to_inv_is_current_player))
{
infostream<<"Cannot move outside of player's inventory: "
<<"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
- a->apply(this, srp);
+ a->apply(this, srp, this);
// Eat the action
delete a;
}
@@ -4049,95 +4016,24 @@ void Server::RespawnPlayer(Player *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)
{
DSTACK(__FUNCTION_NAME);
Player* player = m_env->getPlayer(peer_id);
assert(player);
- ServerRemotePlayer *srp = static_cast<ServerRemotePlayer*>(player);
+ // Get a preview for crafting
+ ItemStack preview;
// No crafting in creative mode
- if(g_settings->getBool("creative_mode"))
- return;
-
- // 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
- // throw the item into main list
- if(!player->craftresult_is_preview && rlist->getUsedSlots() != 0)
- {
- // Grab item out of craftresult
- 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);
- }
+ if(g_settings->getBool("creative_mode") == false)
+ getCraftingResult(&player->inventory, preview, false, this);
+
+ // Put the new preview in
+ InventoryList *plist = player->inventory.getList("craftpreview");
+ assert(plist);
+ assert(plist->getSize() >= 1);
+ plist->changeItem(0, preview);
}
RemoteClient* Server::getClient(u16 peer_id)
diff --git a/src/server.h b/src/server.h
index d979cb518..4fdb60065 100644
--- a/src/server.h
+++ b/src/server.h
@@ -595,7 +595,6 @@ private:
void HandlePlayerHP(Player *player, s16 damage);
void RespawnPlayer(Player *player);
- bool GetCraftingResult(u16 peer_id, ItemStack &result, bool decrementInput);
void UpdateCrafting(u16 peer_id);
// When called, connection mutex should be locked