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.154 by root, Fri Jan 26 21:44:11 2007 UTC vs.
Revision 1.170 by root, Fri Feb 16 19:43:41 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 *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
133void 148void
134attachable::do_check () 149attachable::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
168void 175void
169attachable::destroy () 176attachable::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
345inline SV *to_sv (treasurelist * v) { return to_sv (v->name); } 355inline SV *to_sv (treasurelist * v) { return to_sv (v->name); }
346 356
347inline SV *to_sv (UUID v) 357inline 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
354inline 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; }
355inline 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; }
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
609void cfperl_main () 627void cfperl_main ()
610{ 628{
611 dSP; 629 dSP;
829void 847void
830cfperl_emergency_save () 848cfperl_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
855void
856cfperl_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
837maptile * 864maptile *
838maptile::find_sync (const char *path, maptile *origin) 865maptile::find_sync (const char *path, maptile *origin)
1647 1674
1648void cleanup (const char *cause, bool make_core = false) 1675void cleanup (const char *cause, bool make_core = false)
1649 1676
1650void emergency_save () 1677void emergency_save ()
1651 1678
1679void _exit (int status = EXIT_SUCCESS)
1680
1652UV sv_2watcher (SV *w) 1681UV 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
1658void _exit (int status = 0)
1659 1686
1660#if _POSIX_MEMLOCK 1687#if _POSIX_MEMLOCK
1661 1688
1662int mlockall (int flags = MCL_CURRENT | MCL_FUTURE) 1689int 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
1741bool
1742load_resource_file (const char *filename)
1743
1714MODULE = cf PACKAGE = cf::attachable 1744MODULE = cf PACKAGE = cf::attachable
1715 1745
1716int 1746int
1717valid (SV *obj) 1747valid (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
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;
1722 1759
1723int mortals_size () 1760int mortals_size ()
1724 CODE: 1761 CODE:
1725 RETVAL = attachable::mortals.size (); 1762 RETVAL = attachable::mortals.size ();
1726 OUTPUT: RETVAL 1763 OUTPUT: RETVAL
1803void 1840void
1804set_animation (object *op, int idx) 1841set_animation (object *op, int idx)
1805 CODE: 1842 CODE:
1806 SET_ANIMATION (op, idx); 1843 SET_ANIMATION (op, idx);
1807 1844
1845int
1846num_animations (object *op)
1847 CODE:
1848 RETVAL = NUM_ANIMATIONS (op);
1849 OUTPUT: RETVAL
1850
1808object *find_best_object_match (object *op, const char *match) 1851object *find_best_object_match (object *op, const char *match)
1809 1852
1810object *find_marked_object (object *op) 1853object *find_marked_object (object *op)
1811 1854
1812int need_identify (object *obj); 1855int need_identify (object *obj);
1824 manual_apply (applied, applier, flags); 1867 manual_apply (applied, applier, flags);
1825 1868
1826void apply_below (object *op) 1869void apply_below (object *op)
1827 CODE: 1870 CODE:
1828 player_apply_below (op); 1871 player_apply_below (op);
1872
1873int cast_heal (object *op, object *caster, object *spell, int dir = 0)
1829 1874
1830object *cf_object_present_archname_inside (object *op, char *whatstr) 1875object *cf_object_present_archname_inside (object *op, char *whatstr)
1831 1876
1832int cf_object_transfer (object *op, int x, int y, int r = 0, object_ornull *orig = 0) 1877int cf_object_transfer (object *op, int x, int y, int r = 0, object_ornull *orig = 0)
1833 1878
2048 pl->orig_stats = pl->ob->stats; 2093 pl->orig_stats = pl->ob->stats;
2049 2094
2050void cf_player_move (player *pl, int dir) 2095void cf_player_move (player *pl, int dir)
2051 2096
2052void play_sound_player_only (player *pl, int soundnum, int x = 0, int y = 0); 2097void play_sound_player_only (player *pl, int soundnum, int x = 0, int y = 0);
2053
2054player *first ()
2055 CODE:
2056 RETVAL = first_player;
2057 OUTPUT: RETVAL
2058 2098
2059bool 2099bool
2060cell_visible (player *pl, int dx, int dy) 2100cell_visible (player *pl, int dx, int dy)
2061 CODE: 2101 CODE:
2062 RETVAL = FABS (dx) <= pl->ns->mapx / 2 && FABS (dy) <= pl->ns->mapy / 2 2102 RETVAL = FABS (dx) <= pl->ns->mapx / 2 && FABS (dy) <= pl->ns->mapy / 2
2139 EXTEND (SP, THIS->players); 2179 EXTEND (SP, THIS->players);
2140 for_all_players (pl) 2180 for_all_players (pl)
2141 if (pl->ob && pl->ob->map == THIS) 2181 if (pl->ob && pl->ob->map == THIS)
2142 PUSHs (sv_2mortal (to_sv (pl->ob))); 2182 PUSHs (sv_2mortal (to_sv (pl->ob)));
2143 } 2183 }
2184
2185void
2186maptile::add_underlay (SV *data, int offset, int stride, SV *palette)
2187 CODE:
2188{
2189 if (!SvROK (palette) || SvTYPE (SvRV (palette)) != SVt_PVAV)
2190 croak ("maptile::add_underlay: palette must be arrayref");
2191
2192 palette = SvRV (palette);
2193
2194 STRLEN idxlen;
2195 const uint8_t *idx = (const uint8_t *)SvPVbyte (data, idxlen);
2196
2197 for (int x = 0; x < THIS->width; ++x)
2198 for (int y = 0; y < THIS->height; ++y)
2199 {
2200 for (object *op = THIS->at (x, y).bot; op; op = op->above)
2201 if (op->flag [FLAG_IS_FLOOR])
2202 goto skip_space;
2203
2204 {
2205 int offs = offset + y * stride + x;
2206 if (IN_RANGE_EXC (offs, 0, idxlen))
2207 {
2208 if (SV **elem = av_fetch ((AV *)palette, idx [offs], 0))
2209 {
2210 object *ob = get_archetype (SvPVutf8_nolen (*elem));
2211 ob->flag [FLAG_NO_MAP_SAVE] = true;
2212 THIS->insert (ob, x, y, 0, INS_ABOVE_FLOOR_ONLY);
2213 }
2214 }
2215 }
2216
2217 skip_space: ;
2218 }
2219}
2220
2221void
2222maptile::set_regiondata (SV *data, int offset, int stride, SV *palette)
2223 CODE:
2224{
2225 if (!SvROK (palette) || SvTYPE (SvRV (palette)) != SVt_PVAV)
2226 croak ("maptile::set_regiondata: palette must be arrayref");
2227
2228 palette = SvRV (palette);
2229
2230 STRLEN idxlen;
2231 const uint8_t *idx = (const uint8_t *)SvPVbyte (data, idxlen);
2232
2233 region **regionmap = (region **)malloc (
2234 (av_len ((AV *)palette) + 1) * sizeof (region *));
2235 uint8_t *regions = salloc<uint8_t> (THIS->size ());
2236
2237 for (int i = av_len ((AV *)palette) + 1; i--; )
2238 regionmap [i] = region::find (
2239 SvPVutf8_nolen (*av_fetch ((AV *)palette, i, 1)));
2240
2241 for (int y = 0; y < THIS->height; ++y)
2242 memcpy (regions + y * THIS->width, idx + offset + y * stride, THIS->width);
2243
2244 sfree (THIS->regions, THIS->size ());
2245 free (THIS->regionmap);
2246
2247 THIS->regions = regions;
2248 THIS->regionmap = regionmap;
2249}
2144 2250
2145void play_sound_map (maptile *map, int x, int y, int sound_num) 2251void play_sound_map (maptile *map, int x, int y, int sound_num)
2146 2252
2147int out_of_map (maptile *map, int x, int y) 2253int out_of_map (maptile *map, int x, int y)
2148 2254
2229 OUTPUT: RETVAL 2335 OUTPUT: RETVAL
2230 2336
2231void fix_walls (maptile *map, int x, int y) 2337void fix_walls (maptile *map, int x, int y)
2232 2338
2233void fix_walls_around (maptile *map, int x, int y) 2339void fix_walls_around (maptile *map, int x, int y)
2234
2235const char *
2236region_name (maptile *m)
2237 CODE:
2238 RETVAL = get_name_of_region_for_map (m);
2239 OUTPUT: RETVAL
2240 2340
2241# worst xs function of my life 2341# worst xs function of my life
2242bool 2342bool
2243_create_random_map (\ 2343_create_random_map (\
2244 maptile *self,\ 2344 maptile *self,\
2352 2452
2353INCLUDE: $PERL genacc partylist ../include/player.h | 2453INCLUDE: $PERL genacc partylist ../include/player.h |
2354 2454
2355MODULE = cf PACKAGE = cf::region 2455MODULE = cf PACKAGE = cf::region
2356 2456
2357region *first () 2457void
2358 PROTOTYPE: 2458list ()
2359 CODE: 2459 PPCODE:
2360 RETVAL = first_region; 2460 for_all_regions (rgn)
2361 OUTPUT: RETVAL 2461 XPUSHs (sv_2mortal (to_sv (rgn)));
2362 2462
2363region *find (char *name) 2463region *find (char *name)
2364 PROTOTYPE: $ 2464 PROTOTYPE: $
2365 CODE: 2465 CODE:
2366 RETVAL = get_region_by_name (name); 2466 RETVAL = region::find (name);
2467 OUTPUT: RETVAL
2468
2469region *find_fuzzy (char *name)
2470 PROTOTYPE: $
2471 CODE:
2472 RETVAL = region::find_fuzzy (name);
2367 OUTPUT: RETVAL 2473 OUTPUT: RETVAL
2368 2474
2369INCLUDE: $PERL genacc region ../include/map.h | 2475INCLUDE: $PERL genacc region ../include/map.h |
2370 2476
2371MODULE = cf PACKAGE = cf::living 2477MODULE = cf PACKAGE = cf::living

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines