2002-08-17 05:27:34 +00:00
|
|
|
#include "Entity.h"
|
|
|
|
|
2004-11-11 04:06:00 +00:00
|
|
|
#include "debug.h"
|
|
|
|
#include "entities.h"
|
|
|
|
#include "plist.h"
|
|
|
|
#include "script.h"
|
|
|
|
#include "string.h"
|
|
|
|
|
|
|
|
typedef void () void_function;
|
|
|
|
|
|
|
|
integer PR_SetField (entity ent, string field, string value) = #0;
|
|
|
|
function PR_FindFunction (string func) = #0;
|
|
|
|
|
|
|
|
@static void ParseEntities (string ent_data);
|
|
|
|
|
2002-08-17 05:27:34 +00:00
|
|
|
@implementation Entity
|
|
|
|
|
2004-11-11 04:06:00 +00:00
|
|
|
- (void) own
|
|
|
|
{
|
|
|
|
own = 1;
|
|
|
|
}
|
|
|
|
|
2002-08-17 05:27:34 +00:00
|
|
|
- (id) init
|
|
|
|
{
|
2006-12-16 14:00:00 +00:00
|
|
|
self = [self initWithEntity: spawn ()];
|
|
|
|
[self own];
|
|
|
|
return self;
|
2002-08-17 05:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithEntity: (entity)e
|
|
|
|
{
|
2006-12-16 14:00:00 +00:00
|
|
|
self = [super init];
|
2002-08-17 05:27:34 +00:00
|
|
|
ent = e;
|
|
|
|
ent.@this = self;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2010-12-16 11:01:49 +00:00
|
|
|
- (id) initWithEntity: (entity)e fromPlist: (plitem_t) dict
|
|
|
|
{
|
|
|
|
self = [self initWithEntity: e];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2004-11-11 04:06:00 +00:00
|
|
|
- (void) dealloc
|
2002-08-17 05:27:34 +00:00
|
|
|
{
|
2006-12-16 14:00:00 +00:00
|
|
|
if (own && ent)
|
2004-11-11 04:06:00 +00:00
|
|
|
remove (ent);
|
|
|
|
[super dealloc];
|
2002-08-17 05:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (entity) ent
|
|
|
|
{
|
2004-11-11 04:06:00 +00:00
|
|
|
return ent;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void) load
|
|
|
|
{
|
|
|
|
//XXX EntityParseFunction (ParseEntities);
|
|
|
|
}
|
|
|
|
|
|
|
|
+ createFromPlist:(plitem_t) dict
|
|
|
|
{
|
|
|
|
local string classname;
|
2010-12-16 11:01:49 +00:00
|
|
|
local id class;
|
2004-11-11 04:06:00 +00:00
|
|
|
local entity ent;
|
|
|
|
local integer count;
|
|
|
|
local string field, value;
|
|
|
|
local plitem_t keys;
|
|
|
|
local function func;
|
|
|
|
local Entity e;
|
|
|
|
|
|
|
|
classname = PL_String (PL_ObjectForKey (dict, "classname"));
|
|
|
|
if (classname == "worldspawn")
|
|
|
|
ent = NIL;
|
|
|
|
else
|
|
|
|
ent = spawn ();
|
|
|
|
if ((class = obj_lookup_class (classname))) {
|
|
|
|
e = [[class alloc] initWithEntity:ent fromPlist:dict];
|
|
|
|
} else {
|
|
|
|
e = [[Entity alloc] initWithEntity:ent];
|
|
|
|
keys = PL_D_AllKeys (dict);
|
|
|
|
count = PL_A_NumObjects (keys);
|
|
|
|
while (count--) {
|
|
|
|
field = PL_String (PL_ObjectAtIndex (keys, count));
|
|
|
|
value = PL_String (PL_ObjectForKey (dict, field));
|
|
|
|
PR_SetField (ent, field, value);
|
|
|
|
}
|
|
|
|
PL_Free (keys);
|
|
|
|
if ((func = PR_FindFunction (classname))) {
|
|
|
|
//dprint ("calling " + classname + "\n");
|
|
|
|
@self = ent;
|
|
|
|
func ();
|
|
|
|
} else {
|
|
|
|
//dprint ("no spawn function for " + classname + "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ent)
|
|
|
|
[e own];
|
2002-08-17 05:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
2004-11-11 04:06:00 +00:00
|
|
|
|
|
|
|
@static void ParseEntities (string ent_data)
|
|
|
|
{
|
|
|
|
local script_t script;
|
|
|
|
local plitem_t plist, ent, key, value;
|
|
|
|
local string token;
|
|
|
|
local integer anglehack, i, count;
|
|
|
|
|
|
|
|
script = Script_New ();
|
|
|
|
token = Script_Start (script, "ent data", ent_data);
|
|
|
|
if (Script_GetToken (script, 1)) {
|
|
|
|
if (token == "(") {
|
|
|
|
plist = PL_GetPropertyList (ent_data);
|
|
|
|
} else {
|
|
|
|
Script_UngetToken (script);
|
|
|
|
plist = PL_NewArray ();
|
|
|
|
while (Script_GetToken (script, 1)) {
|
|
|
|
if (token != "{")
|
|
|
|
return; // abort, bad ent data
|
|
|
|
ent = PL_NewDictionary ();
|
|
|
|
while (1) {
|
|
|
|
if (!Script_GetToken (script, 1))
|
|
|
|
return; // abort, bad ent data
|
|
|
|
if (token == "}")
|
|
|
|
break;
|
|
|
|
anglehack = 0;
|
|
|
|
if (token == "angle") {
|
|
|
|
key = PL_NewString ("angles");
|
|
|
|
anglehack = 1;
|
|
|
|
} else if (token == "light") {
|
|
|
|
key = PL_NewString ("light_lev");
|
|
|
|
} else {
|
|
|
|
key = PL_NewString (token);
|
|
|
|
}
|
|
|
|
if (!Script_GetToken (script, 0))
|
|
|
|
return; // abort, bad ent data
|
|
|
|
if (token == "}")
|
|
|
|
return; // abort, bad ent data
|
|
|
|
if (anglehack)
|
|
|
|
value = PL_NewString ("0 " + token + " 0");
|
|
|
|
else
|
|
|
|
value = PL_NewString (token);
|
2006-12-09 06:00:36 +00:00
|
|
|
PL_D_AddObject (ent, PL_String (key), value);
|
|
|
|
PL_Free (key);
|
2004-11-11 04:06:00 +00:00
|
|
|
}
|
|
|
|
PL_A_AddObject (plist, ent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//dprint (PL_WritePropertyList (plist));
|
|
|
|
count = PL_A_NumObjects (plist);
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
ent = PL_ObjectAtIndex (plist, i);
|
|
|
|
[Entity createFromPlist:ent];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Script_Delete (script);
|
|
|
|
}
|