Oolite 1.91.0.7644-241112-7f5034b
Loading...
Searching...
No Matches
OOCommodities(OOPrivate) Category Reference

Instance Methods

(NSDictionary *) - modifyGood:withScript:atStation:inSystem:localMode:
 
(NSDictionary *) - createDefinitionFrom:price:andQuantity:forKey:atStation:inSystem:
 
(OOCargoQuantity- generateQuantityForGood:inEconomy:
 
(OOCreditsQuantity- generatePriceForGood:inEconomy:
 
(float) - economicBiasForGood:inEconomy:
 
(NSDictionary *) - firstModifierForGood:inClasses:fromList:
 
(OOCreditsQuantity- adjustPrice:byRule:
 
(OOCargoQuantity- adjustQuantity:byRule:
 
(NSDictionary *) - updateInfoFor:byRule:maxCapacity:
 

Detailed Description

Definition at line 36 of file OOCommodities.m.

Method Documentation

◆ adjustPrice:byRule:

- (OOCreditsQuantity) adjustPrice: (OOCreditsQuantity) price
byRule: (NSDictionary *) rule 

Extends class OOCommodities.

Definition at line 1 of file OOCommodities.m.

532 :(OOCreditsQuantity)price byRule:(NSDictionary *)rule
533{
534 float p = (float)price; // work in floats to avoid rounding problems
535 float pa = [rule oo_floatForKey:kOOCommodityMarketPriceAdder defaultValue:0.0];
536 float pm = [rule oo_floatForKey:kOOCommodityMarketPriceMultiplier defaultValue:1.0];
537 if (pm <= 0.0 && pa <= 0.0)
538 {
539 // setting a price multiplier of 0 forces the price to zero
540 return 0;
541 }
542 float pr = [rule oo_floatForKey:kOOCommodityMarketPriceRandomiser defaultValue:0.0];
543 p += pa;
544 p = (p * pm) + (p * pr * (randf()-randf()));
545 if (p < 1.0)
546 {
547 // random variation and non-zero price multiplier can't reduce
548 // price below 1 decicredit
549 p = 1.0;
550 }
551 return (OOCreditsQuantity) p;
552}
uint64_t OOCreditsQuantity
Definition OOTypes.h:182
float randf(void)

◆ adjustQuantity:byRule:

- (OOCargoQuantity) adjustQuantity: (OOCargoQuantity) quantity
byRule: (NSDictionary *) rule 

Extends class OOCommodities.

Definition at line 1 of file OOCommodities.m.

555 :(OOCargoQuantity)quantity byRule:(NSDictionary *)rule
556{
557 float q = (float)quantity; // work in floats to avoid rounding problems
558 float qa = [rule oo_floatForKey:kOOCommodityMarketQuantityAdder defaultValue:0.0];
559 float qm = [rule oo_floatForKey:kOOCommodityMarketQuantityMultiplier defaultValue:1.0];
560 if (qm <= 0.0 && qa <= 0.0)
561 {
562 // setting a price multiplier of 0 forces the price to zero
563 return 0;
564 }
565 float qr = [rule oo_floatForKey:kOOCommodityMarketQuantityRandomiser defaultValue:0.0];
566 q += qa;
567 q = (q * qm) + (q * qr * (randf()-randf()));
568 if (q < 0.0)
569 {
570 // random variation and non-zero price multiplier can't reduce
571 // quantity below zero
572 q = 0.0;
573 }
574 // may be over station capacity - that gets capped later
575 return (OOCargoQuantity) q;
576}
uint32_t OOCargoQuantity
Definition OOTypes.h:176

◆ createDefinitionFrom:price:andQuantity:forKey:atStation:inSystem:

- (NSDictionary *) createDefinitionFrom: (NSDictionary *) good
price: (OOCreditsQuantity) p
andQuantity: (OOCargoQuantity) q
forKey: (OOCommodityType) key
atStation: (StationEntity *) station
inSystem: (OOSystemID) system 

Extends class OOCommodities.

Definition at line 1 of file OOCommodities.m.

178 :(NSDictionary *) good price:(OOCreditsQuantity)p andQuantity:(OOCargoQuantity)q forKey:(OOCommodityType)key atStation:(StationEntity *)station inSystem:(OOSystemID)system
179{
180 NSMutableDictionary *definition = [NSMutableDictionary dictionaryWithDictionary:good];
181 [definition oo_setUnsignedInteger:p forKey:kOOCommodityPriceCurrent];
182 [definition oo_setUnsignedInteger:q forKey:kOOCommodityQuantityCurrent];
183 if (station == nil && [definition objectForKey:kOOCommodityCapacity] == nil)
184 {
185 [definition oo_setInteger:MAIN_SYSTEM_MARKET_LIMIT forKey:kOOCommodityCapacity];
186 }
187
188 [definition setObject:key forKey:kOOCommodityKey];
189 if (station != nil && ![station marketMonitored])
190 {
191 // clear legal status indicators if the market is not monitored
192 [definition oo_setUnsignedInteger:0 forKey:kOOCommodityLegalityExport];
193 [definition oo_setUnsignedInteger:0 forKey:kOOCommodityLegalityImport];
194 }
195
196 NSString *goodScriptName = [definition oo_stringForKey:kOOCommodityScript];
197 if (goodScriptName == nil)
198 {
199 return definition;
200 }
201 OOScript *goodScript = [PLAYER commodityScriptNamed:goodScriptName];
202 if (goodScript == nil)
203 {
204 return definition;
205 }
206 return [self modifyGood:definition withScript:goodScript atStation:station inSystem:system localMode:NO];
207}
static NSString *const kOOCommodityCapacity
return nil
NSString * OOCommodityType
Definition OOTypes.h:106
int16_t OOSystemID
Definition OOTypes.h:211

◆ economicBiasForGood:inEconomy:

- (float) economicBiasForGood: (NSDictionary *) good
inEconomy: (OOEconomyID) economy 

Extends class OOCommodities.

Definition at line 1 of file OOCommodities.m.

478 :(NSDictionary *)good inEconomy:(OOEconomyID)economy
479{
480 OOEconomyID exporter = [good oo_intForKey:kOOCommodityPeakExport];
481 OOEconomyID importer = [good oo_intForKey:kOOCommodityPeakImport];
482
483 // *2 and /2 to work in ints at this stage
484 int exDiff = abs(economy-exporter)*2;
485 int imDiff = abs(economy-importer)*2;
486 int distance = (exDiff+imDiff)/2;
487
488 if (exDiff == imDiff)
489 {
490 // neutral economy
491 return 0.0;
492 }
493 else if (exDiff > imDiff)
494 {
495 // closer to the importer, so return -ve
496 return -(1.0-((float)imDiff/(float)distance));
497 }
498 else
499 {
500 // closer to the exporter, so return +ve
501 return 1.0-((float)exDiff/(float)distance);
502 }
503}
uint8_t OOEconomyID
Definition OOTypes.h:207

◆ firstModifierForGood:inClasses:fromList:

- (NSDictionary *) firstModifierForGood: (OOCommodityType) good
inClasses: (NSArray *) classes
fromList: (NSArray *) definitions 

Extends class OOCommodities.

Definition at line 1 of file OOCommodities.m.

506 :(OOCommodityType)good inClasses:(NSArray *)classes fromList:(NSArray *)definitions
507{
508 NSUInteger i;
509 for (i = 0; i < [definitions count]; i++)
510 {
511 NSDictionary *definition = [definitions oo_dictionaryAtIndex:i];
512 if (definition != nil)
513 {
514 NSString *applicationType = [definition oo_stringForKey:kOOCommodityMarketType defaultValue:kOOCommodityMarketTypeValueDefault];
515 NSString *applicationName = [definition oo_stringForKey:kOOCommodityMarketName defaultValue:@""];
516
517 if (
518 [applicationType isEqualToString:kOOCommodityMarketTypeValueDefault]
519 || ([applicationType isEqualToString:kOOCommodityMarketTypeValueGood] && [applicationName isEqualToString:good])
520 || ([applicationType isEqualToString:kOOCommodityMarketTypeValueClass] && [classes containsObject:applicationName])
521 )
522 {
523 return definition;
524 }
525 }
526 }
527 // return a blank dictionary - default values will do the rest
528 return [NSDictionary dictionary];
529}
static NSString *const kOOCommodityMarketTypeValueClass
static NSString *const kOOCommodityMarketTypeValueGood
static NSString *const kOOCommodityMarketTypeValueDefault

◆ generatePriceForGood:inEconomy:

- (OOCreditsQuantity) generatePriceForGood: (NSDictionary *) good
inEconomy: (OOEconomyID) economy 

Extends class OOCommodities.

Definition at line 1 of file OOCommodities.m.

436 :(NSDictionary *)good inEconomy:(OOEconomyID)economy
437{
438 float bias = [self economicBiasForGood:good inEconomy:economy];
439
440 float base = [good oo_floatForKey:kOOCommodityPriceAverage];
441 float econ = base * [good oo_floatForKey:kOOCommodityPriceEconomic] * -bias;
442 float random = base * [good oo_floatForKey:kOOCommodityPriceRandom] * (randf() - randf());
443 base += econ + random;
444 if (base < 0.0)
445 {
446 return 0;
447 }
448 else
449 {
450 return (OOCreditsQuantity)base;
451 }
452}

◆ generateQuantityForGood:inEconomy:

- (OOCargoQuantity) generateQuantityForGood: (NSDictionary *) good
inEconomy: (OOEconomyID) economy 

Extends class OOCommodities.

Definition at line 1 of file OOCommodities.m.

417 :(NSDictionary *)good inEconomy:(OOEconomyID)economy
418{
419 float bias = [self economicBiasForGood:good inEconomy:economy];
420
421 float base = [good oo_floatForKey:kOOCommodityQuantityAverage];
422 float econ = base * [good oo_floatForKey:kOOCommodityQuantityEconomic] * bias;
423 float random = base * [good oo_floatForKey:kOOCommodityQuantityRandom] * (randf() - randf());
424 base += econ + random;
425 if (base < 0.0)
426 {
427 return 0;
428 }
429 else
430 {
431 return (OOCargoQuantity)base;
432 }
433}

◆ modifyGood:withScript:atStation:inSystem:localMode:

- (NSDictionary *) modifyGood: (NSDictionary *) good
withScript: (OOScript *) script
atStation: (StationEntity *) station
inSystem: (OOSystemID) system
localMode: (BOOL) local 

Extends class OOCommodities.

Definition at line 1 of file OOCommodities.m.

210 :(NSDictionary *)good withScript:(OOScript *)script atStation:(StationEntity *)station inSystem:(OOSystemID)system localMode:(BOOL)localMode
211{
212 NSDictionary *result = nil;
213 JSContext *context = OOJSAcquireContext();
214 jsval rval;
215 jsval args[] = {
216 OOJSValueFromNativeObject(context, good),
217 OOJSValueFromNativeObject(context, station),
218 INT_TO_JSVAL(system)
219 };
220 BOOL OK = YES;
221 NSString *errorType = nil;
222
223 if (localMode)
224 {
225 errorType = @"local";
226 OK = [script callMethod:OOJSID("updateLocalCommodityDefinition")
227 inContext:context
228 withArguments:args
229 count:3
230 result:&rval];
231 }
232 else
233 {
234 errorType = @"general";
235 OK = [script callMethod:OOJSID("updateGeneralCommodityDefinition")
236 inContext:context
237 withArguments:args
238 count:3
239 result:&rval];
240 }
241
242 if (!OK)
243 {
244 OOLog(@"script.commodityScript.error",@"Could not update %@ commodity definition for %@ - unable to call updateLocalCommodityDefinition",errorType,[good oo_stringForKey:kOOCommodityName]);
245 OOJSRelinquishContext(context);
246 return good;
247 }
248
249 if (!JSVAL_IS_OBJECT(rval))
250 {
251 OOLog(@"script.commodityScript.error",@"Could not update %@ commodity definition for %@ - return value invalid",errorType,[good oo_stringForKey:kOOCommodityKey]);
252 OOJSRelinquishContext(context);
253 return good;
254 }
255
256 result = OOJSNativeObjectFromJSObject(context, JSVAL_TO_OBJECT(rval));
257 OOJSRelinquishContext(context);
258 if (![result isKindOfClass:[NSDictionary class]])
259 {
260 OOLog(@"script.commodityScript.error",@"Could not update %@ commodity definition for %@ - return value invalid",errorType,[good oo_stringForKey:kOOCommodityKey]);
261 return good;
262 }
263
264 return result;
265}
static NSString *const kOOCommodityKey
static NSString *const kOOCommodityName
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
id OOJSNativeObjectFromJSObject(JSContext *context, JSObject *object)
OOINLINE JSContext * OOJSAcquireContext(void)
OOINLINE void OOJSRelinquishContext(JSContext *context)
#define OOLog(class, format,...)
Definition OOLogging.h:88
BOOL callMethod:inContext:withArguments:count:result:(jsid methodID,[inContext] JSContext *context,[withArguments] jsval *argv,[count] intN argc,[result] jsval *outResult)
Definition OOJSScript.m:652

◆ updateInfoFor:byRule:maxCapacity:

- (NSDictionary *) updateInfoFor: (NSDictionary *) good
byRule: (NSDictionary *) rule
maxCapacity: (OOCargoQuantity) maxCapacity 

Extends class OOCommodities.

Definition at line 1 of file OOCommodities.m.

579 :(NSDictionary *)good byRule:(NSDictionary *)rule maxCapacity:(OOCargoQuantity)maxCapacity
580{
581 NSMutableDictionary *tmp = [NSMutableDictionary dictionaryWithDictionary:good];
582 NSInteger import = [rule oo_integerForKey:kOOCommodityMarketLegalityImport defaultValue:-1];
583 if (import >= 0)
584 {
585 [tmp oo_setInteger:import forKey:kOOCommodityLegalityImport];
586 }
587
588 NSInteger export = [rule oo_integerForKey:kOOCommodityMarketLegalityExport defaultValue:-1];
589 if (export >= 0)
590 {
591 [tmp oo_setInteger:import forKey:kOOCommodityLegalityExport];
592 }
593
594 NSInteger capacity = [rule oo_integerForKey:kOOCommodityMarketCapacity defaultValue:-1];
595 if (capacity >= 0 && capacity <= (NSInteger)maxCapacity)
596 {
597 [tmp oo_setInteger:capacity forKey:kOOCommodityCapacity];
598 }
599 else
600 {
601 // set to the station max capacity
602 [tmp oo_setInteger:maxCapacity forKey:kOOCommodityCapacity];
603 }
604
605 return [[tmp copy] autorelease];
606}

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