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 "housetile.h"
23#include "house.h"
24#include "game.h"
25
26extern Game g_game;
27
28HouseTile::HouseTile(int32_t x, int32_t y, int32_t z, House* house) :
29 DynamicTile(x, y, z), house(house) {}
30
31void HouseTile::addThing(int32_t index, Thing* thing)
32{
33 Tile::addThing(index, thing);
34
35 if (!thing->getParent()) {
36 return;
37 }
38
39 if (Item* item = thing->getItem()) {
40 updateHouse(item);
41 }
42}
43
44void HouseTile::internalAddThing(uint32_t index, Thing* thing)
45{
46 Tile::internalAddThing(index, thing);
47
48 if (!thing->getParent()) {
49 return;
50 }
51
52 if (Item* item = thing->getItem()) {
53 updateHouse(item);
54 }
55}
56
57void HouseTile::updateHouse(Item* item)
58{
59 if (item->getParent() != this) {
60 return;
61 }
62
63 Door* door = item->getDoor();
64 if (door) {
65 if (door->getDoorId() != 0) {
66 house->addDoor(door);
67 }
68 } else {
69 BedItem* bed = item->getBed();
70 if (bed) {
71 house->addBed(bed);
72 }
73 }
74}
75
76ReturnValue HouseTile::queryAdd(int32_t index, const Thing& thing, uint32_t count, uint32_t flags, Creature* actor/* = nullptr*/) const
77{
78 if (const Creature* creature = thing.getCreature()) {
79 if (const Player* player = creature->getPlayer()) {
80 if (!house->isInvited(player)) {
81 return RETURNVALUE_PLAYERISNOTINVITED;
82 }
83 } else {
84 return RETURNVALUE_NOTPOSSIBLE;
85 }
86 } else if (thing.getItem() && actor) {
87 Player* actorPlayer = actor->getPlayer();
88 if (!house->isInvited(actorPlayer)) {
89 return RETURNVALUE_CANNOTTHROW;
90 }
91 }
92 return Tile::queryAdd(index, thing, count, flags, actor);
93}
94
95Tile* HouseTile::queryDestination(int32_t& index, const Thing& thing, Item** destItem, uint32_t& flags)
96{
97 if (const Creature* creature = thing.getCreature()) {
98 if (const Player* player = creature->getPlayer()) {
99 if (!house->isInvited(player)) {
100 const Position& entryPos = house->getEntryPosition();
101 Tile* destTile = g_game.map.getTile(entryPos);
102 if (!destTile) {
103 std::cout << "Error: [HouseTile::queryDestination] House entry not correct"
104 << " - Name: " << house->getName()
105 << " - House id: " << house->getId()
106 << " - Tile not found: " << entryPos << std::endl;
107
108 destTile = g_game.map.getTile(player->getTemplePosition());
109 if (!destTile) {
110 destTile = &(Tile::nullptr_tile);
111 }
112 }
113
114 index = -1;
115 *destItem = nullptr;
116 return destTile;
117 }
118 }
119 }
120
121 return Tile::queryDestination(index, thing, destItem, flags);
122}
123