forked from otland/forgottenserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
house.cpp
707 lines (590 loc) · 15.3 KB
/
house.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
// Copyright 2023 The Forgotten Server Authors. All rights reserved.
// Use of this source code is governed by the GPL-2.0 License that can be found in the LICENSE file.
#include "otpch.h"
#include "house.h"
#include "bed.h"
#include "configmanager.h"
#include "game.h"
#include "housetile.h"
#include "inbox.h"
#include "iologindata.h"
#include "pugicast.h"
extern ConfigManager g_config;
extern Game g_game;
House::House(uint32_t houseId) : id(houseId) {}
void House::addTile(HouseTile* tile)
{
tile->setFlag(TILESTATE_PROTECTIONZONE);
houseTiles.push_back(tile);
}
void House::setOwner(uint32_t guid, bool updateDatabase /* = true*/, Player* player /* = nullptr*/)
{
if (updateDatabase && owner != guid) {
Database& db = Database::getInstance();
db.executeQuery(fmt::format(
"UPDATE `houses` SET `owner` = {:d}, `bid` = 0, `bid_end` = 0, `last_bid` = 0, `highest_bidder` = 0 WHERE `id` = {:d}",
guid, id));
}
if (isLoaded && owner == guid) {
return;
}
isLoaded = true;
if (owner != 0) {
// send items to depot
if (player) {
transferToDepot(player);
} else {
transferToDepot();
}
for (HouseTile* tile : houseTiles) {
if (const CreatureVector* creatures = tile->getCreatures()) {
for (int32_t i = creatures->size(); --i >= 0;) {
kickPlayer(nullptr, (*creatures)[i]->getPlayer());
}
}
}
// Remove players from beds
for (BedItem* bed : bedsList) {
if (bed->getSleeper() != 0) {
bed->wakeUp(nullptr);
}
}
// clean access lists
owner = 0;
ownerAccountId = 0;
setAccessList(SUBOWNER_LIST, "");
setAccessList(GUEST_LIST, "");
for (Door* door : doorSet) {
door->setAccessList("");
}
} else {
std::string strRentPeriod =
boost::algorithm::to_lower_copy(g_config.getString(ConfigManager::HOUSE_RENT_PERIOD));
time_t currentTime = time(nullptr);
if (strRentPeriod == "yearly") {
currentTime += 24 * 60 * 60 * 365;
} else if (strRentPeriod == "monthly") {
currentTime += 24 * 60 * 60 * 30;
} else if (strRentPeriod == "weekly") {
currentTime += 24 * 60 * 60 * 7;
} else if (strRentPeriod == "daily") {
currentTime += 24 * 60 * 60;
} else {
currentTime = 0;
}
paidUntil = currentTime;
}
rentWarnings = 0;
if (guid != 0) {
std::string name = IOLoginData::getNameByGuid(guid);
if (!name.empty()) {
owner = guid;
ownerName = name;
ownerAccountId = IOLoginData::getAccountIdByPlayerName(name);
}
}
}
AccessHouseLevel_t House::getHouseAccessLevel(const Player* player) const
{
if (!player) {
return HOUSE_OWNER;
}
if (g_config.getBoolean(ConfigManager::HOUSE_OWNED_BY_ACCOUNT)) {
if (ownerAccountId == player->getAccount()) {
return HOUSE_OWNER;
}
}
if (player->hasFlag(PlayerFlag_CanEditHouses)) {
return HOUSE_OWNER;
}
if (player->getGUID() == owner) {
return HOUSE_OWNER;
}
if (subOwnerList.isInList(player)) {
return HOUSE_SUBOWNER;
}
if (guestList.isInList(player)) {
return HOUSE_GUEST;
}
return HOUSE_NOT_INVITED;
}
bool House::kickPlayer(Player* player, Player* target)
{
if (!target) {
return false;
}
HouseTile* houseTile = dynamic_cast<HouseTile*>(target->getTile());
if (!houseTile || houseTile->getHouse() != this) {
return false;
}
if (getHouseAccessLevel(player) < getHouseAccessLevel(target) || target->hasFlag(PlayerFlag_CanEditHouses)) {
return false;
}
Position oldPosition = target->getPosition();
if (g_game.internalTeleport(target, getEntryPosition()) == RETURNVALUE_NOERROR) {
g_game.addMagicEffect(oldPosition, CONST_ME_POFF);
g_game.addMagicEffect(getEntryPosition(), CONST_ME_TELEPORT);
}
return true;
}
void House::setAccessList(uint32_t listId, std::string_view textlist)
{
if (listId == GUEST_LIST) {
guestList.parseList(textlist);
} else if (listId == SUBOWNER_LIST) {
subOwnerList.parseList(textlist);
} else {
Door* door = getDoorByNumber(listId);
if (door) {
door->setAccessList(textlist);
}
// We do not have to kick anyone
return;
}
// kick uninvited players
for (HouseTile* tile : houseTiles) {
if (CreatureVector* creatures = tile->getCreatures()) {
for (int32_t i = creatures->size(); --i >= 0;) {
Player* player = (*creatures)[i]->getPlayer();
if (player && !isInvited(player)) {
kickPlayer(nullptr, player);
}
}
}
}
}
bool House::transferToDepot() const
{
if (townId == 0 || owner == 0) {
return false;
}
Player* player = g_game.getPlayerByGUID(owner);
if (player) {
transferToDepot(player);
} else {
Player tmpPlayer(nullptr);
if (!IOLoginData::loadPlayerById(&tmpPlayer, owner)) {
return false;
}
transferToDepot(&tmpPlayer);
IOLoginData::savePlayer(&tmpPlayer);
}
return true;
}
bool House::transferToDepot(Player* player) const
{
if (townId == 0 || owner == 0) {
return false;
}
ItemList moveItemList;
for (HouseTile* tile : houseTiles) {
if (const TileItemVector* items = tile->getItemList()) {
for (Item* item : *items) {
if (item->isPickupable()) {
moveItemList.push_back(item);
} else {
Container* container = item->getContainer();
if (container) {
for (Item* containerItem : container->getItemList()) {
moveItemList.push_back(containerItem);
}
}
}
}
}
}
for (Item* item : moveItemList) {
g_game.internalMoveItem(item->getParent(), player->getInbox(), INDEX_WHEREEVER, item, item->getItemCount(),
nullptr, FLAG_NOLIMIT);
}
return true;
}
bool House::getAccessList(uint32_t listId, std::string& list) const
{
if (listId == GUEST_LIST) {
guestList.getList(list);
return true;
} else if (listId == SUBOWNER_LIST) {
subOwnerList.getList(list);
return true;
}
Door* door = getDoorByNumber(listId);
if (!door) {
return false;
}
return door->getAccessList(list);
}
bool House::isInvited(const Player* player) const { return getHouseAccessLevel(player) != HOUSE_NOT_INVITED; }
void House::addDoor(Door* door)
{
door->incrementReferenceCounter();
doorSet.insert(door);
door->setHouse(this);
}
void House::removeDoor(Door* door)
{
auto it = doorSet.find(door);
if (it != doorSet.end()) {
door->decrementReferenceCounter();
doorSet.erase(it);
}
}
void House::addBed(BedItem* bed)
{
bedsList.push_back(bed);
bed->setHouse(this);
}
Door* House::getDoorByNumber(uint32_t doorId) const
{
for (Door* door : doorSet) {
if (door->getDoorId() == doorId) {
return door;
}
}
return nullptr;
}
Door* House::getDoorByPosition(const Position& pos)
{
for (Door* door : doorSet) {
if (door->getPosition() == pos) {
return door;
}
}
return nullptr;
}
bool House::canEditAccessList(uint32_t listId, const Player* player)
{
switch (getHouseAccessLevel(player)) {
case HOUSE_OWNER:
return true;
case HOUSE_SUBOWNER:
return listId == GUEST_LIST;
default:
return false;
}
}
HouseTransferItem* House::getTransferItem()
{
if (transferItem) {
return nullptr;
}
transfer_container.setParent(nullptr);
transferItem = HouseTransferItem::createHouseTransferItem(this);
transfer_container.addThing(transferItem);
return transferItem;
}
void House::resetTransferItem()
{
if (transferItem) {
Item* tmpItem = transferItem;
transferItem = nullptr;
transfer_container.setParent(nullptr);
transfer_container.removeThing(tmpItem, tmpItem->getItemCount());
g_game.ReleaseItem(tmpItem);
}
}
HouseTransferItem* HouseTransferItem::createHouseTransferItem(House* house)
{
HouseTransferItem* transferItem = new HouseTransferItem(house);
transferItem->incrementReferenceCounter();
transferItem->setID(ITEM_DOCUMENT_RO);
transferItem->setSubType(1);
transferItem->setSpecialDescription(fmt::format("It is a house transfer document for '{:s}'.", house->getName()));
return transferItem;
}
void HouseTransferItem::onTradeEvent(TradeEvents_t event, Player* owner)
{
if (event == ON_TRADE_TRANSFER) {
if (house) {
house->executeTransfer(this, owner);
}
g_game.internalRemoveItem(this, 1);
} else if (event == ON_TRADE_CANCEL) {
if (house) {
house->resetTransferItem();
}
}
}
bool House::executeTransfer(HouseTransferItem* item, Player* newOwner)
{
if (transferItem != item) {
return false;
}
setOwner(newOwner->getGUID());
transferItem = nullptr;
return true;
}
void AccessList::parseList(std::string_view list)
{
playerList.clear();
guildRankList.clear();
allowEveryone = false;
this->list = list;
if (list.empty()) {
return;
}
std::istringstream listStream{this->list};
std::string line;
uint16_t lineNo = 1;
while (getline(listStream, line)) {
if (++lineNo > 100) {
break;
}
boost::algorithm::trim(line);
if (line.empty() || line.front() == '#' || line.length() > 100) {
continue;
}
boost::algorithm::to_lower(line);
std::string::size_type at_pos = line.find("@");
if (at_pos != std::string::npos) {
if (at_pos == 0) {
addGuild(line.substr(1));
} else {
addGuildRank(line.substr(0, at_pos - 1), line.substr(at_pos + 1));
}
} else if (line == "*") {
allowEveryone = true;
} else if (line.find("!") != std::string::npos || line.find("*") != std::string::npos ||
line.find("?") != std::string::npos) {
continue; // regexp no longer supported
} else {
addPlayer(line);
}
}
}
void AccessList::addPlayer(const std::string& name)
{
Player* player = g_game.getPlayerByName(name);
if (player) {
playerList.insert(player->getGUID());
} else {
uint32_t guid = IOLoginData::getGuidByName(name);
if (guid != 0) {
playerList.insert(guid);
}
}
}
namespace {
const Guild* getGuildByName(const std::string& name)
{
uint32_t guildId = IOGuild::getGuildIdByName(name);
if (guildId == 0) {
return nullptr;
}
const Guild* guild = g_game.getGuild(guildId);
if (guild) {
return guild;
}
return IOGuild::loadGuild(guildId);
}
} // namespace
void AccessList::addGuild(const std::string& name)
{
const Guild* guild = getGuildByName(name);
if (guild) {
for (auto rank : guild->getRanks()) {
guildRankList.insert(rank->id);
}
}
}
void AccessList::addGuildRank(const std::string& name, const std::string& rankName)
{
const Guild* guild = getGuildByName(name);
if (guild) {
GuildRank_ptr rank = guild->getRankByName(rankName);
if (rank) {
guildRankList.insert(rank->id);
}
}
}
bool AccessList::isInList(const Player* player) const
{
if (allowEveryone) {
return true;
}
auto playerIt = playerList.find(player->getGUID());
if (playerIt != playerList.end()) {
return true;
}
GuildRank_ptr rank = player->getGuildRank();
return rank && guildRankList.find(rank->id) != guildRankList.end();
}
void AccessList::getList(std::string& list) const { list = this->list; }
Door::Door(uint16_t type) : Item(type) {}
Attr_ReadValue Door::readAttr(AttrTypes_t attr, PropStream& propStream)
{
if (attr == ATTR_HOUSEDOORID) {
uint8_t doorId;
if (!propStream.read<uint8_t>(doorId)) {
return ATTR_READ_ERROR;
}
setDoorId(doorId);
return ATTR_READ_CONTINUE;
}
return Item::readAttr(attr, propStream);
}
void Door::setHouse(House* house)
{
if (this->house) {
return;
}
this->house = house;
if (!accessList) {
accessList.reset(new AccessList());
}
}
bool Door::canUse(const Player* player)
{
if (!house) {
return true;
}
if (house->getHouseAccessLevel(player) >= HOUSE_SUBOWNER) {
return true;
}
return accessList->isInList(player);
}
void Door::setAccessList(std::string_view textlist)
{
if (!accessList) {
accessList.reset(new AccessList());
}
accessList->parseList(textlist);
}
bool Door::getAccessList(std::string& list) const
{
if (!house) {
return false;
}
accessList->getList(list);
return true;
}
void Door::onRemoved()
{
Item::onRemoved();
if (house) {
house->removeDoor(this);
}
}
House* Houses::getHouseByPlayerId(uint32_t playerId)
{
for (const auto& it : houseMap) {
if (it.second->getOwner() == playerId) {
return it.second;
}
}
return nullptr;
}
bool Houses::loadHousesXML(const std::string& filename)
{
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(filename.c_str());
if (!result) {
printXMLError("Error - Houses::loadHousesXML", filename, result);
return false;
}
for (auto houseNode : doc.child("houses").children()) {
pugi::xml_attribute houseIdAttribute = houseNode.attribute("houseid");
if (!houseIdAttribute) {
return false;
}
int32_t houseId = pugi::cast<int32_t>(houseIdAttribute.value());
House* house = getHouse(houseId);
if (!house) {
std::cout << "Error: [Houses::loadHousesXML] Unknown house, id = " << houseId << std::endl;
return false;
}
house->setName(houseNode.attribute("name").as_string());
Position entryPos(pugi::cast<uint16_t>(houseNode.attribute("entryx").value()),
pugi::cast<uint16_t>(houseNode.attribute("entryy").value()),
pugi::cast<uint16_t>(houseNode.attribute("entryz").value()));
if (entryPos.x == 0 && entryPos.y == 0 && entryPos.z == 0) {
std::cout << "[Warning - Houses::loadHousesXML] House entry not set"
<< " - Name: " << house->getName() << " - House id: " << houseId << std::endl;
}
house->setEntryPos(entryPos);
house->setRent(pugi::cast<uint32_t>(houseNode.attribute("rent").value()));
house->setTownId(pugi::cast<uint32_t>(houseNode.attribute("townid").value()));
house->setOwner(0, false);
}
return true;
}
void Houses::payHouses(RentPeriod_t rentPeriod) const
{
if (rentPeriod == RENTPERIOD_NEVER) {
return;
}
time_t currentTime = time(nullptr);
for (const auto& it : houseMap) {
House* house = it.second;
if (house->getOwner() == 0) {
continue;
}
const uint32_t rent = house->getRent();
if (rent == 0 || house->getPaidUntil() > currentTime) {
continue;
}
const uint32_t ownerId = house->getOwner();
Town* town = g_game.map.towns.getTown(house->getTownId());
if (!town) {
continue;
}
Player player(nullptr);
if (!IOLoginData::loadPlayerById(&player, ownerId)) {
// Player doesn't exist, reset house owner
house->setOwner(0);
continue;
}
if (player.getBankBalance() >= rent) {
player.setBankBalance(player.getBankBalance() - rent);
time_t paidUntil = currentTime;
switch (rentPeriod) {
case RENTPERIOD_DAILY:
paidUntil += 24 * 60 * 60;
break;
case RENTPERIOD_WEEKLY:
paidUntil += 24 * 60 * 60 * 7;
break;
case RENTPERIOD_MONTHLY:
paidUntil += 24 * 60 * 60 * 30;
break;
case RENTPERIOD_YEARLY:
paidUntil += 24 * 60 * 60 * 365;
break;
default:
break;
}
house->setPaidUntil(paidUntil);
house->setPayRentWarnings(0);
} else {
if (house->getPayRentWarnings() < 7) {
int32_t daysLeft = 7 - house->getPayRentWarnings();
Item* letter = Item::CreateItem(ITEM_LETTER_STAMPED);
std::string period;
switch (rentPeriod) {
case RENTPERIOD_DAILY:
period = "daily";
break;
case RENTPERIOD_WEEKLY:
period = "weekly";
break;
case RENTPERIOD_MONTHLY:
period = "monthly";
break;
case RENTPERIOD_YEARLY:
period = "annual";
break;
default:
break;
}
letter->setText(fmt::format(
"Warning! \nThe {:s} rent of {:d} gold for your house \"{:s}\" is payable. Have it within {:d} days or you will lose this house.",
period, house->getRent(), house->getName(), daysLeft));
g_game.internalAddItem(player.getInbox(), letter, INDEX_WHEREEVER, FLAG_NOLIMIT);
house->setPayRentWarnings(house->getPayRentWarnings() + 1);
} else {
house->setOwner(0, true, &player);
}
}
IOLoginData::savePlayer(&player);
}
}