-
Notifications
You must be signed in to change notification settings - Fork 21
/
beds.cpp
315 lines (264 loc) · 7.27 KB
/
beds.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
//////////////////////////////////////////////////////////////////////
// OpenTibia - an opensource roleplaying game
//////////////////////////////////////////////////////////////////////
// Beds
//////////////////////////////////////////////////////////////////////
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//////////////////////////////////////////////////////////////////////
#include "otpch.h"
#include "beds.h"
#include "house.h"
#include "iologindata.h"
#include "game.h"
#include "player.h"
extern Game g_game;
BedItem::BedItem(uint16_t _id) : Item(_id)
{
house = NULL;
internalRemoveSleeper();
}
Attr_ReadValue BedItem::readAttr(AttrTypes_t attr, PropStream& propStream)
{
switch(attr)
{
case ATTR_SLEEPERGUID:
{
uint32_t _guid;
if(!propStream.GET_ULONG(_guid))
return ATTR_READ_ERROR;
if(_guid != 0)
{
std::string name;
if(IOLoginData::getInstance()->getNameByGuid(_guid, name))
{
setSpecialDescription(name + " is sleeping there.");
Beds::getInstance().setBedSleeper(this, _guid);
}
}
sleeperGUID = _guid;
return ATTR_READ_CONTINUE;
}
case ATTR_SLEEPSTART:
{
uint32_t sleep_start;
if(!propStream.GET_ULONG(sleep_start))
return ATTR_READ_ERROR;
sleepStart = (uint64_t)sleep_start;
return ATTR_READ_CONTINUE;
}
default:
break;
}
return Item::readAttr(attr, propStream);
}
bool BedItem::serializeAttr(PropWriteStream& propWriteStream) const
{
if(sleeperGUID != 0)
{
propWriteStream.ADD_UCHAR(ATTR_SLEEPERGUID);
propWriteStream.ADD_ULONG(sleeperGUID);
}
if(sleepStart != 0)
{
propWriteStream.ADD_UCHAR(ATTR_SLEEPSTART);
propWriteStream.ADD_ULONG((int32_t)sleepStart);
}
return true;
}
BedItem* BedItem::getNextBedItem()
{
Direction dir = Item::items[getID()].bedPartnerDir;
Position targetPos = getNextPosition(dir, getPosition());
Tile* tile = g_game.getMap()->getTile(targetPos);
if(tile)
return tile->getBedItem();
return NULL;
}
bool BedItem::canUse(Player* player)
{
if(!player || !house || !player->isPremium())
return false;
if(sleeperGUID == 0)
return isBed();
if(house->getHouseAccessLevel(player) != HOUSE_OWNER)
{
std::string name;
if(IOLoginData::getInstance()->getNameByGuid(sleeperGUID, name))
{
Player* sleeper = new Player(name, NULL);
if(IOLoginData::getInstance()->loadPlayer(sleeper, name))
{
if(house->getHouseAccessLevel(sleeper) <= house->getHouseAccessLevel(player))
{
delete sleeper;
sleeper = NULL;
return isBed();
}
}
delete sleeper;
sleeper = NULL;
}
return false;
}
return true;
}
bool BedItem::trySleep(Player* player)
{
if(!house || !player || player->isRemoved())
return false;
if(sleeperGUID != 0)
{
if(Item::items[getID()].transformToFree != 0 && house->getHouseOwner() == player->getGUID())
wakeUp(NULL);
g_game.addMagicEffect(player->getPosition(), NM_ME_POFF);
return false;
}
return true;
}
bool BedItem::sleep(Player* player)
{
if(!house || !player || player->isRemoved())
return false;
if(sleeperGUID != 0)
return false;
BedItem* nextBedItem = getNextBedItem();
internalSetSleeper(player);
if(nextBedItem)
nextBedItem->internalSetSleeper(player);
// update the BedSleepersMap
Beds::getInstance().setBedSleeper(this, player->getGUID());
// make the player walk onto the bed
player->getTile()->moveCreature(player, getTile());
// display 'Zzzz'/sleep effect
g_game.addMagicEffect(player->getPosition(), NM_ME_SLEEP);
// kick player after he sees himself walk onto the bed and it change id
uint32_t playerId = player->getID();
g_scheduler.addEvent(createSchedulerTask(SCHEDULER_MINTICKS, boost::bind(&Game::kickPlayer, &g_game, playerId, false)));
// change self and partner's appearance
updateAppearance(player);
if(nextBedItem)
nextBedItem->updateAppearance(player);
return true;
}
void BedItem::wakeUp(Player* player)
{
// avoid crashes
if(!house)
return;
if(sleeperGUID != 0)
{
if(!player)
{
std::string name;
if(IOLoginData::getInstance()->getNameByGuid(sleeperGUID, name))
{
Player* _player = new Player(name, NULL);
if(IOLoginData::getInstance()->loadPlayer(_player, name))
{
regeneratePlayer(_player);
IOLoginData::getInstance()->savePlayer(_player);
}
delete _player;
_player = NULL;
}
}
else
{
regeneratePlayer(player);
g_game.addCreatureHealth(player);
}
}
// update the BedSleepersMap
Beds::getInstance().setBedSleeper(NULL, sleeperGUID);
BedItem* nextBedItem = getNextBedItem();
// unset sleep info
internalRemoveSleeper();
if(nextBedItem)
nextBedItem->internalRemoveSleeper();
// change self and partner's appearance
updateAppearance(NULL);
if(nextBedItem)
nextBedItem->updateAppearance(NULL);
}
void BedItem::regeneratePlayer(Player* player) const
{
int32_t sleptTime = int32_t(time(NULL) - sleepStart);
Condition* condition = player->getCondition(CONDITION_REGENERATION, CONDITIONID_DEFAULT);
if(condition)
{
int32_t regen = 0;
if(condition->getTicks() != -1)
{
regen = std::min((condition->getTicks() / 1000), sleptTime) / 30;
int32_t newRegenTicks = condition->getTicks() - (regen * 30000);
if(newRegenTicks <= 0)
{
player->removeCondition(condition);
condition = NULL;
}
else
condition->setTicks(newRegenTicks);
}
else
regen = sleptTime / 30;
player->changeHealth(regen, false);
player->changeMana(regen);
}
int32_t soulRegen = sleptTime / (60 * 15);
player->changeSoul(soulRegen);
}
void BedItem::updateAppearance(const Player* player)
{
const ItemType& it = Item::items[getID()];
if(it.type == ITEM_TYPE_BED)
{
if(player && it.transformToOnUse[player->getSex()] != 0)
{
const ItemType& newType = Item::items[it.transformToOnUse[player->getSex()]];
if(newType.type == ITEM_TYPE_BED)
g_game.transformItem(this, it.transformToOnUse[player->getSex()]);
}
else if(it.transformToFree != 0)
{
const ItemType& newType = Item::items[it.transformToFree];
if(newType.type == ITEM_TYPE_BED)
g_game.transformItem(this, it.transformToFree);
}
}
}
void BedItem::internalSetSleeper(const Player* player)
{
std::string desc_str = player->getName() + " is sleeping there.";
setSleeper(player->getGUID());
setSleepStart(time(NULL));
setSpecialDescription(desc_str);
}
void BedItem::internalRemoveSleeper()
{
setSleeper(0);
setSleepStart(0);
setSpecialDescription("Nobody is sleeping there.");
}
BedItem* Beds::getBedBySleeper(uint32_t guid)
{
std::map<uint32_t, BedItem*>::iterator it = BedSleepersMap.find(guid);
if(it != BedSleepersMap.end())
return it->second;
return NULL;
}
void Beds::setBedSleeper(BedItem* bed, uint32_t guid)
{
BedSleepersMap[guid] = bed;
}