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

Comparing deliantra/server/plugins/cfperl/cfperl.xs (file contents):
Revision 1.1.1.1 by elmex, Wed Mar 15 14:05:32 2006 UTC vs.
Revision 1.15 by root, Wed Feb 8 03:46:15 2006 UTC

29#include <XSUB.h> 29#include <XSUB.h>
30 30
31#undef save_long // clashes with libproto.h 31#undef save_long // clashes with libproto.h
32 32
33#define PLUGIN_NAME "perl" 33#define PLUGIN_NAME "perl"
34#define PLUGIN_VERSION "cfperl 0.2" 34#define PLUGIN_VERSION "cfperl 0.0"
35 35
36#ifndef __CEXTRACT__ 36#ifndef __CEXTRACT__
37#include <plugin.h> 37#include <plugin.h>
38#endif 38#endif
39 39
46#include <plugin_common.h> 46#include <plugin_common.h>
47 47
48#include <stdarg.h> 48#include <stdarg.h>
49 49
50#include "perlxsi.c" 50#include "perlxsi.c"
51
52typedef object object_ornull;
53typedef mapstruct mapstruct_ornull;
54 51
55static f_plug_api gethook; 52static f_plug_api gethook;
56static f_plug_api registerGlobalEvent; 53static f_plug_api registerGlobalEvent;
57static f_plug_api unregisterGlobalEvent; 54static f_plug_api unregisterGlobalEvent;
58static f_plug_api systemDirectory; 55static f_plug_api systemDirectory;
59static f_plug_api object_set_property; 56static f_plug_api object_set_property;
60static f_plug_api map_get_map; 57static f_plug_api map_get_map;
61static f_plug_api object_insert;
62 58
63typedef struct 59typedef struct
64{ 60{
65 object* who; 61 object* who;
66 object* activator; 62 object* activator;
84// all objects no longer referenced and empty are 80// all objects no longer referenced and empty are
85// eligible for destruction. 81// eligible for destruction.
86void 82void
87clean_obj_cache () 83clean_obj_cache ()
88{ 84{
89 static int count;
90
91 if (++count & 7)
92 return;
93
94 int todo = 1000; 85 int todo = 10;
95 do 86 do
96 { 87 {
97 I32 klen; 88 I32 klen;
98 char *key; 89 char *key;
99 HE *he = hv_iternext (obj_cache); 90 HE *he = hv_iternext (obj_cache);
100 91
101 if (he) 92 if (he)
102 { 93 {
103 SV *sv = hv_iterval (obj_cache, he); 94 SV *sv = SvRV (hv_iterval (obj_cache, he));
104 95
105 // empty and unreferenced? nuke it 96 // emopty and unreferened? nuke it
106 if (SvREFCNT (sv) == 1 && SvREFCNT (SvRV (sv)) == 1 && !HvFILL ((HV *)(SvRV (sv)))) 97 if (SvREFCNT (sv) == 1 && !HvFILL ((HV *)sv))
107 { 98 {
108 hv_delete (obj_cache, HeKEY (he), HeKLEN (he), G_DISCARD); 99 hv_delete (obj_cache, HeKEY (he), HeKLEN (he), G_DISCARD);
109 todo++; 100 todo++;
110 } 101 }
111 } 102 }
116} 107}
117 108
118static SV * 109static SV *
119newSVptr (void *ptr, const char *klass) 110newSVptr (void *ptr, const char *klass)
120{ 111{
121 SV *sv;
122
123 if (!ptr) 112 if (!ptr)
124 return &PL_sv_undef; 113 return &PL_sv_undef;
125 114
126 sv = newSV (0); 115 HV *hv = newHV ();
127 sv_magic (sv, 0, PERL_MAGIC_ext, (char *)ptr, 0); 116 sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0);
128 return sv_bless (newRV_noinc (sv), gv_stashpv (klass, 1)); 117 return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
129} 118}
130 119
131static SV * 120static SV *
132newSVptr_cached (void *ptr, const char *klass) 121newSVptr_cached (void *ptr, const char *klass)
133{ 122{
140 129
141 if (he) 130 if (he)
142 sv = *he; 131 sv = *he;
143 else 132 else
144 { 133 {
145 HV *hv = newHV (); 134 sv = newSVptr (ptr, klass);
146 sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0);
147 sv = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
148 hv_store (obj_cache, (char *)&ptr, sizeof (ptr), sv, 0); 135 hv_store (obj_cache, (char *)&ptr, sizeof (ptr), sv, 0);
149 } 136 }
150 137
151 return newSVsv (sv); 138 return newSVsv (sv);
152} 139}
171 158
172 if (!mg) 159 if (!mg)
173 croak ("perl code used %s object, but C object is already destroyed, caught", klass); 160 croak ("perl code used %s object, but C object is already destroyed, caught", klass);
174 161
175 return (long)mg->mg_ptr; 162 return (long)mg->mg_ptr;
176}
177
178static long
179SvPTR_ornull (SV *sv, const char *klass)
180{
181 if (SvOK (sv))
182 return SvPTR (sv, klass);
183 else
184 return 0;
185} 163}
186 164
187SV * 165SV *
188newSVcfapi (int type, ...) 166newSVcfapi (int type, ...)
189{ 167{
411 registerGlobalEvent = gethook (&rtype, hooktype, "cfapi_system_register_global_event"); 389 registerGlobalEvent = gethook (&rtype, hooktype, "cfapi_system_register_global_event");
412 unregisterGlobalEvent = gethook (&rtype, hooktype, "cfapi_system_unregister_global_event"); 390 unregisterGlobalEvent = gethook (&rtype, hooktype, "cfapi_system_unregister_global_event");
413 systemDirectory = gethook (&rtype, hooktype, "cfapi_system_directory"); 391 systemDirectory = gethook (&rtype, hooktype, "cfapi_system_directory");
414 object_set_property = gethook (&rtype, hooktype, "cfapi_object_set_property"); 392 object_set_property = gethook (&rtype, hooktype, "cfapi_object_set_property");
415 map_get_map = gethook (&rtype, hooktype, "cfapi_map_get_map"); 393 map_get_map = gethook (&rtype, hooktype, "cfapi_map_get_map");
416 object_insert = gethook (&rtype, hooktype, "cfapi_object_insert");
417 394
418 cf_init_plugin (gethook); 395 cf_init_plugin (gethook);
419 396
420 /* Pick the global events you want to monitor from this plugin */ 397 /* Pick the global events you want to monitor from this plugin */
421 registerGlobalEvent (NULL, EVENT_BORN, PLUGIN_NAME, globalEventListener); 398 registerGlobalEvent (NULL, EVENT_BORN, PLUGIN_NAME, globalEventListener);
540 517
541 va_end (args); 518 va_end (args);
542 519
543 if (context.event_code == EVENT_FREE_OB) 520 if (context.event_code == EVENT_FREE_OB)
544 { 521 {
545 SV *sv = hv_delete (obj_cache, (char *)&context.activator, sizeof (void *), 0); 522 SV *sv = hv_delete (obj_cache, (char *)&context.activator, sizeof (object *), 0);
546 523
547 if (sv) 524 if (sv)
525 {
548 clearSVptr (sv); 526 clearSVptr (sv);
527 SvREFCNT_dec (sv);
528 }
549 } 529 }
550 else 530 else
551 inject_event ("cf::inject_global_event", &context); 531 inject_event ("cf::inject_global_event", &context);
552 532
553 rv = context.returnvalue; 533 rv = context.returnvalue;
749 const_iv (ST_BD_BUILD) 729 const_iv (ST_BD_BUILD)
750 const_iv (ST_BD_REMOVE) 730 const_iv (ST_BD_REMOVE)
751 const_iv (ST_MAT_FLOOR) 731 const_iv (ST_MAT_FLOOR)
752 const_iv (ST_MAT_WALL) 732 const_iv (ST_MAT_WALL)
753 const_iv (ST_MAT_ITEM) 733 const_iv (ST_MAT_ITEM)
754
755 const_iv (AT_PHYSICAL)
756 const_iv (AT_MAGIC)
757 const_iv (AT_FIRE)
758 const_iv (AT_ELECTRICITY)
759 const_iv (AT_COLD)
760 const_iv (AT_CONFUSION)
761 const_iv (AT_ACID)
762 const_iv (AT_DRAIN)
763 const_iv (AT_WEAPONMAGIC)
764 const_iv (AT_GHOSTHIT)
765 const_iv (AT_POISON)
766 const_iv (AT_SLOW)
767 const_iv (AT_PARALYZE)
768 const_iv (AT_TURN_UNDEAD)
769 const_iv (AT_FEAR)
770 const_iv (AT_CANCELLATION)
771 const_iv (AT_DEPLETE)
772 const_iv (AT_DEATH)
773 const_iv (AT_CHAOS)
774 const_iv (AT_COUNTERSPELL)
775 const_iv (AT_GODPOWER)
776 const_iv (AT_HOLYWORD)
777 const_iv (AT_BLIND)
778 const_iv (AT_INTERNAL)
779 const_iv (AT_LIFE_STEALING)
780 const_iv (AT_DISEASE)
781 734
782 const_iv (QUEST_IN_PROGRESS) 735 const_iv (QUEST_IN_PROGRESS)
783 const_iv (QUEST_DONE_QUEST) 736 const_iv (QUEST_DONE_QUEST)
784 const_iv (QUEST_DONE_TASK) 737 const_iv (QUEST_DONE_TASK)
785 const_iv (QUEST_START_QUEST) 738 const_iv (QUEST_START_QUEST)
1197 int unused_type; 1150 int unused_type;
1198 object_set_property (&unused_type, obj, idx, (double)SvNV (newval)); 1151 object_set_property (&unused_type, obj, idx, (double)SvNV (newval));
1199 } 1152 }
1200 break; 1153 break;
1201 case CFAPI_STRING: 1154 case CFAPI_STRING:
1202 cf_object_set_string_property (obj, idx, SvOK (newval) ? SvPV_nolen (newval) : 0); 1155 cf_object_set_string_property (obj, idx, SvPV_nolen (newval));
1203 break;
1204 case CFAPI_POBJECT:
1205 {
1206 int unused_type;
1207 object_set_property (&unused_type, obj, idx, (object *)SvPTR_ornull (newval, "cf::object"));
1208 }
1209 break; 1156 break;
1210 default: 1157 default:
1211 croak ("unhandled type '%d' in set_property '%d'", type, idx); 1158 croak ("unhandled type '%d' in set_property '%d'", type, idx);
1212 } 1159 }
1213 1160
1214# missing properties
1215
1216void
1217set_attacktype (object *obj, U32 attacktype)
1218 CODE:
1219 obj->attacktype = attacktype;
1220
1221U32
1222get_attacktype (object *obj)
1223 ALIAS:
1224 attacktype = 0
1225 CODE:
1226 RETVAL = obj->attacktype;
1227 OUTPUT: RETVAL
1228
1229void
1230set_food (object *obj, int food)
1231 CODE:
1232 obj->stats.food = food;
1233
1234int
1235get_food (object *obj)
1236 ALIAS:
1237 food = 0
1238 CODE:
1239 RETVAL = obj->stats.food;
1240 OUTPUT: RETVAL
1241
1242void 1161void
1243inv (object *obj) 1162inv (object *obj)
1244 PROTOTYPE: $ 1163 PROTOTYPE: $
1245 PPCODE: 1164 PPCODE:
1246{ 1165{
1255int cf_object_get_flag (object *op, int flag) 1174int cf_object_get_flag (object *op, int flag)
1256 ALIAS: flag = 0 1175 ALIAS: flag = 0
1257 1176
1258void cf_object_set_flag (object *op, int flag, int value) 1177void cf_object_set_flag (object *op, int flag, int value)
1259 1178
1260void cf_object_move (object *op, int dir, object *originator = op) 1179void cf_object_move (object *op, object *originator, int dir)
1261 1180
1262void cf_object_apply (object *op, object *author, int flags = 0) 1181void cf_object_apply (object *op, object *author, int flags = 0)
1263 1182
1264void cf_object_apply_below (object *op) 1183void cf_object_apply_below (object *op)
1265 1184
1271 1190
1272int cf_object_transfer (object *op, int x, int y, int r, object *orig) 1191int cf_object_transfer (object *op, int x, int y, int r, object *orig)
1273 1192
1274int cf_object_change_map (object *op, int x, int y, mapstruct *map) 1193int cf_object_change_map (object *op, int x, int y, mapstruct *map)
1275 1194
1276object *cf_object_clone (object *op, int clonetype = 0) 1195object *cf_object_clone (object *op, int clonetype)
1277 1196
1278int cf_object_pay_item (object *op, object *buyer) 1197int cf_object_pay_item (object *op, object *buyer)
1279 1198
1280int cf_object_pay_amount (object *op, double amount) 1199int cf_object_pay_amount (object *op, double amount)
1281 1200
1282int cf_object_cast_spell (object *caster, object *ctoo, int dir, object *spell_ob, char *stringarg = 0) 1201int cf_object_cast_spell (object *caster, object *ctoo, int dir, object *sp_, char *flags)
1283 1202
1284int cf_object_cast_ability (object *caster, object *ctoo, int dir, object *sp_, char *stringarg = 0) 1203int cf_object_cast_ability (object *caster, object *ctoo, int dir, object *sp_, char *flags)
1285 1204
1286void cf_object_learn_spell (object *op, object *sp) 1205void cf_object_learn_spell (object *op, object *sp)
1287 1206
1288void cf_object_forget_spell (object *op, object *sp) 1207void cf_object_forget_spell (object *op, object *sp)
1289 1208
1324 1243
1325char *cf_object_get_key (object *op, char *keyname) 1244char *cf_object_get_key (object *op, char *keyname)
1326 ALIAS: key = 0 1245 ALIAS: key = 0
1327 1246
1328void cf_object_set_key (object *op, char *keyname, char *value) 1247void cf_object_set_key (object *op, char *keyname, char *value)
1329
1330object *cf_create_object_by_name (const char *name)
1331
1332MODULE = cf PACKAGE = cf::object PREFIX = cf_
1333
1334void cf_fix_object (object *pl)
1335 ALIAS: fix = 0
1336
1337object *cf_insert_ob_in_ob (object *ob, object *where)
1338
1339# no clean way to get an object from an archetype - stupid idiotic
1340# dumb kludgy misdesigned plug-in api slowly gets on my nerves.
1341
1342object *new (const char *archetype = 0)
1343 PROTOTYPE: ;$
1344 CODE:
1345 RETVAL = archetype ? get_archetype (archetype) : cf_create_object ();
1346 OUTPUT:
1347 RETVAL
1348
1349object *insert_ob_in_map_at (object *ob, mapstruct *where, object_ornull *orig, int flag, int x, int y)
1350 PROTOTYPE: $$$$$$
1351 CODE:
1352{
1353 int unused_type;
1354 RETVAL = (object *)object_insert (&unused_type, ob, 0, where, orig, flag, x, y);
1355}
1356
1357object *get_nearest_player (object *ob)
1358 ALIAS: nearest_player = 0
1359 PREINIT:
1360 extern object *get_nearest_player (object *);
1361
1362void rangevector (object *ob, object *other, int flags = 0)
1363 PROTOTYPE: $$;$
1364 PPCODE:
1365{
1366 rv_vector rv;
1367 get_rangevector (ob, other, &rv, flags);
1368 EXTEND (SP, 5);
1369 PUSHs (newSVuv (rv.distance));
1370 PUSHs (newSViv (rv.distance_x));
1371 PUSHs (newSViv (rv.distance_y));
1372 PUSHs (newSViv (rv.direction));
1373 PUSHs (newSVcfapi (CFAPI_POBJECT, rv.part));
1374}
1375
1376bool on_same_map_as (object *ob, object *other)
1377 CODE:
1378 RETVAL = on_same_map (ob, other);
1379 OUTPUT: RETVAL
1380 1248
1381char * 1249char *
1382base_name (object *ob, int plural) 1250base_name (object *ob, int plural)
1383 CODE: 1251 CODE:
1384 RETVAL = cf_query_base_name (ob, plural); 1252 RETVAL = cf_query_base_name (ob, plural);
1385 OUTPUT: RETVAL 1253 OUTPUT: RETVAL
1386 1254
1255MODULE = cf PACKAGE = cf::object PREFIX = cf_object_
1256
1257object *cf_create_object_by_name (const char *name = 0)
1258 PROTOTYPE: ;$
1259 ALIAS:
1260 create_object = 0
1261 new = 0
1262 CODE:
1263 RETVAL = name ? cf_create_object_by_name (name) : cf_create_object ();
1264 OUTPUT:
1265 RETVAL
1266
1267void cf_fix_object (object *pl)
1268 ALIAS: fix = 0
1269
1270object *cf_insert_ob_in_ob (object *ob, object *where)
1271
1272void rangevector (object *ob, object *other, int flags = 0)
1273 PROTOTYPE: $$;$
1274 ALIAS:
1275 direction = 1
1276 distance = 2
1277 CODE:
1278{
1279 rv_vector rv;
1280 // get_rangevector uses these if the objects are not on the same map
1281 // which is not the same as "on_same_map".
1282 rv.distance_x = 32767;
1283 rv.distance_y = 32767;
1284 get_rangevector (ob, other, &rv, flags);
1285 EXTEND (SP, 5);
1286 if (!ix || ix == 2) PUSHs (newSVuv (rv.distance));
1287 if (!ix) PUSHs (newSViv (rv.distance_x));
1288 if (!ix) PUSHs (newSViv (rv.distance_y));
1289 if (!ix || ix == 1) PUSHs (newSViv (rv.direction));
1290 if (!ix) PUSHs (newSVcfapi (CFAPI_POBJECT, rv.part));
1291}
1292
1293bool on_same_map_as (object *ob, object *other)
1294 CODE:
1295 RETVAL = on_same_map (ob, other);
1296 OUTPUT: RETVAL
1297
1387 1298
1388MODULE = cf PACKAGE = cf::object::player PREFIX = cf_player_ 1299MODULE = cf PACKAGE = cf::object::player PREFIX = cf_player_
1389 1300
1390player *player (object *op) 1301player *player (object *op)
1391 CODE: 1302 CODE:
1394 1305
1395void cf_player_message (object *obj, char *txt, int flags = NDI_ORANGE | NDI_UNIQUE) 1306void cf_player_message (object *obj, char *txt, int flags = NDI_ORANGE | NDI_UNIQUE)
1396 1307
1397object *cf_player_send_inventory (object *op) 1308object *cf_player_send_inventory (object *op)
1398 1309
1399player *contr (object *op)
1400 CODE:
1401 RETVAL = op->contr;
1402 OUTPUT: RETVAL
1403
1404char *cf_player_get_ip (object *op) 1310char *cf_player_get_ip (object *op)
1405 ALIAS: ip = 0 1311 ALIAS: ip = 0
1406 1312
1407object *cf_player_get_marked_item (object *op) 1313object *cf_player_get_marked_item (object *op)
1408 ALIAS: marked_item = 0 1314 ALIAS: marked_item = 0
1421 1327
1422player *cf_player_find (char *name) 1328player *cf_player_find (char *name)
1423 PROTOTYPE: $ 1329 PROTOTYPE: $
1424 1330
1425void cf_player_move (player *pl, int dir) 1331void cf_player_move (player *pl, int dir)
1426
1427void MapNewmapCmd (player *pl)
1428 1332
1429# nonstandard 1333# nonstandard
1430object *ob (player *pl) 1334object *ob (player *pl)
1431 CODE: 1335 CODE:
1432 RETVAL = pl->ob; 1336 RETVAL = pl->ob;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines