218- (id)roleSetWithAddedRoleIfNotSet:(NSString *)role probability:(
float)probability
220 NSMutableDictionary *dict =
nil;
222 if (role ==
nil || probability < 0 || ([
self hasRole:role] && [
self probabilityForRole:role] == probability))
224 return [[
self copy] autorelease];
227 dict = [[_rolesAndProbabilities mutableCopy] autorelease];
228 [
dict setObject:[
NSNumber numberWithFloat:probability] forKey:role];
229 return [[[[
self class] alloc] initWithRolesAndProbabilities:dict] autorelease];
233- (id)roleSetWithAddedRole:(NSString *)role probability:(
float)probability
235 NSMutableDictionary *dict =
nil;
237 if (role ==
nil || probability < 0 || [
self hasRole:role])
239 return [[
self copy] autorelease];
242 dict = [[_rolesAndProbabilities mutableCopy] autorelease];
243 [
dict setObject:[
NSNumber numberWithFloat:probability] forKey:role];
244 return [[[[
self class] alloc] initWithRolesAndProbabilities:dict] autorelease];
248- (id)roleSetWithRemovedRole:(NSString *)role
250 NSMutableDictionary *dict =
nil;
252 if (![
self hasRole:role])
return [[
self copy] autorelease];
254 dict = [[_rolesAndProbabilities mutableCopy] autorelease];
255 [
dict removeObjectForKey:role];
256 return [[[[
self class] alloc] initWithRolesAndProbabilities:dict] autorelease];
264- (id)initWithRolesAndProbabilities:(NSDictionary *)dict
266 NSEnumerator *roleEnum =
nil;
267 NSString *role =
nil;
277 if (
self ==
nil)
return nil;
283 assert(_roles ==
nil && _roleString ==
nil);
285 NSMutableDictionary *tDict = [[
dict mutableCopy] autorelease];
286 float thargProb = [
dict oo_floatForKey:@"thargon" defaultValue:0.0f];
288 if ( thargProb > 0.0f && [dict objectForKey:
@"EQ_THARGON"] ==
nil)
290 [
tDict setObject:[
NSNumber numberWithFloat:thargProb] forKey:@"EQ_THARGON"];
291 [
tDict removeObjectForKey:@"thargon"];
294 _rolesAndProbabilities = [
tDict copy];
296 for (roleEnum = [dict keyEnumerator]; (role = [
roleEnum nextObject]); )
298 prob = [
dict oo_floatForKey:role defaultValue:-1];
301 OOLog(
@"roleSet.badValue",
@"Attempt to create a role set with negative or non-numerical probability for role %@.", role);
317 NSMutableDictionary *result =
nil;
318 NSArray *tokens =
nil;
320 NSString *role =
nil;
322 NSScanner *scanner =
nil;
325 if (
string ==
nil)
return nil;
328 count = [tokens count];
331 result = [NSMutableDictionary dictionaryWithCapacity:count];
334 for (i = 0; i !=
count; ++i)
336 role = [tokens objectAtIndex:i];
339 if ([role rangeOfString:
@"("].location != NSNotFound)
341 scanner = [[NSScanner alloc] initWithString:role];
342 [scanner scanUpToString:@"(" intoString:&role];
343 [scanner scanString:@"(" intoString:NULL];
344 if (![scanner scanFloat:&probability]) probability = 1.0f;
351 if (0 <= probability && ![role hasPrefix:
@"["])
353 [result setObject:[NSNumber numberWithFloat:probability] forKey:role];
357 if ([result
count] == 0) result =
nil;