Пример #1
0
void Tile::moveCreature(Creature* creature, Cylinder* toCylinder, bool teleport /* = false*/)
{
	Tile* newTile = toCylinder->getTile();
	int32_t oldStackPos = __getIndexOfThing(creature);
	Position oldPos = getPosition();
	Position newPos = newTile->getPosition();
	Player* tmpPlayer = NULL;
	SpectatorVec list;
	SpectatorVec::iterator it;
	g_game.getSpectators(list, oldPos, false, true);
	g_game.getSpectators(list, newPos, true, true);
	std::vector<uint32_t> oldStackPosVector;

	for (it = list.begin(); it != list.end(); ++it)
	{
		if ((tmpPlayer = (*it)->getPlayer()))
		{
			oldStackPosVector.push_back(getClientIndexOfThing(tmpPlayer, creature));
		}
	}

	//remove the creature
	__removeThing(creature, 0);

	// Switch the node ownership
	if (qt_node != newTile->qt_node)
	{
		qt_node->removeCreature(creature);
		newTile->qt_node->addCreature(creature);
	}

	//add the creature
	newTile->__addThing(creature);
	int32_t newStackPos = newTile->__getIndexOfThing(creature);

	if (!teleport)
	{
		if (oldPos.y > newPos.y)
		{
			creature->setDirection(NORTH);
		}
		else if (oldPos.y < newPos.y)
		{
			creature->setDirection(SOUTH);
		}

		if (oldPos.x < newPos.x)
		{
			creature->setDirection(EAST);
		}
		else if (oldPos.x > newPos.x)
		{
			creature->setDirection(WEST);
		}
	}

	//send to client
	uint32_t i = 0;

	for (it = list.begin(); it != list.end(); ++it)
	{
		if ((tmpPlayer = (*it)->getPlayer()))
		{
			tmpPlayer->sendCreatureMove(creature, newTile, newPos, this, oldPos, oldStackPosVector[i], teleport);
			++i;
		}
	}

	//event method
	for (it = list.begin(); it != list.end(); ++it)
	{
		(*it)->onCreatureMove(creature, newTile, newPos, this, oldPos, teleport);
	}

	postRemoveNotification(creature, toCylinder, oldStackPos, true);
	newTile->postAddNotification(creature, this, newStackPos);
}
Пример #2
0
void Tile::moveCreature(Creature* creature, Cylinder* toCylinder, bool forceTeleport/* = false*/)
{
	Tile* newTile = toCylinder->getTile();
	int32_t oldStackPos = __getIndexOfThing(creature);

	Position oldPos = getPosition();
	Position newPos = newTile->getPosition();

	bool teleport = false;

	if (forceTeleport || !newTile->ground || !Position::areInRange<1, 1, 0>(oldPos, newPos)) {
		teleport = true;
	}

	SpectatorVec list;
	g_game.getSpectators(list, oldPos, true);
	g_game.getSpectators(list, newPos, true);

	SpectatorVec::const_iterator end = list.end();

	std::vector<uint32_t> oldStackPosVector;

	for (SpectatorVec::const_iterator it = list.begin(); it != end; ++it) {
		if (Player* tmpPlayer = (*it)->getPlayer()) {
			oldStackPosVector.push_back(getClientIndexOfThing(tmpPlayer, creature));
		}
	}

	//remove the creature
	__removeThing(creature, 0);

	// Switch the node ownership
	if (qt_node != newTile->qt_node) {
		qt_node->removeCreature(creature);
		newTile->qt_node->addCreature(creature);
	}

	//add the creature
	newTile->__addThing(creature);
	int32_t newStackPos = newTile->__getIndexOfThing(creature);

	if (!teleport) {
		if (oldPos.y > newPos.y) {
			creature->setDirection(NORTH);
		} else if (oldPos.y < newPos.y) {
			creature->setDirection(SOUTH);
		}

		if (oldPos.x < newPos.x) {
			creature->setDirection(EAST);
		} else if (oldPos.x > newPos.x) {
			creature->setDirection(WEST);
		}
	}

	//send to client
	uint32_t i = 0;

	for (SpectatorVec::const_iterator it = list.begin(); it != end; ++it) {
		if (Player* tmpPlayer = (*it)->getPlayer()) {
			//Use the correct stackpos
			if (!creature->isInGhostMode() || tmpPlayer->isAccessPlayer()) {
				tmpPlayer->sendCreatureMove(creature, newTile, newPos, this, oldPos, oldStackPosVector[i], teleport);
			}

			++i;
		}
	}

	//event method
	for (SpectatorVec::const_iterator it = list.begin(); it != end; ++it) {
		(*it)->onCreatureMove(creature, newTile, newPos, this, oldPos, teleport);
	}

	postRemoveNotification(creature, toCylinder, oldStackPos, true);
	newTile->postAddNotification(creature, this, newStackPos);
}
Пример #3
0
void Tile::moveCreature(Creature* actor, Creature* creature, Cylinder* toCylinder, bool forceTeleport/* = false*/)
{
	Tile* newTile = toCylinder->getTile();
	SpectatorVec list;
	SpectatorVec::iterator it;

	g_game.getSpectators(list, pos, false, true);
	Position newPos = newTile->getPosition();
	g_game.getSpectators(list, newPos, true, true);

	bool teleport = false;
	if(forceTeleport || !newTile->ground || !Position::areInRange<1,1,0>(pos, newPos))
		teleport = true;

	std::vector<uint32_t> oldStackposVector;
	Player* tmpPlayer = NULL;
	for(it = list.begin(); it != list.end(); ++it)
	{
		if((tmpPlayer = (*it)->getPlayer()))
			oldStackposVector.push_back(getClientIndexOfThing(tmpPlayer, creature));
	}

	int32_t oldStackpos = __getIndexOfThing(creature);
	//remove the creature
	__removeThing(creature, 0);
	//switch the node ownership
	if(qt_node != newTile->qt_node)
	{
		qt_node->removeCreature(creature);
		newTile->qt_node->addCreature(creature);
	}

	//add the creature
	newTile->__addThing(actor, creature);
	int32_t newStackpos = newTile->__getIndexOfThing(creature);
	if(!teleport)
	{
		if(pos.y > newPos.y)
			creature->setDirection(NORTH);
		else if(pos.y < newPos.y)
			creature->setDirection(SOUTH);
		if(pos.x < newPos.x)
			creature->setDirection(EAST);
		else if(pos.x > newPos.x)
			creature->setDirection(WEST);
	}

	//send to client
	int32_t i = 0;
	for(it = list.begin(); it != list.end(); ++it)
	{
		if((tmpPlayer = (*it)->getPlayer()) && tmpPlayer->canSeeCreature(creature))
			tmpPlayer->sendCreatureMove(creature, newTile, newPos, this, pos, oldStackposVector[i++], teleport);
	}

	//event method
	for(it = list.begin(); it != list.end(); ++it)
		(*it)->onCreatureMove(creature, newTile, newPos, this, pos, teleport);

	postRemoveNotification(actor, creature, toCylinder, oldStackpos, true);
	newTile->postAddNotification(actor, creature, this, newStackpos);
}
Пример #4
0
void Tile::__removeThing(Thing* thing, uint32_t count)
{
	Creature* creature = thing->getCreature();

	if (creature) {
		CreatureVector* creatures = getCreatures();

		if (creatures) {
			CreatureVector::iterator it = std::find(creatures->begin(), creatures->end(), thing);

			if (it != creatures->end()) {
				g_game.clearSpectatorCache();
				creatures->erase(it);
			}
		}

		return;
	}

	Item* item = thing->getItem();

	if (item) {
		int32_t index = __getIndexOfThing(item);

		if (index == -1) {
			return;
		}

		if (item == ground) {
			const SpectatorVec& list = g_game.getSpectators(getPosition());

			std::vector<uint32_t> oldStackPosVector;

			for (SpectatorVec::const_iterator it = list.begin(), end = list.end(); it != end; ++it) {
				if (Player* tmpPlayer = (*it)->getPlayer()) {
					oldStackPosVector.push_back(getClientIndexOfThing(tmpPlayer, ground));
				}
			}

			ground->setParent(NULL);
			ground = NULL;
			onRemoveTileItem(list, oldStackPosVector, item);
			return;
		}

		if (item->isAlwaysOnTop()) {
			TileItemVector* items = getItemList();

			if (items) {
				for (ItemVector::iterator it = items->getBeginTopItem(); it != items->getEndTopItem(); ++it) {
					if (*it == item) {
						const SpectatorVec& list = g_game.getSpectators(getPosition());

						std::vector<uint32_t> oldStackPosVector;

						for (SpectatorVec::const_iterator iit = list.begin(), iend = list.end(); iit != iend; ++iit) {
							if (Player* tmpPlayer = (*iit)->getPlayer()) {
								oldStackPosVector.push_back(getClientIndexOfThing(tmpPlayer, *it));
							}
						}

						(*it)->setParent(NULL);
						items->erase(it);
						onRemoveTileItem(list, oldStackPosVector, item);
						return;
					}
				}
			}
		} else {
			TileItemVector* items = getItemList();

			if (items) {
				for (ItemVector::iterator it = items->getBeginDownItem(); it != items->getEndDownItem(); ++it) {
					if (*it == item) {
						if (item->isStackable() && count != item->getItemCount()) {
							uint8_t newCount = (uint8_t)std::max<int32_t>(0, (int32_t)(item->getItemCount() - count));

							updateTileFlags(item, true);
							item->setItemCount(newCount);
							updateTileFlags(item, false);

							const ItemType& it = Item::items[item->getID()];
							onUpdateTileItem(item, it, item, it);
						} else {
							const SpectatorVec& list = g_game.getSpectators(getPosition());

							std::vector<uint32_t> oldStackPosVector;

							for (SpectatorVec::const_iterator iit = list.begin(), iend = list.end(); iit != iend; ++iit) {
								if (Player* tmpPlayer = (*iit)->getPlayer()) {
									oldStackPosVector.push_back(getClientIndexOfThing(tmpPlayer, *it));
								}
							}

							(*it)->setParent(NULL);
							items->erase(it);
							--items->downItemCount;
							onRemoveTileItem(list, oldStackPosVector, item);
						}

						return;
					}
				}
			}
		}
	}
}
Пример #5
0
void Tile::__removeThing(Thing* thing, uint32_t count)
{
	Creature* creature = thing->getCreature();
	if(creature)
	{
		if(CreatureVector* creatures = getCreatures())
		{
			CreatureVector::iterator it = std::find(creatures->begin(), creatures->end(), thing);
			if(it == creatures->end())
			{
#ifdef __DEBUG_MOVESYS__
				std::clog << "[Failure - Tile::__removeThing] creature not found" << std::endl;
#endif
				return/* RET_NOTPOSSIBLE*/;
			}

			g_game.clearSpectatorCache();
			creatures->erase(it);
			--thingCount;
		}
#ifdef __DEBUG_MOVESYS__
		else
			std::clog << "[Failure - Tile::__removeThing] creature not found" << std::endl;
#endif

		return;
	}

	Item* item = thing->getItem();
	if(!item)
	{
#ifdef __DEBUG_MOVESYS__
		std::clog << "[Failure - Tile::__removeThing] item == NULL" << std::endl;
#endif
		return/* RET_NOTPOSSIBLE*/;
	}

	int32_t index = __getIndexOfThing(item);
	if(index == -1)
	{
#ifdef __DEBUG_MOVESYS__
		std::clog << "[Failure - Tile::__removeThing] index == -1" << std::endl;
#endif
		return/* RET_NOTPOSSIBLE*/;
	}

	if(item == ground)
	{
		const SpectatorVec& list = g_game.getSpectators(pos);
		std::vector<int32_t> oldStackposVector;

		Player* tmpPlayer = NULL;
		for(SpectatorVec::const_iterator it = list.begin(); it != list.end(); ++it)
		{
			if((tmpPlayer = (*it)->getPlayer()))
				oldStackposVector.push_back(getClientIndexOfThing(tmpPlayer, ground));
		}

#ifdef __GROUND_CACHE__
		std::map<Item*, int32_t>::iterator it = g_game.grounds.find(ground);
		bool erase = it == g_game.grounds.end();
		if(!erase)
		{
			it->second--;
			erase = it->second < 1;
			if(erase)
				g_game.grounds.erase(it);
		}

		if(erase)
		{
#endif
			ground->setParent(NULL);
			g_game.freeThing(ground);
#ifdef __GROUND_CACHE__
		}
#endif

		ground = NULL;
		--thingCount;

		onRemoveTileItem(list, oldStackposVector, item);
		return/* RET_NOERROR*/;
	}

	TileItemVector* items = getItemList();
	if(!items)
		return;

	if(item->isAlwaysOnTop())
	{
		ItemVector::iterator it = std::find(items->getBeginTopItem(), items->getEndTopItem(), item);
		if(it != items->end())
		{
			const SpectatorVec& list = g_game.getSpectators(pos);
			std::vector<int32_t> oldStackposVector;

			Player* tmpPlayer = NULL;
			for(SpectatorVec::const_iterator iit = list.begin(); iit != list.end(); ++iit)
			{
				if((tmpPlayer = (*iit)->getPlayer()))
					oldStackposVector.push_back(getClientIndexOfThing(tmpPlayer, item));
			}

			item->setParent(NULL);
			items->erase(it);

			--thingCount;
			onRemoveTileItem(list, oldStackposVector, item);
			return/* RET_NOERROR*/;
		}
	}
	else
	{
		ItemVector::iterator it = std::find(items->getBeginDownItem(), items->getEndDownItem(), item);
		if(it != items->end())
		{
			if(item->isStackable() && count != item->getItemCount())
			{
				uint8_t newCount = (uint8_t)std::max((int32_t)0, (int32_t)(item->getItemCount() - count));
				updateTileFlags(item, true);

				item->setItemCount(newCount);
				updateTileFlags(item, false);

				const ItemType& it = Item::items[item->getID()];
				onUpdateTileItem(item, it, item, it);
			}
			else
			{
				const SpectatorVec& list = g_game.getSpectators(pos);
				std::vector<int32_t> oldStackposVector;

				Player* tmpPlayer = NULL;
				for(SpectatorVec::const_iterator iit = list.begin(); iit != list.end(); ++iit)
				{
					if((tmpPlayer = (*iit)->getPlayer()))
						oldStackposVector.push_back(getClientIndexOfThing(tmpPlayer, item));
				}

				item->setParent(NULL);
				items->erase(it);

				--items->downItemCount;
				--thingCount;
				onRemoveTileItem(list, oldStackposVector, item);
			}

			return/* RET_NOERROR*/;
		}
	}
#ifdef __DEBUG_MOVESYS__

	std::clog << "[Failure - Tile::__removeThing] thing not found" << std::endl;
#endif
}