Example #1
0
void Tile::updateThing(Thing* thing, uint16_t itemId, uint32_t count)
{
	int32_t index = getThingIndex(thing);
	if (index == -1) {
		return /*RETURNVALUE_NOTPOSSIBLE*/;
	}

	Item* item = thing->getItem();
	if (item == nullptr) {
		return /*RETURNVALUE_NOTPOSSIBLE*/;
	}

	const ItemType& oldType = Item::items[item->getID()];
	const ItemType& newType = Item::items[itemId];
	resetTileFlags(item);
	item->setID(itemId);
	item->setSubType(count);
	setTileFlags(item);
	onUpdateTileItem(item, oldType, item, newType);
}
Example #2
0
void Tile::__updateThing(Thing* thing, uint32_t count)
{
	int32_t index = __getIndexOfThing(thing);
	if(index == -1){
#ifdef __DEBUG__MOVESYS__
		std::cout << "Failure: [Tile::__updateThing] index == -1" << std::endl;
		int *a = NULL; *a = 1;
#endif
		return /*RET_NOTPOSSIBLE*/;
	}

	Item* item = thing->getItem();
	if(item == NULL){
#ifdef __DEBUG__MOVESYS__
		std::cout << "Failure: [Tile::__updateThing] item == NULL" << std::endl;
		int *a = NULL; *a = 1;
#endif
		return /*RET_NOTPOSSIBLE*/;
	}

	item->setItemCountOrSubtype(count);
	onUpdateTileItem(index, item, item);
}
Example #3
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.map.clearSpectatorCache();
				creatures->erase(it);
			}
		}
		return;
	}

	Item* item = thing->getItem();
	if (!item) {
		return;
	}

	int32_t index = getThingIndex(item);
	if (index == -1) {
		return;
	}

	if (item == ground) {
		ground->setParent(nullptr);
		ground = nullptr;

		SpectatorVec list;
		g_game.map.getSpectators(list, getPosition(), true);
		onRemoveTileItem(list, std::vector<int32_t>(list.size(), 0), item);
		return;
	}

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

	const ItemType& itemType = Item::items[item->getID()];
	if (itemType.alwaysOnTop) {
		auto it = std::find(items->getBeginTopItem(), items->getEndTopItem(), item);
		if (it == items->getEndTopItem()) {
			return;
		}

		std::vector<int32_t> oldStackPosVector;

		SpectatorVec list;
		g_game.map.getSpectators(list, getPosition(), true);
		for (Creature* spectator : list) {
			if (Player* tmpPlayer = spectator->getPlayer()) {
				oldStackPosVector.push_back(getStackposOfItem(tmpPlayer, item));
			}
		}

		item->setParent(nullptr);
		items->erase(it);
		onRemoveTileItem(list, oldStackPosVector, item);
	} else {
		auto it = std::find(items->getBeginDownItem(), items->getEndDownItem(), item);
		if (it == items->getEndDownItem()) {
			return;
		}

		if (itemType.stackable && count != item->getItemCount()) {
			uint8_t newCount = static_cast<uint8_t>(std::max<int32_t>(0, static_cast<int32_t>(item->getItemCount() - count)));
			item->setItemCount(newCount);
			onUpdateTileItem(item, itemType, item, itemType);
		} else {
			std::vector<int32_t> oldStackPosVector;

			SpectatorVec list;
			g_game.map.getSpectators(list, getPosition(), true);
			for (Creature* spectator : list) {
				if (Player* tmpPlayer = spectator->getPlayer()) {
					oldStackPosVector.push_back(getStackposOfItem(tmpPlayer, item));
				}
			}

			item->setParent(nullptr);
			items->erase(it);
			items->addDownItemCount(-1);
			onRemoveTileItem(list, oldStackPosVector, item);
		}
	}
}
Example #4
0
void Tile::replaceThing(uint32_t index, Thing* thing)
{
	int32_t pos = index;

	Item* item = thing->getItem();
	if (item == nullptr) {
		return /*RETURNVALUE_NOTPOSSIBLE*/;
	}

	Item* oldItem = nullptr;
	bool isInserted = false;

	if (ground) {
		if (pos == 0) {
			oldItem = ground;
			ground = item;
			isInserted = true;
		}

		--pos;
	}

	TileItemVector* items = getItemList();
	if (items && !isInserted) {
		int32_t topItemSize = getTopItemCount();
		if (pos < topItemSize) {
			ItemVector::iterator it = items->getBeginTopItem();
			it += pos;

			oldItem = (*it);
			it = items->erase(it);
			items->insert(it, item);
			isInserted = true;
		}

		pos -= topItemSize;
	}

	CreatureVector* creatures = getCreatures();
	if (creatures) {
		if (!isInserted && pos < static_cast<int32_t>(creatures->size())) {
			return /*RETURNVALUE_NOTPOSSIBLE*/;
		}

		pos -= static_cast<uint32_t>(creatures->size());
	}

	if (items && !isInserted) {
		int32_t downItemSize = getDownItemCount();
		if (pos < downItemSize) {
			ItemVector::iterator it = items->getBeginDownItem() + pos;
			oldItem = *it;
			it = items->erase(it);
			items->insert(it, item);
			isInserted = true;
		}
	}

	if (isInserted) {
		item->setParent(this);

		resetTileFlags(oldItem);
		setTileFlags(item);
		const ItemType& oldType = Item::items[oldItem->getID()];
		const ItemType& newType = Item::items[item->getID()];
		onUpdateTileItem(oldItem, oldType, item, newType);

		oldItem->setParent(nullptr);
		return /*RETURNVALUE_NOERROR*/;
	}
}
Example #5
0
void Tile::addThing(int32_t, Thing* thing)
{
	Creature* creature = thing->getCreature();
	if (creature) {
		g_game.map.clearSpectatorCache();
		creature->setParent(this);
		CreatureVector* creatures = makeCreatures();
		creatures->insert(creatures->begin(), creature);
	} else {
		Item* item = thing->getItem();
		if (item == nullptr) {
			return /*RETURNVALUE_NOTPOSSIBLE*/;
		}

		TileItemVector* items = getItemList();
		if (items && items->size() >= 0xFFFF) {
			return /*RETURNVALUE_NOTPOSSIBLE*/;
		}

		item->setParent(this);

		const ItemType& itemType = Item::items[item->getID()];
		if (itemType.isGroundTile()) {
			if (ground == nullptr) {
				ground = item;
				onAddTileItem(item);
			} else {
				const ItemType& oldType = Item::items[ground->getID()];

				Item* oldGround = ground;
				ground->setParent(nullptr);
				g_game.ReleaseItem(ground);
				ground = item;
				resetTileFlags(oldGround);
				setTileFlags(item);
				onUpdateTileItem(oldGround, oldType, item, itemType);
				postRemoveNotification(oldGround, nullptr, 0);
			}
		} else if (itemType.alwaysOnTop) {
			if (itemType.isSplash() && items) {
				//remove old splash if exists
				for (ItemVector::const_iterator it = items->getBeginTopItem(), end = items->getEndTopItem(); it != end; ++it) {
					Item* oldSplash = *it;
					if (!Item::items[oldSplash->getID()].isSplash()) {
						continue;
					}

					removeThing(oldSplash, 1);
					oldSplash->setParent(nullptr);
					g_game.ReleaseItem(oldSplash);
					postRemoveNotification(oldSplash, nullptr, 0);
					break;
				}
			}

			bool isInserted = false;

			if (items) {
				for (ItemVector::iterator it = items->getBeginTopItem(), end = items->getEndTopItem(); it != end; ++it) {
					//Note: this is different from internalAddThing
					if (itemType.alwaysOnTopOrder <= Item::items[(*it)->getID()].alwaysOnTopOrder) {
						items->insert(it, item);
						isInserted = true;
						break;
					}
				}
			} else {
				items = makeItemList();
			}

			if (!isInserted) {
				items->push_back(item);
			}

			onAddTileItem(item);
		} else {
			if (itemType.isMagicField()) {
				//remove old field item if exists
				if (items) {
					for (ItemVector::const_iterator it = items->getBeginDownItem(), end = items->getEndDownItem(); it != end; ++it) {
						MagicField* oldField = (*it)->getMagicField();
						if (oldField) {
							if (oldField->isReplaceable()) {
								removeThing(oldField, 1);

								oldField->setParent(nullptr);
								g_game.ReleaseItem(oldField);
								postRemoveNotification(oldField, nullptr, 0);
								break;
							} else {
								//This magic field cannot be replaced.
								item->setParent(nullptr);
								g_game.ReleaseItem(item);
								return;
							}
						}
					}
				}
			}

			items = makeItemList();
			items->insert(items->getBeginDownItem(), item);
			items->addDownItemCount(1);
			onAddTileItem(item);
		}
	}
}
Example #6
0
void Tile::__addThing(int32_t index, Thing* thing)
{
	Creature* creature = thing->getCreature();

	if (creature)
	{
		g_game.clearSpectatorCache();
		creature->setParent(this);
		CreatureVector* creatures = makeCreatures();
		creatures->insert(creatures->begin(), creature);
		++thingCount;
	}
	else
	{
		Item* item = thing->getItem();

		if (!item)
		{
#ifdef __DEBUG__MOVESYS__
			std::cout << "Failure: [Tile::__addThing] item == NULL" << std::endl;
			DEBUG_REPORT
#endif
			return /*RET_NOTPOSSIBLE*/;
		}

		TileItemVector* items = getItemList();

		if (items && items->size() > 0xFFFF)
		{
			return /*RET_NOTPOSSIBLE*/;
		}

		item->setParent(this);

		if (item->isGroundTile())
		{
			if (!ground)
			{
				ground = item;
				++thingCount;
				onAddTileItem(item);
			}
			else
			{
				const ItemType& oldType = Item::items[ground->getID()];
				const ItemType& newType = Item::items[item->getID()];
				int32_t oldGroundIndex = __getIndexOfThing(ground);
				Item* oldGround = ground;
				ground->setParent(NULL);
				g_game.FreeThing(ground);
				ground = item;
				updateTileFlags(oldGround, true);
				updateTileFlags(item, false);
				onUpdateTileItem(oldGround, oldType, item, newType);
				postRemoveNotification(oldGround, NULL, oldGroundIndex, true);
			}
		}
		else if (item->isAlwaysOnTop())
		{
			if (item->isSplash())
			{
				//remove old splash if exists
				if (items)
				{
					for (ItemVector::iterator it = items->getBeginTopItem(); it != items->getEndTopItem(); ++it)
					{
						if ((*it)->isSplash())
						{
							int32_t oldSplashIndex = __getIndexOfThing(*it);
							Item* oldSplash = *it;
							__removeThing(oldSplash, 1);
							oldSplash->setParent(NULL);
							g_game.FreeThing(oldSplash);
							postRemoveNotification(oldSplash, NULL, oldSplashIndex, true);
							break;
						}
					}
				}
			}

			bool isInserted = false;

			if (items)
			{
				for (ItemVector::iterator it = items->getBeginTopItem(); it != items->getEndTopItem(); ++it)
				{
					//Note: this is different from internalAddThing
					if (Item::items[item->getID()].alwaysOnTopOrder <= Item::items[(*it)->getID()].alwaysOnTopOrder)
					{
						items->insert(it, item);
						++thingCount;
						isInserted = true;
						break;
					}
				}
			}
			else
			{
				items = makeItemList();
			}

			if (!isInserted)
			{
				items->push_back(item);
				++thingCount;
			}

			onAddTileItem(item);
		}
		else
		{
			if (item->isMagicField())
			{
				//remove old field item if exists
				if (items)
				{
					MagicField* oldField = NULL;

					for (ItemVector::iterator it = items->getBeginDownItem(); it != items->getEndDownItem(); ++it)
					{
						if ((oldField = (*it)->getMagicField()))
						{
							if (oldField->isReplaceable())
							{
								int32_t oldFieldIndex = __getIndexOfThing(*it);
								__removeThing(oldField, 1);
								oldField->setParent(NULL);
								g_game.FreeThing(oldField);
								postRemoveNotification(oldField, NULL, oldFieldIndex, true);
								break;
							}
							else
							{
								//This magic field cannot be replaced.
								item->setParent(NULL);
								g_game.FreeThing(item);
								return;
							}
						}
					}
				}
			}

			items = makeItemList();
			items->insert(items->getBeginDownItem(), item);
			++items->downItemCount;
			++thingCount;
			onAddTileItem(item);
		}
	}
Example #7
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;
					}
				}
			}
		}
	}
}
Example #8
0
void Tile::__replaceThing(uint32_t index, Thing* thing)
{
	int32_t pos = index;

	Item* item = thing->getItem();

	if (item == NULL) {
		return /*RET_NOTPOSSIBLE*/;
	}

	Item* oldItem = NULL;
	bool isInserted = false;

	if (ground) {
		if (pos == 0) {
			oldItem = ground;
			ground = item;
			isInserted = true;
		}

		--pos;
	}

	TileItemVector* items = getItemList();

	if (items && !isInserted) {
		int32_t topItemSize = getTopItemCount();

		if (pos < topItemSize) {
			ItemVector::iterator it = items->getBeginTopItem();
			it += pos;

			oldItem = (*it);
			it = items->erase(it);
			items->insert(it, item);
			isInserted = true;
		}

		pos -= topItemSize;
	}

	CreatureVector* creatures = getCreatures();

	if (creatures) {
		if (!isInserted && pos < (int32_t)creatures->size()) {
			return /*RET_NOTPOSSIBLE*/;
		}

		pos -= (uint32_t)creatures->size();
	}

	if (items && !isInserted) {
		int32_t downItemSize = getDownItemCount();

		if (pos < downItemSize) {
			ItemVector::iterator it = items->begin();
			it += pos;
			pos = 0;

			oldItem = (*it);
			it = items->erase(it);
			items->insert(it, item);
			isInserted = true;
		}
	}

	if (isInserted) {
		item->setParent(this);

		updateTileFlags(oldItem, true);
		updateTileFlags(item, false);
		const ItemType& oldType = Item::items[oldItem->getID()];
		const ItemType& newType = Item::items[item->getID()];
		onUpdateTileItem(oldItem, oldType, item, newType);

		oldItem->setParent(NULL);
		return /*RET_NOERROR*/;
	}
}
Example #9
0
void Tile::__removeThing(Thing* thing, uint32_t count)
{
	Creature* creature = thing->getCreature();
	if(creature){
		CreatureVector::iterator it = std::find(creatures.begin(), creatures.end(), thing);

		if(it == creatures.end()){
#ifdef __DEBUG__MOVESYS__
		std::cout << "Failure: [Tile::__removeThing] creature not found" << std::endl;
		int *a = NULL; *a = 1;
#endif
		return; //RET_NOTPOSSIBLE;
		}

		creatures.erase(it);
		return;
	}
	else{
		Item* item = thing->getItem();
		if(item == NULL){
#ifdef __DEBUG__MOVESYS__
			std::cout << "Failure: [Tile::__removeThing] item == NULL" << std::endl;
			int *a = NULL; *a = 1;
#endif
			return /*RET_NOTPOSSIBLE*/;
		}

		uint32_t index = __getIndexOfThing(item);
		if(index == -1){
#ifdef __DEBUG__MOVESYS__
			std::cout << "Failure: [Tile::__removeThing] index == -1" << std::endl;
			int *a = NULL; *a = 1;
#endif
			return /*RET_NOTPOSSIBLE*/;
		}

		if(item == ground){
			
			onRemoveTileItem(index, item);

			ground->setParent(NULL);
			ground = NULL;
			return /*RET_NOERROR*/;
		}

		ItemVector::iterator iit;
		if(item->isAlwaysOnTop()){
			for(iit = topItems.begin(); iit != topItems.end(); ++iit){
				if(*iit == item){

					onRemoveTileItem(index, item);

					(*iit)->setParent(NULL);
					topItems.erase(iit);
					return /*RET_NOERROR*/;
				}
			}
		}
		else{
			for (iit = downItems.begin(); iit != downItems.end(); ++iit){
				if(*iit == item){
					if(item->isStackable() && count != item->getItemCount()){							
						int newCount = std::max(0, (int)(item->getItemCount() - count));
						item->setItemCount(newCount);

						onUpdateTileItem(index, item, item);
					}
					else {
						
						onRemoveTileItem(index, item);

						(*iit)->setParent(NULL);
						downItems.erase(iit);
					}

					return /*RET_NOERROR*/;
				}
			}
		}
	}
#ifdef __DEBUG__MOVESYS__
	std::cout << "Failure: [Tile::__removeThing] thing not found" << std::endl;
	int *a = NULL; *a = 1;
#endif
}
Example #10
0
void Tile::__replaceThing(uint32_t index, Thing* thing)
{
	int32_t pos = index;

	Item* item = thing->getItem();
	if(item == NULL){
#ifdef __DEBUG__MOVESYS__
		std::cout << "Failure: [Tile::__updateThing] item == NULL" << std::endl;
		int *a = NULL; *a = 1;
#endif
		return /*RET_NOTPOSSIBLE*/;
	}

	Item* oldItem = NULL;

	if(ground){
		if(pos == 0){
			oldItem = ground;
			ground = item;
		}

		--pos;
	}

	if(pos >= 0 && pos < topItems.size()){
		ItemVector::iterator it = topItems.begin();
		it += pos;
		pos = 0;

		oldItem = (*it);
		it = topItems.erase(it);
		topItems.insert(it, item);
	}

	pos -= (uint32_t)topItems.size();

	if(pos >= 0 && pos < creatures.size()){
#ifdef __DEBUG__MOVESYS__
		std::cout << "Failure: [Tile::__updateThing] Update object is a creature" << std::endl;
		int *a = NULL; *a = 1;
#endif
		return /*RET_NOTPOSSIBLE*/;
	}

	pos -= (uint32_t)creatures.size();

	if(pos >= 0 && pos < downItems.size()){
		ItemVector::iterator it = downItems.begin();
		it += pos;
		pos = 0;

		oldItem = (*it);
		it = downItems.erase(it);
		downItems.insert(it, item);
	}

	if(pos == 0){
		oldItem->setParent(NULL);
		item->setParent(this);
		onUpdateTileItem(index, oldItem, item);

		return /*RET_NOERROR*/;
	}

#ifdef __DEBUG__MOVESYS__
	std::cout << "Failure: [Tile::__updateThing] Update object not found" << std::endl;
	int *a = NULL; *a = 1;
#endif
}
Example #11
0
void Tile::__addThing(int32_t index, Thing* thing)
{
	Creature* creature = thing->getCreature();
	if(creature){
		creature->setParent(this);
		creatures.insert(creatures.begin(), creature);
	}
	else{
		Item* item = thing->getItem();
		if(item == NULL){
#ifdef __DEBUG__MOVESYS__
			std::cout << "Failure: [Tile::__addThing] item == NULL" << std::endl;
			int *a = NULL; *a = 1;
#endif
			return /*RET_NOTPOSSIBLE*/;
		}
		
		item->setParent(this);

		if(item->isGroundTile()){
			if(ground == NULL){
				onAddTileItem(item);
			}
			else{
				uint32_t index = __getIndexOfThing(ground);				
				onUpdateTileItem(index, ground, item);

				ground->setParent(NULL);
				g_game.FreeThing(ground);
				ground = NULL;
			}

			ground = item;
		}
		else if(item->isAlwaysOnTop()){
			if(item->isSplash()){
				//remove old splash if exists
				ItemVector::iterator iit;
				for(iit = topItems.begin(); iit != topItems.end(); ++iit){
					if((*iit)->isSplash()){
						Item* oldSplash = *iit;
						__removeThing(oldSplash, 1);

						oldSplash->setParent(NULL);
						g_game.FreeThing(oldSplash);
						break;
					}
				}
			}

			bool isInserted = false;
			ItemVector::iterator iit;
			for(iit = topItems.begin(); iit != topItems.end(); ++iit){
				//Note: this is different from internalAddThing
				if(Item::items[item->getID()].alwaysOnTopOrder <= Item::items[(*iit)->getID()].alwaysOnTopOrder){
					topItems.insert(iit, item);
					isInserted = true;
					break;
				}
			}

			if(!isInserted){
				topItems.push_back(item);
			}

			onAddTileItem(item);
		}
		else{
			if(item->isMagicField()){
				//remove old field item if exists
				ItemVector::iterator iit;
				for(iit = downItems.begin(); iit != downItems.end(); ++iit){
					if((*iit)->isMagicField()){
						Item* oldField = *iit;
						__removeThing(oldField, 1);

						oldField->setParent(NULL);
						g_game.FreeThing(oldField);
						break;
					}
				}
			}

			downItems.insert(downItems.begin(), item);
			onAddTileItem(item);
		}
	}
}
Example #12
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
}
Example #13
0
void Tile::__replaceThing(uint32_t index, Thing* thing)
{
	Item* item = thing->getItem();
	if(!item)
	{
#ifdef __DEBUG_MOVESYS__
		std::clog << "[Failure - Tile::__replaceThing] item == NULL" << std::endl;
#endif
		return/* RET_NOTPOSSIBLE*/;
	}

	int32_t pos = index;
	Item* oldItem = NULL;
	if(ground)
	{
		if(!pos)
		{
			oldItem = ground;
			ground = item;
		}

		--pos;
	}

	TileItemVector* items = NULL;
	if(!oldItem)
		items = getItemList();

	if(items)
	{
		int32_t topItemSize = getTopItemCount();
		if(pos < topItemSize)
		{
			ItemVector::iterator it = items->getBeginTopItem();
			it += pos;

			oldItem = (*it);
			it = items->erase(it);
			items->insert(it, item);
		}

		pos -= topItemSize;
	}

	if(!oldItem)
	{
		if(CreatureVector* creatures = getCreatures())
		{
			if(pos < (int32_t)creatures->size())
			{
#ifdef __DEBUG_MOVESYS__
				std::clog << "[Failure - Tile::__replaceThing] Update object is a creature" << std::endl;
#endif
				return/* RET_NOTPOSSIBLE*/;
			}

			pos -= (uint32_t)creatures->size();
		}
	}

	if(!oldItem && items)
	{
		int32_t downItemSize = getDownItemCount();
		if(pos < downItemSize)
		{
			ItemVector::iterator it = items->begin();
			it += pos;
			pos = 0;

			oldItem = (*it);
			it = items->erase(it);
			items->insert(it, item);
		}
	}

	if(oldItem)
	{
		item->setParent(this);
		updateTileFlags(oldItem, true);
		updateTileFlags(item, false);

		onUpdateTileItem(oldItem, Item::items[oldItem->getID()], item, Item::items[item->getID()]);
#ifdef __GROUND_CACHE__

		std::map<Item*, int32_t>::iterator it = g_game.grounds.find(oldItem);
		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
			oldItem->setParent(NULL);
			g_game.freeThing(oldItem);
#ifdef __GROUND_CACHE__
		}
#endif

		return/* RET_NOERROR*/;
	}
#ifdef __DEBUG_MOVESYS__

	std::clog << "[Failure - Tile::__replaceThing] Update object not found" << std::endl;
#endif
}