… | |
… | |
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 |
106 | assert (!(flags & F_BORROWED)); |
118 | #if 0 |
107 | } |
119 | assert (!rc_next); |
108 | |
120 | assert (!refcnt); |
109 | int |
121 | #endif |
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 | flags &= ~F_BORROWED; |
|
|
123 | refcnt_inc (); |
|
|
124 | SvREFCNT_dec ((SV *)self); |
|
|
125 | } |
122 | } |
126 | } |
123 | |
127 | |
124 | // check wether the object really is dead |
128 | // check wether the object really is dead |
125 | void |
129 | void |
126 | attachable::do_check () |
130 | attachable::do_check () |
127 | { |
131 | { |
128 | if (refcnt > 0) |
132 | if (refcnt > 0) |
129 | return; |
133 | return; |
130 | |
134 | |
131 | // try to unborrow the refcnt from perl |
135 | optimise (); |
132 | if (flags & F_BORROWED) |
136 | |
|
|
137 | if (self) |
133 | { |
138 | { |
134 | assert (self);//D//TODO//remove when stable |
139 | if (refcnt + SvREFCNT (self) > 0) |
|
|
140 | return; |
|
|
141 | |
|
|
142 | assert (flags & F_BORROWED); |
|
|
143 | |
135 | flags &= ~F_BORROWED; |
144 | flags &= ~F_BORROWED; |
136 | refcnt_inc (); |
145 | refcnt_inc (); |
137 | SvREFCNT_dec (self); |
146 | SvREFCNT_dec (self); |
|
|
147 | self = 0; |
138 | } |
148 | } |
139 | |
149 | |
140 | if (refcnt > 0 || self) |
150 | if (refcnt > 0) |
141 | return; |
151 | return; |
142 | |
152 | |
143 | destroy (); |
153 | destroy (); |
144 | } |
154 | } |
145 | |
155 | |
… | |
… | |
149 | invoke (EVENT_ATTACHABLE_DESTROY, DT_END); |
159 | invoke (EVENT_ATTACHABLE_DESTROY, DT_END); |
150 | |
160 | |
151 | if (self) |
161 | if (self) |
152 | hv_clear (self); |
162 | hv_clear (self); |
153 | |
163 | |
154 | //TODO: call generic destroy callback |
|
|
155 | mortals.push_back (this); |
164 | mortals.push_back (this); |
156 | } |
165 | } |
157 | |
166 | |
158 | void |
167 | void |
159 | attachable::destroy () |
168 | attachable::destroy () |
… | |
… | |
163 | |
172 | |
164 | flags |= F_DESTROYED; |
173 | flags |= F_DESTROYED; |
165 | do_destroy (); |
174 | do_destroy (); |
166 | } |
175 | } |
167 | |
176 | |
|
|
177 | void |
168 | void attachable::check_mortals () |
178 | attachable::check_mortals () |
169 | { |
179 | { |
170 | for (int i = 0; i < mortals.size (); ) |
180 | for (int i = 0; i < mortals.size (); ) |
171 | { |
181 | { |
172 | attachable *obj = mortals [i]; |
182 | attachable *obj = mortals [i]; |
173 | |
183 | |
… | |
… | |
217 | |
227 | |
218 | static int |
228 | static int |
219 | attachable_free (pTHX_ SV *sv, MAGIC *mg) |
229 | attachable_free (pTHX_ SV *sv, MAGIC *mg) |
220 | { |
230 | { |
221 | attachable *at = (attachable *)mg->mg_ptr; |
231 | attachable *at = (attachable *)mg->mg_ptr; |
222 | assert (!(at->flags & attachable::F_BORROWED));//D//TODO//remove when stable |
232 | assert (!(at->flags & attachable::F_BORROWED)); |
223 | at->self = 0; |
233 | at->self = 0; |
224 | // next line makes sense, but most objects still have refcnt 0 by default |
234 | // next line makes sense, but most objects still have refcnt 0 by default |
225 | //at->refcnt_chk (); |
235 | //at->refcnt_chk (); |
226 | return 0; |
236 | return 0; |
227 | } |
237 | } |
… | |
… | |
240 | sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0); |
250 | sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0); |
241 | |
251 | |
242 | // borrow the refcnt from the object |
252 | // borrow the refcnt from the object |
243 | obj->flags |= attachable::F_BORROWED; |
253 | obj->flags |= attachable::F_BORROWED; |
244 | obj->refcnt_dec (); |
254 | obj->refcnt_dec (); |
|
|
255 | |
|
|
256 | // now bless the object _once_ |
|
|
257 | return sv_bless (newRV_inc ((SV *)obj->self), stash); |
245 | } |
258 | } |
246 | |
259 | else |
247 | return sv_bless (newRV_inc ((SV *)obj->self), stash); |
260 | return newRV_inc ((SV *)obj->self); |
248 | } |
261 | } |
249 | |
262 | |
250 | static void |
263 | static void |
251 | clearSVptr (SV *sv) |
264 | clearSVptr (SV *sv) |
252 | { |
265 | { |
… | |
… | |
513 | } |
526 | } |
514 | |
527 | |
515 | attach = 0; |
528 | attach = 0; |
516 | } |
529 | } |
517 | #endif |
530 | #endif |
518 | |
|
|
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 | |
531 | ///////////////////////////////////////////////////////////////////////////// |
532 | ///////////////////////////////////////////////////////////////////////////// |
532 | |
533 | |
533 | extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr) |
534 | extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr) |
534 | { |
535 | { |
… | |
… | |
2053 | list () |
2054 | list () |
2054 | PPCODE: |
2055 | PPCODE: |
2055 | for_all_players (pl) |
2056 | for_all_players (pl) |
2056 | XPUSHs (sv_2mortal (to_sv (pl))); |
2057 | XPUSHs (sv_2mortal (to_sv (pl))); |
2057 | |
2058 | |
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 | |
|
|
2080 | |
2059 | |
2081 | MODULE = cf PACKAGE = cf::map PREFIX = cf_map_ |
2060 | MODULE = cf PACKAGE = cf::map PREFIX = cf_map_ |
2082 | |
2061 | |
2083 | int invoke (maptile *map, int event, ...) |
2062 | int invoke (maptile *map, int event, ...) |
2084 | CODE: |
2063 | CODE: |