1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra 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 | * Deliantra is free software: you can redistribute it and/or modify |
8 | * Deliantra is free software: you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by |
9 | * it under the terms of the GNU General Public License as published by |
… | |
… | |
24 | #include "autoconf.h" |
24 | #include "autoconf.h" |
25 | |
25 | |
26 | #define PLUGIN_NAME "perl" |
26 | #define PLUGIN_NAME "perl" |
27 | #define PLUGIN_VERSION "cfperl 0.5" |
27 | #define PLUGIN_VERSION "cfperl 0.5" |
28 | |
28 | |
29 | #define CEDES_PER_TICK 3 |
|
|
30 | |
|
|
31 | #if HAVE_EXECINFO_H |
29 | #if HAVE_EXECINFO_H |
32 | # include <execinfo.h> |
30 | # include <execinfo.h> |
33 | #endif |
31 | #endif |
34 | |
32 | |
35 | #include <cstdarg> |
33 | #include <cstdarg> |
36 | |
34 | |
37 | #include "global.h" |
35 | #include "global.h" |
38 | #include "loader.h" |
36 | #include "loader.h" |
39 | #include "../random_maps/random_map.h" |
37 | #include "../random_maps/random_map.h" |
|
|
38 | #include "evthread.h" |
40 | #include <sproto.h> |
39 | #include "sproto.h" |
41 | |
40 | |
42 | #include <unistd.h> |
41 | #include <unistd.h> |
43 | #if _POSIX_MEMLOCK |
42 | #if _POSIX_MEMLOCK |
44 | # include <sys/mman.h> |
43 | # include <sys/mman.h> |
45 | #endif |
44 | #endif |
… | |
… | |
74 | |
73 | |
75 | static PerlInterpreter *perl; |
74 | static PerlInterpreter *perl; |
76 | |
75 | |
77 | tstamp NOW, runtime; |
76 | tstamp NOW, runtime; |
78 | |
77 | |
|
|
78 | static int tick_inhibit; |
|
|
79 | static int tick_pending; |
|
|
80 | |
79 | global gbl_ev; |
81 | global gbl_ev; |
80 | static AV *cb_global, *cb_attachable, *cb_object, *cb_player, *cb_client, *cb_type, *cb_map; |
82 | static AV *cb_global, *cb_attachable, *cb_object, *cb_player, *cb_client, *cb_type, *cb_map; |
81 | static SV *sv_runtime, *sv_next_tick, *sv_now; |
83 | static SV *sv_runtime, *sv_tick_start, *sv_next_tick, *sv_now; |
82 | |
84 | |
83 | bitset<NUM_EVENT_TYPES> ev_want_event; |
85 | bitset<NUM_EVENT_TYPES> ev_want_event; |
84 | bitset<NUM_TYPES> ev_want_type; |
86 | bitset<NUM_TYPES> ev_want_type; |
85 | |
87 | |
86 | static HV |
88 | static HV |
… | |
… | |
909 | } |
911 | } |
910 | |
912 | |
911 | ///////////////////////////////////////////////////////////////////////////// |
913 | ///////////////////////////////////////////////////////////////////////////// |
912 | // various c++ => perl glue functions |
914 | // various c++ => perl glue functions |
913 | |
915 | |
|
|
916 | void cfperl_tick () |
|
|
917 | { |
|
|
918 | tick_pending = 1; |
|
|
919 | |
|
|
920 | if (tick_inhibit) |
|
|
921 | return; |
|
|
922 | |
|
|
923 | tick_pending = 0; |
|
|
924 | |
|
|
925 | dSP; |
|
|
926 | |
|
|
927 | PUSHMARK (SP); |
|
|
928 | PUTBACK; |
|
|
929 | call_pv ("cf::tick", G_DISCARD | G_VOID); |
|
|
930 | |
|
|
931 | SvNV_set (sv_next_tick, get_next_tick ()); SvNOK_only (sv_next_tick); |
|
|
932 | } |
|
|
933 | |
914 | void |
934 | void |
915 | cfperl_emergency_save () |
935 | cfperl_emergency_save () |
916 | { |
936 | { |
917 | CALL_BEGIN (0); |
937 | CALL_BEGIN (0); |
918 | CALL_CALL ("cf::emergency_save", G_VOID); |
938 | CALL_CALL ("cf::emergency_save", G_VOID); |
… | |
… | |
1104 | ///////////////////////////////////////////////////////////////////////////// |
1124 | ///////////////////////////////////////////////////////////////////////////// |
1105 | |
1125 | |
1106 | struct EVAPI *evapi::GEVAPI; |
1126 | struct EVAPI *evapi::GEVAPI; |
1107 | struct CoroAPI *coroapi::GCoroAPI; |
1127 | struct CoroAPI *coroapi::GCoroAPI; |
1108 | |
1128 | |
1109 | int coroapi::cede_counter; |
|
|
1110 | tstamp coroapi::next_cede; |
|
|
1111 | |
|
|
1112 | void coroapi::do_cede_to_tick () |
1129 | void coroapi::do_cede_to_tick () |
1113 | { |
1130 | { |
1114 | cede_counter = 0; |
1131 | cede_pending = 0; |
1115 | |
|
|
1116 | cede (); |
1132 | cede (); |
1117 | |
|
|
1118 | next_cede += TICK / CEDES_PER_TICK; |
|
|
1119 | if (next_cede > SvNVx (sv_next_tick) - 0.002) |
|
|
1120 | next_cede = SvNVx (sv_next_tick); |
|
|
1121 | } |
|
|
1122 | |
|
|
1123 | void coroapi::do_cede_every () |
|
|
1124 | { |
|
|
1125 | cede_counter = 0; |
|
|
1126 | |
|
|
1127 | if (coroapi::nready ()) |
|
|
1128 | coroapi::cede (); |
|
|
1129 | } |
|
|
1130 | |
|
|
1131 | void coroapi::do_cede_to_tick_every () |
|
|
1132 | { |
|
|
1133 | cede_counter = 0; |
|
|
1134 | |
|
|
1135 | cede_to_tick (); |
|
|
1136 | } |
1133 | } |
1137 | |
1134 | |
1138 | void |
1135 | void |
1139 | coroapi::wait_for_tick () |
1136 | coroapi::wait_for_tick () |
1140 | { |
1137 | { |
… | |
… | |
1164 | } |
1161 | } |
1165 | |
1162 | |
1166 | void |
1163 | void |
1167 | _connect_to_perl () |
1164 | _connect_to_perl () |
1168 | { |
1165 | { |
1169 | stash_cf = gv_stashpv ("cf" , 1); |
1166 | stash_cf = gv_stashpv ("cf", 1); |
1170 | |
1167 | |
1171 | stash_cf_object_wrap = gv_stashpv ("cf::object::wrap", 1); |
1168 | stash_cf_object_wrap = gv_stashpv ("cf::object::wrap", 1); |
1172 | stash_cf_object_player_wrap = gv_stashpv ("cf::object::player::wrap", 1); |
1169 | stash_cf_object_player_wrap = gv_stashpv ("cf::object::player::wrap", 1); |
1173 | stash_cf_player_wrap = gv_stashpv ("cf::player::wrap", 1); |
1170 | stash_cf_player_wrap = gv_stashpv ("cf::player::wrap", 1); |
1174 | stash_cf_map_wrap = gv_stashpv ("cf::map::wrap" , 1); |
1171 | stash_cf_map_wrap = gv_stashpv ("cf::map::wrap" , 1); |
… | |
… | |
1176 | stash_cf_arch_wrap = gv_stashpv ("cf::arch::wrap" , 1); |
1173 | stash_cf_arch_wrap = gv_stashpv ("cf::arch::wrap" , 1); |
1177 | stash_cf_party_wrap = gv_stashpv ("cf::party::wrap" , 1); |
1174 | stash_cf_party_wrap = gv_stashpv ("cf::party::wrap" , 1); |
1178 | stash_cf_region_wrap = gv_stashpv ("cf::region::wrap", 1); |
1175 | stash_cf_region_wrap = gv_stashpv ("cf::region::wrap", 1); |
1179 | stash_cf_living_wrap = gv_stashpv ("cf::living::wrap", 1); |
1176 | stash_cf_living_wrap = gv_stashpv ("cf::living::wrap", 1); |
1180 | |
1177 | |
1181 | sv_now = get_sv ("cf::NOW" , 1); SvUPGRADE (sv_now , SVt_NV); |
1178 | sv_now = get_sv ("cf::NOW" , 1); SvUPGRADE (sv_now , SVt_NV); |
1182 | sv_runtime = get_sv ("cf::RUNTIME" , 1); SvUPGRADE (sv_runtime , SVt_NV); |
1179 | sv_runtime = get_sv ("cf::RUNTIME" , 1); SvUPGRADE (sv_runtime , SVt_NV); |
|
|
1180 | sv_tick_start = get_sv ("cf::TICK_START", 1); SvUPGRADE (sv_tick_start, SVt_NV); |
1183 | sv_next_tick = get_sv ("cf::NEXT_TICK", 1); SvUPGRADE (sv_next_tick, SVt_NV); |
1181 | sv_next_tick = get_sv ("cf::NEXT_TICK" , 1); SvUPGRADE (sv_next_tick , SVt_NV); |
1184 | |
1182 | |
1185 | cb_global = get_av ("cf::CB_GLOBAL", 1); |
1183 | cb_global = get_av ("cf::CB_GLOBAL", 1); |
1186 | cb_attachable = get_av ("cf::CB_ATTACHABLE", 1); |
1184 | cb_attachable = get_av ("cf::CB_ATTACHABLE", 1); |
1187 | cb_object = get_av ("cf::CB_OBJECT", 1); |
1185 | cb_object = get_av ("cf::CB_OBJECT", 1); |
1188 | cb_player = get_av ("cf::CB_PLAYER", 1); |
1186 | cb_player = get_av ("cf::CB_PLAYER", 1); |
… | |
… | |
1536 | } |
1534 | } |
1537 | |
1535 | |
1538 | SvCUR_set (data_sv, dst - SvPVX (data_sv)); |
1536 | SvCUR_set (data_sv, dst - SvPVX (data_sv)); |
1539 | } |
1537 | } |
1540 | |
1538 | |
|
|
1539 | void evthread_start () |
|
|
1540 | |
1541 | void _post_tick () |
1541 | void cede_to_tick () |
1542 | CODE: |
1542 | CODE: |
1543 | coroapi::next_cede = SvNVx (sv_next_tick) - TICK * (1. - 1. / CEDES_PER_TICK); |
1543 | coroapi::cede_to_tick (); |
1544 | |
1544 | |
1545 | NV till_cede () |
1545 | NV till_tick () |
1546 | CODE: |
1546 | CODE: |
1547 | RETVAL = coroapi::next_cede - now (); |
1547 | RETVAL = SvNVX (sv_next_tick) - now (); |
1548 | OUTPUT: |
1548 | OUTPUT: |
1549 | RETVAL |
1549 | RETVAL |
1550 | |
1550 | |
1551 | NV till_tick () |
1551 | int tick_inhibit () |
1552 | CODE: |
1552 | CODE: |
1553 | RETVAL = SvNVx (sv_next_tick) - now (); |
1553 | RETVAL = tick_inhibit; |
1554 | OUTPUT: |
1554 | OUTPUT: |
1555 | RETVAL |
1555 | RETVAL |
|
|
1556 | |
|
|
1557 | void tick_inhibit_inc () |
|
|
1558 | CODE: |
|
|
1559 | ++tick_inhibit; |
|
|
1560 | |
|
|
1561 | void tick_inhibit_dec () |
|
|
1562 | CODE: |
|
|
1563 | if (!--tick_inhibit) |
|
|
1564 | if (tick_pending) |
|
|
1565 | { |
|
|
1566 | ev_async_send (EV_DEFAULT, &tick_watcher); |
|
|
1567 | coroapi::cede (); |
|
|
1568 | } |
|
|
1569 | |
|
|
1570 | void server_tick () |
|
|
1571 | CODE: |
|
|
1572 | { |
|
|
1573 | NOW = ev_now (EV_DEFAULT); |
|
|
1574 | SvNV_set (sv_now, NOW); SvNOK_only (sv_now); |
|
|
1575 | SvNV_set (sv_tick_start, NOW); SvNOK_only (sv_tick_start); |
|
|
1576 | runtime = SvNVX (sv_runtime); |
|
|
1577 | |
|
|
1578 | server_tick (); |
|
|
1579 | |
|
|
1580 | NOW = ev_time (); |
|
|
1581 | SvNV_set (sv_now, NOW); SvNOK_only (sv_now); |
|
|
1582 | runtime += TICK; |
|
|
1583 | SvNV_set (sv_runtime, runtime); SvNOK_only (sv_runtime); |
|
|
1584 | } |
1556 | |
1585 | |
1557 | NV floor (NV x) |
1586 | NV floor (NV x) |
1558 | |
1587 | |
1559 | NV ceil (NV x) |
1588 | NV ceil (NV x) |
1560 | |
1589 | |
… | |
… | |
1585 | void weaken (...) |
1614 | void weaken (...) |
1586 | PROTOTYPE: @ |
1615 | PROTOTYPE: @ |
1587 | CODE: |
1616 | CODE: |
1588 | while (items > 0) |
1617 | while (items > 0) |
1589 | sv_rvweaken (ST (--items)); |
1618 | sv_rvweaken (ST (--items)); |
1590 | |
|
|
1591 | void cede_to_tick () |
|
|
1592 | CODE: |
|
|
1593 | coroapi::cede_to_tick (); |
|
|
1594 | |
|
|
1595 | void server_tick () |
|
|
1596 | CODE: |
|
|
1597 | NOW = SvNVx (sv_now); |
|
|
1598 | runtime = SvNVx (sv_runtime); |
|
|
1599 | server_tick (); |
|
|
1600 | |
1619 | |
1601 | void |
1620 | void |
1602 | log_backtrace (utf8_string msg) |
1621 | log_backtrace (utf8_string msg) |
1603 | |
1622 | |
1604 | void |
1623 | void |
… | |
… | |
2803 | extract_tags (object_thawer *self) |
2822 | extract_tags (object_thawer *self) |
2804 | PPCODE: |
2823 | PPCODE: |
2805 | while (self->kw != KW_EOF) |
2824 | while (self->kw != KW_EOF) |
2806 | { |
2825 | { |
2807 | PUTBACK; |
2826 | PUTBACK; |
2808 | coroapi::cede_to_tick_every (500); |
2827 | coroapi::cede_to_tick (); |
2809 | SPAGAIN; |
2828 | SPAGAIN; |
2810 | |
2829 | |
2811 | if (self->kw == KW_tag) |
2830 | if (self->kw == KW_tag) |
2812 | XPUSHs (sv_2mortal (newSVpv_utf8 (self->get_str ()))); |
2831 | XPUSHs (sv_2mortal (newSVpv_utf8 (self->get_str ()))); |
2813 | |
2832 | |