Пример #1
0
bool operator==(ItemStack const &left, ItemStack const &right)
{
    if (&left.getType() == &right.getType())
        return left.getAmount() == right.getAmount();
    else
        return false;
}
Пример #2
0
// add_item(pos, itemstack or itemstring or table) -> ObjectRef or nil
// pos = {x=num, y=num, z=num}
int ModApiEnvMod::l_add_item(lua_State *L)
{
	GET_ENV_PTR;

	// pos
	//v3f pos = checkFloatPos(L, 1);
	// item
	ItemStack item = read_item(L, 2,getServer(L));
	if(item.empty() || !item.isKnown(getServer(L)->idef()))
		return 0;

	lua_pushcfunction(L, script_error_handler);
	int errorhandler = lua_gettop(L);

	// Use spawn_item to spawn a __builtin:item
	lua_getglobal(L, "core");
	lua_getfield(L, -1, "spawn_item");
	lua_remove(L, -2); // Remove core
	if(lua_isnil(L, -1))
		return 0;
	lua_pushvalue(L, 1);
	lua_pushstring(L, item.getItemString().c_str());

	PCALL_RESL(L, lua_pcall(L, 2, 1, errorhandler));

	lua_remove(L, errorhandler); // Remove error handler
	return 1;
}
Пример #3
0
/**
 * Check to see if the player is picking up loot on the ground
 */
void GameStatePlay::checkLoot() {

	if (!pc->stats.alive)
		return;

	if (menu->isDragging())
		return;

	ItemStack pickup;

	// Autopickup
	if (AUTOPICKUP_CURRENCY) {
		pickup = loot->checkAutoPickup(pc->stats.pos);
		if (!pickup.empty()) {
			menu->inv->add(pickup, CARRIED, -1, true, true);
			pickup.clear();
		}
	}

	// Normal pickups
	if (!pc->stats.attacking) {
		pickup = loot->checkPickup(inpt->mouse, mapr->cam, pc->stats.pos);
	}

	if (!pickup.empty()) {
		menu->inv->add(pickup, CARRIED, -1, true, true);
		camp->setStatus(items->items[pickup.item].pickup_status);
		pickup.clear();
	}

}
Пример #4
0
bool MenuStash::add(ItemStack stack, int slot, bool play_sound) {
	if (stack.empty()) {
		return true;
	}

	if (play_sound) {
		items->playSound(stack.item);
	}

	if (items->items[stack.item].quest_item) {
		pc->logMsg(msg->get("Can not store quest items in the stash."), Avatar::MSG_NORMAL);
		drop_stack.push(stack);
		return false;
	}

	ItemStack leftover = stock.add(stack, slot);
	if (!leftover.empty()) {
		if (leftover.quantity != stack.quantity) {
			updated = true;
		}
		pc->logMsg(msg->get("Stash is full."), Avatar::MSG_NORMAL);
		drop_stack.push(leftover);
		return false;
	}
	else {
		updated = true;
	}

	return true;
}
Пример #5
0
// minetest.add_item(pos, itemstack or itemstring or table) -> ObjectRef or nil
// pos = {x=num, y=num, z=num}
int ModApiEnvMod::l_add_item(lua_State *L)
{
	GET_ENV_PTR;

	// pos
	//v3f pos = checkFloatPos(L, 1);
	// item
	ItemStack item = read_item(L, 2,getServer(L));
	if(item.empty() || !item.isKnown(getServer(L)->idef()))
		return 0;
	// Use minetest.spawn_item to spawn a __builtin:item
	lua_getglobal(L, "minetest");
	lua_getfield(L, -1, "spawn_item");
	if(lua_isnil(L, -1))
		return 0;
	lua_pushvalue(L, 1);
	lua_pushstring(L, item.getItemString().c_str());
	if(lua_pcall(L, 2, 1, 0))
		script_error(L, "error: %s", lua_tostring(L, -1));
	return 1;
	/*lua_pushvalue(L, 1);
	lua_pushstring(L, "__builtin:item");
	lua_pushstring(L, item.getItemString().c_str());
	return l_add_entity(L);*/
	/*// Do it
	ServerActiveObject *obj = createItemSAO(env, pos, item.getItemString());
	int objectid = env->addActiveObject(obj);
	// If failed to add, return nothing (reads as nil)
	if(objectid == 0)
		return 0;
	// Return ObjectRef
	objectrefGetOrCreate(L, obj);
	return 1;*/
}
Пример #6
0
	int punch(v3f dir,
			const ToolCapabilities *toolcap,
			ServerActiveObject *puncher,
			float time_from_last_punch)
	{
		// Take item into inventory
		ItemStack item = createItemStack();
		Inventory *inv = puncher->getInventory();
		if(inv != NULL)
		{
			std::string wieldlist = puncher->getWieldList();
			ItemStack leftover = inv->addItem(wieldlist, item);
			puncher->setInventoryModified();
			if(leftover.empty())
			{
				m_removed = true;
			}
			else
			{
				m_itemstring = leftover.getItemString();
				m_itemstring_changed = true;
			}
		}
		
		return 0;
	}
Пример #7
0
/**
 * Click-start dragging in the inventory
 */
ItemStack MenuInventory::click(Point position) {
	ItemStack item;

	drag_prev_src = areaOver(position);
	if (drag_prev_src > -1) {
		item = inventory[drag_prev_src].click(position);

		if (TOUCHSCREEN) {
			tablist.setCurrent(inventory[drag_prev_src].current_slot);
		}

		if (item.empty()) {
			drag_prev_src = -1;
			return item;
		}

		// if dragging equipment, prepare to change stats/sprites
		if (drag_prev_src == EQUIPMENT) {
			if (stats->humanoid) {
				updateEquipment(inventory[EQUIPMENT].drag_prev_slot);
			}
			else {
				itemReturn(item);
				item.clear();
			}
		}
	}

	return item;
}
Пример #8
0
/**
 * Check to see if the player is picking up loot on the ground
 */
void GameStatePlay::checkLoot() {

	if (!pc->stats.alive)
		return;

	if (menu->isDragging())
		return;

	ItemStack pickup;

	// Autopickup
	if (AUTOPICKUP_CURRENCY) {
		pickup = loot->checkAutoPickup(pc->stats.pos, menu->inv);
		if (!pickup.empty()) menu->inv->add(pickup);
	}

	// Normal pickups
	if (!pc->stats.attacking)
		pickup = loot->checkPickup(inpt->mouse, mapr->cam, pc->stats.pos, menu->inv);

	if (!pickup.empty()) {
		menu->inv->add(pickup);
		camp->setStatus(items->items[pickup.item].pickup_status);
	}
	if (loot->full_msg) {
		if (inpt->pressing[MAIN1]) inpt->lock[MAIN1] = true;
		if (inpt->pressing[ACCEPT]) inpt->lock[ACCEPT] = true;
		menu->questlog->add(msg->get("Inventory is full."), LOG_TYPE_MESSAGES);
		menu->hudlog->add(msg->get("Inventory is full."));
		loot->full_msg = false;
	}

}
bool PlayerDatabaseSQLite3::loadPlayer(RemotePlayer *player, PlayerSAO *sao)
{
	verifyDatabase();

	str_to_sqlite(m_stmt_player_load, 1, player->getName());
	if (sqlite3_step(m_stmt_player_load) != SQLITE_ROW) {
		sqlite3_reset(m_stmt_player_load);
		return false;
	}
	sao->setPitch(sqlite_to_float(m_stmt_player_load, 0));
	sao->setYaw(sqlite_to_float(m_stmt_player_load, 1));
	sao->setBasePosition(sqlite_to_v3f(m_stmt_player_load, 2));
	sao->setHPRaw((s16) MYMIN(sqlite_to_int(m_stmt_player_load, 5), S16_MAX));
	sao->setBreath((u16) MYMIN(sqlite_to_int(m_stmt_player_load, 6), U16_MAX), false);
	sqlite3_reset(m_stmt_player_load);

	// Load inventory
	str_to_sqlite(m_stmt_player_load_inventory, 1, player->getName());
	while (sqlite3_step(m_stmt_player_load_inventory) == SQLITE_ROW) {
		InventoryList *invList = player->inventory.addList(
			sqlite_to_string(m_stmt_player_load_inventory, 2),
			sqlite_to_uint(m_stmt_player_load_inventory, 3));
		invList->setWidth(sqlite_to_uint(m_stmt_player_load_inventory, 1));

		u32 invId = sqlite_to_uint(m_stmt_player_load_inventory, 0);

		str_to_sqlite(m_stmt_player_load_inventory_items, 1, player->getName());
		int_to_sqlite(m_stmt_player_load_inventory_items, 2, invId);
		while (sqlite3_step(m_stmt_player_load_inventory_items) == SQLITE_ROW) {
			const std::string itemStr = sqlite_to_string(m_stmt_player_load_inventory_items, 1);
			if (itemStr.length() > 0) {
				ItemStack stack;
				stack.deSerialize(itemStr);
				invList->addItem(sqlite_to_uint(m_stmt_player_load_inventory_items, 0), stack);
			}
		}
		sqlite3_reset(m_stmt_player_load_inventory_items);
	}

	sqlite3_reset(m_stmt_player_load_inventory);

	str_to_sqlite(m_stmt_player_metadata_load, 1, sao->getPlayer()->getName());
	while (sqlite3_step(m_stmt_player_metadata_load) == SQLITE_ROW) {
		std::string attr = sqlite_to_string(m_stmt_player_metadata_load, 0);
		std::string value = sqlite_to_string(m_stmt_player_metadata_load, 1);

		sao->setExtendedAttribute(attr, value);
	}
	sqlite3_reset(m_stmt_player_metadata_load);
	return true;
}
Пример #10
0
void Camera::wield(const ItemStack &item)
{
	IItemDefManager *idef = m_gamedef->idef();
	m_eatable = item.getDefinition(idef).eatable;
	scene::IMesh *wield_mesh = item.getDefinition(idef).wield_mesh;
	if(wield_mesh)
	{
		m_wieldnode->setMesh(wield_mesh);
		m_wieldnode->setVisible(true);
	}
	else
	{
		m_wieldnode->setVisible(false);
	}
}
Пример #11
0
CraftOutput CraftDefinitionToolRepair::getOutput(const CraftInput &input, IGameDef *gamedef) const
{
	ItemStack item1;
	ItemStack item2;
	for (const auto &item : input.items) {
		if (!item.empty()) {
			if (item1.empty())
				item1 = item;
			else if (item2.empty())
				item2 = item;
		}
	}
	ItemStack repaired = craftToolRepair(item1, item2, additional_wear, gamedef);
	return CraftOutput(repaired.getItemString(), 0);
}
Пример #12
0
ItemStack MenuItemStorage::click(Point position) {
    ItemStack item;

    drag_prev_slot = slotOver(position);

    // try to click on the highlighted (aka in focus) slot
    // since mouse clicks defocus slots before this point,
    // we don't have to worry about the mouse being over another slot
    if (drag_prev_slot == -1) {
        for (unsigned int i=0; i<slots.size(); i++) {
            if (slots[i]->in_focus) {
                drag_prev_slot = i;
                break;
            }
        }
    }

    if (drag_prev_slot > -1) {
        item = storage[drag_prev_slot];
        if (TOUCHSCREEN) {
            if (!slots[drag_prev_slot]->in_focus && !item.empty()) {
                slots[drag_prev_slot]->in_focus = true;
                current_slot = slots[drag_prev_slot];
                item.clear();
                drag_prev_slot = -1;
                return item;
            }
            else {
                slots[drag_prev_slot]->in_focus = false;
                current_slot = NULL;
            }
        }
        if (!item.empty()) {
            if (item.quantity > 1 && !inpt->pressing[CTRL] && (inpt->pressing[SHIFT] || NO_MOUSE || inpt->touch_locked)) {
                // we use an external menu to let the player pick the desired quantity
                // we will subtract from this stack after they've made their decision
                return item;
            }
            subtract( drag_prev_slot, item.quantity);
        }
        // item will be cleared if item.empty() == true
        return item;
    }
    else {
        item.clear();
        return item;
    }
}
ItemStack read_item(lua_State *L, int index)
{
	if(index < 0)
		index = lua_gettop(L) + 1 + index;

	if(lua_isnil(L, index))
	{
		return ItemStack();
	}
	else if(lua_isuserdata(L, index))
	{
		// Convert from LuaItemStack
		LuaItemStack *o = LuaItemStack::checkobject(L, index);
		return o->getItem();
	}
	else if(lua_isstring(L, index))
	{
		// Convert from itemstring
		std::string itemstring = lua_tostring(L, index);
		IItemDefManager *idef = get_server(L)->idef();
		try
		{
			ItemStack item;
			item.deSerialize(itemstring, idef);
			return item;
		}
		catch(SerializationError &e)
		{
			infostream<<"WARNING: unable to create item from itemstring"
					<<": "<<itemstring<<std::endl;
			return ItemStack();
		}
	}
	else if(lua_istable(L, index))
	{
		// Convert from table
		IItemDefManager *idef = get_server(L)->idef();
		std::string name = getstringfield_default(L, index, "name", "");
		int count = getintfield_default(L, index, "count", 1);
		int wear = getintfield_default(L, index, "wear", 0);
		std::string metadata = getstringfield_default(L, index, "metadata", "");
		return ItemStack(name, count, wear, metadata, idef);
	}
	else
	{
		throw LuaError(L, "Expecting itemstack, itemstring, table or nil");
	}
}
Пример #14
0
// get_craft_result(input)
int ModApiCraft::l_get_craft_result(lua_State *L)
{
	NO_MAP_LOCK_REQUIRED;

	int input_i = 1;
	std::string method_s = getstringfield_default(L, input_i, "method", "normal");
	enum CraftMethod method = (CraftMethod)getenumfield(L, input_i, "method",
				es_CraftMethod, CRAFT_METHOD_NORMAL);
	int width = 1;
	lua_getfield(L, input_i, "width");
	if(lua_isnumber(L, -1))
		width = luaL_checkinteger(L, -1);
	lua_pop(L, 1);
	lua_getfield(L, input_i, "items");
	std::vector<ItemStack> items = read_items(L, -1,getServer(L));
	lua_pop(L, 1); // items

	IGameDef *gdef = getServer(L);
	ICraftDefManager *cdef = gdef->cdef();
	CraftInput input(method, width, items);
	CraftOutput output;
	std::vector<ItemStack> output_replacements;
	bool got = cdef->getCraftResult(input, output, output_replacements, true, gdef);
	lua_newtable(L); // output table
	if (got) {
		ItemStack item;
		item.deSerialize(output.item, gdef->idef());
		LuaItemStack::create(L, item);
		lua_setfield(L, -2, "item");
		setintfield(L, -1, "time", output.time);
		push_items(L, output_replacements);
		lua_setfield(L, -2, "replacements");
	} else {
		LuaItemStack::create(L, ItemStack());
		lua_setfield(L, -2, "item");
		setintfield(L, -1, "time", 0);
		lua_newtable(L);
		lua_setfield(L, -2, "replacements");
	}
	lua_newtable(L); // decremented input table
	lua_pushstring(L, method_s.c_str());
	lua_setfield(L, -2, "method");
	lua_pushinteger(L, width);
	lua_setfield(L, -2, "width");
	push_items(L, input.items);
	lua_setfield(L, -2, "items");
	return 2;
}
Пример #15
0
shared_ptr<EntityItem> World::dropItem(const ItemStack &items, Point3D location) {
    shared_ptr<EntityItem> item = make_shared<EntityItem>(items.getId(), items.size(), location);
    addEntity(item);
    for (const shared_ptr<Entity> &entity : m->entities) {
        shared_ptr<Player> player = dynamic_pointer_cast<Player>(entity);
        if (!player)
            continue;
        cout << "Found player in world!\n";
        cout << player->getPosition() << " - " << location << " = " << player->getPosition().distanceTo3D(location) << '\n';
        if (player->getPosition().distanceTo3D(location) < Player::ITEM_VIEW_DISTANCE) {
            cout << "Found player in world near to item!\n";
            player->getConnection().sendItemSpawned(item);
        }
    }
    return item;
}
Пример #16
0
/**
 * Check if there is enough currency to buy the given stack, and if so remove it from the current total and add the stack.
 * (Handle the drop into the equipment area, but add() don't handle it well in all circonstances. MenuManager::logic() allow only into the carried area.)
 */
