Apply the new ground_level method.
[crawl.git] / crawl-ref / source / l_item.cc
blob299dfe7a50a48fe87ca53c963cae8dfdad0ad224
1 /*
2 * File: l_items.cc
3 * Summary: functions for managed Lua item manipulation
4 */
6 #include "AppHdr.h"
8 #include <sstream>
10 #include "cluautil.h"
11 #include "l_libs.h"
13 #include "artefact.h"
14 #include "colour.h"
15 #include "command.h"
16 #include "env.h"
17 #include "enum.h"
18 #include "invent.h"
19 #include "item_use.h"
20 #include "itemprop.h"
21 #include "items.h"
22 #include "output.h"
23 #include "player.h"
24 #include "skills2.h"
25 #include "spl-summoning.h"
26 #include "stuff.h"
28 /////////////////////////////////////////////////////////////////////
29 // Item handling
31 struct item_wrapper
33 item_def *item;
34 int turn;
36 bool valid() const { return turn == you.num_turns; }
39 void clua_push_item(lua_State *ls, item_def *item)
42 item_wrapper *iw = clua_new_userdata<item_wrapper>(ls, ITEM_METATABLE);
43 iw->item = item;
44 iw->turn = you.num_turns;
47 item_def *clua_get_item(lua_State *ls, int ndx)
49 item_wrapper *iwrap =
50 clua_get_userdata<item_wrapper>(ls, ITEM_METATABLE, ndx);
51 if (CLua::get_vm(ls).managed_vm && !iwrap->valid())
52 luaL_error(ls, "Invalid item");
53 return iwrap->item;
56 void lua_push_floor_items(lua_State *ls, int link)
58 lua_newtable(ls);
59 int index = 0;
60 for (; link != NON_ITEM; link = mitm[link].link)
62 clua_push_item(ls, &mitm[link]);
63 lua_rawseti(ls, -2, ++index);
67 void lua_push_inv_items(lua_State *ls = NULL)
69 if (!ls)
70 ls = clua.state();
71 lua_newtable(ls);
72 int index = 0;
73 for (unsigned slot = 0; slot < ENDOFPACK; ++slot)
75 if (you.inv[slot].defined())
77 clua_push_item(ls, &you.inv[slot]);
78 lua_rawseti(ls, -2, ++index);
83 #define IDEF(name) \
84 static int l_item_##name(lua_State *ls, item_def *item, \
85 const char *attr) \
87 #define IDEFN(name, closure) \
88 static int l_item_##name(lua_State *ls, item_def *item, const char *attrs) \
89 { \
90 clua_push_item(ls, item); \
91 lua_pushcclosure(ls, l_item_##closure, 1); \
92 return (1); \
95 #define ITEM(name, ndx) \
96 item_def *name = clua_get_item(ls, ndx)
98 #define UDATA_ITEM(name) ITEM(name, lua_upvalueindex(1))
100 static int l_item_do_wield(lua_State *ls)
102 if (you.turn_is_over)
103 return (0);
105 UDATA_ITEM(item);
107 int slot = -1;
108 if (item && item->defined() && in_inventory(*item))
109 slot = item->link;
110 bool res = wield_weapon(true, slot);
111 lua_pushboolean(ls, res);
112 return (1);
115 IDEFN(wield, do_wield)
117 static int l_item_do_wear(lua_State *ls)
119 if (you.turn_is_over)
120 return (0);
122 UDATA_ITEM(item);
124 if (!item || !in_inventory(*item))
125 return (0);
127 bool success = do_wear_armour(item->link, false);
128 lua_pushboolean(ls, success);
129 return (1);
132 IDEFN(wear, do_wear)
134 static int l_item_do_puton(lua_State *ls)
136 if (you.turn_is_over)
137 return (0);
139 UDATA_ITEM(item);
141 if (!item || !in_inventory(*item))
142 return (0);
144 lua_pushboolean(ls, puton_ring(item->link));
145 return (1);
148 IDEFN(puton, do_puton)
150 static int l_item_do_remove(lua_State *ls)
152 if (you.turn_is_over)
154 mpr("Turn is over");
155 return (0);
158 UDATA_ITEM(item);
160 if (!item || !in_inventory(*item))
162 mpr("Bad item");
163 return (0);
166 int eq = get_equip_slot(item);
167 if (eq < 0 || eq >= NUM_EQUIP)
169 mpr("Item is not equipped");
170 return (0);
173 bool result = false;
174 if (eq == EQ_WEAPON)
175 result = wield_weapon(true, SLOT_BARE_HANDS);
176 else if (eq == EQ_LEFT_RING || eq == EQ_RIGHT_RING || eq == EQ_AMULET)
177 result = remove_ring(item->link);
178 else
179 result = takeoff_armour(item->link);
180 lua_pushboolean(ls, result);
181 return (1);
184 IDEFN(remove, do_remove)
186 static int l_item_do_drop(lua_State *ls)
188 if (you.turn_is_over)
189 return (0);
191 UDATA_ITEM(item);
193 if (!item || !in_inventory(*item))
194 return (0);
196 int eq = get_equip_slot(item);
197 if (eq >= 0 && eq < NUM_EQUIP)
199 lua_pushboolean(ls, false);
200 lua_pushstring(ls, "Can't drop worn items");
201 return (2);
204 int qty = item->quantity;
205 if (lua_isnumber(ls, 1))
207 int q = luaL_checkint(ls, 1);
208 if (q >= 1 && q <= item->quantity)
209 qty = q;
211 lua_pushboolean(ls, drop_item(item->link, qty));
212 return (1);
215 IDEFN(drop, do_drop)
217 IDEF(equipped)
219 if (!item || !in_inventory(*item))
220 lua_pushboolean(ls, false);
222 int eq = get_equip_slot(item);
223 if (eq < 0 || eq >= NUM_EQUIP)
224 lua_pushboolean(ls, false);
225 else
226 lua_pushboolean(ls, true);
228 return (1);
231 static int l_item_do_class (lua_State *ls)
233 UDATA_ITEM(item);
235 if (item)
237 bool terse = false;
238 if (lua_isboolean(ls, 1))
239 terse = lua_toboolean(ls, 1);
241 std::string s = item_class_name(item->base_type, terse);
242 lua_pushstring(ls, s.c_str());
244 else
245 lua_pushnil(ls);
246 return (1);
249 IDEFN(class, do_class)
251 // FIXME: Fold this back into itemname.cc.
252 static const char *ring_types[] =
254 "regeneration",
255 "protection",
256 "protection from fire",
257 "poison resistance",
258 "protection from cold",
259 "strength",
260 "slaying",
261 "see invisible",
262 "invisibility",
263 "hunger",
264 "teleportation",
265 "evasion",
266 "sustain abilities",
267 "sustenance",
268 "dexterity",
269 "intelligence",
270 "wizardry",
271 "magical power",
272 "levitation",
273 "life protection",
274 "protection from magic",
275 "fire",
276 "ice",
277 "teleport control"
280 static const char *amulet_types[] =
282 "rage", "resist slowing", "clarity", "warding", "resist corrosion",
283 "gourmand", "conservation", "controlled flight", "inaccuracy",
284 "resist mutation"
287 IDEF(subtype)
289 if (item)
291 if (item_type_known(*item))
293 const char *s = NULL;
294 if (item->base_type == OBJ_JEWELLERY)
296 if (jewellery_is_amulet(*item))
297 s = amulet_types[ item->sub_type - AMU_RAGE ];
298 else
299 s = ring_types[item->sub_type];
301 else if (item->base_type == OBJ_POTIONS)
303 if (item->sub_type == POT_BLOOD)
304 s = "blood";
305 else if (item->sub_type == POT_BLOOD_COAGULATED)
306 s = "coagulated blood";
307 else if (item->sub_type == POT_WATER)
308 s = "water";
309 else if (item->sub_type == POT_PORRIDGE)
310 s = "porridge";
311 else if (item->sub_type == POT_BERSERK_RAGE)
312 s = "berserk";
313 else if (item->sub_type == POT_GAIN_STRENGTH
314 || item->sub_type == POT_GAIN_DEXTERITY
315 || item->sub_type == POT_GAIN_INTELLIGENCE)
317 s = "gain ability";
319 else if (item->sub_type == POT_CURE_MUTATION)
320 s = "cure mutation";
322 else if (item->base_type == OBJ_BOOKS)
324 if (item->sub_type == BOOK_MANUAL)
325 s = "manual";
326 else
327 s = "spellbook";
330 if (s)
331 lua_pushstring(ls, s);
332 else
333 lua_pushnil(ls);
335 lua_pushnumber(ls, item->sub_type);
336 return (2);
340 lua_pushnil(ls);
341 lua_pushnil(ls);
342 return (2);
345 IDEF(cursed)
347 bool cursed = item && item_ident(*item, ISFLAG_KNOW_CURSE)
348 && item->cursed();
349 lua_pushboolean(ls, cursed);
350 return (1);
353 IDEF(worn)
355 int worn = get_equip_slot(item);
356 if (worn != -1)
357 lua_pushnumber(ls, worn);
358 else
359 lua_pushnil(ls);
360 if (worn != -1)
361 lua_pushstring(ls, equip_slot_to_name(worn));
362 else
363 lua_pushnil(ls);
364 return (2);
367 static std::string _item_name(lua_State *ls, item_def* item)
369 description_level_type ndesc = DESC_PLAIN;
370 if (lua_isstring(ls, 1))
371 ndesc = description_type_by_name(lua_tostring(ls, 1));
372 else if (lua_isnumber(ls, 1))
373 ndesc = static_cast<description_level_type>(luaL_checkint(ls, 1));
374 const bool terse = lua_toboolean(ls, 2);
375 return (item->name(ndesc, terse));
378 static int l_item_do_name(lua_State *ls)
380 UDATA_ITEM(item);
382 if (item)
383 lua_pushstring(ls, _item_name(ls, item).c_str());
384 else
385 lua_pushnil(ls);
386 return (1);
389 IDEFN(name, do_name)
391 static int l_item_do_name_coloured(lua_State *ls)
393 UDATA_ITEM(item);
395 if (item)
397 std::string name = _item_name(ls, item);
398 int col = menu_colour(name, menu_colour_item_prefix(*item));
399 std::string colstr = colour_to_str(col);
401 std::ostringstream out;
403 out << "<" << colstr << ">" << name << "</" << colstr << ">";
405 lua_pushstring(ls, out.str().c_str());
407 else
408 lua_pushnil(ls);
409 return (1);
412 IDEFN(name_coloured, do_name_coloured)
414 IDEF(quantity)
416 PLUARET(number, item? item->quantity : 0);
419 IDEF(slot)
421 if (item)
423 int slot = in_inventory(*item) ? item->link
424 : letter_to_index(item->slot);
425 lua_pushnumber(ls, slot);
427 else
428 lua_pushnil(ls);
429 return (1);
432 IDEF(ininventory)
434 PLUARET(boolean, item && in_inventory(*item));
437 IDEF(equip_type)
439 if (!item || !item->defined())
440 return (0);
442 equipment_type eq = EQ_NONE;
444 if (item->base_type == OBJ_WEAPONS || item->base_type == OBJ_STAVES)
445 eq = EQ_WEAPON;
446 else if (item->base_type == OBJ_ARMOUR)
447 eq = get_armour_slot(*item);
448 else if (item->base_type == OBJ_JEWELLERY)
449 eq = item->sub_type >= AMU_RAGE? EQ_AMULET : EQ_RINGS;
451 if (eq != EQ_NONE)
453 lua_pushnumber(ls, eq);
454 lua_pushstring(ls, equip_slot_to_name(eq));
456 else
458 lua_pushnil(ls);
459 lua_pushnil(ls);
461 return (2);
464 IDEF(weap_skill)
466 if (!item || !item->defined())
467 return (0);
469 skill_type skill = range_skill(*item);
470 if (skill == SK_THROWING)
471 skill = weapon_skill(*item);
472 if (skill == SK_FIGHTING)
473 return (0);
475 lua_pushstring(ls, skill_name(skill));
476 lua_pushnumber(ls, skill);
477 return (2);
480 IDEF(dropped)
482 if (!item || !item->defined())
483 return (0);
485 lua_pushboolean(ls, item->flags & ISFLAG_DROPPED);
487 return (1);
490 IDEF(can_cut_meat)
492 if (!item || !item->defined())
493 return (0);
495 lua_pushboolean(ls, can_cut_meat(*item));
497 return (1);
500 IDEF(artefact)
502 if (!item || !item->defined())
503 return (0);
505 lua_pushboolean(ls, is_artefact(*item));
507 return (1);
510 IDEF(branded)
512 if (!item || !item->defined() || !item_type_known(*item))
513 return (0);
515 lua_pushboolean(ls, item_is_branded(*item));
516 return (1);
519 IDEF(snakable)
521 if (!item || !item->defined())
522 return (0);
524 lua_pushboolean(ls, item_is_snakable(*item));
526 return (1);
529 // DLUA-only functions
530 static int l_item_do_pluses (lua_State *ls)
532 ASSERT_DLUA;
534 UDATA_ITEM(item);
536 if (!item || !item->defined() || !item_ident(*item, ISFLAG_KNOW_PLUSES))
538 lua_pushboolean(ls, false);
539 return (1);
542 lua_pushnumber(ls, item->plus);
543 // XXX: May cause issues on items that don't use plus2, ie ammunition.
544 lua_pushnumber(ls, item->plus2);
546 return (2);
549 IDEFN(pluses, do_pluses)
551 static int l_item_do_destroy (lua_State *ls)
553 ASSERT_DLUA;
555 UDATA_ITEM(item);
557 if (!item || !item->defined())
559 lua_pushboolean(ls, false);
560 return (0);
563 item_was_destroyed(*item);
564 destroy_item(item->index());
566 lua_pushboolean(ls, true);
567 return (1);
570 IDEFN(destroy, do_destroy)
572 static int l_item_do_dec_quantity (lua_State *ls)
574 ASSERT_DLUA;
576 UDATA_ITEM(item);
578 if (!item || !item->defined())
580 lua_pushboolean(ls, false);
581 return (1);
584 // The quantity to reduce by.
585 int quantity = luaL_checkint(ls, 1);
587 bool destroyed = false;
589 if (in_inventory(*item))
590 destroyed = dec_inv_item_quantity(item->link, quantity);
591 else
592 destroyed = dec_mitm_item_quantity(item->index(), quantity);
594 lua_pushboolean(ls, destroyed);
595 return (1);
598 IDEFN(dec_quantity, do_dec_quantity)
600 static int l_item_do_inc_quantity (lua_State *ls)
602 ASSERT_DLUA;
604 UDATA_ITEM(item);
606 if (!item || !item->defined())
608 lua_pushboolean(ls, false);
609 return (1);
612 // The quantity to increase by.
613 int quantity = luaL_checkint(ls, 1);
615 if (in_inventory(*item))
616 inc_inv_item_quantity(item->link, quantity);
617 else
618 inc_mitm_item_quantity(item->index(), quantity);
620 return (0);
623 IDEFN(inc_quantity, do_inc_quantity)
625 iflags_t str_to_item_status_flags (std::string flag)
627 iflags_t flags = 0;
628 if (flag.find("curse") != std::string::npos)
629 flags &= ISFLAG_KNOW_CURSE;
630 // type is dealt with using item_type_known.
631 //if (flag.find("type") != std::string::npos)
632 // flags &= ISFLAG_KNOW_TYPE;
633 if (flag.find("pluses") != std::string::npos)
634 flags &= ISFLAG_KNOW_PLUSES;
635 if (flag.find("properties") != std::string::npos)
636 flags &= ISFLAG_KNOW_PROPERTIES;
637 if (flag == "any")
638 flags = ISFLAG_IDENT_MASK;
640 return (flags);
643 static int l_item_do_identified (lua_State *ls)
645 ASSERT_DLUA;
647 UDATA_ITEM(item);
649 if (!item || !item->defined())
651 lua_pushnil(ls);
652 return (1);
655 bool known_status = false;
656 if (lua_isstring(ls, 1))
658 std::string flags = luaL_checkstring(ls, 1);
659 if (trimmed_string(flags).empty())
660 known_status = item_ident(*item, ISFLAG_IDENT_MASK);
661 else
663 const bool check_type = strip_tag(flags, "type");
664 iflags_t item_flags = str_to_item_status_flags(flags);
665 known_status = ((item_flags || check_type)
666 && (!item_flags || item_ident(*item, item_flags))
667 && (!check_type || item_type_known(*item)));
670 else
672 known_status = item_ident(*item, ISFLAG_IDENT_MASK);
675 lua_pushboolean(ls, known_status);
676 return (1);
679 IDEFN(identified, do_identified)
681 // Some dLua convenience functions.
682 IDEF(base_type)
684 ASSERT_DLUA;
686 lua_pushstring(ls, base_type_string(*item).c_str());
687 return (1);
690 IDEF(sub_type)
692 ASSERT_DLUA;
694 lua_pushstring(ls, sub_type_string(*item).c_str());
695 return (1);
698 IDEF(ego_type)
700 if (CLua::get_vm(ls).managed_vm && !item_ident(*item, ISFLAG_KNOW_TYPE))
702 lua_pushstring(ls, "unknown");
703 return (1);
706 lua_pushstring(ls, ego_type_string(*item).c_str());
707 return (1);
710 IDEF(artefact_name)
712 ASSERT_DLUA;
714 if (is_artefact(*item))
715 lua_pushstring(ls, get_artefact_name(*item, true).c_str());
716 else
717 lua_pushnil(ls);
719 return (1);
722 IDEF(is_cursed)
724 ASSERT_DLUA;
726 bool cursed = item->cursed();
728 lua_pushboolean(ls, cursed);
729 return (1);
732 // Library functions below
733 static int l_item_inventory(lua_State *ls)
735 lua_push_inv_items(ls);
736 return (1);
739 static int l_item_index_to_letter(lua_State *ls)
741 int index = luaL_checkint(ls, 1);
742 char sletter[2] = "?";
743 if (index >= 0 && index <= ENDOFPACK)
744 *sletter = index_to_letter(index);
745 lua_pushstring(ls, sletter);
746 return (1);
749 static int l_item_letter_to_index(lua_State *ls)
751 const char *s = luaL_checkstring(ls, 1);
752 if (!s || !*s || s[1])
753 return (0);
754 lua_pushnumber(ls, letter_to_index(*s));
755 return (1);
758 static int l_item_swap_slots(lua_State *ls)
760 int slot1 = luaL_checkint(ls, 1),
761 slot2 = luaL_checkint(ls, 2);
762 bool verbose = lua_toboolean(ls, 3);
763 if (slot1 < 0 || slot1 >= ENDOFPACK
764 || slot2 < 0 || slot2 >= ENDOFPACK
765 || slot1 == slot2 || !you.inv[slot1].defined())
767 return (0);
770 swap_inv_slots(slot1, slot2, verbose);
772 return (0);
775 static item_def *dmx_get_item(lua_State *ls, int ndx, int subndx)
777 if (lua_istable(ls, ndx))
779 lua_rawgeti(ls, ndx, subndx);
780 ITEM(item, -1);
781 lua_pop(ls, 1);
782 return (item);
784 ITEM(item, ndx);
785 return item;
788 static int dmx_get_qty(lua_State *ls, int ndx, int subndx)
790 int qty = -1;
791 if (lua_istable(ls, ndx))
793 lua_rawgeti(ls, ndx, subndx);
794 if (lua_isnumber(ls, -1))
795 qty = luaL_checkint(ls, -1);
796 lua_pop(ls, 1);
798 else if (lua_isnumber(ls, ndx))
800 qty = luaL_checkint(ls, ndx);
802 return (qty);
805 static bool l_item_pickup2(item_def *item, int qty)
807 if (!item || in_inventory(*item))
808 return (false);
810 int floor_link = item_on_floor(*item, you.pos());
811 if (floor_link == NON_ITEM)
812 return (false);
814 return pickup_single_item(floor_link, qty);
817 static int l_item_pickup(lua_State *ls)
819 if (you.turn_is_over)
820 return (0);
822 if (lua_isuserdata(ls, 1))
824 ITEM(item, 1);
825 int qty = item->quantity;
826 if (lua_isnumber(ls, 2))
827 qty = luaL_checkint(ls, 2);
829 if (l_item_pickup2(item, qty))
830 lua_pushnumber(ls, 1);
831 else
832 lua_pushnil(ls);
833 return (1);
835 else if (lua_istable(ls, 1))
837 int dropped = 0;
838 for (int i = 1; ; ++i)
840 lua_rawgeti(ls, 1, i);
841 item_def *item = dmx_get_item(ls, -1, 1);
842 int qty = dmx_get_qty(ls, -1, 2);
843 lua_pop(ls, 1);
845 if (l_item_pickup2(item, qty))
846 dropped++;
847 else
849 // Yes, we bail out on first failure.
850 break;
853 if (dropped)
854 lua_pushnumber(ls, dropped);
855 else
856 lua_pushnil(ls);
857 return (1);
859 return (0);
862 // Returns item equipped in a slot defined in an argument.
863 static int l_item_equipped_at(lua_State *ls)
865 int eq = -1;
866 if (lua_isnumber(ls, 1))
867 eq = luaL_checkint(ls, 1);
868 else if (lua_isstring(ls, 1))
870 const char *eqname = lua_tostring(ls, 1);
871 if (!eqname)
872 return (0);
873 eq = equip_name_to_slot(eqname);
876 if (eq < 0 || eq >= NUM_EQUIP)
877 return (0);
879 if (you.equip[eq] != -1)
880 clua_push_item(ls, &you.inv[you.equip[eq]]);
881 else
882 lua_pushnil(ls);
884 return (1);
887 static int l_item_inslot(lua_State *ls)
889 int index = luaL_checkint(ls, 1);
890 if (index >= 0 && index < 52 && you.inv[index].defined())
891 clua_push_item(ls, &you.inv[index]);
892 else
893 lua_pushnil(ls);
894 return (1);
897 struct ItemAccessor
899 const char *attribute;
900 int (*accessor)(lua_State *ls, item_def *item, const char *attr);
903 static ItemAccessor item_attrs[] =
905 { "artefact", l_item_artefact },
906 { "branded", l_item_branded },
907 { "snakable", l_item_snakable },
908 { "class", l_item_class },
909 { "subtype", l_item_subtype },
910 { "cursed", l_item_cursed },
911 { "worn", l_item_worn },
912 { "name", l_item_name },
913 { "name_coloured", l_item_name_coloured },
914 { "quantity", l_item_quantity },
915 { "slot", l_item_slot },
916 { "ininventory", l_item_ininventory },
917 { "wield", l_item_wield },
918 { "wear", l_item_wear },
919 { "puton", l_item_puton },
920 { "remove", l_item_remove },
921 { "drop", l_item_drop },
922 { "equipped", l_item_equipped },
923 { "equip_type", l_item_equip_type },
924 { "weap_skill", l_item_weap_skill },
925 { "dropped", l_item_dropped },
926 { "can_cut_meat", l_item_can_cut_meat },
927 { "pluses", l_item_pluses },
928 { "destroy", l_item_destroy },
929 { "dec_quantity", l_item_dec_quantity },
930 { "inc_quantity", l_item_inc_quantity },
931 { "identified", l_item_identified },
932 { "base_type", l_item_base_type },
933 { "sub_type", l_item_sub_type },
934 { "ego_type", l_item_ego_type },
935 { "artefact_name", l_item_artefact_name },
936 { "is_cursed", l_item_is_cursed },
939 static int item_get(lua_State *ls)
941 ITEM(iw, 1);
942 if (!iw)
943 return (0);
945 const char *attr = luaL_checkstring(ls, 2);
946 if (!attr)
947 return (0);
949 for (unsigned i = 0; i < sizeof(item_attrs) / sizeof(item_attrs[0]); ++i)
950 if (!strcmp(attr, item_attrs[i].attribute))
951 return (item_attrs[i].accessor(ls, iw, attr));
953 return (0);
956 static const struct luaL_reg item_lib[] =
958 { "inventory", l_item_inventory },
959 { "letter_to_index", l_item_letter_to_index },
960 { "index_to_letter", l_item_index_to_letter },
961 { "swap_slots", l_item_swap_slots },
962 { "pickup", l_item_pickup },
963 { "equipped_at", l_item_equipped_at },
964 { "inslot", l_item_inslot },
965 { NULL, NULL },
968 void cluaopen_item(lua_State *ls)
970 luaL_newmetatable(ls, ITEM_METATABLE);
971 lua_pushstring(ls, "__index");
972 lua_pushcfunction(ls, item_get);
973 lua_settable(ls, -3);
975 // Pop the metatable off the stack.
976 lua_pop(ls, 1);
978 luaL_openlib(ls, "items", item_lib, 0);