Oolite
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 34 of file OORoleSet.h.

Method Documentation

◆ anyRole

- (NSString *) anyRole

Definition at line 173 of file OORoleSet.m.

174{
175 NSString* role = nil;
176 float prob, selected;
177
178 selected = randf() * _totalProb;
179 prob = 0.0f;
180
181 if ([_rolesAndProbabilities count] == 0)
182 return nil;
183
185 {
186 prob += [_rolesAndProbabilities oo_floatForKey:role];
187 if (selected <= prob)
188 break;
189 }
190 if (role == nil) {
191 role = [[self roles] anyObject];
192 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);
193 }
194 return role;
195}
#define foreachkey(VAR, DICT)
#define OOLog(class, format,...)
Definition OOLogging.h:84
unsigned count
return nil
float _totalProb
Definition OORoleSet.h:39
NSDictionary * _rolesAndProbabilities
Definition OORoleSet.h:37
float randf(void)

References _rolesAndProbabilities, _totalProb, anyRole, count, foreachkey, nil, OOLog, randf(), and roles.

Referenced by anyRole.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ copyWithZone:

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

Definition at line 97 of file OORoleSet.m.

97 :(NSZone*)zone
98{
99 // Note: since object is immutable, a copy is no different from the original.
100 return [self retain];
101}

◆ dealloc

- (void) dealloc
implementation

Definition at line 70 of file OORoleSet.m.

71{
72 [_roleString autorelease];
73 [_rolesAndProbabilities autorelease];
74 [_roles autorelease];
75
76 [super dealloc];
77}

References dealloc.

Referenced by dealloc.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ description

- (NSString *) description
implementation

Definition at line 79 of file OORoleSet.m.

80{
81 return [NSString stringWithFormat:@"<%@ %p>{%@}", [self class], self, [self roleString]];
82}

References description, and roleString.

Referenced by description.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hash

- (NSUInteger) hash
implementation

Definition at line 92 of file OORoleSet.m.

93{
94 return [_rolesAndProbabilities hash];
95}

References hash.

Referenced by hash.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hasRole:

- (BOOL) hasRole: (NSString*) role

Definition at line 135 of file OORoleSet.m.

135 :(NSString*)role
136{
137 return role != nil && [_rolesAndProbabilities objectForKey:role] != nil;
138}

References nil.

Referenced by ShipEntity::hasRole:, and ShipEntity::isBoulder.

Here is the caller graph for this function:

◆ initWithRole:probability:

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

Definition at line 60 of file OORoleSet.m.

60 :(NSString*)role probability:(float)probability
61{
62 NSDictionary* dict = nil;
63
64 if (role != nil && 0 <= probability) {
65 dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:probability] forKey:role];
66 }
67 return [self initWithRolesAndProbabilities:dict];
68}

References initWithRolesAndProbabilities:, and nil.

Here is the call graph for this function:

◆ initWithRolesAndProbabilities:

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

Provided by category OORoleSet(OOPrivate).

Definition at line 239 of file OORoleSet.m.

239 :(NSDictionary*)dict
240{
241 NSString* role = nil;
242 float prob;
243
244 if (dict == nil) {
245 [self release];
246 return nil;
247 }
248
249 self = [super init];
250 if (self == nil)
251 return nil;
252
253 // Note: _roles and _roleString are derived on the fly as needed.
254 // MKW 20090815 - if we are re-initialising this OORoleSet object, we need
255 // to ensure that _roles and _roleString are cleared.
256 // Why would we be re-initing? That's never valid. -- Ahruman 2010-02-06
257 assert(_roles == nil && _roleString == nil);
258
259 NSMutableDictionary* tDict = [[dict mutableCopy] autorelease];
260 float thargProb = [dict oo_floatForKey:@"thargon" defaultValue:0.0f];
261
262 if (thargProb > 0.0f && [dict objectForKey:@"EQ_THARGON"] == nil) {
263 [tDict setObject:[NSNumber numberWithFloat:thargProb] forKey:@"EQ_THARGON"];
264 [tDict removeObjectForKey:@"thargon"];
265 }
266
267 _rolesAndProbabilities = [tDict copy];
268
269 foreachkey(role, dict)
270 {
271 prob = [dict oo_floatForKey:role defaultValue:-1];
272 if (prob < 0) {
273 OOLog(@"roleSet.badValue", @"Attempt to create a role set with negative or non-numerical probability for role %@.", role);
274 [self release];
275 return nil;
276 }
277
278 _totalProb += prob;
279 }
280
281 return self;
282}
NSString * _roleString
Definition OORoleSet.h:36
NSSet * _roles
Definition OORoleSet.h:38

Referenced by initWithRole:probability:, and initWithRoleString:.

Here is the caller graph for this function:

◆ initWithRoleString:

- (id) initWithRoleString: (NSString*) roleString

Definition at line 52 of file OORoleSet.m.

52 :(NSString*)roleString
53{
54 NSDictionary* dict = nil;
55
57 return [self initWithRolesAndProbabilities:dict];
58}
NSDictionary * OOParseRolesFromString(NSString *string)
Definition OORoleSet.m:286
NSString * roleString()
Definition OORoleSet.m:103

References initWithRolesAndProbabilities:, nil, OOParseRolesFromString(), and roleString.

Here is the call graph for this function:

◆ intersectsSet:

- (BOOL) intersectsSet: (id) set

Definition at line 145 of file OORoleSet.m.

145 :(id)set
146{
147 if ([set isKindOfClass:[OORoleSet class]])
148 set = [set roles];
149 else if (![set isKindOfClass:[NSSet class]])
150 return NO;
151
152 return [[self roles] intersectsSet:set];
153}

References roles.

Referenced by HasRoleInSetPredicate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isEqual:

- (BOOL) isEqual: (id) other
implementation

Definition at line 84 of file OORoleSet.m.

84 :(id)other
85{
86 if ([other isKindOfClass:[OORoleSet class]]) {
87 return [_rolesAndProbabilities isEqual:[other rolesAndProbabilities]];
88 } else
89 return NO;
90}

◆ probabilityForRole:

- (float) probabilityForRole: (NSString*) role

Definition at line 140 of file OORoleSet.m.

140 :(NSString*)role
141{
142 return [_rolesAndProbabilities oo_floatForKey:role defaultValue:0.0f];
143}

Referenced by roleString.

Here is the caller graph for this function:

◆ roles

- (NSSet *) roles

Definition at line 155 of file OORoleSet.m.

156{
157 if (_roles == nil) {
158 _roles = [[NSSet alloc] initWithArray:[_rolesAndProbabilities allKeys]];
159 }
160 return _roles;
161}

References _roles, nil, and roles.

Referenced by anyRole, intersectsSet:, roles, and roleString.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ rolesAndProbabilities

- (NSDictionary *) rolesAndProbabilities

Definition at line 168 of file OORoleSet.m.

169{
171}

References _rolesAndProbabilities, and rolesAndProbabilities.

Referenced by rolesAndProbabilities, ShipGetProperty(), and ShipGetProperty().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ roleSetWithAddedRole:probability:

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

Definition at line 210 of file OORoleSet.m.

210 :(NSString*)role probability:(float)probability
211{
212 NSMutableDictionary* dict = nil;
213
214 if (role == nil || probability < 0 || [self hasRole:role]) {
215 return [[self copy] autorelease];
216 }
217
218 dict = [[_rolesAndProbabilities mutableCopy] autorelease];
219 [dict setObject:[NSNumber numberWithFloat:probability] forKey:role];
220 return [[[[self class] alloc] initWithRolesAndProbabilities:dict] autorelease];
221}

References nil.

Referenced by ShipEntity::addRole:withProbability:.

