36@interface OOCommodities (OOPrivate)
38- (NSDictionary *) modifyGood:(NSDictionary *)good withScript:(
OOScript *)script atStation:(
StationEntity *)station inSystem:(
OOSystemID)system localMode:(BOOL)local;
45- (float) economicBiasForGood:(NSDictionary *)good inEconomy:(
OOEconomyID)economy;
46- (NSDictionary *) firstModifierForGood:(
OOCommodityType)good inClasses:(NSArray *)classes fromList:(NSArray *)definitions;
49- (NSDictionary *) updateInfoFor:(NSDictionary *)good byRule:(NSDictionary *)rule maxCapacity:(
OOCargoQuantity)maxCapacity;
66 return @"radioactives";
70 return @"liquor_wines";
94 return @"alien_items";
105 if (
self ==
nil)
return nil;
140 NSString *commodity =
nil;
141 NSMutableDictionary *good =
nil;
144 good = [
NSMutableDictionary dictionaryWithDictionary:[_commodityLists oo_dictionaryForKey:commodity]];
145 [
good oo_setUnsignedInteger:0 forKey:kOOCommodityPriceCurrent];
146 [
good oo_setUnsignedInteger:0 forKey:kOOCommodityQuantityCurrent];
149 [
good oo_setUnsignedInteger:UINT32_MAX forKey:kOOCommodityCapacity];
150 [
good setObject:commodity forKey:kOOCommodityKey];
154 return [
market autorelease];
162 NSString *commodity =
nil;
163 NSMutableDictionary *good =
nil;
166 good = [
NSMutableDictionary dictionaryWithDictionary:[_commodityLists oo_dictionaryForKey:commodity]];
167 [
good oo_setUnsignedInteger:0 forKey:kOOCommodityPriceCurrent];
168 [
good oo_setUnsignedInteger:0 forKey:kOOCommodityQuantityCurrent];
169 [
good oo_setUnsignedInteger:0 forKey:kOOCommodityCapacity];
170 [
good setObject:commodity forKey:kOOCommodityKey];
174 return [
market autorelease];
181 [
definition oo_setUnsignedInteger:p forKey:kOOCommodityPriceCurrent];
182 [
definition oo_setUnsignedInteger:q forKey:kOOCommodityQuantityCurrent];
185 [
definition oo_setInteger:MAIN_SYSTEM_MARKET_LIMIT forKey:kOOCommodityCapacity];
188 [
definition setObject:key forKey:kOOCommodityKey];
189 if (station !=
nil && ![station marketMonitored])
192 [
definition oo_setUnsignedInteger:0 forKey:kOOCommodityLegalityExport];
193 [
definition oo_setUnsignedInteger:0 forKey:kOOCommodityLegalityImport];
196 NSString *goodScriptName = [
definition oo_stringForKey:kOOCommodityScript];
197 if (goodScriptName ==
nil)
201 OOScript *goodScript = [PLAYER commodityScriptNamed:goodScriptName];
202 if (goodScript ==
nil)
210- (NSDictionary *) modifyGood:(NSDictionary *)good withScript:(
OOScript *)script atStation:(
StationEntity *)station inSystem:(
OOSystemID)system localMode:(BOOL)localMode
212 NSDictionary *result =
nil;
221 NSString *errorType =
nil;
225 errorType =
@"local";
234 errorType =
@"general";
244 OOLog(
@"script.commodityScript.error",
@"Could not update %@ commodity definition for %@ - unable to call updateLocalCommodityDefinition",errorType,[good oo_stringForKey:
kOOCommodityName]);
249 if (!JSVAL_IS_OBJECT(rval))
251 OOLog(
@"script.commodityScript.error",
@"Could not update %@ commodity definition for %@ - return value invalid",errorType,[good oo_stringForKey:
kOOCommodityKey]);
258 if (![result isKindOfClass:[NSDictionary
class]])
260 OOLog(
@"script.commodityScript.error",
@"Could not update %@ commodity definition for %@ - return value invalid",errorType,[good oo_stringForKey:
kOOCommodityKey]);
270 OOScript *script = [PLAYER commodityScriptNamed:scriptName];
274 NSString *commodity =
nil;
275 NSDictionary *good =
nil;
278 good = [_commodityLists oo_dictionaryForKey:commodity];
281 OOCargoQuantity cap = [
good oo_unsignedIntForKey:kOOCommodityCapacity defaultValue:MAIN_SYSTEM_MARKET_LIMIT];
295 return [
market autorelease];
303 OOScript *marketScript = [PLAYER commodityScriptNamed:marketScriptName];
304 if (marketDefinition ==
nil && marketScript ==
nil)
314 NSString *commodity =
nil;
315 NSDictionary *good =
nil;
318 good = [_commodityLists oo_dictionaryForKey:commodity];
319 OOCargoQuantity baseCapacity = [
good oo_unsignedIntForKey:kOOCommodityCapacity defaultValue:MAIN_SYSTEM_MARKET_LIMIT];
327 if (marketScript ==
nil)
335 if (localCapacity > capacity)
337 localCapacity = capacity;
339 q = (q * localCapacity) / baseCapacity;
341 if (q > localCapacity)
349 q = (q * capacity) / baseCapacity;
353 if (marketScript !=
nil)
360 return [
market autorelease];
366 return [_commodityLists count];
372 return [_commodityLists allKeys];
376- (BOOL) goodDefined:(NSString *)key
381- (NSString *) goodNamed:(NSString *)name
383 NSString *commodity =
nil;
386 NSDictionary *good = [_commodityLists oo_dictionaryForKey:commodity];
398 NSArray *keys = [_commodityLists allKeys];
400 return [
keys oo_stringAtIndex:idx];
406 NSDictionary *definition = [_commodityLists oo_dictionaryForKey:good];
407 if (definition ==
nil)
411 return [
definition oo_unsignedIntForKey:kOOCommodityContainer];
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;
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;
457 NSDictionary *good = [_commodityLists oo_dictionaryForKey:commodity];
465 if (scriptName !=
nil)
467 OOScript *script = [PLAYER commodityScriptNamed:scriptName];
473 return [
good oo_unsignedIntegerForKey:kOOCommodityPriceCurrent];
478- (float) economicBiasForGood:(NSDictionary *)good inEconomy:(
OOEconomyID)economy
484 int exDiff = abs(economy-exporter)*2;
485 int imDiff = abs(economy-importer)*2;
486 int distance = (exDiff+imDiff)/2;
488 if (exDiff == imDiff)
493 else if (exDiff > imDiff)
496 return -(1.0-((float)imDiff/(float)distance));
501 return 1.0-((float)exDiff/(float)distance);
506- (NSDictionary *) firstModifierForGood:(
OOCommodityType)good inClasses:(NSArray *)classes fromList:(NSArray *)definitions
511 NSDictionary *definition = [
definitions oo_dictionaryAtIndex:i];
512 if (definition !=
nil)
514 NSString *applicationType = [
definition oo_stringForKey:kOOCommodityMarketType defaultValue:kOOCommodityMarketTypeValueDefault];
515 NSString *applicationName = [
definition oo_stringForKey:kOOCommodityMarketName defaultValue:@""];
534 float p = (float)price;
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)
542 float pr = [
rule oo_floatForKey:kOOCommodityMarketPriceRandomiser defaultValue:0.0];
557 float q = (float)quantity;
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)
565 float qr = [
rule oo_floatForKey:kOOCommodityMarketQuantityRandomiser defaultValue:0.0];
579- (NSDictionary *) updateInfoFor:(NSDictionary *)good byRule:(NSDictionary *)rule maxCapacity:(
OOCargoQuantity)maxCapacity
582 NSInteger
import = [
rule oo_integerForKey:kOOCommodityMarketLegalityImport defaultValue:-1];
585 [
tmp oo_setInteger:import forKey:kOOCommodityLegalityImport];
588 NSInteger export = [
rule oo_integerForKey:kOOCommodityMarketLegalityExport defaultValue:-1];
591 [
tmp oo_setInteger:import forKey:kOOCommodityLegalityExport];
594 NSInteger capacity = [
rule oo_integerForKey:kOOCommodityMarketCapacity defaultValue:-1];
595 if (capacity >= 0 && capacity <= (NSInteger)maxCapacity)
597 [
tmp oo_setInteger:capacity forKey:kOOCommodityCapacity];
602 [
tmp oo_setInteger:maxCapacity forKey:kOOCommodityCapacity];
605 return [[
tmp copy] autorelease];
#define foreachkey(VAR, DICT)
#define MAIN_SYSTEM_MARKET_LIMIT
static NSString *const kOOCommodityCapacity
static NSString *const kOOCommodityKey
static NSString *const kOOCommodityMarketTypeValueClass
static NSString *const kOOCommodityMarketTypeValueGood
static NSString *const kOOCommodityMarketTypeValueDefault
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,...)
#define OOExpand(string,...)
NSString * OOCommodityType
uint64_t OOCreditsQuantity
OOCreditsQuantity generatePriceForGood:inEconomy:(NSDictionary *good, [inEconomy] OOEconomyID economy)
NSString * getRandomCommodity()
NSDictionary * updateInfoFor:byRule:maxCapacity:(NSDictionary *good, [byRule] NSDictionary *rule, [maxCapacity] OOCargoQuantity maxCapacity)
NSDictionary * _commodityLists
OOCommodityMarket * generateManifestForPlayer()
NSDictionary * modifyGood:withScript:atStation:inSystem:localMode:(NSDictionary *good, [withScript] OOScript *script, [atStation] StationEntity *station, [inSystem] OOSystemID system, [localMode] BOOL localMode)
OOCargoQuantity generateQuantityForGood:inEconomy:(NSDictionary *good, [inEconomy] OOEconomyID economy)
OOCargoQuantity adjustQuantity:byRule:(OOCargoQuantity quantity, [byRule] NSDictionary *rule)
NSDictionary * createDefinitionFrom:price:andQuantity:forKey:atStation:inSystem:(NSDictionary *good, [price] OOCreditsQuantity p, [andQuantity] OOCargoQuantity q, [forKey] OOCommodityType key, [atStation] StationEntity *station, [inSystem] OOSystemID system)
NSDictionary * firstModifierForGood:inClasses:fromList:(OOCommodityType good, [inClasses] NSArray *classes, [fromList] NSArray *definitions)
OOCommodityMarket * generateBlankMarket()
float economicBiasForGood:inEconomy:(NSDictionary *good, [inEconomy] OOEconomyID economy)
OOCreditsQuantity adjustPrice:byRule:(OOCreditsQuantity price, [byRule] NSDictionary *rule)
void setGood:withInfo:(OOCommodityType key,[withInfo] NSDictionary *info)
OOCargoQuantity quantityForGood:(OOCommodityType good)
OOCreditsQuantity priceForGood:(OOCommodityType good)
BOOL callMethod:inContext:withArguments:count:result:(jsid methodID,[inContext] JSContext *context,[withArguments] jsval *argv,[count] intN argc,[result] jsval *outResult)
NSDictionary * dictionaryFromFilesNamed:inFolder:mergeMode:cache:(NSString *fileName,[inFolder] NSString *folderName,[mergeMode] OOResourceMergeMode mergeMode,[cache] BOOL useCache)
OOCargoQuantity marketCapacity
NSArray * marketDefinition
NSString * marketScriptName