-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuse.cpp
481 lines (468 loc) · 12.4 KB
/
use.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
#include "game.hpp"
static constexpr uint8_t WAND_RANGE = 6;
static void identify_item(uint8_t i)
{
auto& it = inv[i];
uint8_t st = it.subtype;
if(it.type >= item::WAND)
it.identified = 1;
switch(it.type)
{
case item::POTION: identify_potion(st); break;
case item::SCROLL: identify_scroll(st); break;
case item::RING: identify_ring(st); break;
case item::AMULET: identify_amulet(st); break;
case item::WAND: identify_wand(st); break;
case item::FOOD:
case item::ARROW:
break;
default:
break;
}
}
void wand_effect(uint8_t i, uint8_t d, uint8_t subtype)
{
scan_result sr;
if(d < 4)
{
scan_dir(i, d, WAND_RANGE, sr);
if(subtype != WND_DIGGING)
draw_ray_anim(ents[i].x, ents[i].y, d, sr.n);
}
else
{
sr.x = ents[i].x;
sr.y = ents[i].y;
sr.i = i;
d = u8rand() % 4;
}
entity* te = get_entity(sr.x, sr.y);
if(subtype == WND_DIGGING)
{
uint8_t x = ents[i].x, y = ents[i].y;
auto c = dircoord(d);
for(uint8_t i = 0; i < WAND_RANGE; ++i)
{
x += c.x;
y += c.y;
if(x >= MAP_W || y >= MAP_H) break;
dig_tile(x, y);
if(door* d = get_door(x, y))
{
d->open = 1;
d->secret = 0;
}
}
return;
}
else if(subtype == WND_FIRE)
{
static int8_t const FIRE[18] PROGMEM =
{
0, 0, -1, -1, 0, -1, 1, -1, 1, 0, 1, 1, 0, 1, -1, 1, -1, 0,
};
draw_dungeon_at_player();
for(uint8_t j = 0; j < 18; j += 2)
{
uint8_t tx = sr.x + pgm_read_byte(&FIRE[j]);
uint8_t ty = sr.y + pgm_read_byte(&FIRE[j + 1]);
draw_sprite_nonprog_rel_and_wait(0x0eae, tx, ty);
if(entity* e = get_entity(tx, ty))
entity_take_fire_damage_from_entity(i, index_of_entity(*e), u8rand(8) + 8);
}
paint_left();
return;
}
else if(!te) return;
uint8_t ti = index_of_entity(*te);
switch(subtype)
{
case WND_FORCE:
{
status_u(PSTR("@U blasted back!"), ti);
scan_result tsr;
scan_dir_pos(te->x, te->y, d, 8, tsr);
if(tsr.i < MAP_ENTITIES)
{
te->x = tsr.px;
te->y = tsr.py;
status_u(PSTR("@S crashes into"), ti);
status_u(PSTR("@O."), tsr.i);
paralyze_entity(tsr.i);
}
else
{
if(tsr.i == 254)
{
status_u(PSTR("@S hits a wall."), ti);
paralyze_entity(ti);
}
te->x = tsr.x;
te->y = tsr.y;
}
// TODO: if(entity* pe = get_entity(tsr.x, tsr.y))
// damage both te and pe
break;
}
case WND_TELEPORT:
if(sr.i < MAP_ENTITIES)
teleport_entity(sr.i);
break;
case WND_STRIKING:
entity_take_magic_damage_from_entity(i, sr.i, u8rand(12) + 12);
break;
case WND_ICE:
slow_entity(sr.i);
entity_take_magic_damage_from_entity(i, sr.i, u8rand(8) + 8);
break;
case WND_POLYMORPH:
{
uint8_t t = te->type;
if(t > entity::BAT && t < entity::DARKNESS)
{
status_u(PSTR("@T form morphs into"), ti);
te->type = u8rand() < 64 ? t + 1 : t - 1;
status_u(PSTR("@O."), ti);
healths[ti] = entity_max_health(sr.i);
}
break;
}
default:
break;
}
}
static void use_wand(uint8_t subtype)
{
draw_dungeon_at_player();
paint_left();
{
bool was_identified = wand_is_identified(subtype);
identify_wand(subtype);
if(!was_identified)
status_i(PSTR2(STRI_YOU_DISCOVER_IT "is a @i."), item::make(item::WAND, subtype));
}
uint8_t d = 255;
direction_menu(d); // canceling will mean target self
wand_effect(0, d, subtype);
}
static void use_scroll(uint8_t subtype)
{
uint8_t i;
{
bool was_identified = scroll_is_identified(subtype);
identify_scroll(subtype);
if(!was_identified)
status_i(STR_YOU_DISCOVER_IT_WAS_A_I, item::make(item::SCROLL, subtype));
}
switch(subtype)
{
case SCR_IDENTIFY:
{
i = inventory_menu(PSTR2("Identify" STRI_WHICH_ITEM_Q));
if(i < INV_ITEMS)
{
identify_item(i);
status_i(PSTR2(STRI_YOU "identified: @i."), inv[i]);
}
else
status_simple(STR_NOTHING_HAPPENS);
break;
}
case SCR_ENCHANT:
{
i = inventory_menu(PSTR2("Enchant" STRI_WHICH_ITEM_Q));
if(i < INV_ITEMS)
{
auto& it = inv[i];
if(it.stackable())
status_simple(STR_NOTHING_HAPPENS);
else
{
uint8_t n = 1;
if(it.type == item::WAND) n = 4;
it.level += n;
if(it.level > ENCHANT_LEVEL_MAX)
it.level = ENCHANT_LEVEL_MAX;
static char const MSG2[] PROGMEM = STRI_THE_I_GLOWS "blue" STRI_FOR_A_MOMENT_P;
status_i(MSG2, it);
}
}
else
status_simple(STR_NOTHING_HAPPENS);
break;
}
case SCR_REMOVE_CURSE:
{
i = inventory_menu(PSTR2("Uncurse" STRI_WHICH_ITEM_Q));
if(i < INV_ITEMS)
{
auto& it = inv[i];
if(it.type <= item::WAND || !it.cursed)
status_simple(STR_NOTHING_HAPPENS);
else
{
it.cursed = 0;
if((it.type == item::AMULET || it.type == item::RING) &&
it.level <= ENCHANT_LEVEL_ZERO)
it.level = ENCHANT_LEVEL_ZERO + 1;
static char const MSG2[] PROGMEM = STRI_THE_I_GLOWS "white" STRI_FOR_A_MOMENT_P;
status_i(MSG2, it);
}
}
else
status_simple(STR_NOTHING_HAPPENS);
break;
}
case SCR_TELEPORT:
teleport_entity(0);
break;
case SCR_MAPPING:
for(auto& t : tfog)
t = 0xff;
status_simple(PSTR2(STRI_YOU "become aware of your surroundings."));
break;
case SCR_MASS_POISON:
status_simple(PSTR("There is a poisonous blast."));
/* fallthrough */
case SCR_FEAR:
case SCR_TORMENT:
case SCR_MASS_CONFUSE:
{
bool found = false;
for(uint8_t i = 0; i < MAP_ENTITIES; ++i)
{
auto& e = ents[i];
if(e.type == entity::NONE) continue;
if(!player_can_see_entity(i)) continue;
aggro_monster(i);
if(subtype == SCR_FEAR)
{
if(i != 0)
{
found = true;
e.scared = 1;
status_u(PSTR("@S flees!"), i);
}
}
else
{
found = true;
if(subtype == SCR_TORMENT)
{
healths[i] /= 2;
status_u(PSTR("@U stricken!"), i);
}
else if(subtype == SCR_MASS_CONFUSE)
confuse_entity(i);
else if(subtype == SCR_MASS_POISON)
poison_entity(i);
}
}
if(!found)
status_simple(STR_NOTHING_HAPPENS);
break;
}
default:
break;
}
}
uint8_t slot_of_item(uint8_t type)
{
switch(type)
{
case item::BOW:
case item::SWORD: return SLOT_WEAPON;
case item::HELM: return SLOT_HELM;
case item::ARMOR: return SLOT_ARMOR;
case item::BOOTS: return SLOT_BOOTS;
case item::RING:
{
uint8_t j = pinfo.equipped[SLOT_RING1];
// if slot 1 empty, return slot 1
if(j >= INV_ITEMS) return SLOT_RING1;
// if slot 2 cursed, return slot 1
j = pinfo.equipped[SLOT_RING2];
if(j < INV_ITEMS && inv[j].cursed) return SLOT_RING1;
// otherwise return slot 2
return SLOT_RING2;
}
case item::AMULET: return SLOT_AMULET;
default: break;
}
return 255;
}
bool unequip_item(uint8_t i)
{
auto it = inv[i];
char const* verb = it.type <= item::SWORD ?
PSTR("stop using") : PSTR("take off");
if(it.cursed)
{
status_si(PSTR2(STRI_YOU_ARE_UNABLE_TO STRI_P_THE_I), verb, it);
return false;
}
for(auto& j : pinfo.equipped)
if(j == i)
{
status_si(STR_YOU_P_THE_I, verb, it);
j = 255;
adjust_health_to_max_health(0);
return true;
}
return false;
}
bool equip_item(uint8_t i)
{
item it = inv[i];
uint8_t type = it.type;
uint8_t slot = slot_of_item(type);
uint8_t& j = pinfo.equipped[slot];
if(j < INV_ITEMS && !unequip_item(j))
return false;
bool weap = type <= item::SWORD;
identify_item(i);
status_si(STR_YOU_P_THE_I,
weap ? PSTR("ready") : PSTR("put on"),
inv[i]);
if(it.is_type(item::AMULET, AMU_CLARITY))
end_confusion(0);
adjust_health_to_max_health(0);
j = i;
return true;
}
bool use_item(uint8_t i)
{
item it = inv[i];
uint8_t subtype = it.subtype;
{
if(it.stackable())
{
int8_t cb = amulet_bonus(AMU_CONSERVATION);
if(cb <= 0 || u8rand() < 192)
player_remove_item(i);
if(cb < 0 && u8rand() < 64)
player_remove_item(i);
}
}
switch(it.type)
{
case item::FOOD:
{
hunger = 0;
status_you_are_no_longer(STR_HUNGRY);
return true;
}
case item::POTION:
entity_apply_potion(subtype, 0);
return true;
case item::SCROLL:
use_scroll(subtype);
return true;
case item::WAND:
{
uint8_t n = it.quant;
if(n <= 1)
{
player_remove_item(i);
status_i(PSTR2(STRI_CAPTHE "@i crumbles to dust."), it);
}
else
inv[i].quant = n - 1;
use_wand(subtype);
return true;
}
case item::BOW:
case item::SWORD:
case item::HELM:
case item::ARMOR:
case item::BOOTS:
case item::RING:
case item::AMULET:
if(item_is_equipped(i))
return unequip_item(i);
return equip_item(i);
default:
break;
}
return false;
}
void entity_apply_potion(uint8_t subtype, uint8_t i)
{
{
bool was_identified = potion_is_identified(subtype);
identify_potion(subtype);
if(!was_identified)
status_i(STR_YOU_DISCOVER_IT_WAS_A_I, item::make(item::POTION, subtype));
}
switch(subtype)
{
case POT_HEALING:
{
uint8_t mhp = entity_max_health(i);
entity_heal(i, u8rand(mhp / 2 + 1) + mhp / 4);
entity_restore_strength(i);
break;
}
case POT_CONFUSION:
confuse_entity(i);
break;
case POT_POISON:
poison_entity(i);
break;
case POT_HARMING:
{
uint8_t dam = entity_max_health(i) / 8 + 1;
dam += u8rand(dam * 2);
if(dam > 10) dam = 10;
status_u(PSTR2("@U harmed by " STRI_THE "potion!"), i);
entity_take_damage_from_entity(0, i, dam);
break;
}
case POT_STRENGTH:
if(ents[i].weakened)
entity_restore_strength(i);
else if(i == 0)
{
status_simple(STR_YOU_FEEL_STRONGER);
pstats.strength += 1;
}
break;
case POT_DEXTERITY:
if(i == 0)
{
status_simple(STR_YOU_FEEL_MORE_AGILE);
pstats.dexterity += 1;
}
break;
case POT_INVIS:
if(i == 0)
{
if(ring_bonus(RNG_INVIS) < 0)
break;
pinfo.invis_rem = u8rand() % 16 + 12;
status_simple(PSTR2(STRI_YOU "turn " STRI_INVISIBLE "."));
}
ents[i].invis = 1;
break;
case POT_PARALYSIS:
paralyze_entity(i);
break;
case POT_SLOWING:
slow_entity(i);
break;
case POT_EXPERIENCE:
if(i == 0)
player_gain_xp(50);
break;
default:
// TODO
//status(PSTR("NOT IMPLEMENTED"));
break;
}
}
NOINLINE bool item_is_equipped(uint8_t i)
{
for(auto j : pinfo.equipped)
if(i == j) return true;
return false;
}