Source:NetHack 5.0.0/src/nhlobj.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to nhlobj.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/nhlobj.c#line12]], for example.

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1. /* NetHack 5.0	nhlobj.c	$NHDT-Date: 1576097301 2019/12/11 20:48:21 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.0 $ */
2. /*      Copyright (c) 2019 by Pasi Kallinen */
3. /* NetHack may be freely redistributed.  See license for details. */
4. 
5. #include "hack.h"
6. #include "sp_lev.h"
7. 
8. struct _lua_obj {
9.     int state; /* UNUSED */
10.     struct obj *obj;
11. };
12. 
13. staticfn struct _lua_obj *l_obj_check(lua_State *, int);
14. staticfn int l_obj_add_to_container(lua_State *);
15. staticfn int l_obj_gc(lua_State *);
16. staticfn int l_obj_getcontents(lua_State *);
17. staticfn int l_obj_isnull(lua_State *);
18. staticfn int l_obj_new_readobjnam(lua_State *);
19. staticfn int l_obj_nextobj(lua_State *);
20. staticfn int l_obj_objects_to_table(lua_State *);
21. staticfn int l_obj_placeobj(lua_State *);
22. staticfn int l_obj_to_table(lua_State *);
23. staticfn int l_obj_at(lua_State *);
24. staticfn int l_obj_container(lua_State *);
25. staticfn int l_obj_timer_has(lua_State *);
26. staticfn int l_obj_timer_peek(lua_State *);
27. staticfn int l_obj_timer_stop(lua_State *);
28. staticfn int l_obj_timer_start(lua_State *);
29. staticfn int l_obj_bury(lua_State *);
30. staticfn struct _lua_obj *l_obj_push(lua_State *, struct obj *);
31. 
32. #define lobj_is_ok(lo) ((lo) && (lo)->obj && (lo)->obj->where != OBJ_LUAFREE)
33. 
34. staticfn struct _lua_obj *
35. l_obj_check(lua_State *L, int indx)
36. {
37.     struct _lua_obj *lo;
38. 
39.     luaL_checktype(L, indx, LUA_TUSERDATA);
40.     lo = (struct _lua_obj *) luaL_checkudata(L, indx, "obj");
41.     if (!lo)
42.         nhl_error(L, "Obj error");
43.     return lo;
44. }
45. 
46. staticfn int
47. l_obj_gc(lua_State *L)
48. {
49.     struct obj *obj, *otmp;
50.     struct _lua_obj *lo = l_obj_check(L, 1);
51. 
52.     if (lo && (obj = lo->obj) != 0) {
53.         if (obj->lua_ref_cnt > 0)
54.             obj->lua_ref_cnt--;
55.         /* free-floating objects with no other refs are deallocated. */
56.         if (!obj->lua_ref_cnt
57.             && (obj->where == OBJ_FREE || obj->where == OBJ_LUAFREE)) {
58.             if (Has_contents(obj)) {
59.                 while ((otmp = obj->cobj) != 0) {
60.                     obj_extract_self(otmp);
61.                     dealloc_obj(otmp);
62.                 }
63.             }
64.             obj->where = OBJ_FREE;
65.             dealloc_obj(obj), obj = 0;
66.         }
67.         lo->obj = NULL;
68.     }
69.     return 0;
70. }
71. 
72. staticfn struct _lua_obj *
73. l_obj_push(lua_State *L, struct obj *otmp)
74. {
75.     struct _lua_obj *lo
76.         = (struct _lua_obj *) lua_newuserdata(L, sizeof (struct _lua_obj));
77.     luaL_getmetatable(L, "obj");
78.     lua_setmetatable(L, -2);
79. 
80.     lo->state = 0;
81.     lo->obj = otmp;
82.     if (otmp)
83.         otmp->lua_ref_cnt++;
84. 
85.     return lo;
86. }
87. 
88. void
89. nhl_push_obj(lua_State *L, struct obj *otmp)
90. {
91.     (void) l_obj_push(L, otmp);
92. }
93. 
94. /* local o = obj.new("large chest");
95.    local cobj = o:contents(); */
96. staticfn int
97. l_obj_getcontents(lua_State *L)
98. {
99.     struct _lua_obj *lo = l_obj_check(L, 1);
100.     struct obj *obj = lo->obj;
101. 
102.     if (!obj)
103.         nhl_error(L, "l_obj_getcontents: no obj");
104. 
105.     (void) l_obj_push(L, obj->cobj);
106.     return 1;
107. }
108. 
109. /* Puts object inside another object. */
110. /* local box = obj.new("large chest");
111.    box:addcontent(obj.new("rock"));
112. */
113. staticfn int
114. l_obj_add_to_container(lua_State *L)
115. {
116.     struct _lua_obj *lobox = l_obj_check(L, 1);
117.     struct _lua_obj *lo = l_obj_check(L, 2);
118.     struct obj *otmp;
119.     int refs;
120. 
121.     if (!lobj_is_ok(lo) || !lobj_is_ok(lobox))
122.         return 0;
123. 
124.     refs = lo->obj->lua_ref_cnt;
125. 
126.     obj_extract_self(lo->obj);
127.     otmp = add_to_container(lobox->obj, lo->obj);
128. 
129.     /* was lo->obj merged? */
130.     if (otmp != lo->obj) {
131.         lo->obj = otmp;
132.         lo->obj->lua_ref_cnt += refs;
133.     }
134.     lobox->obj->owt = weight(lobox->obj);
135. 
136.     return 0;
137. }
138. 
139. /* Put object into player's inventory */
140. /* u.giveobj(obj.new("rock")); */
141. int
142. nhl_obj_u_giveobj(lua_State *L)
143. {
144.     struct _lua_obj *lo = l_obj_check(L, 1);
145.     struct obj *otmp;
146.     int refs;
147. 
148.     if (!lobj_is_ok(lo) || lo->obj->where == OBJ_INVENT)
149.         return 0;
150. 
151.     refs = lo->obj->lua_ref_cnt;
152. 
153.     obj_extract_self(lo->obj);
154.     otmp = addinv(lo->obj);
155. 
156.     if (otmp != lo->obj) {
157.         lo->obj->lua_ref_cnt += refs;
158.         lo->obj = otmp;
159.     }
160. 
161.     return 0;
162. }
163. 
164. DISABLE_WARNING_UNREACHABLE_CODE
165. 
166. /* Get a table of object class data. */
167. /* local odata = obj.class(otbl.otyp); */
168. /* local odata = obj.class(obj.new("rock")); */
169. /* local odata = o:class(); */
170. staticfn int
171. l_obj_objects_to_table(lua_State *L)
172. {
173.     int argc = lua_gettop(L);
174.     int otyp = -1;
175.     struct objclass *o;
176. 
177.     if (argc != 1) {
178.         nhl_error(L, "l_obj_objects_to_table: Wrong args");
179.         /*NOTREACHED*/
180.         return 0;
181.     }
182. 
183.     if (lua_type(L, 1) == LUA_TNUMBER) {
184.         otyp = (int) luaL_checkinteger(L, 1);
185.     } else if (lua_type(L, 1) == LUA_TUSERDATA) {
186.         struct _lua_obj *lo = l_obj_check(L, 1);
187.         if (lo && lo->obj)
188.             otyp = lo->obj->otyp;
189.     }
190.     lua_pop(L, 1);
191. 
192.     if (otyp == -1) {
193.         nhl_error(L, "l_obj_objects_to_table: Wrong args");
194.         /*NOTREACHED*/
195.         return 0;
196.     }
197. 
198.     o = &objects[otyp];
199. 
200.     lua_newtable(L);
201. 
202.     if (OBJ_NAME(objects[otyp]))
203.         nhl_add_table_entry_str(L, "name", OBJ_NAME(objects[otyp]));
204.     if (OBJ_DESCR(objects[otyp]))
205.         nhl_add_table_entry_str(L, "descr",
206.                                 OBJ_DESCR(objects[otyp]));
207.     if (o->oc_uname)
208.         nhl_add_table_entry_str(L, "uname", o->oc_uname);
209. 
210.     nhl_add_table_entry_int(L, "name_known", o->oc_name_known);
211.     nhl_add_table_entry_int(L, "merge", o->oc_merge);
212.     nhl_add_table_entry_int(L, "uses_known", o->oc_uses_known);
213.     nhl_add_table_entry_int(L, "encountered", o->oc_encountered);
214.     nhl_add_table_entry_int(L, "magic", o->oc_magic);
215.     nhl_add_table_entry_int(L, "charged", o->oc_charged);
216.     nhl_add_table_entry_int(L, "unique", o->oc_unique);
217.     nhl_add_table_entry_int(L, "nowish", o->oc_nowish);
218.     nhl_add_table_entry_int(L, "big", o->oc_big);
219.     /* TODO: oc_bimanual, oc_bulky */
220.     nhl_add_table_entry_int(L, "tough", o->oc_tough);
221.     nhl_add_table_entry_int(L, "dir", o->oc_dir); /* TODO: convert to text */
222.     nhl_add_table_entry_str(L, "material", materialnm[o->oc_material]);
223.     /* TODO: oc_subtyp, oc_skill, oc_armcat */
224.     nhl_add_table_entry_int(L, "oprop", o->oc_oprop);
225.     nhl_add_table_entry_char(L, "class",
226.                              def_oc_syms[(uchar) o->oc_class].sym);
227.     nhl_add_table_entry_int(L, "delay", o->oc_delay);
228.     nhl_add_table_entry_int(L, "color", o->oc_color); /* TODO: text? */
229.     nhl_add_table_entry_int(L, "prob", o->oc_prob);
230.     nhl_add_table_entry_int(L, "weight", o->oc_weight);
231.     nhl_add_table_entry_int(L, "cost", o->oc_cost);
232.     nhl_add_table_entry_int(L, "damage_small", o->oc_wsdam);
233.     nhl_add_table_entry_int(L, "damage_large", o->oc_wldam);
234.     /* TODO: oc_oc1, oc_oc2, oc_hitbon, a_ac, a_can, oc_level */
235.     nhl_add_table_entry_int(L, "nutrition", o->oc_nutrition);
236. 
237.     return 1;
238. }
239. 
240. RESTORE_WARNING_UNREACHABLE_CODE
241. 
242. /* Create a lua table representation of the object, unpacking all the
243.    object fields.
244.    local o = obj.new("rock");
245.    local otbl = o:totable(); */
246. staticfn int
247. l_obj_to_table(lua_State *L)
248. {
249.     struct _lua_obj *lo = l_obj_check(L, 1);
250.     struct obj *obj = lo->obj;
251. 
252.     lua_newtable(L);
253. 
254.     if (!obj || obj->where == OBJ_LUAFREE) {
255.         nhl_add_table_entry_int(L, "NO_OBJ", 1);
256.         return 1;
257.     }
258. 
259.     nhl_add_table_entry_int(L, "has_contents", Has_contents(obj));
260.     nhl_add_table_entry_int(L, "is_container", Is_container(obj));
261.     nhl_add_table_entry_int(L, "o_id", obj->o_id);
262.     nhl_add_table_entry_int(L, "ox", obj->ox);
263.     nhl_add_table_entry_int(L, "oy", obj->oy);
264.     nhl_add_table_entry_int(L, "otyp", obj->otyp);
265.     if (OBJ_NAME(objects[obj->otyp]))
266.         nhl_add_table_entry_str(L, "otyp_name", OBJ_NAME(objects[obj->otyp]));
267.     if (OBJ_DESCR(objects[obj->otyp]))
268.         nhl_add_table_entry_str(L, "otyp_descr",
269.                                 OBJ_DESCR(objects[obj->otyp]));
270.     nhl_add_table_entry_int(L, "owt", obj->owt);
271.     nhl_add_table_entry_int(L, "quan", obj->quan);
272.     nhl_add_table_entry_int(L, "spe", obj->spe);
273. 
274.     if (obj->otyp == STATUE)
275.         nhl_add_table_entry_int(L, "historic",
276.                                 (obj->spe & CORPSTAT_HISTORIC) != 0);
277.     if (obj->otyp == CORPSE || obj->otyp == STATUE) {
278.         nhl_add_table_entry_int(L, "male",
279.                                 (obj->spe & CORPSTAT_MALE) != 0);
280.         nhl_add_table_entry_int(L, "female",
281.                                 (obj->spe & CORPSTAT_FEMALE) != 0);
282.     }
283. 
284.     nhl_add_table_entry_char(L, "oclass",
285.                              def_oc_syms[(uchar) obj->oclass].sym);
286.     nhl_add_table_entry_char(L, "invlet", obj->invlet);
287.     /* TODO: nhl_add_table_entry_char(L, "oartifact", obj->oartifact);*/
288.     nhl_add_table_entry_int(L, "where", obj->where);
289.     /* TODO: nhl_add_table_entry_int(L, "timed", obj->timed); */
290.     nhl_add_table_entry_int(L, "cursed", obj->cursed);
291.     nhl_add_table_entry_int(L, "blessed", obj->blessed);
292.     nhl_add_table_entry_int(L, "unpaid", obj->unpaid);
293.     nhl_add_table_entry_int(L, "no_charge", obj->no_charge);
294.     nhl_add_table_entry_int(L, "known", obj->known);
295.     nhl_add_table_entry_int(L, "dknown", obj->dknown);
296.     nhl_add_table_entry_int(L, "bknown", obj->bknown);
297.     nhl_add_table_entry_int(L, "rknown", obj->rknown);
298.     nhl_add_table_entry_int(L, "tknown", obj->tknown);
299.     if (obj->oclass == POTION_CLASS)
300.         nhl_add_table_entry_int(L, "odiluted", obj->odiluted);
301.     else
302.         nhl_add_table_entry_int(L, "oeroded", obj->oeroded);
303.     nhl_add_table_entry_int(L, "oeroded2", obj->oeroded2);
304.     /* TODO: orotten, norevive */
305.     nhl_add_table_entry_int(L, "oerodeproof", obj->oerodeproof);
306.     nhl_add_table_entry_int(L, "olocked", obj->olocked);
307.     nhl_add_table_entry_int(L, "obroken", obj->obroken);
308.     if (is_poisonable(obj))
309.         nhl_add_table_entry_int(L, "opoisoned", obj->opoisoned);
310.     else
311.         nhl_add_table_entry_int(L, "otrapped", obj->otrapped);
312.     /* TODO: degraded_horn */
313.     nhl_add_table_entry_int(L, "recharged", obj->recharged);
314.     /* TODO: on_ice */
315.     nhl_add_table_entry_int(L, "lamplit", obj->lamplit);
316.     nhl_add_table_entry_int(L, "globby", obj->globby);
317.     nhl_add_table_entry_int(L, "greased", obj->greased);
318.     nhl_add_table_entry_int(L, "nomerge", obj->nomerge);
319.     nhl_add_table_entry_int(L, "how_lost", obj->how_lost);
320.     nhl_add_table_entry_int(L, "in_use", obj->in_use);
321.     nhl_add_table_entry_int(L, "bypass", obj->bypass);
322.     nhl_add_table_entry_int(L, "cknown", obj->cknown);
323.     nhl_add_table_entry_int(L, "lknown", obj->lknown);
324.     nhl_add_table_entry_int(L, "corpsenm", obj->corpsenm);
325.     if (obj->corpsenm != NON_PM
326.         && (obj->otyp == TIN || obj->otyp == CORPSE || obj->otyp == EGG
327.             || obj->otyp == FIGURINE || obj->otyp == STATUE))
328.         nhl_add_table_entry_str(L, "corpsenm_name",
329.                                 mons[obj->corpsenm].pmnames[NEUTRAL]);
330.     /* TODO: leashmon, fromsink, novelidx, record_achieve_special */
331.     nhl_add_table_entry_int(L, "usecount", obj->usecount);
332.     /* TODO: spestudied */
333.     nhl_add_table_entry_int(L, "oeaten", obj->oeaten);
334.     nhl_add_table_entry_int(L, "age", obj->age);
335.     nhl_add_table_entry_int(L, "owornmask", obj->owornmask);
336.     /* TODO: more of oextra */
337.     nhl_add_table_entry_int(L, "has_oname", has_oname(obj));
338.     if (has_oname(obj))
339.         nhl_add_table_entry_str(L, "oname", ONAME(obj));
340. 
341.     return 1;
342. }
343. 
344. DISABLE_WARNING_UNREACHABLE_CODE
345. 
346. /* create a new object via wishing routine */
347. /* local o = obj.new("rock"); */
348. /* local o = obj.new({ id = "food ration", class = "%" }); */
349. staticfn int
350. l_obj_new_readobjnam(lua_State *L)
351. {
352.     int argc = lua_gettop(L);
353. 
354.     if (argc == 1 && lua_type(L, 1) == LUA_TSTRING) {
355.         char buf[BUFSZ];
356.         struct obj *otmp;
357. 
358.         Sprintf(buf, "%s", luaL_checkstring(L, 1));
359.         lua_pop(L, 1);
360.         if ((otmp = readobjnam(buf, NULL)) == &hands_obj)
361.             otmp = NULL;
362.         (void) l_obj_push(L, otmp);
363.         return 1;
364.     } else if (argc == 1 && lua_type(L, 1) == LUA_TTABLE) {
365.         short id = get_table_objtype(L);
366.         xint16 class = get_table_objclass(L);
367.         struct obj *otmp;
368. 
369.         if (id >= FIRST_OBJECT) {
370.             otmp = mksobj(id, TRUE, FALSE);
371.         } else {
372.             class = def_char_to_objclass(class);
373.             if (class >= MAXOCLASSES)
374.                 class = RANDOM_CLASS;
375.             otmp = mkobj(class, FALSE);
376.         }
377.         lua_pop(L, 1);
378.         (void) l_obj_push(L, otmp);
379.         return 1;
380.     } else
381.         nhl_error(L, "l_obj_new_readobjname: Wrong args");
382.     /*NOTREACHED*/
383.     return 0;
384. }
385. 
386. /* Get the topmost object on the map at x,y */
387. /* local o = obj.at(x, y); */
388. staticfn int
389. l_obj_at(lua_State *L)
390. {
391.     int argc = lua_gettop(L);
392. 
393.     if (argc == 2) {
394.         coordxy x, y;
395. 
396.         x = (coordxy) luaL_checkinteger(L, 1);
397.         y = (coordxy) luaL_checkinteger(L, 2);
398.         cvt_to_abscoord(&x, &y);
399. 
400.         lua_pop(L, 2);
401.         (void) l_obj_push(L, svl.level.objects[x][y]);
402.         return 1;
403.     } else
404.         nhl_error(L, "l_obj_at: Wrong args");
405.     /*NOTREACHED*/
406.     return 0;
407. }
408. 
409. /* Place an object on the map at (x,y).
410.    local o = obj.new("rock");
411.    o:placeobj(u.ux, u.uy); */
412. staticfn int
413. l_obj_placeobj(lua_State *L)
414. {
415.     int argc = lua_gettop(L);
416.     struct _lua_obj *lo = l_obj_check(L, 1);
417.     coordxy x, y;
418. 
419.     if (argc != 3)
420.         nhl_error(L, "l_obj_placeobj: Wrong args");
421. 
422.     x = (coordxy) luaL_checkinteger(L, 2);
423.     y = (coordxy) luaL_checkinteger(L, 3);
424.     cvt_to_abscoord(&x, &y);
425. 
426.     lua_pop(L, 3);
427. 
428.     if (lobj_is_ok(lo)) {
429.         obj_extract_self(lo->obj);
430.         place_object(lo->obj, x, y);
431.         newsym(x, y);
432.     }
433. 
434.     return 0;
435. }
436. 
437. RESTORE_WARNING_UNREACHABLE_CODE
438. 
439. /* Get the next object in the object chain */
440. /* local o = obj.at(x, y);
441.    local o2 = o:next(true);
442.    local firstobj = obj.next();
443. */
444. staticfn int
445. l_obj_nextobj(lua_State *L)
446. {
447.     int argc = lua_gettop(L);
448. 
449.     if (argc == 0) {
450.         (void) l_obj_push(L, fobj);
451.     } else {
452.         struct _lua_obj *lo = l_obj_check(L, 1);
453.         boolean use_nexthere = FALSE;
454. 
455.         if (argc == 2)
456.             use_nexthere = lua_toboolean(L, 2);
457. 
458.         if (lo && lo->obj)
459.             (void) l_obj_push(L, (use_nexthere && lo->obj->where == OBJ_FLOOR)
460.                                   ? lo->obj->nexthere
461.                                   : lo->obj->nobj);
462.     }
463.     return 1;
464. }
465. 
466. /* Get the container object is in */
467. /* local box = o:container(); */
468. staticfn int
469. l_obj_container(lua_State *L)
470. {
471.     struct _lua_obj *lo = l_obj_check(L, 1);
472. 
473.     if (lo && lo->obj && lo->obj->where == OBJ_CONTAINED)
474.         (void) l_obj_push(L, lo->obj->ocontainer);
475.     else
476.         (void) l_obj_push(L, NULL);
477.     return 1;
478. }
479. 
480. /* Is the object a null? */
481. /* local badobj = o:isnull(); */
482. staticfn int
483. l_obj_isnull(lua_State *L)
484. {
485.     struct _lua_obj *lo = l_obj_check(L, 1);
486. 
487.     lua_pushboolean(L, !lobj_is_ok(lo));
488.     return 1;
489. }
490. 
491. DISABLE_WARNING_UNREACHABLE_CODE
492. 
493. /* does object have a timer of certain type? */
494. /* local hastimer = o:has_timer("rot-organic"); */
495. staticfn int
496. l_obj_timer_has(lua_State *L)
497. {
498.     int argc = lua_gettop(L);
499. 
500.     if (argc == 2) {
501.         struct _lua_obj *lo = l_obj_check(L, 1);
502.         short timertype = nhl_get_timertype(L, 2);
503. 
504.         if (timer_is_obj(timertype) && lo && lo->obj) {
505.             lua_pushboolean(L, obj_has_timer(lo->obj, timertype));
506.             return 1;
507.         } else {
508.             lua_pushboolean(L, FALSE);
509.             return 1;
510.         }
511.     } else
512.         nhl_error(L, "l_obj_timer_has: Wrong args");
513.     return 0;
514. }
515. 
516. /* peek at an object timer. return the turn when timer triggers.
517.    returns 0 if no such timer attached to the object. */
518. /* local timeout = o:peek_timer("hatch-egg"); */
519. staticfn int
520. l_obj_timer_peek(lua_State *L)
521. {
522.     int argc = lua_gettop(L);
523. 
524.     if (argc == 2) {
525.         struct _lua_obj *lo = l_obj_check(L, 1);
526.         short timertype = nhl_get_timertype(L, 2);
527. 
528.         if (timer_is_obj(timertype) && lo && lo->obj) {
529.             lua_pushinteger(L, peek_timer(timertype, obj_to_any(lo->obj)));
530.             return 1;
531.         } else {
532.             lua_pushinteger(L, 0);
533.             return 1;
534.         }
535.     } else
536.         nhl_error(L, "l_obj_timer_peek: Wrong args");
537.     /*NOTREACHED*/
538.     return 0;
539. }
540. 
541. /* stop object timer(s). return the turn when timer triggers.
542.    returns 0 if no such timer attached to the object.
543.    without a timer type parameter, stops all timers for the object. */
544. /* local timeout = o:stop_timer("rot-organic"); */
545. /* o:stop_timer(); */
546. staticfn int
547. l_obj_timer_stop(lua_State *L)
548. {
549.     int argc = lua_gettop(L);
550. 
551.     if (argc == 1) {
552.         struct _lua_obj *lo = l_obj_check(L, 1);
553. 
554.         if (lo && lo->obj)
555.             obj_stop_timers(lo->obj);
556.         return 0;
557. 
558.     } else if (argc == 2) {
559.         struct _lua_obj *lo = l_obj_check(L, 1);
560.         short timertype = nhl_get_timertype(L, 2);
561. 
562.         if (timer_is_obj(timertype) && lo && lo->obj) {
563.             lua_pushinteger(L, stop_timer(timertype, obj_to_any(lo->obj)));
564.             return 1;
565.         } else {
566.             lua_pushinteger(L, 0);
567.             return 1;
568.         }
569.     } else
570.         nhl_error(L, "l_obj_timer_stop: Wrong args");
571.     return 0;
572. }
573. 
574. RESTORE_WARNING_UNREACHABLE_CODE
575. 
576. /* start an object timer. */
577. /* o:start_timer("hatch-egg", 10); */
578. staticfn int
579. l_obj_timer_start(lua_State *L)
580. {
581.     int argc = lua_gettop(L);
582. 
583.     if (argc == 3) {
584.         struct _lua_obj *lo = l_obj_check(L, 1);
585.         short timertype = nhl_get_timertype(L, 2);
586.         long when = luaL_checkinteger(L, 3);
587. 
588.         if (timer_is_obj(timertype) && lo && lo->obj && when > 0) {
589.             if (obj_has_timer(lo->obj, timertype))
590.                 stop_timer(timertype, obj_to_any(lo->obj));
591.             start_timer(when, TIMER_OBJECT, timertype, obj_to_any(lo->obj));
592.         }
593.     } else
594.         nhl_error(L, "l_obj_timer_start: Wrong args");
595.     return 0;
596. }
597. 
598. /* bury an obj. returns true if object is gone (merged with ground),
599.    false otherwise. */
600. /* local ogone = o:bury(); */
601. /* local ogone = o:bury(5,5); */
602. staticfn int
603. l_obj_bury(lua_State *L)
604. {
605.     int argc = lua_gettop(L);
606.     boolean dealloced = FALSE;
607.     struct _lua_obj *lo = l_obj_check(L, 1);
608.     coordxy x = 0, y = 0;
609. 
610.     if (argc == 1) {
611.         x = lo->obj->ox;
612.         y = lo->obj->oy;
613.     } else if (argc == 3) {
614.         x = (coordxy) lua_tointeger(L, 2);
615.         y = (coordxy) lua_tointeger(L, 3);
616.         cvt_to_abscoord(&x, &y);
617.     } else
618.         nhl_error(L, "l_obj_bury: Wrong args");
619. 
620.     if (lobj_is_ok(lo) && isok(x, y)) {
621.         lo->obj->ox = x;
622.         lo->obj->oy = y;
623.         (void) bury_an_obj(lo->obj, &dealloced);
624.     }
625.     lua_pushboolean(L, dealloced);
626.     return 1;
627. }
628. 
629. static const struct luaL_Reg l_obj_methods[] = {
630.     { "new", l_obj_new_readobjnam },
631.     { "isnull", l_obj_isnull },
632.     { "at", l_obj_at },
633.     { "next", l_obj_nextobj },
634.     { "totable", l_obj_to_table },
635.     { "class", l_obj_objects_to_table },
636.     { "placeobj", l_obj_placeobj },
637.     { "container", l_obj_container },
638.     { "contents", l_obj_getcontents },
639.     { "addcontent", l_obj_add_to_container },
640.     { "has_timer", l_obj_timer_has },
641.     { "peek_timer", l_obj_timer_peek },
642.     { "stop_timer", l_obj_timer_stop },
643.     { "start_timer", l_obj_timer_start },
644.     { "bury", l_obj_bury },
645.     { NULL, NULL }
646. };
647. 
648. static const luaL_Reg l_obj_meta[] = {
649.     { "__gc", l_obj_gc },
650.     { NULL, NULL }
651. };
652. 
653. int
654. l_obj_register(lua_State *L)
655. {
656.     /* Table of instance methods (e.g. an_object:isnull())
657.        and static methods (e.g. obj.new("dagger")). */
658.     luaL_newlib(L, l_obj_methods);
659. 
660.     /* metatable = { __name = "obj", __gc = l_obj_gc } */
661.     luaL_newmetatable(L, "obj");
662.     luaL_setfuncs(L, l_obj_meta, 0);
663.     /* metatable.__index points at the object method table. */
664.     lua_pushvalue(L, -2);
665.     lua_setfield(L, -2, "__index");
666. 
667.     /* Don't let lua code mess with the real metatable.
668.        Instead offer a fake one that only contains __gc. */
669.     luaL_newlib(L, l_obj_meta);
670.     lua_setfield(L, -2, "__metatable");
671. 
672.     /* We don't need the metatable anymore. It's safe in the
673.        Lua registry for use by luaL_setmetatable. */
674.     lua_pop(L, 1);
675. 
676.     /* global obj = the method table we created at the start */
677.     lua_setglobal(L, "obj");
678.     return 0;
679. }
680. 
681. /* for 'onefile' processing where end of this file isn't necessarily the
682.    end of the source code seen by the compiler */
683. #undef lobj_is_ok
684. 
685. /*nhlobj.c*/