forked from Whales/Cataclysm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.cpp
630 lines (584 loc) · 18.9 KB
/
player.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
#include "player.h"
#include "cuss.h"
#include "game.h"
#include "files.h" // For CUSS_DIR
#include "rng.h"
#include <sstream>
void populate_item_lists(Player* p, int offset_size,
std::vector<int> item_indices[ITEM_CLASS_MAX],
std::vector<char> item_letters[ITEM_CLASS_MAX],
std::vector<bool> include_item,
std::vector<std::string> &item_name,
std::vector<std::string> &item_weight,
std::vector<std::string> &item_volume);
Player::Player()
{
pos.x = 15;
pos.y = 15;
action_points = 100;
name = "Whales";
for (int i = 0; i < HP_PART_MAX; i++) {
current_hp[i] = 100;
max_hp[i] = 100;
}
}
Player::~Player()
{
}
std::string Player::get_name()
{
return name;
}
std::string Player::get_name_to_player()
{
return "you";
}
std::string Player::get_possessive()
{
return "your";
}
glyph Player::get_glyph()
{
// TODO: Better
return glyph('@', c_white, c_black);
}
int Player::get_genus_uid()
{
return -3;
}
bool Player::has_sense(Sense_type sense)
{
// TODO: Turn off senses if we're blinded, deafened, etc.
switch (sense) {
case SENSE_SIGHT:
case SENSE_SOUND:
return true;
default:
return false;
}
return false;
}
// This function handles messages and yes/no prompts, too!
bool Player::add_item(Item item)
{
// TODO: Weight isn't a hard limit
if (current_weight() + item.get_weight() > maximum_weight()) {
GAME.add_msg("You cannot carry that much weight.");
return false;
}
// TODO: Prompt player to wear/wield/etc the item
if (current_volume() + item.get_volume() > maximum_volume()) {
if (item.get_item_class() == ITEM_CLASS_CLOTHING) {
if (query_yn("No room for that %s. Put on now?",
item.get_name().c_str())) {
inventory.push_back(item);
wear_item_uid(item.get_uid());
} else {
return false;
}
} else {
// If it's not clothing, our only option is to wield it...
std::stringstream wield_prompt;
bool sheath_weap = false;
if (weapon.is_real()) {
if (current_volume() + weapon.get_volume() <= maximum_volume()) {
sheath_weap = true;
}
wield_prompt << (sheath_weap ? "Put away " : "Drop ") << "your " <<
weapon.get_name() << " and w";
} else {
wield_prompt << "W";
}
wield_prompt << "ield that " << item.get_name() << "?";
if (query_yn(wield_prompt.str().c_str())) {
if (sheath_weap) {
GAME.add_msg( sheath_weapon_message() );
sheath_weapon();
} else if (weapon.is_real()) {
GAME.add_msg( drop_item_message(weapon) );
GAME.map->add_item( weapon, pos );
remove_item_uid( weapon.get_uid() );
}
inventory.push_back(item);
GAME.add_msg( wield_item_message(item) );
wield_item_uid( item.get_uid() );
} else {
return false;
}
} // End of non-clothing handling block
} // End of "too much volume" block
GAME.add_msg("You pick up %s.", item.get_name_indefinite().c_str());
if (item.combines()) {
Item* added = ref_item_of_type(item.type);
if (added) {
return (*added).combine_with(item);
}
}
inventory.push_back(item);
return true;
}
int Player::current_weight()
{
int ret = 0;
ret += weapon.get_weight();
for (int i = 0; i < inventory.size(); i++) {
ret += inventory[i].get_weight();
}
for (int i = 0; i < items_worn.size(); i++) {
ret += items_worn[i].get_weight();
}
return ret;
}
int Player::maximum_weight()
{
// 25 pounds, plus 10 per point of strength; 10 strength = 125 lbs
return 250 + 100 * stats.strength;
return 1000;
}
int Player::current_volume()
{
int ret = 0;
for (int i = 0; i < inventory.size(); i++) {
ret += inventory[i].get_volume();
}
return ret;
}
int Player::maximum_volume()
{
int ret = 100;
for (int i = 0; i < items_worn.size(); i++) {
ret += items_worn[i].get_volume_capacity();
}
return ret;
}
Item Player::inventory_single()
{
std::vector<Item> items = inventory_ui(true, false);
if (items.empty()) {
return Item();
}
return items[0];
}
std::vector<Item> Player::drop_items()
{
std::vector<Item> ret = inventory_ui(false, true);
return ret;
}
std::vector<Item> Player::inventory_ui(bool single, bool remove)
{
Window w_inv(0, 0, 80, 24);
cuss::interface i_inv;
std::string inv_file = CUSS_DIR + "/i_inventory.cuss";
// Sanity checks
if (!i_inv.load_from_file(inv_file)) {
return std::vector<Item>();
}
cuss::element *ele_list_items = i_inv.find_by_name("list_items");
if (ele_list_items == NULL) {
debugmsg("No element 'list_items' in %s", inv_file.c_str());
return std::vector<Item>();
}
int offset_size = ele_list_items->sizey;
// Set static text fields, which are different depending on single/remove
// So, we have a vector of indices for each item category.
bool include_weapon = false;
// Set up letter for weapon, if any exists
char letter = 'a';
char weapon_letter = 0;
if (weapon.type) {
weapon_letter = 'a';
letter = 'b';
std::stringstream weapon_ss;
weapon_ss << weapon_letter << " - " << weapon.get_name_full();
i_inv.set_data("text_weapon", weapon_ss.str());
} else {
i_inv.set_data("text_weapon", "<c=dkgray>No weapon<c=/>");
}
// Start with clothing - it's simple!
std::vector<char> clothing_letters;
std::vector<bool> include_clothing;
std::vector<std::string> clothing_name, clothing_weight, clothing_volume;
for (int i = 0; i < items_worn.size(); i++) {
include_clothing.push_back(false);
std::stringstream clothing_ss;
Item_type_clothing *clothing =
static_cast<Item_type_clothing*>(items_worn[i].type);
clothing_ss << letter << " - " << items_worn[i].get_name_full();
clothing_name.push_back(clothing_ss.str());
clothing_weight.push_back( itos( items_worn[i].get_weight() ) );
int capacity = clothing->carry_capacity;
if (capacity == 0) {
clothing_volume.push_back("<c=dkgray>+0<c=/>");
} else {
std::stringstream volume_ss;
volume_ss << "<c=green>+" << capacity << "<c=/>";
clothing_volume.push_back(volume_ss.str());
}
clothing_letters.push_back(letter);
if (letter == 'z') {
letter = 'A';
} else if (letter == 'Z') {
letter = '!';
} else {
letter++;
}
}
// Populate those vectors!
std::vector<int> item_indices[ITEM_CLASS_MAX];
std::vector<char> item_letters[ITEM_CLASS_MAX];
std::vector<bool> include_item;
for (int i = 0; i < inventory.size(); i++) {
include_item.push_back(false);
Item_class iclass = inventory[i].get_item_class();
item_indices[iclass].push_back(i);
item_letters[iclass].push_back(letter);
// TODO: Better inventory letters. This still isn't unlimited.
if (letter == 'z') {
letter = 'A';
} else if (letter == 'Z') {
letter = '!';
} else {
letter++;
}
}
// Now, populate the string lists
std::vector<std::string> item_name, item_weight, item_volume;
populate_item_lists(this, offset_size, item_indices, item_letters,
include_item, item_name, item_weight, item_volume);
// Set interface data
i_inv.set_data("weight_current", current_weight());
i_inv.set_data("weight_maximum", maximum_weight());
i_inv.set_data("volume_current", current_volume());
i_inv.set_data("volume_maximum", maximum_volume());
if (single) {
i_inv.set_data("text_instructions", "\
<c=magenta>Press Esc to cancel.\nPress - to select nothing.<c=/>");
} else {
i_inv.set_data("text_instructions", "\
<c=magenta>Press Esc to cancel.\nPress Enter to confirm selection.<c=/>");
if (remove) {
i_inv.set_data("text_after", "<c=brown>After:<c=/>");
i_inv.set_data("text_after2", "<c=brown>After:<c=/>");
}
}
for (int i = 0; i < offset_size && i < item_name.size(); i++) {
i_inv.add_data("list_items", item_name[i]);
i_inv.add_data("list_weight", item_weight[i]);
i_inv.add_data("list_volume", item_volume[i]);
}
for (int i = 0; i < offset_size && i < clothing_name.size(); i++) {
i_inv.add_data("list_clothing", clothing_name[i]);
i_inv.add_data("list_clothing_weight", clothing_weight[i]);
i_inv.add_data("list_clothing_volume", clothing_volume[i]);
}
int offset = 0;
int weight_after = current_weight(), volume_after = current_volume();
bool done = false;
while (!done) {
if (!single && remove) {
i_inv.set_data("weight_after", weight_after);
i_inv.set_data("volume_after", volume_after);
}
i_inv.draw(&w_inv);
long ch = input();
if (single && ch == '-') {
std::vector<Item> ret;
ret.push_back(Item());
return ret;
} else if (ch == '<' && offset > 0) {
offset--;
i_inv.clear_data("list_items");
i_inv.clear_data("list_weight");
i_inv.clear_data("list_volume");
for (int i = offset * offset_size;
i < (offset + 1) * offset_size && i < item_name.size();
i++) {
i_inv.add_data("list_items", item_name[i]);
i_inv.add_data("list_weight", item_weight[i]);
i_inv.add_data("list_volume", item_volume[i]);
}
} else if (ch == '>' && item_name.size() > (offset + 1) * offset_size) {
offset++;
i_inv.clear_data("list_items");
i_inv.clear_data("list_weight");
i_inv.clear_data("list_volume");
for (int i = offset * offset_size;
i < (offset + 1) * offset_size && i < item_name.size();
i++) {
i_inv.add_data("list_items", item_name[i]);
i_inv.add_data("list_weight", item_weight[i]);
i_inv.add_data("list_volume", item_volume[i]);
}
} else if (ch == KEY_ESC) {
std::vector<Item> empty;
return empty;
} else if (ch == '\n') {
done = true;
} else { // Anything else warrants a check for the matching key!
bool found = false;
if (ch == weapon_letter) {
found = true;
include_weapon = !include_weapon;
std::stringstream weapon_ss;
weapon_ss << (include_weapon ? "<c=green>" : "<c=ltgray>") <<
weapon_letter << (include_weapon ? " + " : " - ") <<
weapon.get_name_full();
i_inv.set_data("text_weapon", weapon_ss.str());
}
if (!found) {
for (int i = 0; i < clothing_letters.size(); i++) {
if (ch == clothing_letters[i]) {
found = true;
include_clothing[i] = !include_clothing[i];
bool inc = include_clothing[i];
std::stringstream clothing_ss;
clothing_ss << (inc ? "<c=green>" : "<c=ltgray>") <<
clothing_letters[i] << (inc ? " + " : " - ") <<
items_worn[i].get_name_full();
clothing_name[i] = clothing_ss.str();
}
}
}
if (!found) { // Not the weapon, not clothing - let's check inventory
for (int n = 0; n < ITEM_CLASS_MAX; n++) {
for (int i = 0; i < item_letters[n].size(); i++) {
if (ch == item_letters[n][i]) {
found = true;
int index = item_indices[n][i];
include_item[index] = !include_item[index];
bool inc = include_item[index];
std::stringstream item_ss;
item_ss << (inc ? "<c=green>" : "<c=ltgray>") <<
item_letters[n][i] << (inc ? " + " : " - ") <<
inventory[index].get_name_full();
// It's easiest to just set up the text lists for items from scratch!
populate_item_lists(this, offset_size, item_indices, item_letters,
include_item, item_name, item_weight,
item_volume);
}
}
}
}
if (found) {
if (single) {
done = true;
}
// Need to refresh our lists!
i_inv.clear_data("list_items");
i_inv.clear_data("list_weight");
i_inv.clear_data("list_volume");
for (int i = offset * offset_size;
i < (offset + 1) * offset_size && i < item_name.size();
i++) {
i_inv.add_data("list_items", item_name[i]);
i_inv.add_data("list_weight", item_weight[i]);
i_inv.add_data("list_volume", item_volume[i]);
}
i_inv.set_data("list_clothing", clothing_name);
}
} // Last check for ch
} // while (!done)
/* If we reach this point, either we're in single-mode and we've selected an
* item, or we're in multiple mode and we've hit Enter - either with some items
* items selected or without.
* Things set at this point:
* include_weapon - a bool marked true if we selected our weapon
* include_item - a set of bools, true if the item with that index is selected
* include_clothing - like include_item but for items_worn
*/
std::vector<Item> ret;
if (include_weapon) {
ret.push_back(weapon);
}
for (int i = 0; i < include_item.size(); i++) {
if (include_item[i]) {
ret.push_back( inventory[i] );
}
}
for (int i = 0; i < include_clothing.size(); i++) {
if (include_clothing[i]) {
ret.push_back( items_worn[i] );
}
}
if (remove) {
if (include_weapon) {
weapon = Item();
}
/* We go through these vectors backwards - to guarantee that the indices remain
* valid, even after removing items!
*/
for (int i = include_item.size() - 1; i >= 0; i--) {
if (include_item[i]) {
inventory.erase(inventory.begin() + i);
}
}
for (int i = include_clothing.size() - 1; i >= 0; i--) {
if (include_clothing[i]) {
items_worn.erase(items_worn.begin() + i);
}
}
}
return ret;
}
Item Player::pick_ammo_for(Item *it)
{
if (!it || !it->can_reload()) {
return Item();
}
if (it->charges == it->get_max_charges()) {
return Item();
}
if (it->get_item_class() != ITEM_CLASS_LAUNCHER) {
GAME.add_msg("You cannot reload %s.", it->get_name_indefinite().c_str());
return Item();
}
if (it->charges > 0 && it->ammo) {
return get_item_of_type(it->ammo);
}
// TODO: Limit this to valid ammo slots
Item ret = inventory_single();
if (!ret.is_real()) {
GAME.add_msg("Never mind.");
return Item();
}
if (ret.get_item_class() != ITEM_CLASS_AMMO) {
GAME.add_msg("That %s is not ammo.", ret.get_name().c_str());
return Item();
}
Item_type_ammo* ammo = static_cast<Item_type_ammo*>(ret.type);
Item_type_launcher* launcher = static_cast<Item_type_launcher*>(it->type);
if (ammo->ammo_type != launcher->ammo_type) {
GAME.add_msg("You picked %s ammo, but your %s needs %s.",
ammo->ammo_type.c_str(), it->get_name().c_str(),
launcher->ammo_type.c_str());
return Item();
}
return ret;
}
Tripoint Player::pick_target_for(Item* it)
{
if (!it || it->get_item_class() != ITEM_CLASS_TOOL) {
return Tripoint(-1, -1, -1);
}
Item_type_tool* tool = static_cast<Item_type_tool*>(it->type);
Tool_action* action = &(tool->applied_action);
std::string verb = action->signal;
Tripoint ret = pos;
switch (action->target) {
case TOOL_TARGET_NULL:
// No need to do anything
break;
case TOOL_TARGET_ADJACENT: {
GAME.add_msg("<c=ltred>%s where? (Press direction key)<c=/>",
verb.c_str());
GAME.draw_all();
Point dir = input_direction(input());
if (dir.x == -2) { // We canceled
return Tripoint(-1, -1, -1);
}
ret.x += dir.x;
ret.y += dir.y;
} break;
case TOOL_TARGET_RANGED: {
int range = action->range;
if (range <= 0) {
range = -1; // -1 means "No range limit" for Game::target_selector()
}
ret = GAME.target_selector(pos.x, pos.y, range);
if (ret.x == -1) { // We canceled
return Tripoint(-1, -1, -1);
}
} break;
default:
debugmsg("Don't know how to handle Tool_target %s",
tool_target_name(action->target).c_str());
return Tripoint(-1, -1, -1);
}
return ret;
}
void Player::take_damage(Damage_type type, int damage, std::string reason,
Body_part part)
{
absorb_damage(type, damage, part);
// TODO: Armor absorbs damage
current_hp[ convert_to_HP(part) ] -= damage;
pain += rng(0, damage);
}
void Player::heal_damage(int damage, HP_part part)
{
current_hp[part] += damage;
if (current_hp[part] > max_hp[part]) {
current_hp[part] = max_hp[part];
}
}
std::string Player::hp_text(Body_part part)
{
return hp_text( convert_to_HP(part) );
}
std::string Player::hp_text(HP_part part)
{
// Sanity check
if (part == HP_PART_MAX) {
return "BP_MAX???";
}
std::stringstream ret;
if (current_hp[part] == max_hp[part]) {
ret << "<c=green>";
} else {
int percent = (100 * current_hp[part]) / max_hp[part];
if (percent >= 75) {
ret << "<c=ltgreen>";
} else if (percent >= 50) {
ret << "<c=yellow>";
} else if (percent >= 25) {
ret << "<c=ltred>";
} else {
ret << "<c=red>";
}
}
ret << current_hp[part] << "<c=/>";
return ret.str();
}
void populate_item_lists(Player* p, int offset_size,
std::vector<int> item_indices[ITEM_CLASS_MAX],
std::vector<char> item_letters[ITEM_CLASS_MAX],
std::vector<bool> include_item,
std::vector<std::string> &item_name,
std::vector<std::string> &item_weight,
std::vector<std::string> &item_volume)
{
item_name.clear();
item_weight.clear();
item_volume.clear();
for (int n = 0; n < ITEM_CLASS_MAX; n++) {
if (!item_indices[n].empty()) {
std::string class_name = "<c=ltblue>" +
item_class_name_plural(Item_class(n)) +
"<c=/>";
item_name.push_back( class_name );
item_weight.push_back("");
item_volume.push_back("");
for (int i = 0; i < item_indices[n].size(); i++) {
// Check to see if we're starting a new page. If so, repeat the category header
if (item_name.size() % offset_size == 0) {
item_name.push_back( class_name + "(cont)" );
item_weight.push_back("");
item_volume.push_back("");
}
int index = item_indices[n][i];
Item* item = &( p->inventory[ index ] );
bool inc = include_item[index];
std::stringstream item_ss, weight_ss, volume_ss;
item_ss << (inc ? "<c=green>" : "<c=ltgray>") << item_letters[n][i] <<
(inc ? " + " : " - ") << item->get_name_full() << "<c=/>";
item_name.push_back( item_ss.str() );
weight_ss << (inc ? "<c=green>" : "<c=ltgray>") << item->get_weight() <<
"<c=/>";
item_weight.push_back( weight_ss.str() );
volume_ss << (inc ? "<c=green>" : "<c=ltgray>") << item->get_volume() <<
"<c=/>";
item_volume.push_back( volume_ss.str() );
}
}
}
}