Oolite
Loading...
Searching...
No Matches
ShipEntity(LoadRestore) Category Reference

#include <ShipEntityLoadRestore.h>

Instance Methods

(NSDictionary *) - savedShipDictionaryWithContext:
 
(void) - simplifyShipdata:andGetDeletes: [implementation]
 

Class Methods

(id) + shipRestoredFromDictionary:useFallback:context:
 

Detailed Description

Definition at line 31 of file ShipEntityLoadRestore.h.

Method Documentation

◆ savedShipDictionaryWithContext:

- (NSDictionary *) savedShipDictionaryWithContext: (NSMutableDictionary *)  context

Definition at line 362 of file ShipEntityLoadRestore.m.

79 :(NSMutableDictionary *)context
80{
81 NSMutableDictionary *result = [NSMutableDictionary dictionary];
82 if (context == nil) context = [NSMutableDictionary dictionary];
83
84 [result setObject:_shipKey forKey:KEY_SHIP_KEY];
85
86 NSMutableDictionary *updatedShipInfo = [NSMutableDictionary dictionaryWithDictionary:shipinfoDictionary];
87
88 [updatedShipInfo setObject:[[self roleSet] roleString] forKey:KEY_ROLES];
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];
94
95 NSArray *deletes = nil;
96 [self simplifyShipdata:updatedShipInfo andGetDeletes:&deletes];
97
98 [result setObject:updatedShipInfo forKey:KEY_SHIPDATA_OVERRIDES];
99 if (deletes != nil) [result setObject:deletes forKey:KEY_SHIPDATA_DELETES];
100
101 if (!HPvector_equal([self position], kZeroHPVector))
102 {
103 [result oo_setHPVector:[self position] forKey:KEY_POSITION];
104 }
105 if (!quaternion_equal([self normalOrientation], kIdentityQuaternion))
106 {
107 [result oo_setQuaternion:[self normalOrientation] forKey:KEY_ORIENTATION];
108 }
109
110 if (energy != maxEnergy) [result oo_setFloat:energy / maxEnergy forKey:KEY_ENERGY_LEVEL];
111
112 [result setObject:[self primaryRole] forKey:KEY_PRIMARY_ROLE];
113
114 // Add equipment.
115 NSArray *equipment = [[self equipmentEnumerator] allObjects];
116 if ([equipment count] != 0) [result setObject:equipment forKey:KEY_EQUIPMENT];
117
118 // Add missiles.
119 if (missiles > 0)
120 {
121 NSMutableArray *missileArray = [NSMutableArray array];
122 unsigned i;
123 for (i = 0; i < missiles; i++)
124 {
125 NSString *missileType = [missile_list[i] identifier];
126 if (missileType != nil) [missileArray addObject:missileType];
127 }
128 [result setObject:missileArray forKey:KEY_MISSILES];
129 }
130
131 // Add groups.
132 if (_group != nil)
133 {
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)
138 {
139 [result setObject:groupName forKey:KEY_GROUP_NAME];
140 }
141 }
142 if (_escortGroup != nil)
143 {
144 [result oo_setUnsignedInteger:GroupIDForGroup(_escortGroup, context) forKey:KEY_ESCORT_GROUP_ID];
145 }
146 /* Eric:
147 The escortGroup property is removed from the lead ship, on entering witchspace.
148 But it is needed in the save file to correctly restore an escorted group.
149 */
150 else if (_group != nil && [_group leader] == self)
151 {
152 [result oo_setUnsignedInteger:GroupIDForGroup(_group, context) forKey:KEY_ESCORT_GROUP_ID];
153 }
154
155 // FIXME: AI.
156 // Eric: I think storing the AI name should be enough. On entering a wormhole, the stack is cleared so there are no preserved AI states.
157 // Also the AI restarts itself with the GLOBAL state, so no need to store any old state.
158 if ([[[self getAI] name] isEqualToString:@"nullAI.plist"])
159 {
160 // might be a JS version
161 [result setObject:[[self getAI] associatedJS] forKey:KEY_AI];
162 // if there isn't, loading nullAI.js will load nullAI.plist anyway
163 }
164 else
165 {
166 [result setObject:[[self getAI] name] forKey:KEY_AI];
167 }
168
169 return result;
170}
const HPVector kZeroHPVector
Definition OOHPVector.m:28
unsigned count
return nil
const Quaternion kIdentityQuaternion

References nil.

◆ shipRestoredFromDictionary:useFallback:context:

+ (id) shipRestoredFromDictionary: (NSDictionary *)  dictionary
useFallback: (BOOL)  fallback
context: (NSMutableDictionary *)  context 

Definition at line 362 of file ShipEntityLoadRestore.m.

