/
OOCommodities.m
610 lines (506 loc) · 18.1 KB
/
OOCommodities.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
OOCommodities.m
Oolite
Copyright (C) 2004-2014 Giles C Williams and contributors
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
#import "OOCommodities.h"
#import "OOCommodityMarket.h"
#import "StationEntity.h"
#import "ResourceManager.h"
#import "legacy_random.h"
#import "OOCollectionExtractors.h"
#import "OOJSScript.h"
#import "PlayerEntity.h"
#import "OOStringExpander.h"
@interface OOCommodities (OOPrivate)
- (NSDictionary *) modifyGood:(NSDictionary *)good withScript:(OOScript *)script atStation:(StationEntity *)station inSystem:(OOSystemID)system localMode:(BOOL)local;
- (NSDictionary *) createDefinitionFrom:(NSDictionary *) good price:(OOCreditsQuantity)p andQuantity:(OOCargoQuantity)q forKey:(OOCommodityType)key atStation:(StationEntity *)station inSystem:(OOSystemID)system;
- (OOCargoQuantity) generateQuantityForGood:(NSDictionary *)good inEconomy:(OOEconomyID)economy;
- (OOCreditsQuantity) generatePriceForGood:(NSDictionary *)good inEconomy:(OOEconomyID)economy;
- (float) economicBiasForGood:(NSDictionary *)good inEconomy:(OOEconomyID)economy;
- (NSDictionary *) firstModifierForGood:(OOCommodityType)good inClasses:(NSArray *)classes fromList:(NSArray *)definitions;
- (OOCreditsQuantity) adjustPrice:(OOCreditsQuantity)price byRule:(NSDictionary *)rule;
- (OOCargoQuantity) adjustQuantity:(OOCargoQuantity)quantity byRule:(NSDictionary *)rule;
- (NSDictionary *) updateInfoFor:(NSDictionary *)good byRule:(NSDictionary *)rule maxCapacity:(OOCargoQuantity)maxCapacity;
@end
@implementation OOCommodities
/* Older save games store some commodity information by its old index. */
+ (OOCommodityType) legacyCommodityType:(NSUInteger)i
{
switch (i)
{
case 0:
return @"food";
case 1:
return @"textiles";
case 2:
return @"radioactives";
case 3:
return @"slaves";
case 4:
return @"liquor_wines";
case 5:
return @"luxuries";
case 6:
return @"narcotics";
case 7:
return @"computers";
case 8:
return @"machinery";
case 9:
return @"alloys";
case 10:
return @"firearms";
case 11:
return @"furs";
case 12:
return @"minerals";
case 13:
return @"gold";
case 14:
return @"platinum";
case 15:
return @"gem_stones";
case 16:
return @"alien_items";
}
// shouldn't happen
return @"food";
}
- (id) init
{
self = [super init];
if (self == nil) return nil;
NSDictionary *rawCommodityLists = [ResourceManager dictionaryFromFilesNamed:@"trade-goods.plist" inFolder:@"Config" mergeMode:MERGE_SMART cache:YES];
/* // TODO: validation of inputs
// TODO: convert 't', 'kg', 'g' in quantity_unit to 0, 1, 2
// for now it needs them entering as the ints
NSMutableDictionary *validatedCommodityLists = [NSMutableDictionary dictionaryWithCapacity:[rawCommodityLists count]];
NSString *commodityName = nil;
foreachkey (commodityName, rawCommodityLists)
{
// validate
}
// _commodityLists = [[NSDictionary dictionaryWithDictionary:validatedCommodityLists] retain];
*/
_commodityLists = [[NSDictionary dictionaryWithDictionary:rawCommodityLists] retain];
return self;
}
- (void) dealloc
{
DESTROY(_commodityLists);
[super dealloc];
}
- (OOCommodityMarket *) generateManifestForPlayer
{
OOCommodityMarket *market = [[OOCommodityMarket alloc] init];
NSString *commodity = nil;
NSMutableDictionary *good = nil;
foreachkey (commodity, _commodityLists)
{
good = [NSMutableDictionary dictionaryWithDictionary:[_commodityLists oo_dictionaryForKey:commodity]];
[good oo_setUnsignedInteger:0 forKey:kOOCommodityPriceCurrent];
[good oo_setUnsignedInteger:0 forKey:kOOCommodityQuantityCurrent];
/* The actual capacity of the player ship is a total, not
* per-good, so is managed separately through PlayerEntity */
[good oo_setUnsignedInteger:UINT32_MAX forKey:kOOCommodityCapacity];
[good setObject:commodity forKey:kOOCommodityKey];
[market setGood:commodity withInfo:good];
}
return [market autorelease];
}
- (OOCommodityMarket *) generateBlankMarket
{
OOCommodityMarket *market = [[OOCommodityMarket alloc] init];
NSString *commodity = nil;
NSMutableDictionary *good = nil;
foreachkey (commodity, _commodityLists)
{
good = [NSMutableDictionary dictionaryWithDictionary:[_commodityLists oo_dictionaryForKey:commodity]];
[good oo_setUnsignedInteger:0 forKey:kOOCommodityPriceCurrent];
[good oo_setUnsignedInteger:0 forKey:kOOCommodityQuantityCurrent];
[good oo_setUnsignedInteger:0 forKey:kOOCommodityCapacity];
[good setObject:commodity forKey:kOOCommodityKey];
[market setGood:commodity withInfo:good];
}
return [market autorelease];
}
- (NSDictionary *) createDefinitionFrom:(NSDictionary *) good price:(OOCreditsQuantity)p andQuantity:(OOCargoQuantity)q forKey:(OOCommodityType)key atStation:(StationEntity *)station inSystem:(OOSystemID)system
{
NSMutableDictionary *definition = [NSMutableDictionary dictionaryWithDictionary:good];
[definition oo_setUnsignedInteger:p forKey:kOOCommodityPriceCurrent];
[definition oo_setUnsignedInteger:q forKey:kOOCommodityQuantityCurrent];
if (station == nil && [definition objectForKey:kOOCommodityCapacity] == nil)
{
[definition oo_setInteger:MAIN_SYSTEM_MARKET_LIMIT forKey:kOOCommodityCapacity];
}
[definition setObject:key forKey:kOOCommodityKey];
if (station != nil && ![station marketMonitored])
{
// clear legal status indicators if the market is not monitored
[definition oo_setUnsignedInteger:0 forKey:kOOCommodityLegalityExport];
[definition oo_setUnsignedInteger:0 forKey:kOOCommodityLegalityImport];
}
NSString *goodScriptName = [definition oo_stringForKey:kOOCommodityScript];
if (goodScriptName == nil)
{
return definition;
}
OOScript *goodScript = [PLAYER commodityScriptNamed:goodScriptName];
if (goodScript == nil)
{
return definition;
}
return [self modifyGood:definition withScript:goodScript atStation:station inSystem:system localMode:NO];
}
- (NSDictionary *) modifyGood:(NSDictionary *)good withScript:(OOScript *)script atStation:(StationEntity *)station inSystem:(OOSystemID)system localMode:(BOOL)localMode
{
NSDictionary *result = nil;
JSContext *context = OOJSAcquireContext();
jsval rval;
jsval args[] = {
OOJSValueFromNativeObject(context, good),
OOJSValueFromNativeObject(context, station),
INT_TO_JSVAL(system)
};
BOOL OK = YES;
NSString *errorType = nil;
if (localMode)
{
errorType = @"local";
OK = [script callMethod:OOJSID("updateLocalCommodityDefinition")
inContext:context
withArguments:args
count:3
result:&rval];
}
else
{
errorType = @"general";
OK = [script callMethod:OOJSID("updateGeneralCommodityDefinition")
inContext:context
withArguments:args
count:3
result:&rval];
}
if (!OK)
{
OOLog(@"script.commodityScript.error",@"Could not update %@ commodity definition for %@ - unable to call updateLocalCommodityDefinition",errorType,[good oo_stringForKey:kOOCommodityName]);
OOJSRelinquishContext(context);
return good;
}
if (!JSVAL_IS_OBJECT(rval))
{
OOLog(@"script.commodityScript.error",@"Could not update %@ commodity definition for %@ - return value invalid",errorType,[good oo_stringForKey:kOOCommodityKey]);
OOJSRelinquishContext(context);
return good;
}
result = OOJSNativeObjectFromJSObject(context, JSVAL_TO_OBJECT(rval));
OOJSRelinquishContext(context);
if (![result isKindOfClass:[NSDictionary class]])
{
OOLog(@"script.commodityScript.error",@"Could not update %@ commodity definition for %@ - return value invalid",errorType,[good oo_stringForKey:kOOCommodityKey]);
return good;
}
return result;
}
- (OOCommodityMarket *) generateMarketForSystemWithEconomy:(OOEconomyID)economy andScript:(NSString *)scriptName
{
OOScript *script = [PLAYER commodityScriptNamed:scriptName];
OOCommodityMarket *market = [[OOCommodityMarket alloc] init];
NSString *commodity = nil;
NSDictionary *good = nil;
foreachkey (commodity, _commodityLists)
{
good = [_commodityLists oo_dictionaryForKey:commodity];
OOCargoQuantity q = [self generateQuantityForGood:good inEconomy:economy];
// main system market limited to 127 units of each item
OOCargoQuantity cap = [good oo_unsignedIntForKey:kOOCommodityCapacity defaultValue:MAIN_SYSTEM_MARKET_LIMIT];
if (q > cap)
{
q = cap;
}
OOCreditsQuantity p = [self generatePriceForGood:good inEconomy:economy];
good = [self createDefinitionFrom:good price:p andQuantity:q forKey:commodity atStation:nil inSystem:[UNIVERSE currentSystemID]];
if (script != nil)
{
good = [self modifyGood:good withScript:script atStation:nil inSystem:[UNIVERSE currentSystemID] localMode:YES];
}
[market setGood:commodity withInfo:good];
}
return [market autorelease];
}
- (OOCommodityMarket *) generateMarketForStation:(StationEntity *)station
{
NSArray *marketDefinition = [station marketDefinition];
NSString *marketScriptName = [station marketScriptName];
OOScript *marketScript = [PLAYER commodityScriptNamed:marketScriptName];
if (marketDefinition == nil && marketScript == nil)
{
OOCommodityMarket *market = [self generateBlankMarket];
return market;
}
OOCommodityMarket *market = [[OOCommodityMarket alloc] init];
OOCargoQuantity capacity = [station marketCapacity];
OOCommodityMarket *mainMarket = [UNIVERSE commodityMarket];
NSString *commodity = nil;
NSDictionary *good = nil;
foreachkey (commodity, _commodityLists)
{
good = [_commodityLists oo_dictionaryForKey:commodity];
OOCargoQuantity baseCapacity = [good oo_unsignedIntForKey:kOOCommodityCapacity defaultValue:MAIN_SYSTEM_MARKET_LIMIT];
// important - ensure baseCapacity cannot be zero
if (!baseCapacity) baseCapacity = MAIN_SYSTEM_MARKET_LIMIT;
OOCargoQuantity q = [mainMarket quantityForGood:commodity];
OOCreditsQuantity p = [mainMarket priceForGood:commodity];
if (marketScript == nil)
{
NSDictionary *modifier = [self firstModifierForGood:commodity inClasses:[good oo_arrayForKey:kOOCommodityClasses] fromList:marketDefinition];
good = [self updateInfoFor:good byRule:modifier maxCapacity:capacity];
p = [self adjustPrice:p byRule:modifier];
// first, scale to this station's capacity for this good
OOCargoQuantity localCapacity = [good oo_unsignedIntForKey:kOOCommodityCapacity];
if (localCapacity > capacity)
{
localCapacity = capacity;
}
q = (q * localCapacity) / baseCapacity;
q = [self adjustQuantity:q byRule:modifier];
if (q > localCapacity)
{
q = localCapacity; // cap
}
}
else
{
// only scale to market at this stage
q = (q * capacity) / baseCapacity;
}
good = [self createDefinitionFrom:good price:p andQuantity:q forKey:commodity atStation:station inSystem:[UNIVERSE currentSystemID]];
if (marketScript != nil)
{
good = [self modifyGood:good withScript:marketScript atStation:station inSystem:[UNIVERSE currentSystemID] localMode:YES];
}
[market setGood:commodity withInfo:good];
}
return [market autorelease];
}
- (NSUInteger) count
{
return [_commodityLists count];
}
- (NSArray *) goods
{
return [_commodityLists allKeys];
}
- (BOOL) goodDefined:(NSString *)key
{
return ([_commodityLists oo_dictionaryForKey:key] != nil);
}
- (NSString *) goodNamed:(NSString *)name
{
NSString *commodity = nil;
foreachkey (commodity, _commodityLists)
{
NSDictionary *good = [_commodityLists oo_dictionaryForKey:commodity];
if ([OOExpand([good oo_stringForKey:kOOCommodityName]) isEqualToString:name]) {
return commodity;
}
}
return nil;
}
- (NSString *) getRandomCommodity
{
NSArray *keys = [_commodityLists allKeys];
NSUInteger idx = Ranrot() % [keys count];
return [keys oo_stringAtIndex:idx];
}
- (OOMassUnit) massUnitForGood:(NSString *)good
{
NSDictionary *definition = [_commodityLists oo_dictionaryForKey:good];
if (definition == nil)
{
return UNITS_TONS;
}
return [definition oo_unsignedIntForKey:kOOCommodityContainer];
}
- (OOCargoQuantity) generateQuantityForGood:(NSDictionary *)good inEconomy:(OOEconomyID)economy
{
float bias = [self economicBiasForGood:good inEconomy:economy];
float base = [good oo_floatForKey:kOOCommodityQuantityAverage];
float econ = base * [good oo_floatForKey:kOOCommodityQuantityEconomic] * bias;
float random = base * [good oo_floatForKey:kOOCommodityQuantityRandom] * (randf() - randf());
base += econ + random;
if (base < 0.0)
{
return 0;
}
else
{
return (OOCargoQuantity)base;
}
}
- (OOCreditsQuantity) generatePriceForGood:(NSDictionary *)good inEconomy:(OOEconomyID)economy
{
float bias = [self economicBiasForGood:good inEconomy:economy];
float base = [good oo_floatForKey:kOOCommodityPriceAverage];
float econ = base * [good oo_floatForKey:kOOCommodityPriceEconomic] * -bias;
float random = base * [good oo_floatForKey:kOOCommodityPriceRandom] * (randf() - randf());
base += econ + random;
if (base < 0.0)
{
return 0;
}
else
{
return (OOCreditsQuantity)base;
}
}
- (OOCreditsQuantity) samplePriceForCommodity:(OOCommodityType)commodity inEconomy:(OOEconomyID)economy withScript:(NSString *)scriptName inSystem:(OOSystemID)system
{
NSDictionary *good = [_commodityLists oo_dictionaryForKey:commodity];
if (good == nil)
{
return 0;
}
OOCreditsQuantity p = [self generatePriceForGood:good inEconomy:economy];
good = [self createDefinitionFrom:good price:p andQuantity:0 forKey:commodity atStation:nil inSystem:system];
if (scriptName != nil)
{
OOScript *script = [PLAYER commodityScriptNamed:scriptName];
if (script != nil)
{
good = [self modifyGood:good withScript:script atStation:nil inSystem:system localMode:YES];
}
}
return [good oo_unsignedIntegerForKey:kOOCommodityPriceCurrent];
}
// positive = exporter; negative = importer; range -1.0 .. +1.0
- (float) economicBiasForGood:(NSDictionary *)good inEconomy:(OOEconomyID)economy
{
OOEconomyID exporter = [good oo_intForKey:kOOCommodityPeakExport];
OOEconomyID importer = [good oo_intForKey:kOOCommodityPeakImport];
// *2 and /2 to work in ints at this stage
int exDiff = abs(economy-exporter)*2;
int imDiff = abs(economy-importer)*2;
int distance = (exDiff+imDiff)/2;
if (exDiff == imDiff)
{
// neutral economy
return 0.0;
}
else if (exDiff > imDiff)
{
// closer to the importer, so return -ve
return -(1.0-((float)imDiff/(float)distance));
}
else
{
// closer to the exporter, so return +ve
return 1.0-((float)exDiff/(float)distance);
}
}
- (NSDictionary *) firstModifierForGood:(OOCommodityType)good inClasses:(NSArray *)classes fromList:(NSArray *)definitions
{
NSUInteger i;
for (i = 0; i < [definitions count]; i++)
{
NSDictionary *definition = [definitions oo_dictionaryAtIndex:i];
if (definition != nil)
{
NSString *applicationType = [definition oo_stringForKey:kOOCommodityMarketType defaultValue:kOOCommodityMarketTypeValueDefault];
NSString *applicationName = [definition oo_stringForKey:kOOCommodityMarketName defaultValue:@""];
if (
[applicationType isEqualToString:kOOCommodityMarketTypeValueDefault]
|| ([applicationType isEqualToString:kOOCommodityMarketTypeValueGood] && [applicationName isEqualToString:good])
|| ([applicationType isEqualToString:kOOCommodityMarketTypeValueClass] && [classes containsObject:applicationName])
)
{
return definition;
}
}
}
// return a blank dictionary - default values will do the rest
return [NSDictionary dictionary];
}
- (OOCreditsQuantity) adjustPrice:(OOCreditsQuantity)price byRule:(NSDictionary *)rule
{
float p = (float)price; // work in floats to avoid rounding problems
float pa = [rule oo_floatForKey:kOOCommodityMarketPriceAdder defaultValue:0.0];
float pm = [rule oo_floatForKey:kOOCommodityMarketPriceMultiplier defaultValue:1.0];
if (pm <= 0.0 && pa <= 0.0)
{
// setting a price multiplier of 0 forces the price to zero
return 0;
}
float pr = [rule oo_floatForKey:kOOCommodityMarketPriceRandomiser defaultValue:0.0];
p += pa;
p = (p * pm) + (p * pr * (randf()-randf()));
if (p < 1.0)
{
// random variation and non-zero price multiplier can't reduce
// price below 1 decicredit
p = 1.0;
}
return (OOCreditsQuantity) p;
}
- (OOCargoQuantity) adjustQuantity:(OOCargoQuantity)quantity byRule:(NSDictionary *)rule
{
float q = (float)quantity; // work in floats to avoid rounding problems
float qa = [rule oo_floatForKey:kOOCommodityMarketQuantityAdder defaultValue:0.0];
float qm = [rule oo_floatForKey:kOOCommodityMarketQuantityMultiplier defaultValue:1.0];
if (qm <= 0.0 && qa <= 0.0)
{
// setting a price multiplier of 0 forces the price to zero
return 0;
}
float qr = [rule oo_floatForKey:kOOCommodityMarketQuantityRandomiser defaultValue:0.0];
q += qa;
q = (q * qm) + (q * qr * (randf()-randf()));
if (q < 0.0)
{
// random variation and non-zero price multiplier can't reduce
// quantity below zero
q = 0.0;
}
// may be over station capacity - that gets capped later
return (OOCargoQuantity) q;
}
- (NSDictionary *) updateInfoFor:(NSDictionary *)good byRule:(NSDictionary *)rule maxCapacity:(OOCargoQuantity)maxCapacity
{
NSMutableDictionary *tmp = [NSMutableDictionary dictionaryWithDictionary:good];
NSInteger import = [rule oo_integerForKey:kOOCommodityMarketLegalityImport defaultValue:-1];
if (import >= 0)
{
[tmp oo_setInteger:import forKey:kOOCommodityLegalityImport];
}
NSInteger export = [rule oo_integerForKey:kOOCommodityMarketLegalityExport defaultValue:-1];
if (export >= 0)
{
[tmp oo_setInteger:import forKey:kOOCommodityLegalityExport];
}
NSInteger capacity = [rule oo_integerForKey:kOOCommodityMarketCapacity defaultValue:-1];
if (capacity >= 0 && capacity <= (NSInteger)maxCapacity)
{
[tmp oo_setInteger:capacity forKey:kOOCommodityCapacity];
}
else
{
// set to the station max capacity
[tmp oo_setInteger:maxCapacity forKey:kOOCommodityCapacity];
}
return [[tmp copy] autorelease];
}
@end