… | |
… | |
21 | * You should have received a copy of the GNU General Public License |
21 | * You should have received a copy of the GNU General Public License |
22 | * along with this program; if not, write to the Free Software |
22 | * along with this program; if not, write to the Free Software |
23 | * Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
23 | * Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
24 | */ |
24 | */ |
25 | |
25 | |
|
|
26 | #include "autoconf.h" |
|
|
27 | |
26 | #define PLUGIN_NAME "perl" |
28 | #define PLUGIN_NAME "perl" |
27 | #define PLUGIN_VERSION "cfperl 0.5" |
29 | #define PLUGIN_VERSION "cfperl 0.5" |
28 | |
30 | |
29 | #define CEDES_PER_TICK 5 |
31 | #define CEDES_PER_TICK 5 |
|
|
32 | |
|
|
33 | #if HAVE_EXECINFO_H |
|
|
34 | # include <execinfo.h> |
|
|
35 | #endif |
30 | |
36 | |
31 | #include <plugin_common.h> |
37 | #include <plugin_common.h> |
32 | #include <sounds.h> |
38 | #include <sounds.h> |
33 | #include <cstdarg> |
39 | #include <cstdarg> |
34 | #include <sproto.h> |
40 | #include <sproto.h> |
… | |
… | |
111 | |
117 | |
112 | unordered_vector<attachable *> attachable::mortals; |
118 | unordered_vector<attachable *> attachable::mortals; |
113 | |
119 | |
114 | attachable::~attachable () |
120 | attachable::~attachable () |
115 | { |
121 | { |
116 | flags |=0x3300;//D |
|
|
117 | assert (!self); |
122 | assert (!self); |
118 | assert (!cb); |
123 | assert (!cb); |
119 | } |
124 | } |
120 | |
125 | |
121 | int |
126 | int |
… | |
… | |
137 | sv_unmagic ((SV *)self, PERL_MAGIC_ext); |
142 | sv_unmagic ((SV *)self, PERL_MAGIC_ext); |
138 | SvREFCNT_dec (self); |
143 | SvREFCNT_dec (self); |
139 | |
144 | |
140 | // self *must* be null now because thats sv_unmagic's job. |
145 | // self *must* be null now because thats sv_unmagic's job. |
141 | assert (!this->self); |
146 | assert (!this->self); |
142 | flags |= 0x80; // severed //D |
|
|
143 | } |
147 | } |
144 | } |
148 | } |
145 | |
149 | |
146 | void |
150 | void |
147 | attachable::optimise () |
151 | attachable::optimise () |
148 | { |
152 | { |
149 | if (self |
153 | if (self |
150 | && SvREFCNT (self) == 1 |
154 | && SvREFCNT (self) == 1 |
151 | && !HvTOTALKEYS (self)) |
155 | && !HvTOTALKEYS (self)) |
152 | flags |= 0x40,//D |
|
|
153 | sever_self (); |
156 | sever_self (); |
154 | } |
157 | } |
155 | |
158 | |
156 | // check wether the object really is dead |
159 | // check wether the object really is dead |
157 | void |
160 | void |
… | |
… | |
172 | { |
175 | { |
173 | SvREFCNT_dec (cb); |
176 | SvREFCNT_dec (cb); |
174 | cb = 0; |
177 | cb = 0; |
175 | } |
178 | } |
176 | |
179 | |
177 | if (self) |
|
|
178 | sever_self (); |
|
|
179 | |
|
|
180 | flags |= 0x20; //D |
|
|
181 | mortals.push_back (this); |
180 | mortals.push_back (this); |
182 | } |
181 | } |
183 | |
182 | |
184 | void |
183 | void |
185 | attachable::destroy () |
184 | attachable::destroy () |
… | |
… | |
187 | if (destroyed ()) |
186 | if (destroyed ()) |
188 | return; |
187 | return; |
189 | |
188 | |
190 | flags |= F_DESTROYED; |
189 | flags |= F_DESTROYED; |
191 | do_destroy (); |
190 | do_destroy (); |
|
|
191 | sever_self (); |
192 | } |
192 | } |
193 | |
193 | |
194 | void |
194 | void |
195 | attachable::check_mortals () |
195 | attachable::check_mortals () |
196 | { |
196 | { |
… | |
… | |
201 | if (i >= mortals.size ()) |
201 | if (i >= mortals.size ()) |
202 | { |
202 | { |
203 | i = 0; |
203 | i = 0; |
204 | |
204 | |
205 | if (mortals.size () > 1000) |
205 | if (mortals.size () > 1000) |
206 | fprintf (stderr, "mortal queue size (%d) exceeds 1000.\n", (int)mortals.size ()); |
206 | LOG (llevInfo, "mortal queue size (%d) exceeds 1000.\n", (int)mortals.size ()); |
207 | |
207 | |
208 | break; |
208 | break; |
209 | } |
209 | } |
210 | |
210 | |
211 | attachable *obj = mortals [i]; |
211 | attachable *obj = mortals [i]; |
212 | |
212 | |
213 | obj->refcnt_chk (); // unborrow from perl, if necessary |
213 | #if 0 |
214 | |
214 | if (obj->self)//D make this an assert later |
215 | //if (obj->refcnt > 0 || obj->self) |
|
|
216 | if (obj->refcnt || obj->self) |
|
|
217 | { |
215 | { |
218 | //printf ("%p rc %d\n", obj, obj->refcnt_cnt ());//D |
216 | LOG (llevError, "check_mortals: object '%s' still has self\n", typeid (obj).name ()); |
|
|
217 | obj->sever_self (); |
|
|
218 | } |
|
|
219 | #endif |
|
|
220 | |
|
|
221 | if (obj->refcnt) |
|
|
222 | { |
219 | ++i; // further delay freeing |
223 | ++i; // further delay freeing |
220 | |
224 | |
221 | if (!(i & 0x3ff)) |
225 | if (!(i & 0x3ff)) |
222 | break; |
226 | break; |
223 | } |
227 | } |
224 | else |
228 | else |
225 | { |
229 | { |
226 | mortals.erase (i); |
230 | mortals.erase (i); |
|
|
231 | obj->sever_self (); |
227 | delete obj; |
232 | delete obj; |
228 | } |
233 | } |
229 | } |
234 | } |
230 | } |
235 | } |
231 | |
236 | |
… | |
… | |
280 | |
285 | |
281 | if (!obj->self) |
286 | if (!obj->self) |
282 | { |
287 | { |
283 | obj->self = newHV (); |
288 | obj->self = newHV (); |
284 | sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0); |
289 | sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0); |
285 | obj->flags |= (obj->flags & 0xc0) << 8; |
|
|
286 | obj->flags &= ~0xc0;//D |
|
|
287 | obj->flags |= 0x10;//D |
|
|
288 | |
290 | |
289 | // now bless the object _once_ |
291 | // now bless the object _once_ |
290 | return sv_bless (newRV_inc ((SV *)obj->self), stash); |
292 | return sv_bless (newRV_inc ((SV *)obj->self), stash); |
291 | } |
293 | } |
292 | else |
294 | else |
… | |
… | |
402 | inline void sv_to (SV *sv, refptr<T> &v) { T *tmp; sv_to (sv, tmp); v = tmp; } |
404 | inline void sv_to (SV *sv, refptr<T> &v) { T *tmp; sv_to (sv, tmp); v = tmp; } |
403 | |
405 | |
404 | template<int N> |
406 | template<int N> |
405 | inline void sv_to (SV *sv, char (&v)[N]) { assign (v, SvPV_nolen (sv)); } |
407 | inline void sv_to (SV *sv, char (&v)[N]) { assign (v, SvPV_nolen (sv)); } |
406 | |
408 | |
407 | inline void sv_to (SV *sv, rangetype &v) { v = (rangetype) SvIV (sv); } |
|
|
408 | inline void sv_to (SV *sv, bowtype_t &v) { v = (bowtype_t) SvIV (sv); } |
409 | inline void sv_to (SV *sv, bowtype_t &v) { v = (bowtype_t) SvIV (sv); } |
409 | inline void sv_to (SV *sv, petmode_t &v) { v = (petmode_t) SvIV (sv); } |
410 | inline void sv_to (SV *sv, petmode_t &v) { v = (petmode_t) SvIV (sv); } |
410 | inline void sv_to (SV *sv, usekeytype &v) { v = (usekeytype) SvIV (sv); } |
411 | inline void sv_to (SV *sv, usekeytype &v) { v = (usekeytype) SvIV (sv); } |
411 | inline void sv_to (SV *sv, unapplymode &v) { v = (unapplymode) SvIV (sv); } |
412 | inline void sv_to (SV *sv, unapplymode &v) { v = (unapplymode) SvIV (sv); } |
412 | |
413 | |
… | |
… | |
615 | perl_construct (perl); |
616 | perl_construct (perl); |
616 | |
617 | |
617 | PL_exit_flags |= PERL_EXIT_DESTRUCT_END; |
618 | PL_exit_flags |= PERL_EXIT_DESTRUCT_END; |
618 | |
619 | |
619 | const char *argv[] = { |
620 | const char *argv[] = { |
620 | "", |
621 | settings.argv [0], |
621 | "-e" |
622 | "-e" |
622 | "use Event; use Coro;" // required for bootstrap |
623 | "use Event; use Coro;" // required for bootstrap |
623 | "cf->bootstrap;" // required for datadir :*> |
624 | "cf->bootstrap;" // required for datadir :*> |
624 | "unshift @INC, cf::datadir ();" |
625 | "unshift @INC, cf::datadir ();" |
625 | "require cf;" |
626 | "require cf;" |
… | |
… | |
947 | CALL_ARG (exit); |
948 | CALL_ARG (exit); |
948 | CALL_CALL ("cf::object::player::enter_exit", G_VOID); |
949 | CALL_CALL ("cf::object::player::enter_exit", G_VOID); |
949 | CALL_END; |
950 | CALL_END; |
950 | } |
951 | } |
951 | |
952 | |
|
|
953 | void |
|
|
954 | log_backtrace (const char *msg) |
|
|
955 | { |
|
|
956 | #if HAVE_BACKTRACE |
|
|
957 | void *addr [20]; |
|
|
958 | int size = backtrace (addr, 20); |
|
|
959 | |
|
|
960 | CALL_BEGIN (size); |
|
|
961 | CALL_ARG (msg); |
|
|
962 | for (int i = 0; i < size; ++i) |
|
|
963 | CALL_ARG ((IV)addr [i]); |
|
|
964 | CALL_CALL ("cf::_log_backtrace", G_VOID); |
|
|
965 | CALL_END; |
|
|
966 | #endif |
|
|
967 | } |
|
|
968 | |
952 | ///////////////////////////////////////////////////////////////////////////// |
969 | ///////////////////////////////////////////////////////////////////////////// |
953 | |
970 | |
954 | struct EventAPI *watcher_base::GEventAPI; |
971 | struct EventAPI *watcher_base::GEventAPI; |
955 | struct CoroAPI *coroapi::GCoroAPI; |
972 | struct CoroAPI *coroapi::GCoroAPI; |
956 | |
973 | |
… | |
… | |
1110 | const char *name; |
1127 | const char *name; |
1111 | IV iv; |
1128 | IV iv; |
1112 | } *civ, const_iv[] = { |
1129 | } *civ, const_iv[] = { |
1113 | # define const_iv(name) { # name, (IV)name }, |
1130 | # define const_iv(name) { # name, (IV)name }, |
1114 | const_iv (llevError) const_iv (llevInfo) const_iv (llevDebug) const_iv (llevMonster) |
1131 | const_iv (llevError) const_iv (llevInfo) const_iv (llevDebug) const_iv (llevMonster) |
|
|
1132 | const_iv (logBacktrace) |
1115 | |
1133 | |
1116 | const_iv (Map0Cmd) const_iv (Map1Cmd) const_iv (Map1aCmd) |
1134 | const_iv (Map0Cmd) const_iv (Map1Cmd) const_iv (Map1aCmd) |
1117 | |
1135 | |
1118 | const_iv (MAP_CLIENT_X) const_iv (MAP_CLIENT_Y) |
1136 | const_iv (MAP_CLIENT_X) const_iv (MAP_CLIENT_Y) |
1119 | |
1137 | |
… | |
… | |
1266 | |
1284 | |
1267 | const_iv (CS_QUERY_YESNO) const_iv (CS_QUERY_SINGLECHAR) const_iv (CS_QUERY_HIDEINPUT) |
1285 | const_iv (CS_QUERY_YESNO) const_iv (CS_QUERY_SINGLECHAR) const_iv (CS_QUERY_HIDEINPUT) |
1268 | |
1286 | |
1269 | const_iv (ST_DEAD) const_iv (ST_SETUP) const_iv (ST_PLAYING) const_iv (ST_CUSTOM) |
1287 | const_iv (ST_DEAD) const_iv (ST_SETUP) const_iv (ST_PLAYING) const_iv (ST_CUSTOM) |
1270 | |
1288 | |
1271 | const_iv (ST_CHANGE_CLASS) |
|
|
1272 | |
|
|
1273 | const_iv (IO_HEADER) const_iv (IO_OBJECTS) const_iv (IO_UNIQUES) |
1289 | const_iv (IO_HEADER) const_iv (IO_OBJECTS) const_iv (IO_UNIQUES) |
1274 | |
1290 | |
1275 | // random map generator |
1291 | // random map generator |
1276 | const_iv (LAYOUT_NONE) const_iv (LAYOUT_ONION) const_iv (LAYOUT_MAZE) const_iv (LAYOUT_SPIRAL) |
1292 | const_iv (LAYOUT_NONE) const_iv (LAYOUT_ONION) const_iv (LAYOUT_MAZE) const_iv (LAYOUT_SPIRAL) |
1277 | const_iv (LAYOUT_ROGUELIKE) const_iv (LAYOUT_SNAKE) const_iv (LAYOUT_SQUARE_SPIRAL) |
1293 | const_iv (LAYOUT_ROGUELIKE) const_iv (LAYOUT_SNAKE) const_iv (LAYOUT_SQUARE_SPIRAL) |
… | |
… | |
1280 | const_iv (RMOPT_BOTTOM_C) const_iv (RMOPT_BOTTOM_R) const_iv (RMOPT_IRR_SPACE) |
1296 | const_iv (RMOPT_BOTTOM_C) const_iv (RMOPT_BOTTOM_R) const_iv (RMOPT_IRR_SPACE) |
1281 | const_iv (RMOPT_WALL_OFF) const_iv (RMOPT_WALLS_ONLY) const_iv (RMOPT_NO_DOORS) |
1297 | const_iv (RMOPT_WALL_OFF) const_iv (RMOPT_WALLS_ONLY) const_iv (RMOPT_NO_DOORS) |
1282 | |
1298 | |
1283 | const_iv (SYMMETRY_RANDOM) const_iv (SYMMETRY_NONE) const_iv (SYMMETRY_X) |
1299 | const_iv (SYMMETRY_RANDOM) const_iv (SYMMETRY_NONE) const_iv (SYMMETRY_X) |
1284 | const_iv (SYMMETRY_Y) const_iv (SYMMETRY_XY) |
1300 | const_iv (SYMMETRY_Y) const_iv (SYMMETRY_XY) |
|
|
1301 | |
|
|
1302 | const_iv (GT_ENVIRONMENT) const_iv (GT_INVISIBLE) const_iv (GT_STARTEQUIP) |
|
|
1303 | const_iv (GT_APPLY) const_iv (GT_ONLY_GOOD) const_iv (GT_UPDATE_INV) |
|
|
1304 | const_iv (GT_MINIMAL) |
1285 | }; |
1305 | }; |
1286 | |
1306 | |
1287 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
1307 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
1288 | newCONSTSUB (stash_cf, (char *)civ->name, newSViv (civ->iv)); |
1308 | newCONSTSUB (stash_cf, (char *)civ->name, newSViv (civ->iv)); |
1289 | |
1309 | |
… | |
… | |
1389 | CODE: |
1409 | CODE: |
1390 | runtime = SvNVx (sv_runtime); |
1410 | runtime = SvNVx (sv_runtime); |
1391 | server_tick (); |
1411 | server_tick (); |
1392 | |
1412 | |
1393 | void |
1413 | void |
|
|
1414 | log_backtrace (utf8_string msg) |
|
|
1415 | |
|
|
1416 | void |
1394 | LOG (int level, utf8_string msg) |
1417 | LOG (int flags, utf8_string msg) |
1395 | PROTOTYPE: $$ |
1418 | PROTOTYPE: $$ |
1396 | C_ARGS: (LogLevel)level, "%s", msg |
1419 | C_ARGS: flags, "%s", msg |
1397 | |
1420 | |
1398 | octet_string path_combine (octet_string base, octet_string path) |
1421 | octet_string path_combine (octet_string base, octet_string path) |
1399 | PROTOTYPE: $$ |
1422 | PROTOTYPE: $$ |
1400 | |
1423 | |
1401 | octet_string path_combine_and_normalize (octet_string base, octet_string path) |
1424 | octet_string path_combine_and_normalize (octet_string base, octet_string path) |
… | |
… | |
1438 | } |
1461 | } |
1439 | OUTPUT: RETVAL |
1462 | OUTPUT: RETVAL |
1440 | |
1463 | |
1441 | void abort () |
1464 | void abort () |
1442 | |
1465 | |
|
|
1466 | void reset_signals () |
|
|
1467 | |
1443 | void fork_abort (octet_string cause = "cf::fork_abort") |
1468 | void fork_abort (octet_string cause = "cf::fork_abort") |
1444 | |
1469 | |
1445 | void cleanup (octet_string cause, bool make_core = false) |
1470 | void cleanup (octet_string cause, bool make_core = false) |
1446 | |
1471 | |
1447 | void emergency_save () |
1472 | void emergency_save () |
… | |
… | |
1973 | if (SV **elem = av_fetch ((AV *)palette, idx [offs], 0)) |
1998 | if (SV **elem = av_fetch ((AV *)palette, idx [offs], 0)) |
1974 | { |
1999 | { |
1975 | object *ob = get_archetype (SvPVutf8_nolen (*elem)); |
2000 | object *ob = get_archetype (SvPVutf8_nolen (*elem)); |
1976 | ob->flag [FLAG_NO_MAP_SAVE] = true; |
2001 | ob->flag [FLAG_NO_MAP_SAVE] = true; |
1977 | THIS->insert (ob, x, y, 0, INS_ABOVE_FLOOR_ONLY); |
2002 | THIS->insert (ob, x, y, 0, INS_ABOVE_FLOOR_ONLY); |
|
|
2003 | |
|
|
2004 | if (ob->randomitems) |
|
|
2005 | { |
|
|
2006 | if (!ob->above) |
|
|
2007 | { |
|
|
2008 | ob->create_treasure (ob->randomitems); |
|
|
2009 | |
|
|
2010 | for (object *op = ob->above; op; op = op->above) |
|
|
2011 | op->flag [FLAG_NO_MAP_SAVE] = true; |
|
|
2012 | } |
|
|
2013 | |
|
|
2014 | ob->randomitems = 0; |
|
|
2015 | } |
1978 | } |
2016 | } |
1979 | } |
2017 | } |
1980 | } |
2018 | } |
1981 | |
2019 | |
1982 | skip_space: ; |
2020 | skip_space: ; |