3 * Summary: Item-related functions in lua library "dgn".
19 #define ITEMLIST_METATABLE "crawldgn.item_list"
21 static item_list
_lua_get_ilist(lua_State
*ls
, int ndx
)
23 if (lua_isstring(ls
, ndx
))
25 const char *spec
= lua_tostring(ls
, ndx
);
28 const std::string err
= ilist
.add_item(spec
);
30 luaL_error(ls
, err
.c_str());
37 clua_get_userdata
<item_list
*>(ls
, ITEMLIST_METATABLE
, ndx
);
41 luaL_argerror(ls
, ndx
, "Expected item list object or string");
46 void register_itemlist(lua_State
*ls
)
48 clua_register_metatable(ls
, ITEMLIST_METATABLE
, NULL
,
49 lua_object_gc
<item_list
>);
52 static int dgn_item_from_index(lua_State
*ls
)
54 const int index
= luaL_checkint(ls
, 1);
56 item_def
*item
= &mitm
[index
];
59 clua_push_item(ls
, item
);
66 static int dgn_items_at(lua_State
*ls
)
69 lua_push_floor_items(ls
, env
.igrid(c
));
73 static int _dgn_item_spec(lua_State
*ls
)
75 const item_list ilist
= _lua_get_ilist(ls
, 1);
76 dlua_push_object_type
<item_list
>(ls
, ITEMLIST_METATABLE
, ilist
);
80 static int dgn_create_item(lua_State
*ls
)
84 item_list ilist
= _lua_get_ilist(ls
, 3);
86 lua_isnumber(ls
, 4) ? lua_tointeger(ls
, 4) : you
.absdepth0
;
88 dgn_place_multiple_items(ilist
, c
, level
);
93 static int dgn_item_property_remove(lua_State
*ls
)
95 if (item_def
*item
= clua_get_item(ls
, 1))
97 item
->props
.erase(luaL_checkstring(ls
, 2));
102 static int dgn_item_property_set(lua_State
*ls
)
104 if (item_def
*item
= clua_get_item(ls
, 1))
106 const std::string key
= luaL_checkstring(ls
, 2);
107 const std::string type
= luaL_checkstring(ls
, 3);
108 if (type
.empty() || type
.length() > 1)
109 luaL_error(ls
, "Expected type: [BbSifsC], got: '%s'",
115 item
->props
[key
].get_bool() = lua_toboolean(ls
, 4);
118 item
->props
[key
].get_byte() = luaL_checkint(ls
, 4);
121 item
->props
[key
].get_short() = luaL_checkint(ls
, 4);
124 item
->props
[key
].get_int() = luaL_checkint(ls
, 4);
127 item
->props
[key
].get_float() = luaL_checknumber(ls
, 4);
130 item
->props
[key
].get_string() = luaL_checkstring(ls
, 4);
133 item
->props
[key
].get_coord() = coord_def(luaL_checkint(ls
, 4),
134 luaL_checkint(ls
, 5));
137 luaL_error(ls
, "Unknown type: '%s'", type
.c_str());
144 static int dgn_item_property(lua_State
*ls
)
146 if (item_def
*item
= clua_get_item(ls
, 1))
148 const std::string key
= luaL_checkstring(ls
, 2);
149 const std::string type
= luaL_checkstring(ls
, 3);
150 if (type
.empty() || type
.length() > 1)
151 luaL_error(ls
, "Expected type: [BbSifsC], got: '%s'",
154 if (!item
->props
.exists(key
))
163 lua_pushboolean(ls
, item
->props
[key
].get_byte());
166 lua_pushnumber(ls
, item
->props
[key
].get_byte());
169 lua_pushnumber(ls
, item
->props
[key
].get_short());
172 lua_pushnumber(ls
, item
->props
[key
].get_int());
175 lua_pushnumber(ls
, item
->props
[key
].get_float());
178 lua_pushstring(ls
, item
->props
[key
].get_string().c_str());
182 const coord_def
p(item
->props
[key
].get_coord());
183 lua_pushnumber(ls
, p
.x
);
184 lua_pushnumber(ls
, p
.y
);
188 luaL_error(ls
, "Unknown type: '%s'", type
.c_str());
195 // Returns two arrays: one of floor items, one of shop items.
196 static int dgn_stash_items(lua_State
*ls
)
198 unsigned min_value
= lua_isnumber(ls
, 1) ? luaL_checkint(ls
, 1) : 0;
199 bool skip_stackable
= lua_isboolean(ls
, 2) ? lua_toboolean(ls
, 2)
201 std::vector
<const item_def
*> floor_items
;
202 std::vector
<const item_def
*> shop_items
;
204 for (ST_ItemIterator stii
; stii
; ++stii
)
206 if (skip_stackable
&& is_stackable_item(*stii
))
212 if (stii
.price() < min_value
)
215 else if (item_value(*stii
, true) < min_value
)
219 shop_items
.push_back(&(*stii
));
221 floor_items
.push_back(&(*stii
));
227 for (unsigned int i
= 0; i
< floor_items
.size(); i
++)
229 clua_push_item(ls
, const_cast<item_def
*>(floor_items
[i
]));
230 lua_rawseti(ls
, -2, ++index
);
236 for (unsigned int i
= 0; i
< shop_items
.size(); i
++)
238 clua_push_item(ls
, const_cast<item_def
*>(shop_items
[i
]));
239 lua_rawseti(ls
, -2, ++index
);
245 const struct luaL_reg dgn_item_dlib
[] =
247 { "item_from_index", dgn_item_from_index
},
248 { "items_at", dgn_items_at
},
249 { "create_item", dgn_create_item
},
250 { "item_property_remove", dgn_item_property_remove
},
251 { "item_property_set", dgn_item_property_set
},
252 { "item_property", dgn_item_property
},
253 { "item_spec", _dgn_item_spec
},
254 { "stash_items", dgn_stash_items
},