bool MenuInventory::buy(ItemStack stack, int tab, bool dragging) {
	if (stack.empty()) {
		return true;
	}

	int value_each;
	if (tab == VENDOR_BUY) value_each = items->items[stack.item].getPrice();
	else value_each = items->items[stack.item].getSellPrice();

	int count = value_each * stack.quantity;
	if( inventory[CARRIED].count(CURRENCY_ID) >= count) {
		if (dragging) {
			drop(inpt->mouse, stack);
		}
		else {
			add(stack, CARRIED, -1, true, true);
		}

		removeCurrency(count);
		items->playSound(CURRENCY_ID);
		return true;
	}
	else {
		pc->logMsg(msg->get("Not enough %s.", CURRENCY), true);
		drop_stack.push(stack);
		return false;
	}
}
Пример #17
0
/**
 * Sell a specific stack of items
 */
bool MenuInventory::sell(ItemStack stack) {
	if (stack.empty()) {
		return false;
	}

	// can't sell currency
	if (stack.item == CURRENCY_ID) return false;

	// items that have no price cannot be sold
	if (items->items[stack.item].getPrice() == 0) {
		items->playSound(stack.item);
		pc->logMsg(msg->get("This item can not be sold."), true);
		return false;
	}

	// quest items can not be sold
	if (items->items[stack.item].quest_item) {
		items->playSound(stack.item);
		pc->logMsg(msg->get("This item can not be sold."), true);
		return false;
	}

	int value_each = items->items[stack.item].getSellPrice();
	int value = value_each * stack.quantity;
	addCurrency(value);
	items->playSound(CURRENCY_ID);
	drag_prev_src = -1;
	return true;
}
Пример #18
0
void Camera::wield(const ItemStack &item, u16 playeritem)
{
	IItemDefManager *idef = m_gamedef->idef();
	std::string itemname = item.getDefinition(idef).name;
	m_wield_mesh_next = idef->getWieldMesh(itemname, m_gamedef);
	if(playeritem != m_previous_playeritem &&
			!(m_previous_itemname == "" && itemname == "")) {
		m_previous_playeritem = playeritem;
		m_previous_itemname = itemname;
		if(m_wield_change_timer >= 0.125)
			m_wield_change_timer = -0.125;
		else if(m_wield_change_timer > 0) {
			m_wield_change_timer = -m_wield_change_timer;
		}
	} else {
		if(m_wield_mesh_next) {
			m_wieldnode->setMesh(m_wield_mesh_next);
			m_wieldnode->setVisible(true);
		} else {
			m_wieldnode->setVisible(false);
		}
		m_wield_mesh_next = NULL;
		if(m_previous_itemname != itemname) {
			m_previous_itemname = itemname;
			m_wield_change_timer = 0;
		}
		else
			m_wield_change_timer = 0.125;
	}
}
Пример #19
0
void WieldMeshSceneNode::setItem(const ItemStack &item, IGameDef *gamedef)
{
	ITextureSource *tsrc = gamedef->getTextureSource();
	IItemDefManager *idef = gamedef->getItemDefManager();

	const ItemDefinition &def = item.getDefinition(idef);

	// If wield_image is defined, it overrides everything else
	if (def.wield_image != "") {
		setExtruded(def.wield_image, def.wield_scale, tsrc);
		return;
	}

	// Handle nodes
	// See also CItemDefManager::createClientCached()
	if (def.type == ITEM_NODE) {
		INodeDefManager *ndef = gamedef->getNodeDefManager();
		const ContentFeatures &f = ndef->get(def.name);
		if (f.mesh_ptr[0]) {
			// e.g. mesh nodes and nodeboxes
			changeToMesh(f.mesh_ptr[0]);
			// mesh_ptr[0] is pre-scaled by BS * f->visual_scale
			m_meshnode->setScale(
					def.wield_scale * WIELD_SCALE_FACTOR
					/ (BS * f.visual_scale));
			// Customize materials
			for (u32 i = 0; i < m_meshnode->getMaterialCount(); ++i) {
				assert(i < 6);
				video::SMaterial &material = m_meshnode->getMaterial(i);
				material.setTexture(0, f.tiles[i].texture);
				f.tiles[i].applyMaterialOptions(material);
			}
			return;
		} else if (f.drawtype == NDT_NORMAL || f.drawtype == NDT_ALLFACES) {
			setCube(f.tiles, def.wield_scale, tsrc);
			return;
		} else if (f.drawtype == NDT_AIRLIKE) {
			changeToMesh(NULL);
			return;
		}

		// If none of the above standard cases worked, use the wield mesh from ClientCached
		scene::IMesh *mesh = idef->getWieldMesh(item.name, gamedef);
		if (mesh) {
			changeToMesh(mesh);
			m_meshnode->setScale(def.wield_scale * WIELD_SCALE_FACTOR);
			return;
		}
	}

	// default to inventory_image
	if (def.inventory_image != "") {
		setExtruded(def.inventory_image, def.wield_scale, tsrc);
		return;
	}

	// no wield mesh found
	changeToMesh(NULL);
}
Пример #20
0
CraftOutput CraftDefinitionToolRepair::getOutput(const CraftInput &input, IGameDef *gamedef) const
{
	ItemStack item1;
	ItemStack item2;
	for (std::vector<ItemStack>::const_iterator
			it = input.items.begin();
			it != input.items.end(); it++) {
		if (!it->empty()) {
			if (item1.empty())
				item1 = *it;
			else if (item2.empty())
				item2 = *it;
		}
	}
	ItemStack repaired = craftToolRepair(item1, item2, additional_wear, gamedef);
	return CraftOutput(repaired.getItemString(), 0);
}
Пример #21
0
// remove_item(self, listname, itemstack or itemstring or table or nil) -> itemstack
// Returns the items that were actually removed
int InvRef::l_remove_item(lua_State *L)
{
	NO_MAP_LOCK_REQUIRED;
	InvRef *ref = checkobject(L, 1);
	const char *listname = luaL_checkstring(L, 2);
	ItemStack item = read_item(L, 3, getServer(L)->idef());
	InventoryList *list = getlist(L, ref, listname);
	if(list){
		ItemStack removed = list->removeItem(item);
		if(!removed.empty())
			reportInventoryChange(L, ref);
		LuaItemStack::create(L, removed);
	} else {
		LuaItemStack::create(L, ItemStack());
	}
	return 1;
}
Пример #22
0
ItemStack MenuItemStorage::click(Point position) {
	ItemStack item;

	drag_prev_slot = slotOver(position);

	// try to click on the highlighted (aka in focus) slot
	// since mouse clicks defocus slots before this point,
	// we don't have to worry about the mouse being over another slot
	if (drag_prev_slot == -1) {
		for (unsigned int i=0; i<slots.size(); i++) {
			if (slots[i]->in_focus) {
				drag_prev_slot = i;
				break;
			}
		}
	}

	if (drag_prev_slot > -1) {
		item = storage[drag_prev_slot];
		if (TOUCHSCREEN) {
			if (!slots[drag_prev_slot]->in_focus && !item.empty()) {
				slots[drag_prev_slot]->in_focus = true;
				current_slot = slots[drag_prev_slot];
				item.clear();
				return item;
			}
			else {
				slots[drag_prev_slot]->in_focus = false;
				current_slot = NULL;
			}
		}
		if (!item.empty()) {
			if (inpt->pressing[SHIFT] || NO_MOUSE || inpt->touch_locked) {
				item.quantity = 1;
			}
			substract( drag_prev_slot, item.quantity);
		}
		// item will be cleared if item.empty() == true
		return item;
	}
	else {
		item.clear();
		return item;
	}
}
Пример #23
0
bool MenuStash::add(ItemStack stack, int slot, bool play_sound) {
	if (stack.empty()) {
		return true;
	}

	if (play_sound) {
		items->playSound(stack.item);
	}

	if (items->items[stack.item].quest_item) {
		pc->logMsg(msg->get("Can not store quest items in the stash."), Avatar::MSG_NORMAL);
		drop_stack.push(stack);
		return false;
	}
	else if (items->items[stack.item].no_stash == Item::NO_STASH_ALL) {
		pc->logMsg(msg->get("This item can not be stored in the stash."), Avatar::MSG_NORMAL);
		drop_stack.push(stack);
		return false;
	}
	else if (activetab == STASH_PRIVATE && items->items[stack.item].no_stash == Item::NO_STASH_PRIVATE) {
		pc->logMsg(msg->get("This item can not be stored in the private stash."), Avatar::MSG_NORMAL);
		drop_stack.push(stack);
		return false;
	}
	else if (activetab == STASH_SHARED && items->items[stack.item].no_stash == Item::NO_STASH_SHARED) {
		pc->logMsg(msg->get("This item can not be stored in the shared stash."), Avatar::MSG_NORMAL);
		drop_stack.push(stack);
		return false;
	}

	ItemStack leftover = stock[activetab].add(stack, slot);
	if (!leftover.empty()) {
		if (leftover.quantity != stack.quantity) {
			updated = true;
		}
		pc->logMsg(msg->get("Stash is full."), Avatar::MSG_NORMAL);
		drop_stack.push(leftover);
		return false;
	}
	else {
		updated = true;
	}

	return true;
}
Пример #24
0
	ItemStack createItemStack()
	{
		try{
			IItemDefManager *idef = m_env->getGameDef()->idef();
			ItemStack item;
			item.deSerialize(m_itemstring, idef);
			infostream<<__FUNCTION_NAME<<": m_itemstring=\""<<m_itemstring
					<<"\" -> item=\""<<item.getItemString()<<"\""
					<<std::endl;
			return item;
		}
		catch(SerializationError &e)
		{
			infostream<<__FUNCTION_NAME<<": serialization error: "
					<<"m_itemstring=\""<<m_itemstring<<"\""<<std::endl;
			return ItemStack();
		}
	}
Пример #25
0
bool CraftDefinitionToolRepair::check(const CraftInput &input, IGameDef *gamedef) const
{
	if (input.method != CRAFT_METHOD_NORMAL)
		return false;

	ItemStack item1;
	ItemStack item2;
	for (const auto &item : input.items) {
		if (!item.empty()) {
			if (item1.empty())
				item1 = item;
			else if (item2.empty())
				item2 = item;
			else
				return false;
		}
	}
	ItemStack repaired = craftToolRepair(item1, item2, additional_wear, gamedef);
	return !repaired.empty();
}
Пример #26
0
/**
 * Insert item into first available carried slot, preferably in the optionnal specified slot
 * Returns an ItemStack containing anything that couldn't fit
 *
 * @param ItemStack Stack of items
 * @param slot Slot number where it will try to store the item
 */
ItemStack ItemStorage::add( ItemStack stack, int slot) {
    if (!stack.empty()) {
        if (items->items.empty() || stack.item <= 0 || static_cast<unsigned>(stack.item) > items->items.size()-1) {
            items->addUnknownItem(stack.item);
        }

        int max_quantity = items->items[stack.item].max_quantity;
        if (slot > -1) {
            // a slot is specified
            if (storage[slot].item != 0 && storage[slot].item != stack.item) {
                // the proposed slot isn't available
                slot = -1;
            }
        }
        else {
            // first search of stack to complete if the item is stackable
            int i = 0;
            while (max_quantity > 1 && slot == -1 && i < slot_number) {
                if (storage[i].item == stack.item && storage[i].quantity < max_quantity) {
                    slot = i;
                }
                i++;
            }
            // then an empty slot
            i = 0;
            while (slot == -1 && i < slot_number) {
                if (storage[i].empty()) {
                    slot = i;
                }
                i++;
            }
        }
        if (slot != -1) {
            // Add
            int quantity_added = std::min( stack.quantity, max_quantity - storage[slot].quantity);
            storage[slot].item = stack.item;
            storage[slot].quantity += quantity_added;
            stack.quantity -= quantity_added;
            // Add back the remaining, recursivly, until there's no more left to add or we run out of space.
            if (stack.quantity > 0) {
                return add(stack);
            }
            // everything added successfully, so return an empty ItemStack
            return ItemStack();
        }
        else {
            // Returns an ItemStack containing the remaining quantity if we run out of space.
            // This stack will likely be dropped on the ground
            return stack;
        }
    }
    return ItemStack();
}
Пример #27
0
boolean PlayerControllerSP::func_729_b(int i, int j, int k, int l) 
{
        int i1 = mc_08.field_6324_e.getBlockId(i, j, k);
        int j1 = mc_08.field_6324_e.getBlockMetadata_01(i, j, k);
        boolean flag = super.func_729_b(i, j, k, l);
        ItemStack itemstack = mc_08.field_6322_g.func_6416_v();
        boolean flag1 = mc_08.field_6322_g.func_454_b(Block.allBlocks[i1]);
        if(itemstack != null)
        {
            itemstack.hitBlock_00(i1, i, j, k);
            if(itemstack.stackSize == 0)
            {
                itemstack.func_1097_a(mc_08.field_6322_g);
                mc_08.field_6322_g.func_448_u();
            }
        }
        if(flag && flag1)
        {
            Block.allBlocks[i1].func_220_a_(mc_08.field_6324_e, i, j, k, j1);
        }
        return flag;
}
Пример #28
0
// Removes 1 from each item stack with replacement support
// Example: if replacements contains the pair ("bucket:bucket_water", "bucket:bucket_empty"),
//   a water bucket will not be removed but replaced by an empty bucket.
static void craftDecrementOrReplaceInput(CraftInput &input,
		std::vector<ItemStack> &output_replacements,
		const CraftReplacements &replacements,
		IGameDef *gamedef)
{
	if (replacements.pairs.empty()) {
		craftDecrementInput(input, gamedef);
		return;
	}

	// Make a copy of the replacements pair list
	std::vector<std::pair<std::string, std::string> > pairs = replacements.pairs;

	for (auto &item : input.items) {
		// Find an appropriate replacement
		bool found_replacement = false;
		for (auto j = pairs.begin(); j != pairs.end(); ++j) {
			if (inputItemMatchesRecipe(item.name, j->first, gamedef->idef())) {
				if (item.count == 1) {
					item.deSerialize(j->second, gamedef->idef());
					found_replacement = true;
					pairs.erase(j);
					break;
				}

				ItemStack rep;
				rep.deSerialize(j->second, gamedef->idef());
				item.remove(1);
				found_replacement = true;
				output_replacements.push_back(rep);
				break;

			}
		}
		// No replacement was found, simply decrement count by one
		if (!found_replacement && item.count > 0)
			item.remove(1);
	}
}
Пример #29
0
bool CraftDefinitionToolRepair::check(const CraftInput &input, IGameDef *gamedef) const
{
	if (input.method != CRAFT_METHOD_NORMAL)
		return false;

	ItemStack item1;
	ItemStack item2;
	for (std::vector<ItemStack>::const_iterator
			it = input.items.begin();
			it != input.items.end(); it++) {
		if (!it->empty()) {
			if (item1.empty())
				item1 = *it;
			else if (item2.empty())
				item2 = *it;
			else
				return false;
		}
	}
	ItemStack repaired = craftToolRepair(item1, item2, additional_wear, gamedef);
	return !repaired.empty();
}
// Crafting helper
bool getCraftingResult(Inventory *inv, ItemStack& result,
		std::vector<ItemStack> &output_replacements,
		bool decrementInput, IGameDef *gamedef)
{
	DSTACK(__FUNCTION_NAME);
	
	result.clear();

	// Get the InventoryList in which we will operate
	InventoryList *clist = inv->getList("craft");
	if(!clist)
		return false;

	// Mangle crafting grid to an another format
	CraftInput ci;
	ci.method = CRAFT_METHOD_NORMAL;
	ci.width = clist->getWidth() ? clist->getWidth() : 3;
	for(u16 i=0; i<clist->getSize(); 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, output_replacements, 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<clist->getSize(); i++)
		{
			clist->changeItem(i, ci.items[i]);
		}
	}

	return found;
}