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;
121 _commodityLists = [[NSDictionary dictionaryWithDictionary:rawCommodityLists] retain];
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];
180 NSMutableDictionary *definition = [NSMutableDictionary dictionaryWithDictionary:good];
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)
206 return [
self modifyGood:definition withScript:goodScript atStation:station inSystem:system localMode:NO];
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";
226 OK = [script
callMethod:OOJSID("updateLocalCommodityDefinition")
234 errorType =
@"general";
235 OK = [script
callMethod:OOJSID("updateGeneralCommodityDefinition")
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];
279 OOCargoQuantity q = [
self generateQuantityForGood:good inEconomy:economy];
281 OOCargoQuantity cap = [good oo_unsignedIntForKey:kOOCommodityCapacity defaultValue:MAIN_SYSTEM_MARKET_LIMIT];
287 good = [
self createDefinitionFrom:good price:p andQuantity:q forKey:commodity atStation:nil inSystem:[UNIVERSE currentSystemID]];
291 good = [
self modifyGood:good withScript:script atStation:nil inSystem:[UNIVERSE currentSystemID] localMode:YES];
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)
329 NSDictionary *modifier = [
self firstModifierForGood:commodity inClasses:[good oo_arrayForKey:kOOCommodityClasses] fromList:marketDefinition];
330 good = [
self updateInfoFor:good byRule:modifier maxCapacity:capacity];
331 p = [
self adjustPrice:p byRule:modifier];
334 OOCargoQuantity localCapacity = [good oo_unsignedIntForKey:kOOCommodityCapacity];
335 if (localCapacity > capacity)
337 localCapacity = capacity;
339 q = (q * localCapacity) / baseCapacity;
340 q = [
self adjustQuantity:q byRule:modifier];
341 if (q > localCapacity)
349 q = (q * capacity) / baseCapacity;
352 good = [
self createDefinitionFrom:good price:p andQuantity:q forKey:commodity atStation:station inSystem:[UNIVERSE currentSystemID]];
353 if (marketScript !=
nil)
355 good = [
self modifyGood:good withScript:marketScript atStation:station inSystem:[UNIVERSE currentSystemID] localMode:YES];
360 return [market autorelease];
366 return [_commodityLists count];
372 return [_commodityLists allKeys];
376- (BOOL) goodDefined:(NSString *)key
378 return ([_commodityLists oo_dictionaryForKey:key] !=
nil);
381- (NSString *) goodNamed:(NSString *)name
383 NSString *commodity =
nil;
386 NSDictionary *good = [_commodityLists oo_dictionaryForKey:commodity];
396- (NSString *) getRandomCommodity
398 NSArray *keys = [_commodityLists allKeys];
399 NSUInteger idx =
Ranrot() % [keys count];
400 return [keys oo_stringAtIndex:idx];
404- (
OOMassUnit) massUnitForGood:(NSString *)good
406 NSDictionary *definition = [_commodityLists oo_dictionaryForKey:good];
407 if (definition ==
nil)
411 return [definition oo_unsignedIntForKey:kOOCommodityContainer];
419 float bias = [
self economicBiasForGood:good inEconomy:economy];
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;
438 float bias = [
self economicBiasForGood:good inEconomy:economy];
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];
464 good = [
self createDefinitionFrom:good price:p andQuantity:0 forKey:commodity atStation:nil inSystem:system];
465 if (scriptName !=
nil)
467 OOScript *script = [PLAYER commodityScriptNamed:scriptName];
470 good = [
self modifyGood:good withScript:script atStation:nil inSystem:system localMode:YES];
473 return [good oo_unsignedIntegerForKey:kOOCommodityPriceCurrent];
478- (float) economicBiasForGood:(NSDictionary *)good inEconomy:(
OOEconomyID)economy
480 OOEconomyID exporter = [good oo_intForKey:kOOCommodityPeakExport];
481 OOEconomyID importer = [good oo_intForKey:kOOCommodityPeakImport];
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
509 for (i = 0; i < [definitions count]; i++)
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:@""];
528 return [NSDictionary dictionary];
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];