… | |
… | |
3 | * |
3 | * |
4 | * Copyright (©) 2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
4 | * Copyright (©) 2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
5 | * Copyright (©) 2001-2005,2007 by Chachkoff Yann |
5 | * Copyright (©) 2001-2005,2007 by Chachkoff Yann |
6 | * Copyright (©) 2006,2007 by Marc Lehmann <cf@schmorp.de> |
6 | * Copyright (©) 2006,2007 by Marc Lehmann <cf@schmorp.de> |
7 | * |
7 | * |
8 | * Crossfire TRT is free software; you can redistribute it and/or modify it |
8 | * Crossfire TRT is free software: you can redistribute it and/or modify |
9 | * under the terms of the GNU General Public License as published by the Free |
9 | * it under the terms of the GNU General Public License as published by |
10 | * Software Foundation; either version 2 of the License, or (at your option) |
10 | * the Free Software Foundation, either version 3 of the License, or |
11 | * any later version. |
11 | * (at your option) any later version. |
12 | * |
12 | * |
13 | * This program is distributed in the hope that it will be useful, but |
13 | * This program is distributed in the hope that it will be useful, |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * for more details. |
16 | * GNU General Public License for more details. |
17 | * |
17 | * |
18 | * You should have received a copy of the GNU General Public License along |
18 | * You should have received a copy of the GNU General Public License |
19 | * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51 |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
|
21 | * |
20 | * |
22 | * The authors can be reached via e-mail to <crossfire@schmorp.de> |
21 | * The authors can be reached via e-mail to <crossfire@schmorp.de> |
23 | */ |
22 | */ |
24 | |
23 | |
25 | #include "autoconf.h" |
24 | #include "autoconf.h" |
… | |
… | |
75 | # define newSVval64 newSVnv |
74 | # define newSVval64 newSVnv |
76 | # define SvVAL64 SvNV |
75 | # define SvVAL64 SvNV |
77 | #endif |
76 | #endif |
78 | |
77 | |
79 | static f_plug_api gethook = cfapi_get_hooks; |
78 | static f_plug_api gethook = cfapi_get_hooks; |
80 | static f_plug_api object_set_property = cfapi_object_set_property; |
|
|
81 | static f_plug_api object_insert = cfapi_object_insert; |
79 | static f_plug_api object_insert = cfapi_object_insert; |
82 | |
80 | |
83 | static PerlInterpreter *perl; |
81 | static PerlInterpreter *perl; |
84 | |
82 | |
85 | double runtime; |
83 | tstamp NOW, runtime; |
86 | |
84 | |
87 | global gbl_ev; |
85 | global gbl_ev; |
88 | static AV *cb_global, *cb_attachable, *cb_object, *cb_player, *cb_client, *cb_type, *cb_map; |
86 | static AV *cb_global, *cb_attachable, *cb_object, *cb_player, *cb_client, *cb_type, *cb_map; |
89 | static SV *sv_runtime, *sv_next_tick; |
87 | static SV *sv_runtime, *sv_next_tick; |
90 | |
88 | |
… | |
… | |
202 | { |
200 | { |
203 | if (i >= mortals.size ()) |
201 | if (i >= mortals.size ()) |
204 | { |
202 | { |
205 | i = 0; |
203 | i = 0; |
206 | |
204 | |
207 | if (mortals.size () > 1000) |
205 | if (mortals.size () >= 512) |
|
|
206 | { |
|
|
207 | static int last_mortalcount; |
|
|
208 | if (mortals.size () != last_mortalcount) |
|
|
209 | { |
|
|
210 | last_mortalcount = mortals.size (); |
208 | LOG (llevInfo, "mortal queue size (%d) exceeds 1000.\n", (int)mortals.size ()); |
211 | LOG (llevInfo, "%d mortals.\n", (int)mortals.size ()); |
|
|
212 | |
|
|
213 | if (0) |
|
|
214 | { |
|
|
215 | for (int j = 0; j < mortals.size (); ++j)//D |
|
|
216 | fprintf (stderr, "%d:%s %p ", j, &((object *)mortals[j])->name, mortals[j]);//D |
|
|
217 | fprintf (stderr, "\n");//D |
|
|
218 | } |
|
|
219 | } |
|
|
220 | } |
209 | |
221 | |
210 | break; |
222 | break; |
211 | } |
223 | } |
212 | |
224 | |
213 | attachable *obj = mortals [i]; |
225 | attachable *obj = mortals [i]; |
… | |
… | |
244 | |
256 | |
245 | attach = src.attach; |
257 | attach = src.attach; |
246 | return *this; |
258 | return *this; |
247 | } |
259 | } |
248 | |
260 | |
|
|
261 | template<typename T> |
|
|
262 | static bool |
|
|
263 | find_backref (void *ptr, T *obj) |
|
|
264 | { |
|
|
265 | char *s = (char *)obj; |
|
|
266 | while (s < (char *)obj + sizeof (T)) |
|
|
267 | { |
|
|
268 | if (ptr == *(void **)s) |
|
|
269 | return true; |
|
|
270 | |
|
|
271 | s += sizeof (void *); // assume natural alignment |
|
|
272 | } |
|
|
273 | |
|
|
274 | return false; |
|
|
275 | } |
|
|
276 | |
|
|
277 | // for debugging, find "live" objects containing this ptr |
|
|
278 | void |
|
|
279 | find_backref (void *ptr) |
|
|
280 | { |
|
|
281 | for_all_objects (op) |
|
|
282 | if (find_backref (ptr, op)) |
|
|
283 | fprintf (stderr, "O %p %d:'%s'\n", op, op->count, &op->name); |
|
|
284 | |
|
|
285 | for_all_players (pl) |
|
|
286 | if (find_backref (ptr, pl)) |
|
|
287 | fprintf (stderr, "P %p\n", pl); |
|
|
288 | |
|
|
289 | for_all_clients (ns) |
|
|
290 | if (find_backref (ptr, ns)) |
|
|
291 | fprintf (stderr, "C %p\n", ns); |
|
|
292 | |
|
|
293 | } |
|
|
294 | |
249 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
295 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
250 | |
296 | |
251 | static SV * |
297 | static SV * |
252 | newSVptr (void *ptr, HV *stash, HV *hv = newHV ()) |
298 | newSVptr (void *ptr, HV *stash, HV *hv = newHV ()) |
253 | { |
299 | { |
… | |
… | |
335 | return SvPTR (sv, klass); |
381 | return SvPTR (sv, klass); |
336 | else |
382 | else |
337 | return 0; |
383 | return 0; |
338 | } |
384 | } |
339 | |
385 | |
|
|
386 | static inline SV * |
|
|
387 | newSVpv_utf8 (const char *s) |
|
|
388 | { |
|
|
389 | SV *sv = newSVpv (s, 0); |
|
|
390 | SvUTF8_on (sv); |
|
|
391 | return sv; |
|
|
392 | } |
|
|
393 | |
|
|
394 | static inline SV * |
|
|
395 | newSVpvn_utf8 (const char *s, STRLEN l) |
|
|
396 | { |
|
|
397 | SV *sv = newSVpvn (s, l); |
|
|
398 | SvUTF8_on (sv); |
|
|
399 | return sv; |
|
|
400 | } |
|
|
401 | |
340 | inline SV *to_sv (const shstr & v) { return v ? newSVpvn ((const char *)v, v.length ()) : &PL_sv_undef; } |
402 | inline SV *to_sv (const shstr & v) { return v ? newSVpvn_utf8 ((const char *)v, v.length ()) : &PL_sv_undef; } |
341 | inline SV *to_sv (const char * v) { return newSVpv (v, 0); } |
403 | inline SV *to_sv (const char * v) { return newSVpv (v, 0); } |
342 | inline SV *to_sv (bool v) { return newSViv (v); } |
404 | inline SV *to_sv (bool v) { return newSViv (v); } |
343 | inline SV *to_sv ( signed char v) { return newSViv (v); } |
405 | inline SV *to_sv ( signed char v) { return newSViv (v); } |
344 | inline SV *to_sv (unsigned char v) { return newSViv (v); } |
406 | inline SV *to_sv (unsigned char v) { return newSViv (v); } |
345 | inline SV *to_sv ( signed short v) { return newSViv (v); } |
407 | inline SV *to_sv ( signed short v) { return newSViv (v); } |
… | |
… | |
372 | char buf[128]; |
434 | char buf[128]; |
373 | snprintf (buf, 128, "<1.%" PRIx64 ">", v.seq); |
435 | snprintf (buf, 128, "<1.%" PRIx64 ">", v.seq); |
374 | return newSVpv (buf, 0); |
436 | return newSVpv (buf, 0); |
375 | } |
437 | } |
376 | |
438 | |
377 | inline void sv_to (SV *sv, shstr &v) { v = SvOK (sv) ? SvPV_nolen (sv) : 0; } |
439 | inline void sv_to (SV *sv, shstr &v) { v = SvOK (sv) ? SvPVutf8_nolen (sv) : 0; } |
378 | inline void sv_to (SV *sv, char * &v) { free (v); v = SvOK (sv) ? strdup (SvPV_nolen (sv)) : 0; } |
440 | inline void sv_to (SV *sv, char * &v) { free (v); v = SvOK (sv) ? strdup (SvPV_nolen (sv)) : 0; } |
379 | inline void sv_to (SV *sv, bool &v) { v = SvIV (sv); } |
441 | inline void sv_to (SV *sv, bool &v) { v = SvIV (sv); } |
380 | inline void sv_to (SV *sv, signed char &v) { v = SvIV (sv); } |
442 | inline void sv_to (SV *sv, signed char &v) { v = SvIV (sv); } |
381 | inline void sv_to (SV *sv, unsigned char &v) { v = SvIV (sv); } |
443 | inline void sv_to (SV *sv, unsigned char &v) { v = SvIV (sv); } |
382 | inline void sv_to (SV *sv, signed short &v) { v = SvIV (sv); } |
444 | inline void sv_to (SV *sv, signed short &v) { v = SvIV (sv); } |
… | |
… | |
939 | CALL_ARG (level); |
1001 | CALL_ARG (level); |
940 | CALL_CALL ("ext::books::make_book", G_VOID); |
1002 | CALL_CALL ("ext::books::make_book", G_VOID); |
941 | CALL_END; |
1003 | CALL_END; |
942 | } |
1004 | } |
943 | |
1005 | |
|
|
1006 | void |
|
|
1007 | cfperl_send_msg (client *ns, int color, const char *type, const char *msg) |
|
|
1008 | { |
|
|
1009 | CALL_BEGIN (4); |
|
|
1010 | CALL_ARG (ns); |
|
|
1011 | CALL_ARG (color); |
|
|
1012 | CALL_ARG (type); |
|
|
1013 | CALL_ARG_SV (newSVpv_utf8 (msg)); |
|
|
1014 | CALL_CALL ("cf::client::send_msg", G_VOID); |
|
|
1015 | CALL_END; |
|
|
1016 | } |
|
|
1017 | |
944 | maptile * |
1018 | maptile * |
945 | maptile::find_sync (const char *path, maptile *origin) |
1019 | maptile::find_sync (const char *path, maptile *origin) |
946 | { |
1020 | { |
947 | CALL_BEGIN (2); |
1021 | CALL_BEGIN (2); |
948 | CALL_ARG (path); |
1022 | CALL_ARG (path); |
… | |
… | |
1032 | |
1106 | |
1033 | struct EventAPI *watcher_base::GEventAPI; |
1107 | struct EventAPI *watcher_base::GEventAPI; |
1034 | struct CoroAPI *coroapi::GCoroAPI; |
1108 | struct CoroAPI *coroapi::GCoroAPI; |
1035 | |
1109 | |
1036 | int coroapi::cede_counter; |
1110 | int coroapi::cede_counter; |
1037 | double (*coroapi::time)(); |
|
|
1038 | double coroapi::next_cede; |
1111 | tstamp coroapi::next_cede; |
1039 | |
1112 | |
1040 | void coroapi::do_cede_to_tick () |
1113 | void coroapi::do_cede_to_tick () |
1041 | { |
1114 | { |
1042 | cede_counter = 0; |
1115 | cede_counter = 0; |
1043 | |
1116 | |
… | |
… | |
1174 | |
1247 | |
1175 | _connect_to_perl (); |
1248 | _connect_to_perl (); |
1176 | |
1249 | |
1177 | newCONSTSUB (stash_cf, "VERSION", newSVpv (VERSION, sizeof (VERSION) - 1)); |
1250 | newCONSTSUB (stash_cf, "VERSION", newSVpv (VERSION, sizeof (VERSION) - 1)); |
1178 | |
1251 | |
1179 | { |
1252 | //{ |
1180 | require_pv ("Time::HiRes"); |
1253 | // require_pv ("Time::HiRes"); |
1181 | |
1254 | // |
1182 | SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
1255 | // SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
1183 | if (!svp) croak ("Time::HiRes is required"); |
1256 | // if (!svp) croak ("Time::HiRes is required"); |
1184 | if (!SvIOK(*svp)) croak ("Time::NVtime isn’t a function pointer"); |
1257 | // if (!SvIOK(*svp)) croak ("Time::NVtime isn’t a function pointer"); |
1185 | coroapi::time = INT2PTR (double(*)(), SvIV(*svp)); |
1258 | // coroapi::time = INT2PTR (double(*)(), SvIV(*svp)); |
1186 | } |
1259 | //} |
1187 | |
1260 | |
1188 | static const struct { |
1261 | static const struct { |
1189 | const char *name; |
1262 | const char *name; |
1190 | IV iv; |
1263 | IV iv; |
1191 | } *civ, const_iv[] = { |
1264 | } *civ, const_iv[] = { |
… | |
… | |
1541 | CODE: |
1614 | CODE: |
1542 | coroapi::cede_to_tick (); |
1615 | coroapi::cede_to_tick (); |
1543 | |
1616 | |
1544 | void server_tick () |
1617 | void server_tick () |
1545 | CODE: |
1618 | CODE: |
|
|
1619 | NOW = now (); |
1546 | runtime = SvNVx (sv_runtime); |
1620 | runtime = SvNVx (sv_runtime); |
1547 | server_tick (); |
1621 | server_tick (); |
1548 | |
1622 | |
1549 | void |
1623 | void |
1550 | log_backtrace (utf8_string msg) |
1624 | log_backtrace (utf8_string msg) |
… | |
… | |
1767 | PROTOTYPE: $ |
1841 | PROTOTYPE: $ |
1768 | CODE: |
1842 | CODE: |
1769 | RETVAL = op->head_ (); |
1843 | RETVAL = op->head_ (); |
1770 | OUTPUT: RETVAL |
1844 | OUTPUT: RETVAL |
1771 | |
1845 | |
1772 | int is_head (object *op) |
|
|
1773 | PROTOTYPE: $ |
|
|
1774 | CODE: |
|
|
1775 | RETVAL = op->head_ () == op; |
|
|
1776 | OUTPUT: RETVAL |
|
|
1777 | |
|
|
1778 | void |
1846 | void |
1779 | inv (object *obj) |
1847 | inv (object *obj) |
1780 | PROTOTYPE: $ |
1848 | PROTOTYPE: $ |
1781 | PPCODE: |
1849 | PPCODE: |
1782 | { |
1850 | { |
… | |
… | |
1937 | # dumb kludgy misdesigned plug-in api slowly gets on my nerves. |
2005 | # dumb kludgy misdesigned plug-in api slowly gets on my nerves. |
1938 | |
2006 | |
1939 | object *new (utf8_string archetype = 0) |
2007 | object *new (utf8_string archetype = 0) |
1940 | PROTOTYPE: ;$ |
2008 | PROTOTYPE: ;$ |
1941 | CODE: |
2009 | CODE: |
1942 | RETVAL = archetype ? get_archetype (archetype) : cf_create_object (); |
2010 | RETVAL = archetype ? get_archetype (archetype) : object::create (); |
1943 | OUTPUT: |
2011 | OUTPUT: |
1944 | RETVAL |
2012 | RETVAL |
1945 | |
2013 | |
|
|
2014 | object *find_object (U32 tag) |
|
|
2015 | |
|
|
2016 | # TODO: nuke |
1946 | object *insert_ob_in_map_at (object *ob, maptile *where, object_ornull *orig, int flag, int x, int y) |
2017 | object *insert_ob_in_map_at (object *ob, maptile *where, object_ornull *orig, int flag, int x, int y) |
1947 | PROTOTYPE: $$$$$$ |
2018 | PROTOTYPE: $$$$$$ |
1948 | CODE: |
2019 | CODE: |
1949 | { |
2020 | { |
1950 | int unused_type; |
2021 | int unused_type; |
1951 | RETVAL = (object *)object_insert (&unused_type, ob, 0, where, orig, flag, x, y); |
2022 | RETVAL = (object *)object_insert (&unused_type, ob, 0, where, orig, flag, x, y); |
1952 | } |
2023 | } |
1953 | |
|
|
1954 | player *contr (object *op) |
|
|
1955 | CODE: |
|
|
1956 | RETVAL = op->contr; |
|
|
1957 | OUTPUT: RETVAL |
|
|
1958 | |
2024 | |
1959 | const_utf8_string get_ob_key_value (object *op, utf8_string key) |
2025 | const_utf8_string get_ob_key_value (object *op, utf8_string key) |
1960 | |
2026 | |
1961 | bool set_ob_key_value (object *op, utf8_string key, utf8_string value = 0, int add_key = 1) |
2027 | bool set_ob_key_value (object *op, utf8_string key, utf8_string value = 0, int add_key = 1) |
1962 | |
2028 | |
… | |
… | |
2417 | CODE: |
2483 | CODE: |
2418 | RETVAL = archetype::find (name); |
2484 | RETVAL = archetype::find (name); |
2419 | OUTPUT: |
2485 | OUTPUT: |
2420 | RETVAL |
2486 | RETVAL |
2421 | |
2487 | |
2422 | archetype *first() |
2488 | int archetypes_size () |
2423 | PROTOTYPE: |
2489 | CODE: |
2424 | CODE: |
|
|
2425 | RETVAL = first_archetype; |
2490 | RETVAL = archetypes.size (); |
2426 | OUTPUT: RETVAL |
2491 | OUTPUT: RETVAL |
|
|
2492 | |
|
|
2493 | archetype *archetypes (U32 index) |
|
|
2494 | CODE: |
|
|
2495 | RETVAL = index < archetypes.size () ? archetypes [index] : 0; |
|
|
2496 | OUTPUT: RETVAL |
2427 | |
2497 | |
2428 | object *instantiate (archetype *arch) |
2498 | object *instantiate (archetype *arch) |
2429 | CODE: |
2499 | CODE: |
2430 | RETVAL = arch_to_object (arch); |
2500 | RETVAL = arch_to_object (arch); |
2431 | OUTPUT: |
2501 | OUTPUT: |
… | |
… | |
2533 | if (!strcmp (name, BLANK_FACE_NAME)) blank_face = RETVAL; |
2603 | if (!strcmp (name, BLANK_FACE_NAME)) blank_face = RETVAL; |
2534 | if (!strcmp (name, EMPTY_FACE_NAME)) empty_face = RETVAL; |
2604 | if (!strcmp (name, EMPTY_FACE_NAME)) empty_face = RETVAL; |
2535 | } |
2605 | } |
2536 | OUTPUT: RETVAL |
2606 | OUTPUT: RETVAL |
2537 | |
2607 | |
2538 | void set (faceidx idx, int visibility, int magicmap) |
2608 | void set_type (faceidx idx, int value) |
|
|
2609 | ALIAS: |
|
|
2610 | set_type = 0 |
|
|
2611 | set_visibility = 1 |
|
|
2612 | set_magicmap = 2 |
|
|
2613 | set_smooth = 3 |
|
|
2614 | set_smoothlevel = 4 |
2539 | CODE: |
2615 | CODE: |
2540 | faceinfo *f = face_info (idx); |
2616 | faceinfo *f = face_info (idx); |
2541 | assert (f); |
2617 | assert (f); |
2542 | f->visibility = visibility; |
2618 | switch (ix) |
2543 | f->magicmap = magicmap; |
2619 | { |
2544 | |
2620 | case 0: f->type = value; break; |
2545 | void set_smooth (faceidx idx, faceidx smooth, int smoothlevel) |
2621 | case 1: f->visibility = value; break; |
2546 | CODE: |
2622 | case 2: f->magicmap = value; break; |
2547 | faceinfo *f = face_info (idx); assert (f); |
2623 | case 3: f->smooth = value; break; |
2548 | f->smooth = smooth; |
2624 | case 4: f->smoothlevel = value; break; |
2549 | f->smoothlevel = smoothlevel; |
2625 | } |
2550 | |
2626 | |
2551 | void set_data (faceidx idx, int faceset, SV *data, SV *chksum) |
2627 | void set_data (faceidx idx, int faceset, SV *data, SV *chksum) |
2552 | CODE: |
2628 | CODE: |
2553 | { |
2629 | { |
2554 | facedata *d = face_data (idx, faceset); |
2630 | facedata *d = face_data (idx, faceset); |