Oolite 1.91.0.7644-241112-7f5034b
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

Extends class ShipEntity.

Definition at line 365 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 

Extends class ShipEntity.

Definition at line 365 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 NSEnumerator *eqEnum = nil;
224 NSString *eqKey = nil;
225 for (eqEnum = [[dict oo_arrayForKey:KEY_EQUIPMENT] objectEnumerator]; (eqKey = [eqEnum nextObject]); )
226 {
227 [ship addEquipmentItem:eqKey withValidation:NO inContext:@"loading"];
228 }
229
230 [ship removeMissiles];
231 for (eqEnum = [[dict oo_arrayForKey:KEY_MISSILES] objectEnumerator]; (eqKey = [eqEnum nextObject]); )
232 {
233 [ship addEquipmentItem:eqKey withValidation:NO inContext:@"loading"];
234 }
235
236 // Groups.
237 NSUInteger groupID = [dict oo_integerForKey:KEY_GROUP_ID defaultValue:NSNotFound];
238 if (groupID != NSNotFound)
239 {
240 OOShipGroup *group = GroupForGroupID(groupID, context);
241 [ship setGroup:group]; // Handles adding to group
242 if ([dict oo_boolForKey:KEY_IS_GROUP_LEADER]) [group setLeader:ship];
243 NSString *groupName = [dict oo_stringForKey:KEY_GROUP_NAME];
244 if (groupName != nil) [group setName:groupName];
245 if ([ship hasPrimaryRole:@"escort"] && ship != [group leader])
246 {
247 [ship setOwner:[group leader]];
248 }
249 }
250
251 groupID = [dict oo_integerForKey:KEY_ESCORT_GROUP_ID defaultValue:NSNotFound];
252 if (groupID != NSNotFound)
253 {
254 OOShipGroup *group = GroupForGroupID(groupID, context);
255 [group setLeader:ship];
256 [group setName:@"escort group"];
257 [ship setEscortGroup:group];
258 }
259
260 return ship;
261}
#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:744
void setEnergy:(GLfloat amount)
Definition Entity.m:811
void setPosition:(HPVector posn)
Definition Entity.m:647
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

Extends class ShipEntity.

Definition at line 365 of file ShipEntityLoadRestore.m.

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

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