forked from OTAcademy/RME
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathitem.cpp
More file actions
556 lines (495 loc) · 11.7 KB
/
item.cpp
File metadata and controls
556 lines (495 loc) · 11.7 KB
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
//////////////////////////////////////////////////////////////////////
// This file is part of Remere's Map Editor
//////////////////////////////////////////////////////////////////////
// Remere's Map Editor 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 3 of the License, or
// (at your option) any later version.
//
// Remere's Map Editor 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, see <http://www.gnu.org/licenses/>.
//////////////////////////////////////////////////////////////////////
#include "main.h"
#include "brush.h"
#include "graphics.h"
#include "gui.h"
#include "tile.h"
#include "complexitem.h"
#include "iomap.h"
#include "item.h"
#include "ground_brush.h"
#include "carpet_brush.h"
#include "table_brush.h"
#include "wall_brush.h"
Item* Item::Create(uint16_t _type, uint16_t _subtype /*= 0xFFFF*/) {
if (_type == 0) {
return nullptr;
}
Item* newItem = nullptr;
const ItemType& it = g_items[_type];
if (it.id != 0) {
if (it.isDepot()) {
newItem = newd Depot(_type);
} else if (it.isContainer()) {
newItem = newd Container(_type);
} else if (it.isTeleport()) {
newItem = newd Teleport(_type);
} else if (it.isDoor()) {
newItem = newd Door(_type);
} else if (it.isPodium()) {
newItem = newd Podium(_type);
} else if (_subtype == 0xFFFF) {
if (it.isFluidContainer()) {
newItem = newd Item(_type, LIQUID_NONE);
} else if (it.isSplash()) {
newItem = newd Item(_type, LIQUID_WATER);
} else if (it.charges > 0) {
newItem = newd Item(_type, it.charges);
} else {
newItem = newd Item(_type, 1);
}
} else {
newItem = newd Item(_type, _subtype);
}
} else {
newItem = newd Item(_type, _subtype);
}
return newItem;
}
Item::Item(unsigned short _type, unsigned short _count) :
id(_type),
subtype(1),
selected(false),
frame(0) {
if (hasSubtype()) {
subtype = _count;
}
}
Item::~Item() {
////
}
Item* Item::deepCopy() const {
Item* copy = Create(id, subtype);
if (copy) {
copy->selected = selected;
if (attributes) {
copy->attributes = newd ItemAttributeMap(*attributes);
}
}
return copy;
}
Item* transformItem(Item* old_item, uint16_t new_id, Tile* parent) {
if (old_item == nullptr) {
return nullptr;
}
old_item->setID(new_id);
// Through the magic of deepCopy, this will now be a pointer to an item of the correct type.
Item* new_item = old_item->deepCopy();
if (parent) {
// Find the old item and remove it from the tile, insert this one instead!
if (old_item == parent->ground) {
delete old_item;
parent->ground = new_item;
return new_item;
}
std::queue<Container*> containers;
for (ItemVector::iterator item_iter = parent->items.begin(); item_iter != parent->items.end(); ++item_iter) {
if (*item_iter == old_item) {
delete old_item;
item_iter = parent->items.erase(item_iter);
parent->items.insert(item_iter, new_item);
return new_item;
}
Container* c = dynamic_cast<Container*>(*item_iter);
if (c) {
containers.push(c);
}
}
while (containers.size() != 0) {
Container* container = containers.front();
ItemVector& v = container->getVector();
for (ItemVector::iterator item_iter = v.begin(); item_iter != v.end(); ++item_iter) {
Item* i = *item_iter;
Container* c = dynamic_cast<Container*>(i);
if (c) {
containers.push(c);
}
if (i == old_item) {
// Found it!
item_iter = v.erase(item_iter);
v.insert(item_iter, new_item);
return new_item;
}
}
containers.pop();
}
}
delete new_item;
return nullptr;
}
uint32_t Item::memsize() const {
uint32_t mem = sizeof(*this);
return mem;
}
void Item::setID(uint16_t newid) {
id = newid;
}
void Item::setSubtype(uint16_t n) {
subtype = n;
}
bool Item::hasSubtype() const {
const ItemType& it = g_items[id];
return (it.isFluidContainer() || it.isSplash() || isCharged() || it.stackable || it.charges != 0);
}
uint16_t Item::getSubtype() const {
if (hasSubtype()) {
return subtype;
}
return 0;
}
bool Item::hasProperty(enum ITEMPROPERTY prop) const {
const ItemType& it = g_items[id];
switch (prop) {
case BLOCKSOLID:
if (it.unpassable) {
return true;
}
break;
case MOVEABLE:
if (it.moveable && getUniqueID() == 0) {
return true;
}
break;
/*
case HASHEIGHT:
if(it.height != 0 )
return true;
break;
*/
case BLOCKPROJECTILE:
if (it.blockMissiles) {
return true;
}
break;
case BLOCKPATHFIND:
if (it.blockPathfinder) {
return true;
}
break;
case HOOK_SOUTH:
if (it.hookSouth) {
return true;
}
break;
case HOOK_EAST:
if (it.hookEast) {
return true;
}
break;
case BLOCKINGANDNOTMOVEABLE:
if (it.unpassable && (!it.moveable || getUniqueID() != 0)) {
return true;
}
break;
default:
return false;
}
return false;
}
std::pair<int, int> Item::getDrawOffset() const {
ItemType& it = g_items[id];
if (it.sprite != nullptr) {
return it.sprite->getDrawOffset();
}
return std::make_pair(0, 0);
}
bool Item::hasLight() const {
const ItemType& type = g_items.getItemType(id);
if (type.sprite) {
return type.sprite->hasLight();
}
return false;
}
SpriteLight Item::getLight() const {
const ItemType& type = g_items.getItemType(id);
if (type.sprite) {
return type.sprite->getLight();
}
return SpriteLight { 0, 0 };
}
double Item::getWeight() const {
ItemType& it = g_items[id];
if (it.stackable) {
return it.weight * std::max(1, (int)subtype);
}
return it.weight;
}
void Item::setUniqueID(unsigned short n) {
setAttribute("uid", n);
}
void Item::setActionID(unsigned short n) {
setAttribute("aid", n);
}
void Item::setText(const std::string& str) {
setAttribute("text", str);
}
void Item::setDescription(const std::string& str) {
setAttribute("desc", str);
}
void Item::setTier(unsigned short n) {
setAttribute("tier", n);
}
double Item::getWeight() {
ItemType& it = g_items[id];
if (it.isStackable()) {
return it.weight * subtype;
}
return it.weight;
}
bool Item::canHoldText() const {
return isReadable() || canWriteText();
}
bool Item::canHoldDescription() const {
return g_items[id].allowDistRead;
}
uint8_t Item::getMiniMapColor() const {
GameSprite* spr = g_items[id].sprite;
if (spr) {
return spr->getMiniMapColor();
}
return 0;
}
GroundBrush* Item::getGroundBrush() const {
ItemType& item_type = g_items.getItemType(id);
if (item_type.isGroundTile() && item_type.brush && item_type.brush->isGround()) {
return item_type.brush->asGround();
}
return nullptr;
}
TableBrush* Item::getTableBrush() const {
ItemType& item_type = g_items.getItemType(id);
if (item_type.isTable && item_type.brush && item_type.brush->isTable()) {
return item_type.brush->asTable();
}
return nullptr;
}
CarpetBrush* Item::getCarpetBrush() const {
ItemType& item_type = g_items.getItemType(id);
if (item_type.isCarpet && item_type.brush && item_type.brush->isCarpet()) {
return item_type.brush->asCarpet();
}
return nullptr;
}
DoorBrush* Item::getDoorBrush() const {
ItemType& item_type = g_items.getItemType(id);
if (!item_type.isWall || !item_type.isBrushDoor || !item_type.brush || !item_type.brush->isWall()) {
return nullptr;
}
DoorType door_type = item_type.brush->asWall()->getDoorTypeFromID(id);
DoorBrush* door_brush = nullptr;
// Quite a horrible dependency on a global here, meh.
switch (door_type) {
case WALL_DOOR_NORMAL: {
door_brush = g_gui.normal_door_brush;
break;
}
case WALL_DOOR_LOCKED: {
door_brush = g_gui.locked_door_brush;
break;
}
case WALL_DOOR_QUEST: {
door_brush = g_gui.quest_door_brush;
break;
}
case WALL_DOOR_MAGIC: {
door_brush = g_gui.magic_door_brush;
break;
}
case WALL_DOOR_NORMAL_ALT: {
door_brush = g_gui.normal_door_alt_brush;
break;
}
case WALL_ARCHWAY: {
door_brush = g_gui.archway_door_brush;
break;
}
case WALL_WINDOW: {
door_brush = g_gui.window_door_brush;
break;
}
case WALL_HATCH_WINDOW: {
door_brush = g_gui.hatch_door_brush;
break;
}
default: {
break;
}
}
return door_brush;
}
WallBrush* Item::getWallBrush() const {
ItemType& item_type = g_items.getItemType(id);
if (item_type.isWall && item_type.brush && item_type.brush->isWall()) {
return item_type.brush->asWall();
}
return nullptr;
}
BorderType Item::getWallAlignment() const {
ItemType& it = g_items[id];
if (!it.isWall) {
return BORDER_NONE;
}
return it.border_alignment;
}
BorderType Item::getBorderAlignment() const {
ItemType& it = g_items[id];
return it.border_alignment;
}
void Item::animate() {
ItemType& type = g_items[id];
GameSprite* sprite = type.sprite;
if (!sprite || !sprite->animator) {
return;
}
frame = sprite->animator->getFrame();
}
// ============================================================================
// Static conversions
std::string Item::LiquidID2Name(uint16_t id) {
switch (id) {
case LIQUID_NONE:
return "None";
case LIQUID_WATER:
return "Water";
case LIQUID_BLOOD:
return "Blood";
case LIQUID_BEER:
return "Beer";
case LIQUID_SLIME:
return "Slime";
case LIQUID_LEMONADE:
return "Lemonade";
case LIQUID_MILK:
return "Milk";
case LIQUID_MANAFLUID:
return "Manafluid";
case LIQUID_WATER2:
return "Water";
case LIQUID_LIFEFLUID:
return "Lifefluid";
case LIQUID_OIL:
return "Oil";
case LIQUID_SLIME2:
return "Slime";
case LIQUID_URINE:
return "Urine";
case LIQUID_COCONUT_MILK:
return "Coconut Milk";
case LIQUID_WINE:
return "Wine";
case LIQUID_MUD:
return "Mud";
case LIQUID_FRUIT_JUICE:
return "Fruit Juice";
case LIQUID_LAVA:
return "Lava";
case LIQUID_RUM:
return "Rum";
case LIQUID_SWAMP:
return "Swamp";
case LIQUID_INK:
return "Ink";
case LIQUID_TEA:
return "Tea";
case LIQUID_MEAD:
return "Mead";
default:
return "Unknown";
}
}
uint16_t Item::LiquidName2ID(std::string liquid) {
to_lower_str(liquid);
if (liquid == "none") {
return LIQUID_NONE;
}
if (liquid == "water") {
return LIQUID_WATER;
}
if (liquid == "blood") {
return LIQUID_BLOOD;
}
if (liquid == "beer") {
return LIQUID_BEER;
}
if (liquid == "slime") {
return LIQUID_SLIME;
}
if (liquid == "lemonade") {
return LIQUID_LEMONADE;
}
if (liquid == "milk") {
return LIQUID_MILK;
}
if (liquid == "manafluid") {
return LIQUID_MANAFLUID;
}
if (liquid == "lifefluid") {
return LIQUID_LIFEFLUID;
}
if (liquid == "oil") {
return LIQUID_OIL;
}
if (liquid == "urine") {
return LIQUID_URINE;
}
if (liquid == "coconut milk") {
return LIQUID_COCONUT_MILK;
}
if (liquid == "wine") {
return LIQUID_WINE;
}
if (liquid == "mud") {
return LIQUID_MUD;
}
if (liquid == "fruit juice") {
return LIQUID_FRUIT_JUICE;
}
if (liquid == "lava") {
return LIQUID_LAVA;
}
if (liquid == "rum") {
return LIQUID_RUM;
}
if (liquid == "swamp") {
return LIQUID_SWAMP;
}
if (liquid == "ink") {
return LIQUID_INK;
}
if (liquid == "tea") {
return LIQUID_TEA;
}
if (liquid == "mead") {
return LIQUID_MEAD;
}
return LIQUID_NONE;
}
// ============================================================================
// XML Saving & loading
Item* Item::Create(pugi::xml_node xml) {
pugi::xml_attribute attribute;
int16_t id = 0;
if ((attribute = xml.attribute("id"))) {
id = attribute.as_ushort();
}
int16_t count = 1;
if ((attribute = xml.attribute("count")) || (attribute = xml.attribute("subtype"))) {
count = attribute.as_ushort();
}
return Create(id, count);
}