31static NSComparisonResult
goodsSorter(
id a,
id b,
void *context);
39 if (
self ==
nil)
return nil;
41 _commodityList = [[NSMutableDictionary dictionaryWithCapacity:24] retain];
59 return [_commodityList count];
65 NSMutableDictionary *definition = [NSMutableDictionary dictionaryWithDictionary:info];
66 [_commodityList setObject:definition forKey:key];
75 NSArray *keys = [_commodityList allKeys];
76 _sortedKeys = [[keys sortedArrayUsingFunction:goodsSorter context:_commodityList] retain];
84 return [[_commodityList copy] autorelease];
90 NSMutableDictionary *definition = [_commodityList oo_mutableDictionaryForKey:good];
91 if (definition ==
nil)
95 [definition oo_setUnsignedInteger:price forKey:kOOCommodityPriceCurrent];
102 NSMutableDictionary *definition = [_commodityList oo_mutableDictionaryForKey:good];
103 if (definition ==
nil || quantity > [
self capacityForGood:good])
107 [definition oo_setUnsignedInteger:quantity forKey:kOOCommodityQuantityCurrent];
115 if (current + quantity > [
self capacityForGood:good])
119 [
self setQuantity:(current+quantity) forGood:good];
127 if (current < quantity)
131 [
self setQuantity:(current-quantity) forGood:good];
141 [
self setQuantity:0 forGood:good];
148 NSMutableDictionary *definition = [_commodityList oo_mutableDictionaryForKey:good];
149 if (definition ==
nil)
153 [definition setObject:comment forKey:kOOCommodityComment];
158- (BOOL) setShortComment:(NSString *)comment forGood:(
OOCommodityType)good
160 NSMutableDictionary *definition = [_commodityList oo_mutableDictionaryForKey:good];
161 if (definition ==
nil)
165 [definition setObject:comment forKey:kOOCommodityShortComment];
172 NSDictionary *definition = [_commodityList oo_dictionaryForKey:good];
173 if (definition ==
nil)
175 return OOExpand(
@"[oolite-unknown-commodity-name]");
183 NSDictionary *definition = [_commodityList oo_dictionaryForKey:good];
184 if (definition ==
nil)
186 return OOExpand(
@"[oolite-unknown-commodity-name]");
194 NSDictionary *definition = [_commodityList oo_dictionaryForKey:good];
195 if (definition ==
nil)
197 return OOExpand(
@"[oolite-unknown-commodity-name]");
205 NSDictionary *definition = [_commodityList oo_dictionaryForKey:good];
206 if (definition ==
nil)
210 return [definition oo_unsignedIntegerForKey:kOOCommodityPriceCurrent];
216 NSDictionary *definition = [_commodityList oo_dictionaryForKey:good];
217 if (definition ==
nil)
221 return [definition oo_unsignedIntForKey:kOOCommodityQuantityCurrent];
227 NSDictionary *definition = [_commodityList oo_dictionaryForKey:good];
228 if (definition ==
nil)
232 return [definition oo_unsignedIntForKey:kOOCommodityContainer];
238 NSDictionary *definition = [_commodityList oo_dictionaryForKey:good];
239 if (definition ==
nil)
243 return [definition oo_unsignedIntegerForKey:kOOCommodityLegalityExport];
249 NSDictionary *definition = [_commodityList oo_dictionaryForKey:good];
250 if (definition ==
nil)
254 return [definition oo_unsignedIntegerForKey:kOOCommodityLegalityImport];
260 NSDictionary *definition = [_commodityList oo_dictionaryForKey:good];
261 if (definition ==
nil)
267 return [definition oo_unsignedIntForKey:kOOCommodityCapacity defaultValue:MAIN_SYSTEM_MARKET_LIMIT];
273 NSDictionary *definition = [_commodityList oo_dictionaryForKey:good];
274 if (definition ==
nil)
278 return [definition oo_floatForKey:kOOCommodityTrumbleOpinion];
284 return [[[_commodityList oo_dictionaryForKey:good] copy] autorelease];
291 NSMutableArray *amounts = [NSMutableArray arrayWithCapacity:[
self count]];
293 foreach (good, [
self goods])
295 [amounts addObject:[NSArray arrayWithObjects:good,[NSNumber numberWithUnsignedInt:[
self quantityForGood:good]],nil]];
297 return [NSArray arrayWithArray:amounts];
301- (void) loadPlayerAmounts:(NSArray *)amounts
305 NSString *good =
nil;
306 foreach (good, [
self goods])
309 [
self setQuantity:0 forGood:good];
313 NSArray *loaded =
nil;
314 foreach (loaded, amounts)
317 good = [loaded oo_stringAtIndex:0];
318 q = [loaded oo_unsignedIntAtIndex:1];
320 if (![
self setQuantity:q forGood:good])
326 foreach (key, [
self goods])
328 if ([good isEqualToString:[
self nameForGood:key]])
330 [
self setQuantity:q forGood:key];
342 OOLog(
@"setCommanderDataFromDictionary.warning.cargo",
@"Cargo %@ (%u units) could not be loaded from the saved game, as it is no longer defined",good,q);
350 NSMutableArray *amounts = [NSMutableArray arrayWithCapacity:[
self count]];
352 foreach (good, [
self goods])
354 [amounts addObject:[NSArray arrayWithObjects:good,[NSNumber numberWithUnsignedInt:[
self quantityForGood:good]],[NSNumber numberWithUnsignedInteger:[
self priceForGood:good]],nil]];
356 return [NSArray arrayWithArray:amounts];
360- (void) loadStationAmounts:(NSArray *)amounts
365 NSString *good =
nil;
367 NSArray *loaded =
nil;
368 foreach (loaded, amounts)
371 good = [loaded oo_stringAtIndex:0];
372 q = [loaded oo_unsignedIntAtIndex:1];
373 p = [loaded oo_unsignedIntegerAtIndex:2];
375 if (![
self setQuantity:q forGood:good])
381 foreach (key, [
self goods])
383 if ([good isEqualToString:[
self nameForGood:key]])
385 [
self setQuantity:q forGood:key];
386 [
self setPrice:p forGood:key];
394 [
self setPrice:p forGood:good];
399 OOLog(
@"load.warning.cargo",
@"Station market good %@ (%u units) could not be loaded from the saved game, as it is no longer defined",good,q);
410 NSDictionary *commodityList = (NSDictionary *)context;
411 int v1 = [[commodityList oo_dictionaryForKey:(NSString *)a] oo_intForKey:kOOCommoditySortOrder];
412 int v2 = [[commodityList oo_dictionaryForKey:(NSString *)b] oo_intForKey:kOOCommoditySortOrder];
416 return NSOrderedAscending;
420 return NSOrderedDescending;
424 return NSOrderedSame;
static NSString *const kOOCommodityComment
static NSString *const kOOCommodityName
static NSString *const kOOCommodityShortComment
static NSComparisonResult goodsSorter(id a, id b, void *context)
#define OOLog(class, format,...)
#define OOExpand(string,...)
NSString * OOCommodityType
uint64_t OOCreditsQuantity
NSArray * saveStationAmounts()
NSArray * savePlayerAmounts()
NSDictionary * dictionaryForScripting()
NSMutableDictionary * _commodityList