1/**
2 * The Forgotten Server - a free and open-source MMORPG server emulator
3 * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "otpch.h"
21
22#include "teleport.h"
23#include "game.h"
24
25extern Game g_game;
26
27Attr_ReadValue Teleport::readAttr(AttrTypes_t attr, PropStream& propStream)
28{
29 if (attr == ATTR_TELE_DEST) {
30 if (!propStream.read<uint16_t>(destPos.x) || !propStream.read<uint16_t>(destPos.y) || !propStream.read<uint8_t>(destPos.z)) {
31 return ATTR_READ_ERROR;
32 }
33 return ATTR_READ_CONTINUE;
34 }
35 return Item::readAttr(attr, propStream);
36}
37
38void Teleport::serializeAttr(PropWriteStream& propWriteStream) const
39{
40 Item::serializeAttr(propWriteStream);
41
42 propWriteStream.write<uint8_t>(ATTR_TELE_DEST);
43 propWriteStream.write<uint16_t>(destPos.x);
44 propWriteStream.write<uint16_t>(destPos.y);
45 propWriteStream.write<uint8_t>(destPos.z);
46}
47
48ReturnValue Teleport::queryAdd(int32_t, const Thing&, uint32_t, uint32_t, Creature*) const
49{
50 return RETURNVALUE_NOTPOSSIBLE;
51}
52
53ReturnValue Teleport::queryMaxCount(int32_t, const Thing&, uint32_t, uint32_t&, uint32_t) const
54{
55 return RETURNVALUE_NOTPOSSIBLE;
56}
57
58ReturnValue Teleport::queryRemove(const Thing&, uint32_t, uint32_t) const
59{
60 return RETURNVALUE_NOERROR;
61}
62
63Cylinder* Teleport::queryDestination(int32_t&, const Thing&, Item**, uint32_t&)
64{
65 return this;
66}
67
68void Teleport::addThing(Thing* thing)
69{
70 return addThing(0, thing);
71}
72
73bool Teleport::checkInfinityLoop(Tile* destTile)
74{
75 if (!destTile) {
76 return false;
77 }
78
79 if (Teleport* teleport = destTile->getTeleportItem()) {
80 const Position& nextDestPos = teleport->getDestPos();
81 if (getPosition() == nextDestPos) {
82 return true;
83 }
84 return checkInfinityLoop(g_game.map.getTile(nextDestPos));
85 }
86 return false;
87}
88
89void Teleport::addThing(int32_t, Thing* thing)
90{
91 Tile* destTile = g_game.map.getTile(destPos);
92 if (!destTile) {
93 return;
94 }
95
96 // Prevent infinity loop
97 if (checkInfinityLoop(destTile)) {
98 const Position& pos = getPosition();
99 std::cout << "Warning: infinity loop teleport. " << pos << std::endl;
100 return;
101 }
102
103 const MagicEffectClasses effect = Item::items[id].magicEffect;
104
105 if (Creature* creature = thing->getCreature()) {
106 Position origPos = creature->getPosition();
107 g_game.internalCreatureTurn(creature, origPos.x > destPos.x ? DIRECTION_WEST : DIRECTION_EAST);
108 g_game.map.moveCreature(*creature, *destTile);
109 if (effect != CONST_ME_NONE) {
110 g_game.addMagicEffect(origPos, effect);
111 g_game.addMagicEffect(destTile->getPosition(), effect);
112 }
113 } else if (Item* item = thing->getItem()) {
114 if (effect != CONST_ME_NONE) {
115 g_game.addMagicEffect(destTile->getPosition(), effect);
116 g_game.addMagicEffect(item->getPosition(), effect);
117 }
118 g_game.internalMoveItem(getTile(), destTile, INDEX_WHEREEVER, item, item->getItemCount(), nullptr, FLAG_NOLIMIT);
119 }
120}
121
122void Teleport::updateThing(Thing*, uint16_t, uint32_t)
123{
124 //
125}
126
127void Teleport::replaceThing(uint32_t, Thing*)
128{
129 //
130}
131
132void Teleport::removeThing(Thing*, uint32_t)
133{
134 //
135}
136
137void Teleport::postAddNotification(Thing* thing, const Cylinder* oldParent, int32_t index, cylinderlink_t)
138{
139 getParent()->postAddNotification(thing, oldParent, index, LINK_PARENT);
140}
141
142void Teleport::postRemoveNotification(Thing* thing, const Cylinder* newParent, int32_t index, cylinderlink_t)
143{
144 getParent()->postRemoveNotification(thing, newParent, index, LINK_PARENT);
145}
146