Oolite 1.91.0.7644-241112-7f5034b
Loading...
Searching...
No Matches
OORoleSet Class Reference

#include <OORoleSet.h>

+ Inheritance diagram for OORoleSet:
+ Collaboration diagram for OORoleSet:

Instance Methods

(id) - initWithRoleString:
 
(id) - initWithRole:probability:
 
(NSString *) - roleString
 
(BOOL) - hasRole:
 
(float) - probabilityForRole:
 
(BOOL) - intersectsSet:
 
(NSSet *) - roles
 
(NSArray *) - sortedRoles
 
(NSDictionary *) - rolesAndProbabilities
 
(NSString *) - anyRole
 
(id) - roleSetWithAddedRole:probability:
 
(id) - roleSetWithAddedRoleIfNotSet:probability:
 
(id) - roleSetWithRemovedRole:
 
(void) - dealloc [implementation]
 
(NSString *) - description [implementation]
 
(BOOL) - isEqual: [implementation]
 
(NSUInteger) - hash [implementation]
 
(id) - copyWithZone: [implementation]
 
(id) - initWithRolesAndProbabilities: [implementation]
 

Class Methods

(instancetype) + roleSetWithString:
 
(instancetype) + roleSetWithRole:probability:
 

Private Attributes

NSString * _roleString
 
NSDictionary * _rolesAndProbabilities
 
NSSet * _roles
 
float _totalProb
 

Detailed Description

Definition at line 35 of file OORoleSet.h.

Method Documentation

◆ anyRole

- (NSString *) anyRole

Definition at line 315 of file OORoleSet.m.

194{
195 NSEnumerator *roleEnum = nil;
196 NSString *role = nil;
197 float prob, selected;
198
199 selected = randf() * _totalProb;
200 prob = 0.0f;
201
202 if ([_rolesAndProbabilities count] == 0) return nil;
203
204 for (roleEnum = [_rolesAndProbabilities keyEnumerator]; (role = [roleEnum nextObject]); )
205 {
206 prob += [_rolesAndProbabilities oo_floatForKey:role];
207 if (selected <= prob) break;
208 }
209 if (role == nil)
210 {
211 role = [[self roles] anyObject];
212 OOLog(@"roleSet.anyRole.failed", @"Could not get a weighted-random role from role set %@, returning unweighted selection %@. TotalProb: %g, selected: %g, prob at end: %f", self, role, _totalProb, selected, prob);
213 }
214 return role;
215}
#define OOLog(class, format,...)
Definition OOLogging.h:88
unsigned count
return nil
float _totalProb
Definition OORoleSet.h:42
NSDictionary * _rolesAndProbabilities
Definition OORoleSet.h:40
float randf(void)

References count, nil, and ScanTokensFromString().

+ Here is the call graph for this function:

◆ copyWithZone:

- (id) copyWithZone: (NSZone *) zone
implementation

Definition at line 315 of file OORoleSet.m.

108 :(NSZone *)zone
109{
110 // Note: since object is immutable, a copy is no different from the original.
111 return [self retain];
112}

◆ dealloc

- (void) dealloc
implementation

Definition at line 315 of file OORoleSet.m.

77{
78 [_roleString autorelease];
79 [_rolesAndProbabilities autorelease];
80 [_roles autorelease];
81
82 [super dealloc];
83}

◆ description

- (NSString *) description
implementation

Definition at line 315 of file OORoleSet.m.

87{
88 return [NSString stringWithFormat:@"<%@ %p>{%@}", [self class], self, [self roleString]];
89}

◆ hash

- (NSUInteger) hash
implementation

Definition at line 315 of file OORoleSet.m.

103{
104 return [_rolesAndProbabilities hash];
105}

◆ hasRole:

- (BOOL) hasRole: (NSString *) role

Definition at line 315 of file OORoleSet.m.

150 :(NSString *)role
151{
152 return role != nil && [_rolesAndProbabilities objectForKey:role] != nil;
153}

◆ initWithRole:probability:

- (id) initWithRole: (NSString *) role
probability: (float) probability 

Definition at line 315 of file OORoleSet.m.

64 :(NSString *)role probability:(float)probability
65{
66 NSDictionary *dict = nil;
67
68 if (role != nil && 0 <= probability)
69 {
70 dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:probability] forKey:role];
71 }
72 return [self initWithRolesAndProbabilities:dict];
73}

◆ initWithRolesAndProbabilities:

- (id) initWithRolesAndProbabilities: (NSDictionary *) dict
implementation

Provided by category OORoleSet(OOPrivate).

Definition at line 315 of file OORoleSet.m.

264 :(NSDictionary *)dict
265{
266 NSEnumerator *roleEnum = nil;
267 NSString *role = nil;
268 float prob;
269
270 if (dict == nil)
271 {
272 [self release];
273 return nil;
274 }
275
276 self = [super init];
277 if (self == nil) return nil;
278
279 // Note: _roles and _roleString are derived on the fly as needed.
280 // MKW 20090815 - if we are re-initialising this OORoleSet object, we need
281 // to ensure that _roles and _roleString are cleared.
282 // Why would we be re-initing? That's never valid. -- Ahruman 2010-02-06
283 assert(_roles == nil && _roleString == nil);
284
285 NSMutableDictionary *tDict = [[dict mutableCopy] autorelease];
286 float thargProb = [dict oo_floatForKey:@"thargon" defaultValue:0.0f];
287
288 if ( thargProb > 0.0f && [dict objectForKey:@"EQ_THARGON"] == nil)
289 {
290 [tDict setObject:[NSNumber numberWithFloat:thargProb] forKey:@"EQ_THARGON"];
291 [tDict removeObjectForKey:@"thargon"];
292 }
293
294 _rolesAndProbabilities = [tDict copy];
295
296 for (roleEnum = [dict keyEnumerator]; (role = [roleEnum nextObject]); )
297 {
298 prob = [dict oo_floatForKey:role defaultValue:-1];
299 if (prob < 0)
300 {
301 OOLog(@"roleSet.badValue", @"Attempt to create a role set with negative or non-numerical probability for role %@.", role);
302 [self release];
303 return nil;
304 }
305
306 _totalProb += prob;
307 }
308
309 return self;
310}

◆ initWithRoleString:

- (id) initWithRoleString: (NSString *) roleString

Definition at line 315 of file OORoleSet.m.

55 :(NSString *)roleString
56{
57 NSDictionary *dict = nil;
58
60 return [self initWithRolesAndProbabilities:dict];
61}
NSDictionary * OOParseRolesFromString(NSString *string)
NSString * roleString()
Definition OORoleSet.m:115

◆ intersectsSet:

- (BOOL) intersectsSet: (id) set

Definition at line 315 of file OORoleSet.m.

162 :(id)set
163{
164 if ([set isKindOfClass:[OORoleSet class]]) set = [set roles];
165 else if (![set isKindOfClass:[NSSet class]]) return NO;
166
167 return [[self roles] intersectsSet:set];
168}

◆ isEqual:

- (BOOL) isEqual: (id) other
implementation

Definition at line 315 of file OORoleSet.m.

92 :(id)other
93{
94 if ([other isKindOfClass:[OORoleSet class]])
95 {
96 return [_rolesAndProbabilities isEqual:[other rolesAndProbabilities]];
97 }
98 else return NO;
99}

◆ probabilityForRole:

- (float) probabilityForRole: (NSString *) role

Definition at line 315 of file OORoleSet.m.

156 :(NSString *)role
157{
158 return [_rolesAndProbabilities oo_floatForKey:role defaultValue:0.0f];
159}

◆ roles

- (NSSet *) roles

Definition at line 315 of file OORoleSet.m.

172{
173 if (_roles == nil)
174 {
175 _roles = [[NSSet alloc] initWithArray:[_rolesAndProbabilities allKeys]];
176 }
177 return _roles;
178}
NSSet * _roles
Definition OORoleSet.h:41

◆ rolesAndProbabilities

- (NSDictionary *) rolesAndProbabilities

Definition at line 315 of file OORoleSet.m.

188{
190}

Referenced by ShipGetProperty().

+ Here is the caller graph for this function:

◆ roleSetWithAddedRole:probability:

- (id) roleSetWithAddedRole: (NSString *) role
probability: (float) probability 

Definition at line 315 of file OORoleSet.m.

233 :(NSString *)role probability:(float)probability
234{
235 NSMutableDictionary *dict = nil;
236
237 if (role == nil || probability < 0 || [self hasRole:role])
238 {
239 return [[self copy] autorelease];
240 }
241
242 dict = [[_rolesAndProbabilities mutableCopy] autorelease];
243 [dict setObject:[NSNumber numberWithFloat:probability] forKey:role];
244 return [[[[self class] alloc] initWithRolesAndProbabilities:dict] autorelease];
245}

◆ roleSetWithAddedRoleIfNotSet:probability:

- (id) roleSetWithAddedRoleIfNotSet: (NSString *) role
probability: (float) probability 

Definition at line 315 of file OORoleSet.m.

218 :(NSString *)role probability:(float)probability
219{
220 NSMutableDictionary *dict = nil;
221
222 if (role == nil || probability < 0 || ([self hasRole:role] && [self probabilityForRole:role] == probability))
223 {
224 return [[self copy] autorelease];
225 }
226
227 dict = [[_rolesAndProbabilities mutableCopy] autorelease];
228 [dict setObject:[NSNumber numberWithFloat:probability] forKey:role];
229 return [[[[self class] alloc] initWithRolesAndProbabilities:dict] autorelease];
230}

◆ roleSetWithRemovedRole:

- (id) roleSetWithRemovedRole: (NSString *) role

Definition at line 315 of file OORoleSet.m.

248 :(NSString *)role
249{
250 NSMutableDictionary *dict = nil;
251
252 if (![self hasRole:role]) return [[self copy] autorelease];
253
254 dict = [[_rolesAndProbabilities mutableCopy] autorelease];
255 [dict removeObjectForKey:role];
256 return [[[[self class] alloc] initWithRolesAndProbabilities:dict] autorelease];
257}

◆ roleSetWithRole:probability:

+ (instancetype) roleSetWithRole: (NSString *) role
probability: (float) probability 

Definition at line 315 of file OORoleSet.m.

50 :(NSString *)role probability:(float)probability
51{
52 return [[[self alloc] initWithRole:role probability:probability] autorelease];
53}

◆ roleSetWithString:

+ (instancetype) roleSetWithString: (NSString *) roleString

Definition at line 315 of file OORoleSet.m.

44 :(NSString *)roleString
45{
46 return [[[self alloc] initWithRoleString:roleString] autorelease];
47}

◆ roleString

- (NSString *) roleString

Definition at line 315 of file OORoleSet.m.

116{
117 NSArray *roles = nil;
118 NSEnumerator *roleEnum = nil;
119 NSString *role = nil;
120 float probability;
121 NSMutableString *result = nil;
122 BOOL first = YES;
123
124 if (_roleString == nil)
125 {
126 // Construct role string. We always do this so that it's in a normalized form.
127 result = [NSMutableString string];
128 roles = [self sortedRoles];
129 for (roleEnum = [roles objectEnumerator]; (role = [roleEnum nextObject]); )
130 {
131 if (!first) [result appendString:@" "];
132 else first = NO;
133
134 [result appendString:role];
135
136 probability = [self probabilityForRole:role];
137 if (probability != 1.0f)
138 {
139 [result appendFormat:@"(%g)", probability];
140 }
141 }
142
143 _roleString = [result copy];
144 }
145
146 return _roleString;
147}
NSSet * roles()
Definition OORoleSet.m:171
NSString * _roleString
Definition OORoleSet.h:39

◆ sortedRoles

- (NSArray *) sortedRoles

Definition at line 315 of file OORoleSet.m.

182{
183 return [[_rolesAndProbabilities allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
184}

Referenced by ShipGetProperty().

+ Here is the caller graph for this function:

Member Data Documentation

◆ _roles

- (NSSet*) _roles
private

Definition at line 41 of file OORoleSet.h.

◆ _rolesAndProbabilities

- (NSDictionary*) _rolesAndProbabilities
private

Definition at line 40 of file OORoleSet.h.

◆ _roleString

- (NSString*) _roleString
private

Definition at line 39 of file OORoleSet.h.

◆ _totalProb

- (float) _totalProb
private

Definition at line 42 of file OORoleSet.h.


The documentation for this class was generated from the following files: