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
|
/* Gridlock
Copyright (c) 2002-2003 by Brian Nenninger. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#import "GameConfiguration.h"
#import "GenericAI.h"
#import "Preferences.h"
static NSArray *gGameList = nil;
@implementation NSDictionary(GameConfiguration_DisplayName)
-(NSString *)displayName_ {
NSString *result = [self objectForKey:@"displayName"];
if (result) return result;
return [self objectForKey:@"name"];
}
@end
@implementation GameConfiguration
+(NSArray *)gameList {
if (!gGameList) {
gGameList = [[[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Games" ofType:@"plist"]] sortedArrayUsingKey_:@"name"] retain];
}
return gGameList;
}
+(NSDictionary *)dictionaryForGameWithName:(NSString *)name {
return [[self gameList] objectWithValue:name
forSelector:@selector(objectForKey:)
withArgument:@"name"];
}
-(NSDictionary *)gameDictionary {
return [[self class] dictionaryForGameWithName:[self gameName]];
}
-(NSDictionary *)dictionaryForVariation {
NSArray *variations = [[self gameDictionary] objectForKey:@"variations"];
return [variations objectWithValue:[self variationName]
forSelector:@selector(objectForKey:)
withArgument:@"name"];
}
idAccessor(initialGame, setInitialGame)
-(id)initWithName:(NSString *)name variation:(NSString *)variation {
NSArray *varNames;
self = [super init];
gameName = [name copy];
varNames = [self allVariationNames];
if (!variation) {
// look for default
variation = [[Preferences sharedInstance] configurationForGame:gameName];
}
if (![varNames containsObject:variation]) {
variation = nil;
}
if (!variation) {
// if not specified and no default, or if nonexistent, use first
if ([varNames count]>0) variation = [varNames objectAtIndex:0];
}
variationName = [variation copy];
[self createNewInitialGame];
return self;
}
/*
+(GameConfiguration *)configurationWithDisplayName:(NSString *)name
variationDisplayName:(NSString *)variation {
}
*/
+(GameConfiguration *)configurationFromDefaults {
NSString *initialGameName = [[Preferences sharedInstance] initialGame];
NSDictionary *gameDict;
NSString *varname = nil;
if (!initialGameName) initialGameName = [[[self gameList] objectAtIndex:0] objectForKey:@"name"];
gameDict = [self dictionaryForGameWithName:initialGameName];
if (!gameDict) {
// invalid game name, default to first
initialGameName = [[[self gameList] objectAtIndex:0] objectForKey:@"name"];
gameDict = [self dictionaryForGameWithName:initialGameName];
}
// see if game has variations, if so read from prefs or default to first
if ([[gameDict objectForKey:@"variations"] count]>0) {
varname = [[Preferences sharedInstance] configurationForGame:initialGameName];
if (nil==varname || ![[[gameDict objectForKey:@"variations"] valuesByObjectsPerformingSelector:@selector(objectForKey:) withObject_:@"name"] containsObject:varname]) {
// variation missing or invalid, use first
varname = [[[gameDict objectForKey:@"variations"] objectAtIndex:0] objectForKey:@"name"];
}
}
return [[[self alloc] initWithName:initialGameName variation:varname] autorelease];
}
-(void)saveToDefaults {
[[Preferences sharedInstance] setInitialGame:[self gameName]];
[[Preferences sharedInstance] setConfiguration:[self variationName] forGame:[self gameName]];
}
-(void)dealloc {
[gameName release];
[variationName release];
[self setInitialGame:nil];
[super dealloc];
}
-(NSString *)gameName {
return gameName;
}
-(NSString *)variationName {
return variationName;
}
// FIXME
-(NSString *)gameDisplayName {
return gameName;
}
-(NSString *)variationDisplayName {
return variationName;
}
-(Class)gameClass {
NSString *className = [[self gameDictionary] objectForKey:@"class"];
return NSClassFromString(className);
}
-(Game *)gameInStartingPosition {
Game *game = [[[[self gameClass] alloc] init] autorelease];
[game setConfigurationInfo:[[self dictionaryForVariation] objectForKey:@"params"]];
[game reset];
return game;
}
-(void)createNewInitialGame {
[self setInitialGame:[self gameInStartingPosition]];
}
-(id)aiWithName:(NSString *)aiName {
NSDictionary *dict = [[[self gameDictionary] objectForKey:@"aiTypes"] objectWithValue:aiName
forSelector:@selector(objectForKey:)
withArgument:@"name"];
NSString *aiClassName = [dict objectForKey:@"class"];
NSDictionary *aiParams = [dict objectForKey:@"params"];
GenericAI *ai = (aiClassName) ? [[[NSClassFromString(aiClassName) alloc] init] autorelease] : nil;
if (aiParams) {
[ai takeValuesFromDictionary:aiParams];
[ai setName:aiName];
}
return ai;
}
-(id)viewDelegate {
NSString *className = [[self gameDictionary] objectForKey:@"viewDelegate"];
if (className) {
id delegate = [[[NSClassFromString(className) alloc] init] autorelease];
if ([[self gameDictionary] objectForKey:@"viewDelegateParams"]) {
[delegate takeValuesFromDictionary:[[self gameDictionary] objectForKey:@"viewDelegateParams"]];
}
return delegate;
}
return nil;
}
+(NSArray *)allGameDisplayNames {
return [[self gameList] valuesByObjectsPerformingSelector:@selector(displayName_)];
}
+(NSArray *)allGameNames {
return [[self gameList] valuesByObjectsPerformingSelector:@selector(objectForKey:) withObject_:@"name"];
}
-(NSArray *)allVariationDisplayNames {
NSArray *variations = [[self gameDictionary] objectForKey:@"variations"];
return [variations valuesByObjectsPerformingSelector:@selector(displayName_)];
}
-(NSArray *)allVariationNames {
NSArray *variations = [[self gameDictionary] objectForKey:@"variations"];
return [variations valuesByObjectsPerformingSelector:@selector(objectForKey:) withObject_:@"name"];
}
-(NSArray *)allAINames {
NSArray *aiTypes = [[self gameDictionary] objectForKey:@"aiTypes"];
return [aiTypes valuesByObjectsPerformingSelector:@selector(objectForKey:) withObject_:@"name"];
}
-(NSArray *)allAIDisplayNames {
NSArray *aiTypes = [[self gameDictionary] objectForKey:@"aiTypes"];
return [aiTypes valuesByObjectsPerformingSelector:@selector(displayName_)];
}
-(id)propertyList {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:[self gameName] forKey:@"game"];
if ([self variationName]) [dict setObject:[self variationName] forKey:@"variation"];
if ([self initialGame]) [dict setObject:[[self initialGame] propertyList] forKey:@"initialGame"];
return dict;
}
-(id)initFromPropertyList:(id)plist {
[self initWithName:[plist objectForKey:@"game"] variation:[plist objectForKey:@"variation"]];
if ([plist objectForKey:@"initialGame"]) [[self initialGame] updateFromPropertyList:[plist objectForKey:@"initialGame"]];
return self;
}
@end
|