… | |
… | |
4 | |
4 | |
5 | /* |
5 | /* |
6 | * This code is placed under the GNU General Public Licence (GPL) |
6 | * This code is placed under the GNU General Public Licence (GPL) |
7 | * |
7 | * |
8 | * Copyright (C) 2001-2005 by Chachkoff Yann |
8 | * Copyright (C) 2001-2005 by Chachkoff Yann |
9 | * Copyright (C) 2006 by Marc Lehmann <cf@schmorp.de> |
9 | * Copyright (C) 2006,2007 by Marc Lehmann <cf@schmorp.de> |
10 | * |
10 | * |
11 | * This program is free software; you can redistribute it and/or modify |
11 | * This program is free software; you can redistribute it and/or modify |
12 | * it under the terms of the GNU General Public License as published by |
12 | * it under the terms of the GNU General Public License as published by |
13 | * the Free Software Foundation; either version 2 of the License, or |
13 | * the Free Software Foundation; either version 2 of the License, or |
14 | * (at your option) any later version. |
14 | * (at your option) any later version. |
… | |
… | |
29 | #include <plugin_common.h> |
29 | #include <plugin_common.h> |
30 | #include <sounds.h> |
30 | #include <sounds.h> |
31 | #include <cstdarg> |
31 | #include <cstdarg> |
32 | #include <sproto.h> |
32 | #include <sproto.h> |
33 | |
33 | |
|
|
34 | #include "loader.h" |
34 | #include "cfperl.h" |
35 | #include "cfperl.h" |
35 | #include "shstr.h" |
36 | #include "shstr.h" |
36 | |
37 | |
37 | #include <unistd.h> |
38 | #include <unistd.h> |
38 | #if _POSIX_MEMLOCK |
39 | #if _POSIX_MEMLOCK |
… | |
… | |
101 | |
102 | |
102 | unordered_vector<attachable *> attachable::mortals; |
103 | unordered_vector<attachable *> attachable::mortals; |
103 | |
104 | |
104 | attachable::~attachable () |
105 | attachable::~attachable () |
105 | { |
106 | { |
|
|
107 | flags |=0x3300;//D |
106 | assert (!self); |
108 | assert (!self); |
107 | assert (!cb); |
109 | assert (!cb); |
108 | } |
110 | } |
109 | |
111 | |
110 | int |
112 | int |
111 | attachable::refcnt_cnt () const |
113 | attachable::refcnt_cnt () const |
112 | { |
114 | { |
113 | return refcnt + (self ? SvREFCNT (self) - 1 : 0); |
115 | return refcnt + (self ? SvREFCNT (self) - 1 : 0); |
|
|
116 | } |
|
|
117 | |
|
|
118 | void |
|
|
119 | attachable::sever_self () |
|
|
120 | { |
|
|
121 | if (HV *self = this->self) |
|
|
122 | { |
|
|
123 | // keep a refcount because sv_unmagic might call attachable_free, |
|
|
124 | // which might clear self, causing sv_unmagic to crash on a now |
|
|
125 | // invalid object. |
|
|
126 | SvREFCNT_inc (self); |
|
|
127 | hv_clear (self); |
|
|
128 | sv_unmagic ((SV *)self, PERL_MAGIC_ext); |
|
|
129 | SvREFCNT_dec (self); |
|
|
130 | |
|
|
131 | // self *must* be null now because thats sv_unmagic's job. |
|
|
132 | assert (!this->self); |
|
|
133 | flags |= 0x80; // severed //D |
|
|
134 | } |
114 | } |
135 | } |
115 | |
136 | |
116 | void |
137 | void |
117 | attachable::optimise () |
138 | attachable::optimise () |
118 | { |
139 | { |
119 | if (self |
140 | if (self |
120 | && SvREFCNT (self) == 1 |
141 | && SvREFCNT (self) == 1 |
121 | && !HvTOTALKEYS (self)) |
142 | && !HvTOTALKEYS (self)) |
122 | { |
143 | flags |= 0x40,//D |
123 | SV *self = (SV *)this->self; |
144 | sever_self (); |
124 | |
|
|
125 | SvREFCNT_inc (self); |
|
|
126 | sv_unmagic (self, PERL_MAGIC_ext); |
|
|
127 | SvREFCNT_dec (self); |
|
|
128 | assert (!this->self); |
|
|
129 | } |
|
|
130 | } |
145 | } |
131 | |
146 | |
132 | // check wether the object really is dead |
147 | // check wether the object really is dead |
133 | void |
148 | void |
134 | attachable::do_check () |
149 | attachable::do_check () |
… | |
… | |
149 | SvREFCNT_dec (cb); |
164 | SvREFCNT_dec (cb); |
150 | cb = 0; |
165 | cb = 0; |
151 | } |
166 | } |
152 | |
167 | |
153 | if (self) |
168 | if (self) |
154 | { |
169 | sever_self (); |
155 | hv_clear (self); |
|
|
156 | |
170 | |
157 | SV *self = (SV *)this->self; |
171 | flags |= 0x20; //D |
158 | SvREFCNT_inc (self); |
|
|
159 | sv_unmagic (self, PERL_MAGIC_ext); |
|
|
160 | SvREFCNT_dec (self); |
|
|
161 | // self is now 0 |
|
|
162 | assert (!this->self);//D//TODO remove soon |
|
|
163 | } |
|
|
164 | |
|
|
165 | mortals.push_back (this); |
172 | mortals.push_back (this); |
166 | } |
173 | } |
167 | |
174 | |
168 | void |
175 | void |
169 | attachable::destroy () |
176 | attachable::destroy () |
… | |
… | |
264 | |
271 | |
265 | if (!obj->self) |
272 | if (!obj->self) |
266 | { |
273 | { |
267 | obj->self = newHV (); |
274 | obj->self = newHV (); |
268 | sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0); |
275 | sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0); |
|
|
276 | obj->flags |= (obj->flags & 0xc0) << 8; |
|
|
277 | obj->flags &= ~0xc0;//D |
|
|
278 | obj->flags |= 0x10;//D |
269 | |
279 | |
270 | // now bless the object _once_ |
280 | // now bless the object _once_ |
271 | return sv_bless (newRV_inc ((SV *)obj->self), stash); |
281 | return sv_bless (newRV_inc ((SV *)obj->self), stash); |
272 | } |
282 | } |
273 | else |
283 | else |
… | |
… | |
345 | inline SV *to_sv (treasurelist * v) { return to_sv (v->name); } |
355 | inline SV *to_sv (treasurelist * v) { return to_sv (v->name); } |
346 | |
356 | |
347 | inline SV *to_sv (UUID v) |
357 | inline SV *to_sv (UUID v) |
348 | { |
358 | { |
349 | char buf[128]; |
359 | char buf[128]; |
350 | snprintf (buf, 128, "<1,%" PRIx64 ">", v.seq); |
360 | snprintf (buf, 128, "<1.%" PRIx64 ">", v.seq); |
351 | return newSVpv (buf, 0); |
361 | return newSVpv (buf, 0); |
352 | } |
362 | } |
353 | |
363 | |
354 | inline void sv_to (SV *sv, shstr &v) { v = SvOK (sv) ? SvPV_nolen (sv) : 0; } |
364 | inline void sv_to (SV *sv, shstr &v) { v = SvOK (sv) ? SvPV_nolen (sv) : 0; } |
355 | inline void sv_to (SV *sv, char * &v) { free (v); v = SvOK (sv) ? strdup (SvPV_nolen (sv)) : 0; } |
365 | inline void sv_to (SV *sv, char * &v) { free (v); v = SvOK (sv) ? strdup (SvPV_nolen (sv)) : 0; } |
… | |
… | |
602 | if (perl_parse (perl, xs_init, 2, argv, (char **)NULL) || perl_run (perl)) |
612 | if (perl_parse (perl, xs_init, 2, argv, (char **)NULL) || perl_run (perl)) |
603 | { |
613 | { |
604 | printf ("unable to initialize perl-interpreter, aborting.\n"); |
614 | printf ("unable to initialize perl-interpreter, aborting.\n"); |
605 | exit (EXIT_FAILURE); |
615 | exit (EXIT_FAILURE); |
606 | } |
616 | } |
|
|
617 | |
|
|
618 | { |
|
|
619 | dSP; |
|
|
620 | |
|
|
621 | PUSHMARK (SP); |
|
|
622 | PUTBACK; |
|
|
623 | call_pv ("cf::init", G_DISCARD | G_VOID); |
|
|
624 | } |
607 | } |
625 | } |
608 | |
626 | |
609 | void cfperl_main () |
627 | void cfperl_main () |
610 | { |
628 | { |
611 | dSP; |
629 | dSP; |
… | |
… | |
829 | void |
847 | void |
830 | cfperl_emergency_save () |
848 | cfperl_emergency_save () |
831 | { |
849 | { |
832 | CALL_BEGIN (0); |
850 | CALL_BEGIN (0); |
833 | CALL_CALL ("cf::emergency_save", G_VOID); |
851 | CALL_CALL ("cf::emergency_save", G_VOID); |
|
|
852 | CALL_END; |
|
|
853 | } |
|
|
854 | |
|
|
855 | void |
|
|
856 | cfperl_cleanup (int make_core) |
|
|
857 | { |
|
|
858 | CALL_BEGIN (1); |
|
|
859 | CALL_ARG (make_core); |
|
|
860 | CALL_CALL ("cf::post_cleanup", G_VOID); |
834 | CALL_END; |
861 | CALL_END; |
835 | } |
862 | } |
836 | |
863 | |
837 | maptile * |
864 | maptile * |
838 | maptile::find_sync (const char *path, maptile *origin) |
865 | maptile::find_sync (const char *path, maptile *origin) |
… | |
… | |
1647 | |
1674 | |
1648 | void cleanup (const char *cause, bool make_core = false) |
1675 | void cleanup (const char *cause, bool make_core = false) |
1649 | |
1676 | |
1650 | void emergency_save () |
1677 | void emergency_save () |
1651 | |
1678 | |
|
|
1679 | void _exit (int status = EXIT_SUCCESS) |
|
|
1680 | |
1652 | UV sv_2watcher (SV *w) |
1681 | UV sv_2watcher (SV *w) |
1653 | CODE: |
1682 | CODE: |
1654 | RETVAL = (UV)GEventAPI->sv_2watcher (w); |
1683 | RETVAL = (UV)GEventAPI->sv_2watcher (w); |
1655 | OUTPUT: |
1684 | OUTPUT: |
1656 | RETVAL |
1685 | RETVAL |
1657 | |
|
|
1658 | void _exit (int status = 0) |
|
|
1659 | |
1686 | |
1660 | #if _POSIX_MEMLOCK |
1687 | #if _POSIX_MEMLOCK |
1661 | |
1688 | |
1662 | int mlockall (int flags = MCL_CURRENT | MCL_FUTURE) |
1689 | int mlockall (int flags = MCL_CURRENT | MCL_FUTURE) |
1663 | |
1690 | |
… | |
… | |
1709 | RETVAL = newSVpv (resist_plus[atnr], 0); |
1736 | RETVAL = newSVpv (resist_plus[atnr], 0); |
1710 | else |
1737 | else |
1711 | XSRETURN_UNDEF; |
1738 | XSRETURN_UNDEF; |
1712 | OUTPUT: RETVAL |
1739 | OUTPUT: RETVAL |
1713 | |
1740 | |
|
|
1741 | bool |
|
|
1742 | load_resource_file (const char *filename) |
|
|
1743 | |
1714 | MODULE = cf PACKAGE = cf::attachable |
1744 | MODULE = cf PACKAGE = cf::attachable |
1715 | |
1745 | |
1716 | int |
1746 | int |
1717 | valid (SV *obj) |
1747 | valid (SV *obj) |
1718 | CODE: |
1748 | CODE: |
1719 | RETVAL = SvROK (obj) && mg_find (SvRV (obj), PERL_MAGIC_ext); |
1749 | RETVAL = SvROK (obj) && mg_find (SvRV (obj), PERL_MAGIC_ext); |
1720 | OUTPUT: |
1750 | OUTPUT: |
1721 | RETVAL |
1751 | RETVAL |
|
|
1752 | |
|
|
1753 | void |
|
|
1754 | debug_trace (attachable *obj, bool on = true) |
|
|
1755 | CODE: |
|
|
1756 | obj->flags &= ~attachable::F_DEBUG_TRACE; |
|
|
1757 | if (on) |
|
|
1758 | obj->flags |= attachable::F_DEBUG_TRACE; |
1722 | |
1759 | |
1723 | int mortals_size () |
1760 | int mortals_size () |
1724 | CODE: |
1761 | CODE: |
1725 | RETVAL = attachable::mortals.size (); |
1762 | RETVAL = attachable::mortals.size (); |
1726 | OUTPUT: RETVAL |
1763 | OUTPUT: RETVAL |
… | |
… | |
1803 | void |
1840 | void |
1804 | set_animation (object *op, int idx) |
1841 | set_animation (object *op, int idx) |
1805 | CODE: |
1842 | CODE: |
1806 | SET_ANIMATION (op, idx); |
1843 | SET_ANIMATION (op, idx); |
1807 | |
1844 | |
|
|
1845 | int |
|
|
1846 | num_animations (object *op) |
|
|
1847 | CODE: |
|
|
1848 | RETVAL = NUM_ANIMATIONS (op); |
|
|
1849 | OUTPUT: RETVAL |
|
|
1850 | |
1808 | object *find_best_object_match (object *op, const char *match) |
1851 | object *find_best_object_match (object *op, const char *match) |
1809 | |
1852 | |
1810 | object *find_marked_object (object *op) |
1853 | object *find_marked_object (object *op) |
1811 | |
1854 | |
1812 | int need_identify (object *obj); |
1855 | int need_identify (object *obj); |
1813 | |
1856 | |
1814 | int apply_shop_mat (object *shop_mat, object *op); |
1857 | int apply_shop_mat (object *shop_mat, object *op); |
|
|
1858 | |
|
|
1859 | int move_player (object *op, int dir) |
|
|
1860 | CODE: |
|
|
1861 | RETVAL = move_player (op, dir); |
|
|
1862 | OUTPUT: |
|
|
1863 | RETVAL |
1815 | |
1864 | |
1816 | int move (object *op, int dir, object *originator = op) |
1865 | int move (object *op, int dir, object *originator = op) |
1817 | CODE: |
1866 | CODE: |
1818 | RETVAL = move_ob (op, dir, originator); |
1867 | RETVAL = move_ob (op, dir, originator); |
1819 | OUTPUT: |
1868 | OUTPUT: |
… | |
… | |
1824 | manual_apply (applied, applier, flags); |
1873 | manual_apply (applied, applier, flags); |
1825 | |
1874 | |
1826 | void apply_below (object *op) |
1875 | void apply_below (object *op) |
1827 | CODE: |
1876 | CODE: |
1828 | player_apply_below (op); |
1877 | player_apply_below (op); |
|
|
1878 | |
|
|
1879 | int cast_heal (object *op, object *caster, object *spell, int dir = 0) |
1829 | |
1880 | |
1830 | object *cf_object_present_archname_inside (object *op, char *whatstr) |
1881 | object *cf_object_present_archname_inside (object *op, char *whatstr) |
1831 | |
1882 | |
1832 | int cf_object_transfer (object *op, int x, int y, int r = 0, object_ornull *orig = 0) |
1883 | int cf_object_transfer (object *op, int x, int y, int r = 0, object_ornull *orig = 0) |
1833 | |
1884 | |
… | |
… | |
2018 | |
2069 | |
2019 | int command_summon (object *op, char *params) |
2070 | int command_summon (object *op, char *params) |
2020 | |
2071 | |
2021 | int command_arrest (object *op, char *params) |
2072 | int command_arrest (object *op, char *params) |
2022 | |
2073 | |
2023 | int command_kick (object *op, char *params) |
|
|
2024 | |
|
|
2025 | int command_banish (object *op, char *params) |
2074 | int command_banish (object *op, char *params) |
2026 | |
2075 | |
2027 | |
2076 | |
2028 | MODULE = cf PACKAGE = cf::player PREFIX = cf_player_ |
2077 | MODULE = cf PACKAGE = cf::player PREFIX = cf_player_ |
2029 | |
2078 | |
… | |
… | |
2048 | pl->orig_stats = pl->ob->stats; |
2097 | pl->orig_stats = pl->ob->stats; |
2049 | |
2098 | |
2050 | void cf_player_move (player *pl, int dir) |
2099 | void cf_player_move (player *pl, int dir) |
2051 | |
2100 | |
2052 | void play_sound_player_only (player *pl, int soundnum, int x = 0, int y = 0); |
2101 | void play_sound_player_only (player *pl, int soundnum, int x = 0, int y = 0); |
2053 | |
|
|
2054 | player *first () |
|
|
2055 | CODE: |
|
|
2056 | RETVAL = first_player; |
|
|
2057 | OUTPUT: RETVAL |
|
|
2058 | |
2102 | |
2059 | bool |
2103 | bool |
2060 | cell_visible (player *pl, int dx, int dy) |
2104 | cell_visible (player *pl, int dx, int dy) |
2061 | CODE: |
2105 | CODE: |
2062 | RETVAL = FABS (dx) <= pl->ns->mapx / 2 && FABS (dy) <= pl->ns->mapy / 2 |
2106 | RETVAL = FABS (dx) <= pl->ns->mapx / 2 && FABS (dy) <= pl->ns->mapy / 2 |
… | |
… | |
2139 | EXTEND (SP, THIS->players); |
2183 | EXTEND (SP, THIS->players); |
2140 | for_all_players (pl) |
2184 | for_all_players (pl) |
2141 | if (pl->ob && pl->ob->map == THIS) |
2185 | if (pl->ob && pl->ob->map == THIS) |
2142 | PUSHs (sv_2mortal (to_sv (pl->ob))); |
2186 | PUSHs (sv_2mortal (to_sv (pl->ob))); |
2143 | } |
2187 | } |
|
|
2188 | |
|
|
2189 | void |
|
|
2190 | maptile::add_underlay (SV *data, int offset, int stride, SV *palette) |
|
|
2191 | CODE: |
|
|
2192 | { |
|
|
2193 | if (!SvROK (palette) || SvTYPE (SvRV (palette)) != SVt_PVAV) |
|
|
2194 | croak ("maptile::add_underlay: palette must be arrayref"); |
|
|
2195 | |
|
|
2196 | palette = SvRV (palette); |
|
|
2197 | |
|
|
2198 | STRLEN idxlen; |
|
|
2199 | const uint8_t *idx = (const uint8_t *)SvPVbyte (data, idxlen); |
|
|
2200 | |
|
|
2201 | for (int x = 0; x < THIS->width; ++x) |
|
|
2202 | for (int y = 0; y < THIS->height; ++y) |
|
|
2203 | { |
|
|
2204 | for (object *op = THIS->at (x, y).bot; op; op = op->above) |
|
|
2205 | if (op->flag [FLAG_IS_FLOOR]) |
|
|
2206 | goto skip_space; |
|
|
2207 | |
|
|
2208 | { |
|
|
2209 | int offs = offset + y * stride + x; |
|
|
2210 | if (IN_RANGE_EXC (offs, 0, idxlen)) |
|
|
2211 | { |
|
|
2212 | if (SV **elem = av_fetch ((AV *)palette, idx [offs], 0)) |
|
|
2213 | { |
|
|
2214 | object *ob = get_archetype (SvPVutf8_nolen (*elem)); |
|
|
2215 | ob->flag [FLAG_NO_MAP_SAVE] = true; |
|
|
2216 | THIS->insert (ob, x, y, 0, INS_ABOVE_FLOOR_ONLY); |
|
|
2217 | } |
|
|
2218 | } |
|
|
2219 | } |
|
|
2220 | |
|
|
2221 | skip_space: ; |
|
|
2222 | } |
|
|
2223 | } |
|
|
2224 | |
|
|
2225 | void |
|
|
2226 | maptile::set_regiondata (SV *data, int offset, int stride, SV *palette) |
|
|
2227 | CODE: |
|
|
2228 | { |
|
|
2229 | if (!SvROK (palette) || SvTYPE (SvRV (palette)) != SVt_PVAV) |
|
|
2230 | croak ("maptile::set_regiondata: palette must be arrayref"); |
|
|
2231 | |
|
|
2232 | palette = SvRV (palette); |
|
|
2233 | |
|
|
2234 | STRLEN idxlen; |
|
|
2235 | const uint8_t *idx = (const uint8_t *)SvPVbyte (data, idxlen); |
|
|
2236 | |
|
|
2237 | region **regionmap = (region **)malloc ( |
|
|
2238 | (av_len ((AV *)palette) + 1) * sizeof (region *)); |
|
|
2239 | uint8_t *regions = salloc<uint8_t> (THIS->size ()); |
|
|
2240 | |
|
|
2241 | for (int i = av_len ((AV *)palette) + 1; i--; ) |
|
|
2242 | regionmap [i] = region::find ( |
|
|
2243 | SvPVutf8_nolen (*av_fetch ((AV *)palette, i, 1))); |
|
|
2244 | |
|
|
2245 | for (int y = 0; y < THIS->height; ++y) |
|
|
2246 | memcpy (regions + y * THIS->width, idx + offset + y * stride, THIS->width); |
|
|
2247 | |
|
|
2248 | sfree (THIS->regions, THIS->size ()); |
|
|
2249 | free (THIS->regionmap); |
|
|
2250 | |
|
|
2251 | THIS->regions = regions; |
|
|
2252 | THIS->regionmap = regionmap; |
|
|
2253 | } |
2144 | |
2254 | |
2145 | void play_sound_map (maptile *map, int x, int y, int sound_num) |
2255 | void play_sound_map (maptile *map, int x, int y, int sound_num) |
2146 | |
2256 | |
2147 | int out_of_map (maptile *map, int x, int y) |
2257 | int out_of_map (maptile *map, int x, int y) |
2148 | |
2258 | |
… | |
… | |
2229 | OUTPUT: RETVAL |
2339 | OUTPUT: RETVAL |
2230 | |
2340 | |
2231 | void fix_walls (maptile *map, int x, int y) |
2341 | void fix_walls (maptile *map, int x, int y) |
2232 | |
2342 | |
2233 | void fix_walls_around (maptile *map, int x, int y) |
2343 | void fix_walls_around (maptile *map, int x, int y) |
2234 | |
|
|
2235 | const char * |
|
|
2236 | region_name (maptile *m) |
|
|
2237 | CODE: |
|
|
2238 | RETVAL = get_name_of_region_for_map (m); |
|
|
2239 | OUTPUT: RETVAL |
|
|
2240 | |
2344 | |
2241 | # worst xs function of my life |
2345 | # worst xs function of my life |
2242 | bool |
2346 | bool |
2243 | _create_random_map (\ |
2347 | _create_random_map (\ |
2244 | maptile *self,\ |
2348 | maptile *self,\ |
… | |
… | |
2352 | |
2456 | |
2353 | INCLUDE: $PERL genacc partylist ../include/player.h | |
2457 | INCLUDE: $PERL genacc partylist ../include/player.h | |
2354 | |
2458 | |
2355 | MODULE = cf PACKAGE = cf::region |
2459 | MODULE = cf PACKAGE = cf::region |
2356 | |
2460 | |
2357 | region *first () |
2461 | void |
2358 | PROTOTYPE: |
2462 | list () |
2359 | CODE: |
2463 | PPCODE: |
2360 | RETVAL = first_region; |
2464 | for_all_regions (rgn) |
2361 | OUTPUT: RETVAL |
2465 | XPUSHs (sv_2mortal (to_sv (rgn))); |
2362 | |
2466 | |
2363 | region *find (char *name) |
2467 | region *find (char *name) |
2364 | PROTOTYPE: $ |
2468 | PROTOTYPE: $ |
2365 | CODE: |
2469 | CODE: |
2366 | RETVAL = get_region_by_name (name); |
2470 | RETVAL = region::find (name); |
|
|
2471 | OUTPUT: RETVAL |
|
|
2472 | |
|
|
2473 | region *find_fuzzy (char *name) |
|
|
2474 | PROTOTYPE: $ |
|
|
2475 | CODE: |
|
|
2476 | RETVAL = region::find_fuzzy (name); |
2367 | OUTPUT: RETVAL |
2477 | OUTPUT: RETVAL |
2368 | |
2478 | |
2369 | INCLUDE: $PERL genacc region ../include/map.h | |
2479 | INCLUDE: $PERL genacc region ../include/map.h | |
2370 | |
2480 | |
2371 | MODULE = cf PACKAGE = cf::living |
2481 | MODULE = cf PACKAGE = cf::living |