Here is the caller graph for this function:

◆ roleSetWithAddedRoleIfNotSet:probability:

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

Definition at line 197 of file OORoleSet.m.

197 :(NSString*)role probability:(float)probability
198{
199 NSMutableDictionary* dict = nil;
200
201 if (role == nil || probability < 0 || ([self hasRole:role] && [self probabilityForRole:role] == probability)) {
202 return [[self copy] autorelease];
203 }
204
205 dict = [[_rolesAndProbabilities mutableCopy] autorelease];
206 [dict setObject:[NSNumber numberWithFloat:probability] forKey:role];
207 return [[[[self class] alloc] initWithRolesAndProbabilities:dict] autorelease];
208}

References nil.

◆ roleSetWithRemovedRole:

- (id) roleSetWithRemovedRole: (NSString*) role

Definition at line 223 of file OORoleSet.m.

223 :(NSString*)role
224{
225 NSMutableDictionary* dict = nil;
226
227 if (![self hasRole:role])
228 return [[self copy] autorelease];
229
230 dict = [[_rolesAndProbabilities mutableCopy] autorelease];
231 [dict removeObjectForKey:role];
232 return [[[[self class] alloc] initWithRolesAndProbabilities:dict] autorelease];
233}

References nil.

Referenced by ShipEntity::removeRole:.

Here is the caller graph for this function:

◆ roleSetWithRole:probability:

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

Definition at line 47 of file OORoleSet.m.

47 :(NSString*)role probability:(float)probability
48{
49 return [[[self alloc] initWithRole:role probability:probability] autorelease];
50}

Referenced by ShipEntity::addRole:withProbability:.

Here is the caller graph for this function:

◆ roleSetWithString:

+ (instancetype) roleSetWithString: (NSString*) roleString

Definition at line 42 of file OORoleSet.m.

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

Referenced by ShipEntity::setUpShipFromDictionary:.

Here is the caller graph for this function:

◆ roleString

- (NSString *) roleString

Definition at line 103 of file OORoleSet.m.

104{
105 NSArray* roles = nil;
106 NSString* role = nil;
107 float probability;
108 NSMutableString* result = nil;
109 BOOL first = YES;
110
111 if (_roleString == nil) {
112 // Construct role string. We always do this so that it's in a normalized form.
113 result = [NSMutableString string];
114 roles = [self sortedRoles];
115 foreach (role, roles) {
116 if (!first)
117 [result appendString:@" "];
118 else
119 first = NO;
120
121 [result appendString:role];
122
123 probability = [self probabilityForRole:role];
124 if (probability != 1.0f) {
125 [result appendFormat:@"(%g)", probability];
126 }
127 }
128
129 _roleString = [result copy];
130 }
131
132 return _roleString;
133}
NSSet * roles()
Definition OORoleSet.m:155

References _roleString, nil, probabilityForRole:, roles, roleString, and sortedRoles.

Referenced by description, initWithRoleString:, roleString, and ShipEntity(LoadRestore)::savedShipDictionaryWithContext:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sortedRoles

- (NSArray *) sortedRoles

Definition at line 163 of file OORoleSet.m.

164{
165 return [[_rolesAndProbabilities allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
166}

References sortedRoles.

Referenced by roleString, ShipGetProperty(), ShipGetProperty(), and sortedRoles.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ _roles

- (NSSet*) _roles
private

Definition at line 38 of file OORoleSet.h.

Referenced by roles.

◆ _rolesAndProbabilities

- (NSDictionary*) _rolesAndProbabilities
private

Definition at line 37 of file OORoleSet.h.

Referenced by anyRole, and rolesAndProbabilities.

◆ _roleString

- (NSString*) _roleString
private

Definition at line 36 of file OORoleSet.h.

Referenced by roleString.

◆ _totalProb

- (float) _totalProb
private

Definition at line 39 of file OORoleSet.h.

Referenced by anyRole.


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