Oolite 1.91.0.7645-241119-222d325
Loading...
Searching...
No Matches
OOCommodityMarket Class Reference

#include <OOCommodityMarket.h>

+ Inheritance diagram for OOCommodityMarket:
+ Collaboration diagram for OOCommodityMarket:

Instance Methods

(NSUInteger) - count
 
(void) - setGood:withInfo:
 
(NSArray *) - goods
 
(NSDictionary *) - dictionaryForScripting
 
(BOOL) - setPrice:forGood:
 
(BOOL) - setQuantity:forGood:
 
(BOOL) - addQuantity:forGood:
 
(BOOL) - removeQuantity:forGood:
 
(void) - removeAllGoods
 
(BOOL) - setComment:forGood:
 
(BOOL) - setShortComment:forGood:
 
(NSString *) - nameForGood:
 
(NSString *) - commentForGood:
 
(NSString *) - shortCommentForGood:
 
(OOCreditsQuantity- priceForGood:
 
(OOCargoQuantity- quantityForGood:
 
(OOMassUnit- massUnitForGood:
 
(NSUInteger) - exportLegalityForGood:
 
(NSUInteger) - importLegalityForGood:
 
(OOCargoQuantity- capacityForGood:
 
(float) - trumbleOpinionForGood:
 
(NSDictionary *) - definitionForGood:
 
(NSArray *) - savePlayerAmounts
 
(void) - loadPlayerAmounts:
 
(NSArray *) - saveStationAmounts
 
(void) - loadStationAmounts:
 
(id) - init [implementation]
 
(void) - dealloc [implementation]
 

Private Attributes

NSMutableDictionary * _commodityList
 
NSArray * _sortedKeys
 

Detailed Description

Definition at line 31 of file OOCommodityMarket.h.

Method Documentation

◆ addQuantity:forGood:

- (BOOL) addQuantity: (OOCargoQuantity) quantity
forGood: (OOCommodityType) good 

Definition at line 408 of file OOCommodityMarket.m.

112 :(OOCargoQuantity)quantity forGood:(OOCommodityType)good
113{
114 OOCargoQuantity current = [self quantityForGood:good];
115 if (current + quantity > [self capacityForGood:good])
116 {
117 return NO;
118 }
119 [self setQuantity:(current+quantity) forGood:good];
120 return YES;
121}
NSString * OOCommodityType
Definition OOTypes.h:106
uint32_t OOCargoQuantity
Definition OOTypes.h:176

◆ capacityForGood:

- (OOCargoQuantity) capacityForGood: (OOCommodityType) good

Definition at line 408 of file OOCommodityMarket.m.

258 :(OOCommodityType)good
259{
260 NSDictionary *definition = [_commodityList oo_dictionaryForKey:good];
261 if (definition == nil)
262 {
263 return 0;
264 }
265 // should only be undefined for main system markets, not secondary stations
266 // meaningless for player ship, though
267 return [definition oo_unsignedIntForKey:kOOCommodityCapacity defaultValue:MAIN_SYSTEM_MARKET_LIMIT];
268}
return nil

◆ commentForGood:

- (NSString *) commentForGood: (OOCommodityType) good

Definition at line 408 of file OOCommodityMarket.m.

181 :(OOCommodityType)good
182{
183 NSDictionary *definition = [_commodityList oo_dictionaryForKey:good];
184 if (definition == nil)
185 {
186 return OOExpand(@"[oolite-unknown-commodity-name]");
187 }
188 return OOExpand([definition oo_stringForKey:kOOCommodityComment defaultValue:@"[oolite-commodity-no-comment]"]);
189}
static NSString *const kOOCommodityComment
#define OOExpand(string,...)

◆ count

- (NSUInteger) count

Definition at line 408 of file OOCommodityMarket.m.

58{
59 return [_commodityList count];
60}

◆ dealloc

- (void) dealloc
implementation

Definition at line 408 of file OOCommodityMarket.m.

50{
53 [super dealloc];
54}
#define DESTROY(x)
Definition OOCocoa.h:77
NSMutableDictionary * _commodityList

◆ definitionForGood:

- (NSDictionary *) definitionForGood: (OOCommodityType) good

Definition at line 408 of file OOCommodityMarket.m.

282 :(OOCommodityType)good
283{
284 return [[[_commodityList oo_dictionaryForKey:good] copy] autorelease];
285}

◆ dictionaryForScripting

- (NSDictionary *) dictionaryForScripting

Definition at line 408 of file OOCommodityMarket.m.

83{
84 return [[_commodityList copy] autorelease];
85}

◆ exportLegalityForGood:

- (NSUInteger) exportLegalityForGood: (OOCommodityType) good

Definition at line 408 of file OOCommodityMarket.m.

236 :(OOCommodityType)good
237{
238 NSDictionary *definition = [_commodityList oo_dictionaryForKey:good];
239 if (definition == nil)
240 {
241 return 0;
242 }
243 return [definition oo_unsignedIntegerForKey:kOOCommodityLegalityExport];
244}

◆ goods

- (NSArray *) goods

Definition at line 408 of file OOCommodityMarket.m.

72{
73 if (_sortedKeys == nil)
74 {
75 NSArray *keys = [_commodityList allKeys];
76 _sortedKeys = [[keys sortedArrayUsingFunction:goodsSorter context:_commodityList] retain];
77 }
78 return _sortedKeys;
79}

◆ importLegalityForGood:

- (NSUInteger) importLegalityForGood: (OOCommodityType) good

Definition at line 408 of file OOCommodityMarket.m.

247 :(OOCommodityType)good
248{
249 NSDictionary *definition = [_commodityList oo_dictionaryForKey:good];
250 if (definition == nil)
251 {
252 return 0;
253 }
254 return [definition oo_unsignedIntegerForKey:kOOCommodityLegalityImport];
255}

◆ init

- (id) init
implementation

Definition at line 408 of file OOCommodityMarket.m.

37{
38 self = [super init];
39 if (self == nil) return nil;
40
41 _commodityList = [[NSMutableDictionary dictionaryWithCapacity:24] retain];
42
44
45 return self;
46}

◆ loadPlayerAmounts:

- (void) loadPlayerAmounts: (NSArray *) amounts

Definition at line 408 of file OOCommodityMarket.m.

301 :(NSArray *)amounts
302{
304 BOOL loadedOK;
305 NSString *good = nil;
306 foreach (good, [self goods])
307 {
308 // make sure that any goods not defined in the save game are zeroed
309 [self setQuantity:0 forGood:good];
310 }
311
312
313 NSArray *loaded = nil;
314 foreach (loaded, amounts)
315 {
316 loadedOK = NO;
317 good = [loaded oo_stringAtIndex:0];
318 q = [loaded oo_unsignedIntAtIndex:1];
319 // old save games might have more in the array, but we don't care
320 if (![self setQuantity:q forGood:good])
321 {
322 // then it's an array from a 1.80-or-earlier save game and
323 // the good name is the description string (maybe a
324 // translated one)
325 OOCommodityType key = nil;
326 foreach (key, [self goods])
327 {
328 if ([good isEqualToString:[self nameForGood:key]])
329 {
330 [self setQuantity:q forGood:key];
331 loadedOK = YES;
332 break;
333 }
334 }
335 }
336 else
337 {
338 loadedOK = YES;
339 }
340 if (!loadedOK)
341 {
342 OOLog(@"setCommanderDataFromDictionary.warning.cargo",@"Cargo %@ (%u units) could not be loaded from the saved game, as it is no longer defined",good,q);
343 }
344 }
345}
#define OOLog(class, format,...)
Definition OOLogging.h:88

◆ loadStationAmounts:

- (void) loadStationAmounts: (NSArray *) amounts

Definition at line 408 of file OOCommodityMarket.m.

360 :(NSArray *)amounts
361{
364 BOOL loadedOK;
365 NSString *good = nil;
366
367 NSArray *loaded = nil;
368 foreach (loaded, amounts)
369 {
370 loadedOK = NO;
371 good = [loaded oo_stringAtIndex:0];
372 q = [loaded oo_unsignedIntAtIndex:1];
373 p = [loaded oo_unsignedIntegerAtIndex:2];
374 // old save games might have more in the array, but we don't care
375 if (![self setQuantity:q forGood:good])
376 {
377 // then it's an array from a 1.80-or-earlier save game and
378 // the good name is the description string (maybe a
379 // translated one)
380 OOCommodityType key = nil;
381 foreach (key, [self goods])
382 {
383 if ([good isEqualToString:[self nameForGood:key]])
384 {
385 [self setQuantity:q forGood:key];
386 [self setPrice:p forGood:key];
387 loadedOK = YES;
388 break;
389 }
390 }
391 }
392 else
393 {
394 [self setPrice:p forGood:good];
395 loadedOK = YES;
396 }
397 if (!loadedOK)
398 {
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);
400 }
401 }
402}
uint64_t OOCreditsQuantity
Definition OOTypes.h:182

◆ massUnitForGood:

- (OOMassUnit) massUnitForGood: (OOCommodityType) good

Definition at line 408 of file OOCommodityMarket.m.

225 :(OOCommodityType)good
226{
227 NSDictionary *definition = [_commodityList oo_dictionaryForKey:good];
228 if (definition == nil)
229 {
230 return UNITS_TONS;
231 }
232 return [definition oo_unsignedIntForKey:kOOCommodityContainer];
233}
@ UNITS_TONS
Definition OOTypes.h:124

Referenced by PlayerEntity::activateCloakingDevice.

+ Here is the caller graph for this function:

◆ nameForGood:

- (NSString *) nameForGood: (OOCommodityType) good

Definition at line 408 of file OOCommodityMarket.m.

170 :(OOCommodityType)good
171{
172 NSDictionary *definition = [_commodityList oo_dictionaryForKey:good];
173 if (definition == nil)
174 {
175 return OOExpand(@"[oolite-unknown-commodity-name]");
176 }
177 return OOExpand([definition oo_stringForKey:kOOCommodityName defaultValue:@"[oolite-unknown-commodity-name]"]);
178}
static NSString *const kOOCommodityName

Referenced by marketSorterByName().

+ Here is the caller graph for this function:

◆ priceForGood:

- (OOCreditsQuantity) priceForGood: (OOCommodityType) good

Definition at line 408 of file OOCommodityMarket.m.

203 :(OOCommodityType)good
204{
205 NSDictionary *definition = [_commodityList oo_dictionaryForKey:good];
206 if (definition == nil)
207 {
208 return 0;
209 }
210 return [definition oo_unsignedIntegerForKey:kOOCommodityPriceCurrent];
211}

Referenced by marketSorterByPrice().

+ Here is the caller graph for this function:

◆ quantityForGood:

- (OOCargoQuantity) quantityForGood: (OOCommodityType) good

Definition at line 408 of file OOCommodityMarket.m.

214 :(OOCommodityType)good
215{
216 NSDictionary *definition = [_commodityList oo_dictionaryForKey:good];
217 if (definition == nil)
218 {
219 return 0;
220 }
221 return [definition oo_unsignedIntForKey:kOOCommodityQuantityCurrent];
222}

Referenced by marketSorterByQuantity().

+ Here is the caller graph for this function:

◆ removeAllGoods

- (void) removeAllGoods

Definition at line 408 of file OOCommodityMarket.m.

137{
138 OOCommodityType good = nil;
139 foreach (good, [_commodityList allKeys])
140 {
141 [self setQuantity:0 forGood:good];
142 }
143}

◆ removeQuantity:forGood:

- (BOOL) removeQuantity: (OOCargoQuantity) quantity
forGood: (OOCommodityType) good 

Definition at line 408 of file OOCommodityMarket.m.

124 :(OOCargoQuantity)quantity forGood:(OOCommodityType)good
125{
126 OOCargoQuantity current = [self quantityForGood:good];
127 if (current < quantity)
128 {
129 return NO;
130 }
131 [self setQuantity:(current-quantity) forGood:good];
132 return YES;
133}

◆ savePlayerAmounts

- (NSArray *) savePlayerAmounts

Definition at line 408 of file OOCommodityMarket.m.

290{
291 NSMutableArray *amounts = [NSMutableArray arrayWithCapacity:[self count]];
292 OOCommodityType good = nil;
293 foreach (good, [self goods])
294 {
295 [amounts addObject:[NSArray arrayWithObjects:good,[NSNumber numberWithUnsignedInt:[self quantityForGood:good]],nil]];
296 }
297 return [NSArray arrayWithArray:amounts];
298}

◆ saveStationAmounts

- (NSArray *) saveStationAmounts

Definition at line 408 of file OOCommodityMarket.m.

349{
350 NSMutableArray *amounts = [NSMutableArray arrayWithCapacity:[self count]];
351 OOCommodityType good = nil;
352 foreach (good, [self goods])
353 {
354 [amounts addObject:[NSArray arrayWithObjects:good,[NSNumber numberWithUnsignedInt:[self quantityForGood:good]],[NSNumber numberWithUnsignedInteger:[self priceForGood:good]],nil]];
355 }
356 return [NSArray arrayWithArray:amounts];
357}

◆ setComment:forGood:

- (BOOL) setComment: (NSString *) comment
forGood: (OOCommodityType) good 

Definition at line 408 of file OOCommodityMarket.m.

146 :(NSString *)comment forGood:(OOCommodityType)good
147{
148 NSMutableDictionary *definition = [_commodityList oo_mutableDictionaryForKey:good];
149 if (definition == nil)
150 {
151 return NO;
152 }
153 [definition setObject:comment forKey:kOOCommodityComment];
154 return YES;
155}

◆ setGood:withInfo:

- (void) setGood: (OOCommodityType) key
withInfo: (NSDictionary *) info 

Definition at line 408 of file OOCommodityMarket.m.

63 :(OOCommodityType)key withInfo:(NSDictionary *)info
64{
65 NSMutableDictionary *definition = [NSMutableDictionary dictionaryWithDictionary:info];
66 [_commodityList setObject:definition forKey:key];
67 DESTROY(_sortedKeys); // reset
68}

◆ setPrice:forGood:

- (BOOL) setPrice: (OOCreditsQuantity) price
forGood: (OOCommodityType) good 

Definition at line 408 of file OOCommodityMarket.m.

88 :(OOCreditsQuantity)price forGood:(OOCommodityType)good
89{
90 NSMutableDictionary *definition = [_commodityList oo_mutableDictionaryForKey:good];
91 if (definition == nil)
92 {
93 return NO;
94 }
95 [definition oo_setUnsignedInteger:price forKey:kOOCommodityPriceCurrent];
96 return YES;
97}

◆ setQuantity:forGood:

- (BOOL) setQuantity: (OOCargoQuantity) quantity
forGood: (OOCommodityType) good 

Definition at line 408 of file OOCommodityMarket.m.

100 :(OOCargoQuantity)quantity forGood:(OOCommodityType)good
101{
102 NSMutableDictionary *definition = [_commodityList oo_mutableDictionaryForKey:good];
103 if (definition == nil || quantity > [self capacityForGood:good])
104 {
105 return NO;
106 }
107 [definition oo_setUnsignedInteger:quantity forKey:kOOCommodityQuantityCurrent];
108 return YES;
109}

◆ setShortComment:forGood:

- (BOOL) setShortComment: (NSString *) comment
forGood: (OOCommodityType) good 

Definition at line 408 of file OOCommodityMarket.m.

158 :(NSString *)comment forGood:(OOCommodityType)good
159{
160 NSMutableDictionary *definition = [_commodityList oo_mutableDictionaryForKey:good];
161 if (definition == nil)
162 {
163 return NO;
164 }
165 [definition setObject:comment forKey:kOOCommodityShortComment];
166 return YES;
167}

◆ shortCommentForGood:

- (NSString *) shortCommentForGood: (OOCommodityType) good

Definition at line 408 of file OOCommodityMarket.m.

192 :(OOCommodityType)good
193{
194 NSDictionary *definition = [_commodityList oo_dictionaryForKey:good];
195 if (definition == nil)
196 {
197 return OOExpand(@"[oolite-unknown-commodity-name]");
198 }
199 return OOExpand([definition oo_stringForKey:kOOCommodityShortComment defaultValue:@"[oolite-commodity-no-short-comment]"]);
200}
static NSString *const kOOCommodityShortComment

◆ trumbleOpinionForGood:

- (float) trumbleOpinionForGood: (OOCommodityType) good

Definition at line 408 of file OOCommodityMarket.m.

271 :(OOCommodityType)good
272{
273 NSDictionary *definition = [_commodityList oo_dictionaryForKey:good];
274 if (definition == nil)
275 {
276 return 0;
277 }
278 return [definition oo_floatForKey:kOOCommodityTrumbleOpinion];
279}

Member Data Documentation

◆ _commodityList

- (NSMutableDictionary*) _commodityList
private

Definition at line 34 of file OOCommodityMarket.h.

◆ _sortedKeys

- (NSArray*) _sortedKeys
private

Definition at line 35 of file OOCommodityMarket.h.


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