ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/cfperl.xs
(Generate patch)

Comparing deliantra/server/server/cfperl.xs (file contents):
Revision 1.153 by root, Fri Jan 26 20:59:57 2007 UTC vs.
Revision 1.173 by root, Mon Mar 5 19:54:49 2007 UTC

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
102unordered_vector<attachable *> attachable::mortals; 103unordered_vector<attachable *> attachable::mortals;
103 104
104attachable::~attachable () 105attachable::~attachable ()
105{ 106{
107 flags |=0x3300;//D
106 assert (!self); 108 assert (!self);
107 assert (!cb); 109 assert (!cb);
108} 110}
109 111
110int 112int
111attachable::refcnt_cnt () const 113attachable::refcnt_cnt () const
112{ 114{
113 return refcnt + (self ? SvREFCNT (self) - 1 : 0); 115 return refcnt + (self ? SvREFCNT (self) - 1 : 0);
116}
117
118void
119attachable::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
116void 137void
117attachable::optimise () 138attachable::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_unmagic ((SV *)self, PERL_MAGIC_ext); 144 sever_self ();
124 assert (!self);
125 }
126} 145}
127 146
128// check wether the object really is dead 147// check wether the object really is dead
129void 148void
130attachable::do_check () 149attachable::do_check ()
145 SvREFCNT_dec (cb); 164 SvREFCNT_dec (cb);
146 cb = 0; 165 cb = 0;
147 } 166 }
148 167
149 if (self) 168 if (self)
150 { 169 sever_self ();
151 hv_clear (self);
152 sv_unmagic ((SV *)self, PERL_MAGIC_ext);
153 // self is now 0
154 assert (!self);//D//TODO remove soon
155 }
156 170
171 flags |= 0x20; //D
157 mortals.push_back (this); 172 mortals.push_back (this);
158} 173}
159 174
160void 175void
161attachable::destroy () 176attachable::destroy ()
256 271
257 if (!obj->self) 272 if (!obj->self)
258 { 273 {
259 obj->self = newHV (); 274 obj->self = newHV ();
260 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
261 279
262 // now bless the object _once_ 280 // now bless the object _once_
263 return sv_bless (newRV_inc ((SV *)obj->self), stash); 281 return sv_bless (newRV_inc ((SV *)obj->self), stash);
264 } 282 }
265 else 283 else
337inline SV *to_sv (treasurelist * v) { return to_sv (v->name); } 355inline SV *to_sv (treasurelist * v) { return to_sv (v->name); }
338 356
339inline SV *to_sv (UUID v) 357inline SV *to_sv (UUID v)
340{ 358{
341 char buf[128]; 359 char buf[128];
342 snprintf (buf, 128, "<1,%" PRIx64 ">", v.seq); 360 snprintf (buf, 128, "<1.%" PRIx64 ">", v.seq);
343 return newSVpv (buf, 0); 361 return newSVpv (buf, 0);
344} 362}
345 363
346inline void sv_to (SV *sv, shstr &v) { v = SvOK (sv) ? SvPV_nolen (sv) : 0; } 364inline void sv_to (SV *sv, shstr &v) { v = SvOK (sv) ? SvPV_nolen (sv) : 0; }
347inline void sv_to (SV *sv, char * &v) { free (v); v = SvOK (sv) ? strdup (SvPV_nolen (sv)) : 0; } 365inline void sv_to (SV *sv, char * &v) { free (v); v = SvOK (sv) ? strdup (SvPV_nolen (sv)) : 0; }
594 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))
595 { 613 {
596 printf ("unable to initialize perl-interpreter, aborting.\n"); 614 printf ("unable to initialize perl-interpreter, aborting.\n");
597 exit (EXIT_FAILURE); 615 exit (EXIT_FAILURE);
598 } 616 }
617
618 {
619 dSP;
620
621 PUSHMARK (SP);
622 PUTBACK;
623 call_pv ("cf::init", G_DISCARD | G_VOID);
624 }
599} 625}
600 626
601void cfperl_main () 627void cfperl_main ()
602{ 628{
603 dSP; 629 dSP;
821void 847void
822cfperl_emergency_save () 848cfperl_emergency_save ()
823{ 849{
824 CALL_BEGIN (0); 850 CALL_BEGIN (0);
825 CALL_CALL ("cf::emergency_save", G_VOID); 851 CALL_CALL ("cf::emergency_save", G_VOID);
852 CALL_END;
853}
854
855void
856cfperl_cleanup (int make_core)
857{
858 CALL_BEGIN (1);
859 CALL_ARG (make_core);
860 CALL_CALL ("cf::post_cleanup", G_VOID);
826 CALL_END; 861 CALL_END;
827} 862}
828 863
829maptile * 864maptile *
830maptile::find_sync (const char *path, maptile *origin) 865maptile::find_sync (const char *path, maptile *origin)
1639 1674
1640void cleanup (const char *cause, bool make_core = false) 1675void cleanup (const char *cause, bool make_core = false)
1641 1676
1642void emergency_save () 1677void emergency_save ()
1643 1678
1679void _exit (int status = EXIT_SUCCESS)
1680
1644UV sv_2watcher (SV *w) 1681UV sv_2watcher (SV *w)
1645 CODE: 1682 CODE:
1646 RETVAL = (UV)GEventAPI->sv_2watcher (w); 1683 RETVAL = (UV)GEventAPI->sv_2watcher (w);
1647 OUTPUT: 1684 OUTPUT:
1648 RETVAL 1685 RETVAL
1649
1650void _exit (int status = 0)
1651 1686
1652#if _POSIX_MEMLOCK 1687#if _POSIX_MEMLOCK
1653 1688
1654int mlockall (int flags = MCL_CURRENT | MCL_FUTURE) 1689int mlockall (int flags = MCL_CURRENT | MCL_FUTURE)
1655 1690
1701 RETVAL = newSVpv (resist_plus[atnr], 0); 1736 RETVAL = newSVpv (resist_plus[atnr], 0);
1702 else 1737 else
1703 XSRETURN_UNDEF; 1738 XSRETURN_UNDEF;
1704 OUTPUT: RETVAL 1739 OUTPUT: RETVAL
1705 1740
1741bool
1742load_resource_file (const char *filename)
1743
1706MODULE = cf PACKAGE = cf::attachable 1744MODULE = cf PACKAGE = cf::attachable
1707 1745
1708int 1746int
1709valid (SV *obj) 1747valid (SV *obj)
1710 CODE: 1748 CODE:
1711 RETVAL = SvROK (obj) && mg_find (SvRV (obj), PERL_MAGIC_ext); 1749 RETVAL = SvROK (obj) && mg_find (SvRV (obj), PERL_MAGIC_ext);
1712 OUTPUT: 1750 OUTPUT:
1713 RETVAL 1751 RETVAL
1752
1753void
1754debug_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;
1714 1759
1715int mortals_size () 1760int mortals_size ()
1716 CODE: 1761 CODE:
1717 RETVAL = attachable::mortals.size (); 1762 RETVAL = attachable::mortals.size ();
1718 OUTPUT: RETVAL 1763 OUTPUT: RETVAL
1720#object *mortals (U32 index) 1765#object *mortals (U32 index)
1721# CODE: 1766# CODE:
1722# RETVAL = index < attachable::mortals.size () ? attachable::mortals [index] : 0; 1767# RETVAL = index < attachable::mortals.size () ? attachable::mortals [index] : 0;
1723# OUTPUT: RETVAL 1768# OUTPUT: RETVAL
1724 1769
1725INCLUDE: $PERL genacc attachable ../include/cfperl.h | 1770INCLUDE: $PERL $srcdir/genacc attachable ../include/cfperl.h |
1726 1771
1727MODULE = cf PACKAGE = cf::global 1772MODULE = cf PACKAGE = cf::global
1728 1773
1729int invoke (SV *klass, int event, ...) 1774int invoke (SV *klass, int event, ...)
1730 CODE: 1775 CODE:
1734 RETVAL = gbl_ev.invoke ((event_type)event, ARG_AV (av), DT_END); 1779 RETVAL = gbl_ev.invoke ((event_type)event, ARG_AV (av), DT_END);
1735 OUTPUT: RETVAL 1780 OUTPUT: RETVAL
1736 1781
1737MODULE = cf PACKAGE = cf::object PREFIX = cf_object_ 1782MODULE = cf PACKAGE = cf::object PREFIX = cf_object_
1738 1783
1739INCLUDE: $PERL genacc object ../include/object.h | 1784INCLUDE: $PERL $srcdir/genacc object ../include/object.h |
1740 1785
1741int invoke (object *op, int event, ...) 1786int invoke (object *op, int event, ...)
1742 CODE: 1787 CODE:
1743 if (KLASS_OF (event) != KLASS_OBJECT) croak ("event class must be OBJECT"); 1788 if (KLASS_OF (event) != KLASS_OBJECT) croak ("event class must be OBJECT");
1744 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); 1789 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
1795void 1840void
1796set_animation (object *op, int idx) 1841set_animation (object *op, int idx)
1797 CODE: 1842 CODE:
1798 SET_ANIMATION (op, idx); 1843 SET_ANIMATION (op, idx);
1799 1844
1845int
1846num_animations (object *op)
1847 CODE:
1848 RETVAL = NUM_ANIMATIONS (op);
1849 OUTPUT: RETVAL
1850
1800object *find_best_object_match (object *op, const char *match) 1851object *find_best_object_match (object *op, const char *match)
1801 1852
1802object *find_marked_object (object *op) 1853object *find_marked_object (object *op)
1803 1854
1804int need_identify (object *obj); 1855int need_identify (object *obj);
1805 1856
1806int apply_shop_mat (object *shop_mat, object *op); 1857int apply_shop_mat (object *shop_mat, object *op);
1858
1859int move_player (object *op, int dir)
1860 CODE:
1861 RETVAL = move_player (op, dir);
1862 OUTPUT:
1863 RETVAL
1807 1864
1808int move (object *op, int dir, object *originator = op) 1865int move (object *op, int dir, object *originator = op)
1809 CODE: 1866 CODE:
1810 RETVAL = move_ob (op, dir, originator); 1867 RETVAL = move_ob (op, dir, originator);
1811 OUTPUT: 1868 OUTPUT:
1816 manual_apply (applied, applier, flags); 1873 manual_apply (applied, applier, flags);
1817 1874
1818void apply_below (object *op) 1875void apply_below (object *op)
1819 CODE: 1876 CODE:
1820 player_apply_below (op); 1877 player_apply_below (op);
1878
1879int cast_heal (object *op, object *caster, object *spell, int dir = 0)
1821 1880
1822object *cf_object_present_archname_inside (object *op, char *whatstr) 1881object *cf_object_present_archname_inside (object *op, char *whatstr)
1823 1882
1824int cf_object_transfer (object *op, int x, int y, int r = 0, object_ornull *orig = 0) 1883int cf_object_transfer (object *op, int x, int y, int r = 0, object_ornull *orig = 0)
1825 1884
2010 2069
2011int command_summon (object *op, char *params) 2070int command_summon (object *op, char *params)
2012 2071
2013int command_arrest (object *op, char *params) 2072int command_arrest (object *op, char *params)
2014 2073
2015int command_kick (object *op, char *params)
2016
2017int command_banish (object *op, char *params) 2074int command_banish (object *op, char *params)
2018 2075
2019 2076
2020MODULE = cf PACKAGE = cf::player PREFIX = cf_player_ 2077MODULE = cf PACKAGE = cf::player PREFIX = cf_player_
2021 2078
2022INCLUDE: $PERL genacc player ../include/player.h | 2079INCLUDE: $PERL $srcdir/genacc player ../include/player.h |
2023 2080
2024int invoke (player *pl, int event, ...) 2081int invoke (player *pl, int event, ...)
2025 CODE: 2082 CODE:
2026 if (KLASS_OF (event) != KLASS_PLAYER) croak ("event class must be PLAYER"); 2083 if (KLASS_OF (event) != KLASS_PLAYER) croak ("event class must be PLAYER");
2027 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); 2084 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
2040 pl->orig_stats = pl->ob->stats; 2097 pl->orig_stats = pl->ob->stats;
2041 2098
2042void cf_player_move (player *pl, int dir) 2099void cf_player_move (player *pl, int dir)
2043 2100
2044void play_sound_player_only (player *pl, int soundnum, int x = 0, int y = 0); 2101void play_sound_player_only (player *pl, int soundnum, int x = 0, int y = 0);
2045
2046player *first ()
2047 CODE:
2048 RETVAL = first_player;
2049 OUTPUT: RETVAL
2050 2102
2051bool 2103bool
2052cell_visible (player *pl, int dx, int dy) 2104cell_visible (player *pl, int dx, int dy)
2053 CODE: 2105 CODE:
2054 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
2107 RETVAL = map->invoke ((event_type)event, ARG_AV (av), DT_END); 2159 RETVAL = map->invoke ((event_type)event, ARG_AV (av), DT_END);
2108 OUTPUT: RETVAL 2160 OUTPUT: RETVAL
2109 2161
2110SV *registry (maptile *map) 2162SV *registry (maptile *map)
2111 2163
2112INCLUDE: $PERL genacc maptile ../include/map.h | 2164INCLUDE: $PERL $srcdir/genacc maptile ../include/map.h |
2113 2165
2114void 2166void
2115maptile::instantiate () 2167maptile::instantiate ()
2116 2168
2117maptile *new () 2169maptile *new ()
2131 EXTEND (SP, THIS->players); 2183 EXTEND (SP, THIS->players);
2132 for_all_players (pl) 2184 for_all_players (pl)
2133 if (pl->ob && pl->ob->map == THIS) 2185 if (pl->ob && pl->ob->map == THIS)
2134 PUSHs (sv_2mortal (to_sv (pl->ob))); 2186 PUSHs (sv_2mortal (to_sv (pl->ob)));
2135 } 2187 }
2188
2189void
2190maptile::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
2225void
2226maptile::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}
2136 2254
2137void play_sound_map (maptile *map, int x, int y, int sound_num) 2255void play_sound_map (maptile *map, int x, int y, int sound_num)
2138 2256
2139int out_of_map (maptile *map, int x, int y) 2257int out_of_map (maptile *map, int x, int y)
2140 2258
2221 OUTPUT: RETVAL 2339 OUTPUT: RETVAL
2222 2340
2223void fix_walls (maptile *map, int x, int y) 2341void fix_walls (maptile *map, int x, int y)
2224 2342
2225void fix_walls_around (maptile *map, int x, int y) 2343void fix_walls_around (maptile *map, int x, int y)
2226
2227const char *
2228region_name (maptile *m)
2229 CODE:
2230 RETVAL = get_name_of_region_for_map (m);
2231 OUTPUT: RETVAL
2232 2344
2233# worst xs function of my life 2345# worst xs function of my life
2234bool 2346bool
2235_create_random_map (\ 2347_create_random_map (\
2236 maptile *self,\ 2348 maptile *self,\
2330 PROTOTYPE: 2442 PROTOTYPE:
2331 CODE: 2443 CODE:
2332 RETVAL = first_archetype; 2444 RETVAL = first_archetype;
2333 OUTPUT: RETVAL 2445 OUTPUT: RETVAL
2334 2446
2335INCLUDE: $PERL genacc archetype ../include/object.h | 2447INCLUDE: $PERL $srcdir/genacc archetype ../include/object.h |
2336 2448
2337MODULE = cf PACKAGE = cf::party 2449MODULE = cf PACKAGE = cf::party
2338 2450
2339partylist *first () 2451partylist *first ()
2340 PROTOTYPE: 2452 PROTOTYPE:
2341 CODE: 2453 CODE:
2342 RETVAL = get_firstparty (); 2454 RETVAL = get_firstparty ();
2343 OUTPUT: RETVAL 2455 OUTPUT: RETVAL
2344 2456
2345INCLUDE: $PERL genacc partylist ../include/player.h | 2457INCLUDE: $PERL $srcdir/genacc partylist ../include/player.h |
2346 2458
2347MODULE = cf PACKAGE = cf::region 2459MODULE = cf PACKAGE = cf::region
2348 2460
2349region *first () 2461void
2350 PROTOTYPE: 2462list ()
2351 CODE: 2463 PPCODE:
2352 RETVAL = first_region; 2464 for_all_regions (rgn)
2353 OUTPUT: RETVAL 2465 XPUSHs (sv_2mortal (to_sv (rgn)));
2354 2466
2355region *find (char *name) 2467region *find (char *name)
2356 PROTOTYPE: $ 2468 PROTOTYPE: $
2357 CODE: 2469 CODE:
2358 RETVAL = get_region_by_name (name); 2470 RETVAL = region::find (name);
2359 OUTPUT: RETVAL 2471 OUTPUT: RETVAL
2360 2472
2473region *find_fuzzy (char *name)
2474 PROTOTYPE: $
2475 CODE:
2476 RETVAL = region::find_fuzzy (name);
2477 OUTPUT: RETVAL
2478
2361INCLUDE: $PERL genacc region ../include/map.h | 2479INCLUDE: $PERL $srcdir/genacc region ../include/map.h |
2362 2480
2363MODULE = cf PACKAGE = cf::living 2481MODULE = cf PACKAGE = cf::living
2364 2482
2365INCLUDE: $PERL genacc living ../include/living.h | 2483INCLUDE: $PERL $srcdir/genacc living ../include/living.h |
2366 2484
2367MODULE = cf PACKAGE = cf::settings 2485MODULE = cf PACKAGE = cf::settings
2368 2486
2369INCLUDE: $PERL genacc Settings ../include/global.h | 2487INCLUDE: $PERL $srcdir/genacc Settings ../include/global.h |
2370 2488
2371MODULE = cf PACKAGE = cf::client 2489MODULE = cf PACKAGE = cf::client
2372 2490
2373INCLUDE: $PERL genacc client ../include/client.h | 2491INCLUDE: $PERL $srcdir/genacc client ../include/client.h |
2374 2492
2375int invoke (client *ns, int event, ...) 2493int invoke (client *ns, int event, ...)
2376 CODE: 2494 CODE:
2377 if (KLASS_OF (event) != KLASS_CLIENT) croak ("event class must be CLIENT"); 2495 if (KLASS_OF (event) != KLASS_CLIENT) croak ("event class must be CLIENT");
2378 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); 2496 AV *av = (AV *)sv_2mortal ((SV *)newAV ());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines