Oolite
Loading...
Searching...
No Matches
OOCharacter.m
Go to the documentation of this file.
1/*
2
3OOCharacter.m
4
5Oolite
6Copyright (C) 2004-2013 Giles C Williams and contributors
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21MA 02110-1301, USA.
22
23*/
24
25#import "OOCharacter.h"
26
28#import "OOJSScript.h"
29#import "OOStringExpander.h"
30#import "OOStringParsing.h"
31#import "Universe.h"
32
33@interface OOCharacter (Private)
34
35- (id)initWithGenSeed:(Random_Seed)characterSeed andOriginalSystem:(OOSystemID)systemSeed;
36- (void)setCharacterFromDictionary:(NSDictionary*)dict;
37
38- (void)setOriginSystem:(OOSystemID)value;
40
41@end
42
43@implementation OOCharacter
44
46{
47 return [NSString stringWithFormat:@"%@, %@. bounty: %i insurance: %zu", [self name], [self shortDescription], [self legalStatus], [self insuranceCredits]];
48}
49
50- (NSString*)oo_jsClassName
51{
52 return @"Character";
53}
54
55- (void)dealloc
56{
57 [_name release];
58 [_shortDescription release];
59 [_scriptActions release];
61
62 [super dealloc];
63}
64
65- (id)initWithGenSeed:(Random_Seed)characterSeed andOriginalSystem:(OOSystemID)system
66{
67 if ((self = [super init])) {
68 // do character set-up
69 _genSeed = characterSeed;
70 _originSystem = system;
71
72 [self basicSetUp];
73 }
74 return self;
75}
76
77- (id)initWithRole:(NSString*)role andOriginalSystem:(OOSystemID)system
78{
79 Random_Seed seed;
81
82 if ((self = [self initWithGenSeed:seed andOriginalSystem:system])) {
83 [self castInRole:role];
84 }
85
86 return self;
87}
88
89+ (OOCharacter*)characterWithRole:(NSString*)role andOriginalSystem:(OOSystemID)system
90{
91 return [[[self alloc] initWithRole:role andOriginalSystem:system] autorelease];
92}
93
94+ (OOCharacter*)randomCharacterWithRole:(NSString*)role andOriginalSystem:(OOSystemID)system
95{
96 Random_Seed seed;
97
98 seed.a = (Ranrot() & 0xff);
99 seed.b = (Ranrot() & 0xff);
100 seed.c = (Ranrot() & 0xff);
101 seed.d = (Ranrot() & 0xff);
102 seed.e = (Ranrot() & 0xff);
103 seed.f = (Ranrot() & 0xff);
104
105 OOCharacter* character = [[[OOCharacter alloc] initWithGenSeed:seed andOriginalSystem:system] autorelease];
106 [character castInRole:role];
107
108 return character;
109}
110
111+ (OOCharacter*)characterWithDictionary:(NSDictionary*)dict
112{
113 OOCharacter* character = [[[OOCharacter alloc] init] autorelease];
115
116 return character;
117}
118
119- (NSString*)planetOfOrigin
120{
121 // determine the planet of origin
122 NSDictionary* originInfo = [UNIVERSE generateSystemData:[self planetIDOfOrigin]];
123 return [originInfo objectForKey:KEY_NAME];
124}
125
127{
128 // determine the planet of origin
129 return _originSystem;
130}
131
132- (NSString*)species
133{
134 // determine the character's species
135 int species = [self genSeed].f & 0x03; // 0-1 native to home system, 2 human colonial, 3 other
136 NSString* speciesString = nil;
137 if (species == 3)
138 speciesString = [UNIVERSE getSystemInhabitants:[self genSeed].e plural:NO];
139 else
140 speciesString = [UNIVERSE getSystemInhabitants:[self planetIDOfOrigin] plural:NO];
141
142 if (![[UNIVERSE descriptions] oo_boolForKey:@"lowercase_ignore"]) {
143 speciesString = [speciesString lowercaseString];
144 }
145
146 return [speciesString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
147}
148
150{
151 // save random seeds for restoration later
152 RNG_Seed savedRNGSeed = currentRandomSeed();
153 RANROTSeed savedRANROTSeed = RANROTGetFullSeed();
154 // set RNG to character seed
155 Random_Seed genSeed = [self genSeed];
157
158 // determine the planet of origin
159 NSDictionary* originInfo = [UNIVERSE generateSystemData:[self planetIDOfOrigin]];
160 NSString* planet = [originInfo oo_stringForKey:KEY_NAME];
161 OOGovernmentID government = [originInfo oo_intForKey:KEY_GOVERNMENT]; // 0 .. 7 (0 anarchic .. 7 most stable)
162 int criminalTendency = government ^ 0x07;
163
164 // determine the character's species
165 NSString* species = [self species];
166
167 // determine the character's name
169 NSString* genName = nil;
170 if ([species hasPrefix:@"human"]) {
171 genName = [NSString stringWithFormat:@"%@ %@", OOExpandWithSeed(genSeed, @"%R"), OOExpandKeyWithSeed(genSeed, @"nom")];
172 } else {
173 /* NOTE: we can't use "%R %R" because that will produce the same string
174 twice. TODO: is there a reason not to use %N and kOOExpandGoodRNG
175 here? Is there some context where we rely on being able to get the
176 same name for a given genSeed?
177 */
178 genName = [NSString stringWithFormat:@"%@ %@", OOExpandWithSeed(genSeed, @"%R"), OOExpandWithSeed(genSeed, @"%R")];
179 }
180 [self setName:genName];
181
182 [self setShortDescription:OOExpandKeyWithSeed(genSeed, @"character-generic-description", species, planet)];
183
184 // determine _legalStatus for a completely random character
185 [self setLegalStatus:0]; // clean
186 int legalIndex = gen_rnd_number() & gen_rnd_number() & 0x03;
187 while (((gen_rnd_number() & 0xf) < criminalTendency) && (legalIndex < 3)) {
188 legalIndex++;
189 }
190 if (legalIndex == 3) {
191 // criminal
192 [self setLegalStatus:criminalTendency + criminalTendency * (gen_rnd_number() & 0x03) + (gen_rnd_number() & gen_rnd_number() & 0x7f)];
193 }
194 legalIndex = 0;
195 if (_legalStatus > 0)
196 legalIndex = (_legalStatus <= 50) ? 1 : 2;
197
198 // if clean - determine insurance level (if any)
199 [self setInsuranceCredits:0];
200 if (legalIndex == 0) {
201 int insuranceIndex = gen_rnd_number() & gen_rnd_number() & 0x03;
202 switch (insuranceIndex) {
203 case 1:
204 [self setInsuranceCredits:125];
205 break;
206 case 2:
207 [self setInsuranceCredits:250];
208 break;
209 case 3:
210 [self setInsuranceCredits:500];
211 }
212 }
213
214 // restore random seed
215 setRandomSeed(savedRNGSeed);
216 RANROTSetFullSeed(savedRANROTSeed);
217}
218
219- (BOOL)castInRole:(NSString*)role
220{
221 BOOL specialSetUpDone = NO;
222
223 role = [role lowercaseString];
224 if ([role hasPrefix:@"pirate"]) {
225 // determine _legalStatus for a completely random character
226 Random_Seed genSeed = [self genSeed];
227 int sins = 0x08 | (genSeed.a & genSeed.b);
228 [self setLegalStatus:sins & 0x7f];
229
230 specialSetUpDone = YES;
231 } else if ([role hasPrefix:@"trader"]) {
232 [self setLegalStatus:0]; // clean
233
234 int insuranceIndex = gen_rnd_number() & 0x03;
235 switch (insuranceIndex) {
236 case 0:
237 [self setInsuranceCredits:0];
238 break;
239 case 1:
240 [self setInsuranceCredits:125];
241 break;
242 case 2:
243 [self setInsuranceCredits:250];
244 break;
245 case 3:
246 [self setInsuranceCredits:500];
247 }
248 specialSetUpDone = YES;
249 } else if ([role hasPrefix:@"hunter"]) {
250 [self setLegalStatus:0]; // clean
251 int insuranceIndex = gen_rnd_number() & 0x03;
252 if (insuranceIndex == 3)
253 [self setInsuranceCredits:500];
254 specialSetUpDone = YES;
255 } else if ([role hasPrefix:@"police"]) {
256 [self setLegalStatus:0]; // clean
257 [self setInsuranceCredits:125];
258 specialSetUpDone = YES;
259 } else if ([role isEqual:@"miner"]) {
260 [self setLegalStatus:0]; // clean
261 [self setInsuranceCredits:25];
262 specialSetUpDone = YES;
263 } else if ([role isEqual:@"passenger"]) {
264 [self setLegalStatus:0]; // clean
265 int insuranceIndex = gen_rnd_number() & 0x03;
266 switch (insuranceIndex) {
267 case 0:
268 [self setInsuranceCredits:25];
269 break;
270 case 1:
271 [self setInsuranceCredits:125];
272 break;
273 case 2:
274 [self setInsuranceCredits:250];
275 break;
276 case 3:
277 [self setInsuranceCredits:500];
278 }
279 specialSetUpDone = YES;
280 } else if ([role isEqual:@"slave"]) {
281 [self setLegalStatus:0]; // clean
282 [self setInsuranceCredits:0];
283 specialSetUpDone = YES;
284 } else if ([role isEqual:@"thargoid"]) {
285 [self setLegalStatus:100];
286 [self setInsuranceCredits:0];
287 [self setName:DESC(@"character-thargoid-name")];
288 [self setShortDescription:DESC(@"character-a-thargoid")];
289 specialSetUpDone = YES;
290 }
291
292 // do long description here
293
294 return specialSetUpDone;
295}
296
297- (NSString*)name
298{
299 return _name;
300}
301
303{
304 return _shortDescription;
305}
306
308{
309 return _genSeed;
310}
311
313{
314 return _legalStatus;
315}
316
321
322- (NSArray*)legacyScript
323{
324 return _scriptActions;
325}
326
327- (NSDictionary*)infoForScripting
328{
329 return [NSDictionary dictionaryWithObjectsAndKeys:
330 [self name], @"name",
331 [self shortDescription], @"description",
332 [self species], @"species",
333 [NSNumber numberWithInt:[self legalStatus]], @"legalStatus",
334 [NSNumber numberWithUnsignedLongLong:[self insuranceCredits]], @"insuranceCredits",
335 [NSNumber numberWithInt:[self planetIDOfOrigin]], @"homeSystem",
336 nil];
337}
338
339- (void)setName:(NSString*)value
340{
341 [_name autorelease];
342 _name = [value copy];
343}
344
345- (void)setShortDescription:(NSString*)value
346{
347 [_shortDescription autorelease];
348 _shortDescription = [value copy];
349}
350
351- (void)setOriginSystem:(OOSystemID)value
352{
353 _originSystem = value;
354}
355
356- (void)setGenSeed:(Random_Seed)value
357{
358 _genSeed = value;
359}
360
361- (void)setLegalStatus:(int)value
362{
363 _legalStatus = value;
364}
365
366- (void)setInsuranceCredits:(OOCreditsQuantity)value
367{
368 _insuranceCredits = value;
369}
370
371- (void)setLegacyScript:(NSArray*)some_actions
372{
373 [_scriptActions autorelease];
375}
376
378{
379 return _script;
380}
381
382- (void)setCharacterScript:(NSString*)scriptName
383{
384 [_script autorelease];
386 properties:[NSDictionary dictionaryWithObject:self forKey:@"character"]];
387 [_script retain];
388}
389
390- (void)doScriptEvent:(OOJSPropID)message
391{
392 JSContext* context = OOJSAcquireContext();
393 [_script callMethod:message inContext:context withArguments:NULL count:0 result:NULL];
394 OOJSRelinquishContext(context);
395}
396
397- (void)setCharacterFromDictionary:(NSDictionary*)dict
398{
399 id origin = nil;
400 Random_Seed seed;
401
402 origin = [dict objectForKey:@"origin"];
403 if ([origin isKindOfClass:[NSNumber class]] || ([origin respondsToSelector:@selector(intValue)] && ([origin intValue] != 0 || [origin isEqual:@"0"]))) {
404 // Number or numerical string
405 [self setOriginSystem:[origin intValue]];
406 } else if ([origin isKindOfClass:[NSString class]]) {
407 OOSystemID sys = [UNIVERSE findSystemFromName:origin];
408 if (sys < 0) {
409 OOLogERR(@"character.load.unknownSystem", @"could not find a system named '%@' in this galaxy.", origin);
410 [self setOriginSystem:(ranrot_rand() & 0xff)];
411 } else {
412 [self setOriginSystem:sys];
413 }
414 } else {
415 // no origin defined, select one at random.
416 [self setOriginSystem:(ranrot_rand() & 0xff)];
417 }
418
419 if ([dict objectForKey:@"random_seed"]) {
420 seed = RandomSeedFromString([dict oo_stringForKey:@"random_seed"]); // returns kNilRandomSeed on failure
421 } else {
422 seed.a = (ranrot_rand() & 0xff);
423 seed.b = (ranrot_rand() & 0xff);
424 seed.c = (ranrot_rand() & 0xff);
425 seed.d = (ranrot_rand() & 0xff);
426 seed.e = (ranrot_rand() & 0xff);
427 seed.f = (ranrot_rand() & 0xff);
428 }
429 [self setGenSeed:seed];
430 [self basicSetUp];
431
432 if ([dict oo_stringForKey:@"role"])
433 [self castInRole:[dict oo_stringForKey:@"role"]];
434 if ([dict oo_stringForKey:@"name"])
435 [self setName:[dict oo_stringForKey:@"name"]];
436 if ([dict oo_stringForKey:@"short_description"])
437 [self setShortDescription:[dict oo_stringForKey:@"short_description"]];
438 if ([dict objectForKey:@"legal_status"])
439 [self setLegalStatus:[dict oo_intForKey:@"legal_status"]];
440 if ([dict objectForKey:@"bounty"])
441 [self setLegalStatus:[dict oo_intForKey:@"bounty"]];
442 if ([dict objectForKey:@"insurance"])
443 [self setInsuranceCredits:[dict oo_unsignedLongLongForKey:@"insurance"]];
444 if ([dict oo_stringForKey:@"script"])
445 [self setCharacterScript:[dict oo_stringForKey:@"script"]];
446 if ([dict oo_arrayForKey:@"script_actions"])
447 [self setLegacyScript:[dict oo_arrayForKey:@"script_actions"]];
448}
449
450@end
#define DESTROY(x)
jsid OOJSPropID
Definition OOJSPropID.h:31
OOINLINE JSContext * OOJSAcquireContext(void)
OOINLINE void OOJSRelinquishContext(JSContext *context)
#define OOLogERR(class, format,...)
Definition OOLogging.h:125
return nil
Random_Seed RandomSeedFromString(NSString *abcdefString)
uint64_t OOCreditsQuantity
Definition OOTypes.h:160
int16_t OOSystemID
Definition OOTypes.h:183
uint8_t OOGovernmentID
Definition OOTypes.h:179
#define UNIVERSE
Definition Universe.h:827
Random_Seed genSeed()
OOJSScript * script()
void setGenSeed:(Random_Seed value)
NSString * _name
Definition OOCharacter.h:36
void basicSetUp()
Random_Seed _genSeed
Definition OOCharacter.h:39
OOSystemID planetIDOfOrigin()
void setCharacterScript:(NSString *scriptName)
Random_Seed genSeed()
OOJSScript * _script
Definition OOCharacter.h:43
void setLegacyScript:(NSArray *scriptActions)
BOOL castInRole:(NSString *role)
OOCreditsQuantity insuranceCredits()
OOCreditsQuantity _insuranceCredits
Definition OOCharacter.h:41
NSString * planetOfOrigin()
OOSystemID _originSystem
Definition OOCharacter.h:38
NSArray * _scriptActions
Definition OOCharacter.h:42
void setName:(NSString *value)
NSString * name()
void setLegalStatus:(int value)
NSString * species()
void setOriginSystem:(OOSystemID value)
void setInsuranceCredits:(OOCreditsQuantity value)
NSDictionary * infoForScripting()
void setCharacterFromDictionary:(NSDictionary *dict)
void setShortDescription:(NSString *value)
NSString * shortDescription()
NSString * oo_jsClassName()
Definition OOCharacter.m:50
NSArray * legacyScript()
NSString * _shortDescription
Definition OOCharacter.h:37
NSString * descriptionComponents()
Definition OOCharacter.m:45
BOOL callMethod:inContext:withArguments:count:result:(jsid methodID,[inContext] JSContext *context,[withArguments] jsval *argv,[count] intN argc,[result] jsval *outResult)
Definition OOJSScript.m:328
id jsScriptFromFileNamed:properties:(NSString *fileName,[properties] NSDictionary *properties)
Definition OOScript.m:177
voidpf uLong int origin
Definition ioapi.h:132
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
void make_pseudo_random_seed(Random_Seed *seed_ptr)
RANROTSeed RANROTGetFullSeed(void)
void seed_RNG_only_for_planet_description(Random_Seed s_seed)
void setRandomSeed(RNG_Seed a_seed)
RNG_Seed currentRandomSeed(void)
void RANROTSetFullSeed(RANROTSeed seed)
int gen_rnd_number(void)
void seed_for_planet_description(Random_Seed s_seed)
unsigned Ranrot(void)
#define ranrot_rand()