39#define KEY_SHIP_KEY @"ship_key"
40#define KEY_SHIPDATA_OVERRIDES @"shipdata_overrides"
41#define KEY_SHIPDATA_DELETES @"shipdata_deletes"
42#define KEY_PRIMARY_ROLE @"primary_role"
43#define KEY_POSITION @"position"
44#define KEY_ORIENTATION @"orientation"
45#define KEY_ROLES @"roles"
46#define KEY_FUEL @"fuel"
47#define KEY_BOUNTY @"bounty"
48#define KEY_ENERGY_LEVEL @"energy_level"
49#define KEY_EQUIPMENT @"equipment"
50#define KEY_MISSILES @"missiles"
51#define KEY_FORWARD_WEAPON @"forward_weapon_type"
52#define KEY_AFT_WEAPON @"aft_weapon_type"
53#define KEY_SCAN_CLASS @"scan_class"
59#define KEY_GROUP_ID @"group"
60#define KEY_GROUP_NAME @"group_name"
61#define KEY_IS_GROUP_LEADER @"is_group_leader"
62#define KEY_ESCORT_GROUP_ID @"escort_group"
70@interface ShipEntity (LoadRestoreInternal)
72- (void) simplifyShipdata:(NSMutableDictionary *)data andGetDeletes:(NSArray **)deletes;
77@implementation ShipEntity (LoadRestore)
79- (NSDictionary *) savedShipDictionaryWithContext:(NSMutableDictionary *)context
81 NSMutableDictionary *result = [NSMutableDictionary dictionary];
82 if (context ==
nil) context = [NSMutableDictionary dictionary];
84 [result setObject:_shipKey forKey:KEY_SHIP_KEY];
86 NSMutableDictionary *updatedShipInfo = [NSMutableDictionary dictionaryWithDictionary:shipinfoDictionary];
89 [updatedShipInfo oo_setUnsignedInteger:fuel forKey:KEY_FUEL];
90 [updatedShipInfo oo_setUnsignedLongLong:bounty forKey:KEY_BOUNTY];
91 [updatedShipInfo setObject:OOStringFromWeaponType(forward_weapon_type) forKey:KEY_FORWARD_WEAPON];
92 [updatedShipInfo setObject:OOStringFromWeaponType(aft_weapon_type) forKey:KEY_AFT_WEAPON];
93 [updatedShipInfo setObject:OOStringFromScanClass(scanClass) forKey:KEY_SCAN_CLASS];
95 NSArray *deletes =
nil;
98 [result setObject:updatedShipInfo forKey:KEY_SHIPDATA_OVERRIDES];
99 if (deletes !=
nil) [result setObject:deletes forKey:KEY_SHIPDATA_DELETES];
103 [result oo_setHPVector:[
self position] forKey:KEY_POSITION];
110 if (energy != maxEnergy) [result oo_setFloat:energy / maxEnergy forKey:KEY_ENERGY_LEVEL];
112 [result setObject:[
self primaryRole] forKey:KEY_PRIMARY_ROLE];
116 if ([equipment
count] != 0) [result setObject:equipment forKey:KEY_EQUIPMENT];
121 NSMutableArray *missileArray = [NSMutableArray array];
123 for (i = 0; i < missiles; i++)
125 NSString *missileType = [missile_list[i] identifier];
126 if (missileType !=
nil) [missileArray addObject:missileType];
128 [result setObject:missileArray forKey:KEY_MISSILES];
134 [result oo_setUnsignedInteger:GroupIDForGroup(_group, context) forKey:KEY_GROUP_ID];
135 if ([_group leader] ==
self) [result oo_setBool:YES forKey:KEY_IS_GROUP_LEADER];
136 NSString *groupName = [_group name];
137 if (groupName !=
nil)
139 [result setObject:groupName forKey:KEY_GROUP_NAME];
142 if (_escortGroup !=
nil)
144 [result oo_setUnsignedInteger:GroupIDForGroup(_escortGroup, context) forKey:KEY_ESCORT_GROUP_ID];
150 else if (_group !=
nil && [_group leader] ==
self)
152 [result oo_setUnsignedInteger:GroupIDForGroup(_group, context) forKey:KEY_ESCORT_GROUP_ID];
158 if ([[[
self getAI] name] isEqualToString:
@"nullAI.plist"])
166 [result setObject:[[
self getAI]
name] forKey:KEY_AI];
173+ (id) shipRestoredFromDictionary:(NSDictionary *)dict
174 useFallback:(BOOL)fallback
175 context:(NSMutableDictionary *)context
177 if (dict ==
nil)
return nil;
178 if (context ==
nil) context = [NSMutableDictionary dictionary];
182 NSString *shipKey = [dict oo_stringForKey:KEY_SHIP_KEY];
187 NSMutableDictionary *mergedData = [NSMutableDictionary dictionaryWithDictionary:shipData];
190 NSArray *deletes = [dict oo_arrayForKey:KEY_SHIPDATA_DELETES];
191 if (deletes !=
nil) [mergedData removeObjectsForKeys:deletes];
192 [mergedData addEntriesFromDictionary:[dict oo_dictionaryForKey:KEY_SHIPDATA_OVERRIDES]];
193 [mergedData oo_setBool:NO forKey:@"auto_ai"];
194 [mergedData oo_setUnsignedInteger:0 forKey:@"escorts"];
196 Class shipClass = [UNIVERSE shipClassForShipDictionary:mergedData];
197 ship = [[[shipClass alloc] initWithKey:shipKey definition:mergedData] autorelease];
200 [ship
setAITo:[dict oo_stringForKey:KEY_AI defaultValue:@"nullAI.plist"]];
208 NSString *shipPrimaryRole = [dict oo_stringForKey:KEY_PRIMARY_ROLE];
209 if (!fallback || shipPrimaryRole ==
nil)
return nil;
211 ship = [[UNIVERSE newShipWithRole:shipPrimaryRole] autorelease];
212 if (ship ==
nil)
return nil;
216 [ship
setPosition:[dict oo_hpvectorForKey:KEY_POSITION]];
219 float energyLevel = [dict oo_floatForKey:KEY_ENERGY_LEVEL defaultValue:1.0f];
223 NSEnumerator *eqEnum =
nil;
224 NSString *eqKey =
nil;
225 for (eqEnum = [[dict oo_arrayForKey:
KEY_EQUIPMENT] objectEnumerator]; (eqKey = [eqEnum nextObject]); )
231 for (eqEnum = [[dict oo_arrayForKey:
KEY_MISSILES] objectEnumerator]; (eqKey = [eqEnum nextObject]); )
237 NSUInteger groupID = [dict oo_integerForKey:KEY_GROUP_ID defaultValue:NSNotFound];
238 if (groupID != NSNotFound)
243 NSString *groupName = [dict oo_stringForKey:KEY_GROUP_NAME];
244 if (groupName !=
nil) [group
setName:groupName];
245 if ([ship hasPrimaryRole:
@"escort"] && ship != [group leader])
251 groupID = [dict oo_integerForKey:KEY_ESCORT_GROUP_ID defaultValue:NSNotFound];
252 if (groupID != NSNotFound)
256 [group
setName:@"escort group"];
264- (void) simplifyShipdata:(NSMutableDictionary *)data andGetDeletes:(NSArray **)deletes
266 NSParameterAssert(data !=
nil && deletes != NULL);
277 NSMutableArray *foundDeletes = [NSMutableArray array];
278 NSEnumerator *enumerator =
nil;
280 for (enumerator = [referenceData keyEnumerator]; (key = [enumerator nextObject]); )
282 if ([data objectForKey:key] ==
nil)
284 [foundDeletes addObject:key];
287 if ([foundDeletes
count] != 0) *deletes = foundDeletes;
309 static NSArray *ignoredKeys =
nil;
310 if (ignoredKeys ==
nil) ignoredKeys = [[NSArray alloc] initWithObjects:@"ai_type", @"has_ecm", @"has_scoop", @"has_escape_pod", @"has_energy_bomb", @"has_fuel_injection", @"has_cloaking_device", @"has_military_jammer", @"has_military_scanner_filter", @"has_shield_booster", @"has_shield_enhancer", @"escorts", @"escort_role", @"escort-ship", @"conditions", @"missiles", @"auto_ai", nil];
312 NSEnumerator *keyEnum =
nil;
314 for (keyEnum = [ignoredKeys objectEnumerator]; (key = [keyEnum nextObject]); )
316 [dict removeObjectForKey:key];
323 NSMutableDictionary *groupIDs = [context objectForKey:@"groupIDs"];
326 groupIDs = [NSMutableDictionary dictionary];
327 [context setObject:groupIDs forKey:@"groupIDs"];
330 NSValue *key = [NSValue valueWithNonretainedObject:group];
331 NSNumber *groupIDObj = [groupIDs objectForKey:key];
333 if (groupIDObj ==
nil)
336 groupID = [context oo_unsignedIntForKey:@"nextGroupID"];
337 groupIDObj = [NSNumber numberWithUnsignedInt:groupID];
338 [context oo_setUnsignedInteger:groupID + 1 forKey:@"nextGroupID"];
339 [groupIDs setObject:groupIDObj forKey:key];
347 NSMutableSet *groups = [context objectForKey:@"groups"];
350 groups = [NSMutableSet set];
351 [context setObject:groups forKey:@"groups"];
353 [groups addObject:group];
357 groupID = [groupIDObj unsignedIntValue];
367 NSNumber *key = [NSNumber numberWithUnsignedInteger:groupID];
369 NSMutableDictionary *groups = [context objectForKey:@"groupsByID"];
372 groups = [NSMutableDictionary dictionary];
373 [context setObject:groups forKey:@"groupsByID"];
380 [groups setObject:group forKey:key];
const HPVector kZeroHPVector
const Quaternion kIdentityQuaternion
static NSUInteger GroupIDForGroup(OOShipGroup *group, NSMutableDictionary *context)
static void StripIgnoredKeys(NSMutableDictionary *dict)
#define KEY_IS_GROUP_LEADER
static OOShipGroup * GroupForGroupID(NSUInteger groupID, NSMutableDictionary *context)
NSString * associatedJS()
void setNormalOrientation:(Quaternion quat)
void setEnergy:(GLfloat amount)
Quaternion normalOrientation()
void setPosition:(HPVector posn)
void setName:(NSString *name)
void setLeader:(ShipEntity *leader)
OOShipRegistry * sharedRegistry()
NSDictionary * shipInfoForKey:(NSString *key)
BOOL addEquipmentItem:withValidation:inContext:(NSString *equipmentKey,[withValidation] BOOL validateAddition,[inContext] NSString *context)
void setGroup:(OOShipGroup *group)
void setPrimaryRole:(NSString *role)
OOCreditsQuantity removeMissiles()
NSEnumerator * equipmentEnumerator()
void removeAllEquipment()
void setAITo:(NSString *aiString)
void simplifyShipdata:andGetDeletes:(NSMutableDictionary *data, [andGetDeletes] NSArray **deletes)
void setEscortGroup:(OOShipGroup *group)
void setOwner:(Entity *who_owns_entity)