173 :(NSDictionary *)dict
174 useFallback:(BOOL)fallback
175 context:(NSMutableDictionary *)context
176{
177 if (dict == nil) return nil;
178 if (context == nil) context = [NSMutableDictionary dictionary];
179
180 ShipEntity *ship = nil;
181
182 NSString *shipKey = [dict oo_stringForKey:KEY_SHIP_KEY];
183 NSDictionary *shipData = [[OOShipRegistry sharedRegistry] shipInfoForKey:shipKey];
184
185 if (shipData != nil)
186 {
187 NSMutableDictionary *mergedData = [NSMutableDictionary dictionaryWithDictionary:shipData];
188
189 StripIgnoredKeys(mergedData);
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"];
195
196 Class shipClass = [UNIVERSE shipClassForShipDictionary:mergedData];
197 ship = [[[shipClass alloc] initWithKey:shipKey definition:mergedData] autorelease];
198
199 // FIXME: restore AI.
200 [ship setAITo:[dict oo_stringForKey:KEY_AI defaultValue:@"nullAI.plist"]];
201
202 [ship setPrimaryRole:[dict oo_stringForKey:KEY_PRIMARY_ROLE]];
203
204 }
205 else
206 {
207 // Unknown ship; fall back on role if desired and possible.
208 NSString *shipPrimaryRole = [dict oo_stringForKey:KEY_PRIMARY_ROLE];
209 if (!fallback || shipPrimaryRole == nil) return nil;
210
211 ship = [[UNIVERSE newShipWithRole:shipPrimaryRole] autorelease];
212 if (ship == nil) return nil;
213 }
214
215 // The following stuff is deliberately set up the same way even if using role fallback.
216 [ship setPosition:[dict oo_hpvectorForKey:KEY_POSITION]];
217 [ship setNormalOrientation:[dict oo_quaternionForKey:KEY_ORIENTATION]];
218
219 float energyLevel = [dict oo_floatForKey:KEY_ENERGY_LEVEL defaultValue:1.0f];
220 [ship setEnergy:energyLevel * [ship maxEnergy]];
221
222 [ship removeAllEquipment];
223 NSString *eqKey = nil;
224 foreach (eqKey, [dict oo_arrayForKey:KEY_EQUIPMENT])
225 {
226 [ship addEquipmentItem:eqKey withValidation:NO inContext:@"loading"];
227 }
228
229 [ship removeMissiles];
230 foreach (eqKey, [dict oo_arrayForKey:KEY_MISSILES])
231 {
232 [ship addEquipmentItem:eqKey withValidation:NO inContext:@"loading"];
233 }
234
235 // Groups.
236 NSUInteger groupID = [dict oo_integerForKey:KEY_GROUP_ID defaultValue:NSNotFound];
237 if (groupID != NSNotFound)
238 {
239 OOShipGroup *group = GroupForGroupID(groupID, context);
240 [ship setGroup:group]; // Handles adding to group
241 if ([dict oo_boolForKey:KEY_IS_GROUP_LEADER]) [group setLeader:ship];
242 NSString *groupName = [dict oo_stringForKey:KEY_GROUP_NAME];
243 if (groupName != nil) [group setName:groupName];
244 if ([ship hasPrimaryRole:@"escort"] && ship != [group leader])
245 {
246 [ship setOwner:[group leader]];
247 }
248 }
249
250 groupID = [dict oo_integerForKey:KEY_ESCORT_GROUP_ID defaultValue:NSNotFound];
251 if (groupID != NSNotFound)
252 {
253 OOShipGroup *group = GroupForGroupID(groupID, context);
254 [group setLeader:ship];
255 [group setName:@"escort group"];
256 [ship setEscortGroup:group];
257 }
258
259 return ship;
260}
#define KEY_EQUIPMENT
#define KEY_MISSILES
static void StripIgnoredKeys(NSMutableDictionary *dict)
#define KEY_IS_GROUP_LEADER
static OOShipGroup * GroupForGroupID(NSUInteger groupID, NSMutableDictionary *context)
GLfloat maxEnergy
Definition Entity.h:143
void setNormalOrientation:(Quaternion quat)
Definition Entity.m:745
void setEnergy:(GLfloat amount)
Definition Entity.m:812
void setPosition:(HPVector posn)
Definition Entity.m:648
void setName:(NSString *name)
void setLeader:(ShipEntity *leader)
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()
void removeAllEquipment()
void setAITo:(NSString *aiString)
void setEscortGroup:(OOShipGroup *group)
void setOwner:(Entity *who_owns_entity)

◆ simplifyShipdata:andGetDeletes:

- (void) simplifyShipdata: (NSMutableDictionary *)  data
andGetDeletes: (NSArray **)  deletes 
implementation

Definition at line 362 of file ShipEntityLoadRestore.m.

263 :(NSMutableDictionary *)data andGetDeletes:(NSArray **)deletes
264{
265 NSParameterAssert(data != nil && deletes != NULL);
266 *deletes = nil;
267
268 // Get original ship data.
269 NSMutableDictionary *referenceData = [NSMutableDictionary dictionaryWithDictionary:[[OOShipRegistry sharedRegistry] shipInfoForKey:[self shipDataKey]]];
270
271 // Discard stuff that we handle separately.
272 StripIgnoredKeys(referenceData);
273 StripIgnoredKeys(data);
274
275 // Note items that are in referenceData, but not data.
276 NSMutableArray *foundDeletes = [NSMutableArray array];
277 NSString *key = nil;
278 foreachkey (key, referenceData)
279 {
280 if ([data objectForKey:key] == nil)
281 {
282 [foundDeletes addObject:key];
283 }
284 }
285 if ([foundDeletes count] != 0) *deletes = foundDeletes;
286
287 // after rev3010 this loop was using cycles without doing anything - commenting this whole loop out for now. -- kaks 20100207
288/*
289 // Discard anything that hasn't changed.
290 for (enumerator = [data keyEnumerator]; (key = [enumerator nextObject]); )
291 {
292 id referenceVal = [referenceData objectForKey:key];
293 id myVal = [data objectForKey:key];
294 if ([referenceVal isEqual:myVal])
295 {
296 // [data removeObjectForKey:key];
297 }
298 }
299*/
300}
#define foreachkey(VAR, DICT)
Definition OOCocoa.h:353

The documentation for this category was generated from the following files: