aboutsummaryrefslogtreecommitdiff
path: root/games/devtest/mods/basenodes/textures/default_water_flowing.png
blob: 7cdafd51d7f2542ccb43892fb53fa3c9b14cd233 (plain)
ofshex dumpascii
0000 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 10 00 00 00 10 08 02 00 00 00 90 91 68 .PNG........IHDR...............h
0020 36 00 00 00 3a 49 44 41 54 78 01 63 30 0c ac 00 22 0d cb 00 db d8 56 62 18 74 d0 00 67 11 c9 20 6...:IDATx.c0...".....Vb.t..g...
0040 4a 83 51 50 25 42 03 ba 28 26 03 d3 06 fc 7a 08 79 9a 72 0d 94 86 12 ed 35 0c f9 b4 04 44 00 8b J.QP%B..(&....z.y.r.....5....D..
0060 e0 ef 07 76 bf 5e 86 00 00 00 00 49 45 4e 44 ae 42 60 82 ...v.^.....IEND.B`.
0'>60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
/*
Minetest
Copyright (C) 2010-2020 Minetest core development team

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "serverinventorymgr.h"
#include "map.h"
#include "nodemetadata.h"
#include "player_sao.h"
#include "remoteplayer.h"
#include "server.h"
#include "serverenvironment.h"

ServerInventoryManager::ServerInventoryManager() : InventoryManager()
{
}

ServerInventoryManager::~ServerInventoryManager()
{
	// Delete detached inventories
	for (auto &detached_inventory : m_detached_inventories) {
		delete detached_inventory.second.inventory;
	}
}

Inventory *ServerInventoryManager::getInventory(const InventoryLocation &loc)
{
	switch (loc.type) {
	case InventoryLocation::UNDEFINED:
	case InventoryLocation::CURRENT_PLAYER:
		break;
	case InventoryLocation::PLAYER: {
		RemotePlayer *player = m_env->getPlayer(loc.name.c_str());
		if (!player)
			return NULL;
		PlayerSAO *playersao = player->getPlayerSAO();
		if (!playersao)
			return NULL;
		return playersao->getInventory();
	} break;
	case InventoryLocation::NODEMETA: {
		NodeMetadata *meta = m_env->getMap().getNodeMetadata(loc.p);
		if (!meta)
			return NULL;
		return meta->getInventory();
	} break;
	case InventoryLocation::DETACHED: {
		auto it = m_detached_inventories.find(loc.name);
		if (it == m_detached_inventories.end())
			return nullptr;
		return it->second.inventory;
	} break;
	default:
		sanity_check(false); // abort
		break;
	}
	return NULL;
}

void ServerInventoryManager::setInventoryModified(const InventoryLocation &loc)
{
	switch (loc.type) {
	case InventoryLocation::UNDEFINED:
		break;
	case InventoryLocation::PLAYER: {

		RemotePlayer *player = m_env->getPlayer(loc.name.c_str());

		if (!player)
			return;

		player->setModified(true);
		player->inventory.setModified(true);
		// Updates are sent in ServerEnvironment::step()
	} break;
	case InventoryLocation::NODEMETA: {
		MapEditEvent event;
		event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
		event.p = loc.p;
		m_env->getMap().dispatchEvent(event);
	} break;
	case InventoryLocation::DETACHED: {
		// Updates are sent in ServerEnvironment::step()
	} break;
	default:
		sanity_check(false); // abort
		break;
	}
}

Inventory *ServerInventoryManager::createDetachedInventory(
		const std::string &name, IItemDefManager *idef, const std::string &player)
{
	if (m_detached_inventories.count(name) > 0) {
		infostream << "Server clearing detached inventory \"" << name << "\""
			   << std::endl;
		delete m_detached_inventories[name].inventory;
	} else {
		infostream << "Server creating detached inventory \"" << name << "\""
			   << std::endl;
	}

	Inventory *inv = new Inventory(idef);
	sanity_check(inv);
	m_detached_inventories[name].inventory = inv;
	if (!player.empty()) {
		m_detached_inventories[name].owner = player;

		if (!m_env)
			return inv; // Mods are not loaded yet, ignore

		RemotePlayer *p = m_env->getPlayer(name.c_str());

		// if player is connected, send him the inventory
		if (p && p->getPeerId() != PEER_ID_INEXISTENT) {
			m_env->getGameDef()->sendDetachedInventory(
					inv, name, p->getPeerId());
		}
	} else {
		if (!m_env)
			return inv; // Mods are not loaded yet, don't send

		// Inventory is for everybody, broadcast
		m_env->getGameDef()->sendDetachedInventory(inv, name, PEER_ID_INEXISTENT);
	}

	return inv;
}

bool ServerInventoryManager::removeDetachedInventory(const std::string &name)
{
	const auto &inv_it = m_detached_inventories.find(name);
	if (inv_it == m_detached_inventories.end())
		return false;

	delete inv_it->second.inventory;
	const std::string &owner = inv_it->second.owner;

	if (!owner.empty()) {
		RemotePlayer *player = m_env->getPlayer(owner.c_str());

		if (player && player->getPeerId() != PEER_ID_INEXISTENT)
			m_env->getGameDef()->sendDetachedInventory(
					nullptr, name, player->getPeerId());

	} else {
		// Notify all players about the change
		m_env->getGameDef()->sendDetachedInventory(
				nullptr, name, PEER_ID_INEXISTENT);
	}

	m_detached_inventories.erase(inv_it);

	return true;
}

void ServerInventoryManager::sendDetachedInventories(const std::string &peer_name,
		bool incremental,
		std::function<void(const std::string &, Inventory *)> apply_cb)
{
	for (const auto &detached_inventory : m_detached_inventories) {
		const DetachedInventory &dinv = detached_inventory.second;
		if (incremental) {
			if (!dinv.inventory || !dinv.inventory->checkModified())
				continue;
		}

		// if we are pushing inventories to a specific player
		// we should filter to send only the right inventories
		if (!peer_name.empty()) {
			const std::string &attached_player = dinv.owner;
			if (!attached_player.empty() && peer_name != attached_player)
				continue;
		}

		apply_cb(detached_inventory.first, detached_inventory.second.inventory);
	}
}