… | |
… | |
99 | |
99 | |
100 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
100 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
101 | |
101 | |
102 | unordered_vector<attachable *> attachable::mortals; |
102 | unordered_vector<attachable *> attachable::mortals; |
103 | |
103 | |
104 | #if 0 |
|
|
105 | attachable *attachable::rc_first; |
|
|
106 | |
|
|
107 | attachable::attachable () |
|
|
108 | { |
|
|
109 | refcnt = 0; |
|
|
110 | rc_next = rc_first; |
|
|
111 | rc_first = this; |
|
|
112 | } |
|
|
113 | #endif |
|
|
114 | |
|
|
115 | attachable::~attachable () |
104 | attachable::~attachable () |
116 | { |
105 | { |
117 | assert (!(flags & F_BORROWED));//D//TODO//remove when stable |
|
|
118 | #if 0 |
|
|
119 | assert (!rc_next); |
|
|
120 | assert (!refcnt); |
106 | assert (!self); |
121 | #endif |
107 | } |
|
|
108 | |
|
|
109 | int |
|
|
110 | attachable::refcnt_cnt () const |
|
|
111 | { |
|
|
112 | return refcnt + (self ? SvREFCNT (self) : 0); |
|
|
113 | } |
|
|
114 | |
|
|
115 | void |
|
|
116 | attachable::optimise () |
|
|
117 | { |
|
|
118 | if (self |
|
|
119 | && SvREFCNT (self) == 1 |
|
|
120 | && !HvTOTALKEYS (self)) |
|
|
121 | { |
|
|
122 | refcnt_inc (); |
|
|
123 | SvREFCNT_dec ((SV *)self); |
|
|
124 | } |
122 | } |
125 | } |
123 | |
126 | |
124 | // check wether the object really is dead |
127 | // check wether the object really is dead |
125 | void |
128 | void |
126 | attachable::do_check () |
129 | attachable::do_check () |
127 | { |
130 | { |
128 | if (refcnt > 0) |
131 | if (refcnt > 0 || refcnt_cnt () > 0) |
129 | return; |
132 | return; |
130 | |
133 | |
131 | // try to unborrow the refcnt from perl |
134 | destroy (); |
132 | if (flags & F_BORROWED) |
135 | |
|
|
136 | if (self) |
133 | { |
137 | { |
134 | assert (self);//D//TODO//remove when stable |
138 | hv_clear (self); |
135 | flags &= ~F_BORROWED; |
139 | sv_unmagic ((SV *)self, PERL_MAGIC_ext); |
136 | refcnt_inc (); |
|
|
137 | SvREFCNT_dec (self); |
140 | SvREFCNT_dec (self); |
|
|
141 | self = 0; |
138 | } |
142 | } |
139 | |
|
|
140 | if (refcnt > 0 || self) |
|
|
141 | return; |
|
|
142 | |
|
|
143 | destroy (); |
|
|
144 | } |
143 | } |
145 | |
144 | |
146 | void |
145 | void |
147 | attachable::do_destroy () |
146 | attachable::do_destroy () |
148 | { |
147 | { |
149 | invoke (EVENT_ATTACHABLE_DESTROY, DT_END); |
148 | invoke (EVENT_ATTACHABLE_DESTROY, DT_END); |
150 | |
149 | |
151 | if (self) |
150 | if (self) |
152 | hv_clear (self); |
151 | hv_clear (self); |
153 | |
152 | |
154 | //TODO: call generic destroy callback |
|
|
155 | mortals.push_back (this); |
153 | mortals.push_back (this); |
156 | } |
154 | } |
157 | |
155 | |
158 | void |
156 | void |
159 | attachable::destroy () |
157 | attachable::destroy () |
… | |
… | |
163 | |
161 | |
164 | flags |= F_DESTROYED; |
162 | flags |= F_DESTROYED; |
165 | do_destroy (); |
163 | do_destroy (); |
166 | } |
164 | } |
167 | |
165 | |
|
|
166 | void |
168 | void attachable::check_mortals () |
167 | attachable::check_mortals () |
169 | { |
168 | { |
170 | for (int i = 0; i < mortals.size (); ) |
169 | for (int i = 0; i < mortals.size (); ) |
171 | { |
170 | { |
172 | attachable *obj = mortals [i]; |
171 | attachable *obj = mortals [i]; |
173 | |
172 | |
… | |
… | |
217 | |
216 | |
218 | static int |
217 | static int |
219 | attachable_free (pTHX_ SV *sv, MAGIC *mg) |
218 | attachable_free (pTHX_ SV *sv, MAGIC *mg) |
220 | { |
219 | { |
221 | attachable *at = (attachable *)mg->mg_ptr; |
220 | attachable *at = (attachable *)mg->mg_ptr; |
222 | assert (!(at->flags & attachable::F_BORROWED));//D//TODO//remove when stable |
|
|
223 | at->self = 0; |
221 | at->self = 0; |
224 | // next line makes sense, but most objects still have refcnt 0 by default |
222 | // next line makes sense, but most objects still have refcnt 0 by default |
225 | //at->refcnt_chk (); |
223 | //at->refcnt_chk (); |
226 | return 0; |
224 | return 0; |
227 | } |
225 | } |
… | |
… | |
238 | { |
236 | { |
239 | obj->self = newHV (); |
237 | obj->self = newHV (); |
240 | sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0); |
238 | sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0); |
241 | |
239 | |
242 | // borrow the refcnt from the object |
240 | // borrow the refcnt from the object |
243 | obj->flags |= attachable::F_BORROWED; |
241 | // it is important thta no refcnt_chk is being executed here |
244 | obj->refcnt_dec (); |
242 | obj->refcnt_dec (); |
|
|
243 | |
|
|
244 | // now bless the object _once_ |
|
|
245 | return sv_bless (newRV_inc ((SV *)obj->self), stash); |
245 | } |
246 | } |
246 | |
247 | else |
247 | return sv_bless (newRV_inc ((SV *)obj->self), stash); |
248 | return newRV_inc ((SV *)obj->self); |
248 | } |
249 | } |
249 | |
250 | |
250 | static void |
251 | static void |
251 | clearSVptr (SV *sv) |
252 | clearSVptr (SV *sv) |
252 | { |
253 | { |
… | |
… | |
514 | |
515 | |
515 | attach = 0; |
516 | attach = 0; |
516 | } |
517 | } |
517 | #endif |
518 | #endif |
518 | |
519 | |
519 | void attachable::optimise () |
|
|
520 | { |
|
|
521 | if (self |
|
|
522 | && SvREFCNT (self) == 1 |
|
|
523 | && !HvTOTALKEYS (self)) |
|
|
524 | { |
|
|
525 | flags &= ~F_BORROWED; |
|
|
526 | refcnt_inc (); |
|
|
527 | SvREFCNT_dec ((SV *)self); |
|
|
528 | } |
|
|
529 | } |
|
|
530 | |
|
|
531 | ///////////////////////////////////////////////////////////////////////////// |
520 | ///////////////////////////////////////////////////////////////////////////// |
532 | |
521 | |
533 | extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr) |
522 | extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr) |
534 | { |
523 | { |
535 | return 0; |
524 | return 0; |
… | |
… | |
829 | } |
818 | } |
830 | |
819 | |
831 | ///////////////////////////////////////////////////////////////////////////// |
820 | ///////////////////////////////////////////////////////////////////////////// |
832 | |
821 | |
833 | void |
822 | void |
834 | maptile::emergency_save () |
823 | cfperl_emergency_save () |
835 | { |
824 | { |
836 | CALL_BEGIN (0); |
825 | CALL_BEGIN (0); |
837 | CALL_CALL ("cf::map::emergency_save", G_VOID); |
826 | CALL_CALL ("cf::emergency_save", G_VOID); |
838 | CALL_END; |
827 | CALL_END; |
839 | } |
828 | } |
840 | |
829 | |
841 | maptile * |
830 | maptile * |
842 | maptile::find_sync (const char *path, maptile *origin) |
831 | maptile::find_sync (const char *path, maptile *origin) |
… | |
… | |
1615 | } |
1604 | } |
1616 | OUTPUT: RETVAL |
1605 | OUTPUT: RETVAL |
1617 | |
1606 | |
1618 | void abort () |
1607 | void abort () |
1619 | |
1608 | |
|
|
1609 | void cleanup (const char *cause, bool make_core = false) |
|
|
1610 | |
1620 | void emergency_save () |
1611 | void emergency_save () |
1621 | |
1612 | |
1622 | void _exit (int status = 0) |
1613 | void _exit (int status = 0) |
1623 | |
1614 | |
1624 | #if _POSIX_MEMLOCK |
1615 | #if _POSIX_MEMLOCK |
… | |
… | |
1714 | PPCODE: |
1705 | PPCODE: |
1715 | EXTEND (SP, object::mortals.size ()); |
1706 | EXTEND (SP, object::mortals.size ()); |
1716 | for (AUTODECL (i, object::mortals.begin ()); i != object::mortals.end (); ++i) |
1707 | for (AUTODECL (i, object::mortals.begin ()); i != object::mortals.end (); ++i) |
1717 | PUSHs (to_sv (*i)); |
1708 | PUSHs (to_sv (*i)); |
1718 | |
1709 | |
1719 | object *first () |
1710 | int objects_size () |
1720 | CODE: |
1711 | CODE: |
1721 | RETVAL = object::first; |
1712 | RETVAL = objects.size (); |
|
|
1713 | OUTPUT: RETVAL |
|
|
1714 | |
|
|
1715 | object *objects (U32 index) |
|
|
1716 | CODE: |
|
|
1717 | RETVAL = index < objects.size () ? objects [index] : 0; |
|
|
1718 | OUTPUT: RETVAL |
|
|
1719 | |
|
|
1720 | int actives_size () |
|
|
1721 | CODE: |
|
|
1722 | RETVAL = actives.size (); |
|
|
1723 | OUTPUT: RETVAL |
|
|
1724 | |
|
|
1725 | object *actives (U32 index) |
|
|
1726 | CODE: |
|
|
1727 | RETVAL = index < actives.size () ? actives [index] : 0; |
1722 | OUTPUT: RETVAL |
1728 | OUTPUT: RETVAL |
1723 | |
1729 | |
1724 | # missing properties |
1730 | # missing properties |
1725 | |
1731 | |
1726 | object *head (object *op) |
1732 | object *head (object *op) |
1727 | PROTOTYPE: $ |
1733 | PROTOTYPE: $ |
1728 | CODE: |
1734 | CODE: |
1729 | RETVAL = op->head ? op->head : op; |
1735 | RETVAL = op->head_ (); |
1730 | OUTPUT: RETVAL |
1736 | OUTPUT: RETVAL |
1731 | |
1737 | |
1732 | int is_head (object *op) |
1738 | int is_head (object *op) |
1733 | PROTOTYPE: $ |
1739 | PROTOTYPE: $ |
1734 | CODE: |
1740 | CODE: |
1735 | RETVAL = !op->head; |
1741 | RETVAL = op->head_ () == op; |
1736 | OUTPUT: RETVAL |
1742 | OUTPUT: RETVAL |
1737 | |
1743 | |
1738 | void |
1744 | void |
1739 | inv (object *obj) |
1745 | inv (object *obj) |
1740 | PROTOTYPE: $ |
1746 | PROTOTYPE: $ |
… | |
… | |
2052 | void |
2058 | void |
2053 | list () |
2059 | list () |
2054 | PPCODE: |
2060 | PPCODE: |
2055 | for_all_players (pl) |
2061 | for_all_players (pl) |
2056 | XPUSHs (sv_2mortal (to_sv (pl))); |
2062 | XPUSHs (sv_2mortal (to_sv (pl))); |
2057 | |
|
|
2058 | bool |
|
|
2059 | peaceful (player *pl, bool new_setting = 0) |
|
|
2060 | PROTOTYPE: $;$ |
|
|
2061 | CODE: |
|
|
2062 | RETVAL = pl->peaceful; |
|
|
2063 | if (items > 1) |
|
|
2064 | pl->peaceful = new_setting; |
|
|
2065 | OUTPUT: |
|
|
2066 | RETVAL |
|
|
2067 | |
|
|
2068 | living * |
|
|
2069 | orig_stats (player *pl) |
|
|
2070 | CODE: |
|
|
2071 | RETVAL = &pl->orig_stats; |
|
|
2072 | OUTPUT: RETVAL |
|
|
2073 | |
|
|
2074 | living * |
|
|
2075 | last_stats (player *pl) |
|
|
2076 | CODE: |
|
|
2077 | RETVAL = &pl->last_stats; |
|
|
2078 | OUTPUT: RETVAL |
|
|
2079 | |
2063 | |
2080 | |
2064 | |
2081 | MODULE = cf PACKAGE = cf::map PREFIX = cf_map_ |
2065 | MODULE = cf PACKAGE = cf::map PREFIX = cf_map_ |
2082 | |
2066 | |
2083 | int invoke (maptile *map, int event, ...) |
2067 | int invoke (maptile *map, int event, ...) |