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
|
/* 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 "GridlockGameState.h"
#import "Preferences.h"
@implementation GridlockGameState
-(id)init {
[super init];
aiPlayers = [[NSMutableDictionary alloc] init];
networkPlayers = [[NSMutableDictionary alloc] init];
return self;
}
-(void)dealloc {
[aiPlayers release];
aiPlayers = nil;
[networkPlayers release];
networkPlayers = nil;
[super dealloc];
}
-(NSArray *)aiNamesForCurrentGame:(BOOL)firstTime requireHuman:(BOOL)requireHuman {
NSString *gameName = [[self gameConfiguration] gameName];
// read AI types from preferences
NSString *player1AIType = [[Preferences sharedInstance] aiTypeForGame:gameName player:1];
NSString *player2AIType = [[Preferences sharedInstance] aiTypeForGame:gameName player:2];
if (firstTime) {
// if both unset, we will default to the first AI type available for player 2
NSArray *aiTypes = [[self gameConfiguration] allAINames];
if (player1AIType==nil && player2AIType==nil && [aiTypes count]>0) {
player2AIType = [aiTypes objectAtIndex:0];
}
}
if (firstTime || requireHuman) {
// if both set, revert player 1 to human (so we don't start out computer vs computer)
if ([player1AIType length]>0 && [player2AIType length]>0) {
player1AIType = @"";
}
}
if (!player1AIType) player1AIType = @"";
if (!player2AIType) player2AIType = @"";
return [NSArray arrayWithObjects:player1AIType, player2AIType, nil];
}
// called when game is switched to update AIs from NSUserDefaults
-(void)updateAIsForNewGame:(BOOL)firstTime requireHuman:(BOOL)requireHuman {
NSArray *aiNames = [self aiNamesForCurrentGame:firstTime requireHuman:requireHuman];
int i;
for(i=0; i<[aiNames count]; i++) {
[self setAIName:[aiNames objectAtIndex:i] forPlayer:i+1];
}
}
-(GameHistory *)gameHistory {
return gameHistory;
}
-(void)setGameHistory:(GameHistory *)newHistory {
if (newHistory!=gameHistory) {
BOOL isSameGame = [[[newHistory gameConfiguration] gameName] isEqual:[[gameHistory gameConfiguration] gameName]];
id temp = gameHistory;
gameHistory = [newHistory retain];
// twiddle the AIs for the first game (if we're not playing a network game)
if (![self hasNetConnections]) [self updateAIsForNewGame:(temp==nil)
requireHuman:!isSameGame];
[temp release];
}
}
-(GameConfiguration *)gameConfiguration {
return [[self gameHistory] gameConfiguration];
}
-(Game *)currentGame {
return [[self gameHistory] currentGame];
}
-(int)currentPlayerNumber {
return [[self currentGame] currentPlayerNumber];
}
-(BOOL)isPlayerHuman:(int)pnum {
id key = [NSNumber numberWithInt:pnum];
return (nil==[aiPlayers objectForKey:key] && nil==[networkPlayers objectForKey:key]);
}
-(BOOL)isCurrentPlayerHuman {
return [self isPlayerHuman:[self currentPlayerNumber]];
}
-(NSArray *)availableAINames {
return [[self gameConfiguration] allAINames];
}
-(NSArray *)availableAIDisplayNames {
return [[self gameConfiguration] allAIDisplayNames];
}
-(NSString *)aiNameForPlayer:(int)pnum {
NSString *name = [[aiPlayers objectForKey:[NSNumber numberWithInt:pnum]] name];
return (name ? name : @"");
}
-(void)setAIName:(NSString *)aiName forPlayer:(int)pnum {
id key = [NSNumber numberWithInt:pnum];
[aiPlayers removeObjectForKey:key];
[networkPlayers removeObjectForKey:key];
if ([aiName length]>0) {
GenericAI *ai = [[self gameConfiguration] aiWithName:aiName];
if (ai) {
[aiPlayers setObject:ai forKey:key];
}
}
[[Preferences sharedInstance] setAIType:(aiName ? aiName : @"")
forGame:[[self gameConfiguration] gameName]
player:pnum];
}
-(void)makeAIMove {
GenericAI *ai = [aiPlayers objectForKey:[NSNumber numberWithInt:[self currentPlayerNumber]]];
// this move may be executing in a worker thread. If so, we need to protect the AI and game objects because they may be released
// by the main thread, for example if the player is switched to human while the AI is still thinking.
Game *game = [self currentGame];
[[ai retain] autorelease];
[[game retain] autorelease];
[ai computeBestMoveForGame:game];
}
-(void)setNetConnection:(NetConnection *)conn forPlayer:(int)pnum {
id key = [NSNumber numberWithInt:pnum];
[aiPlayers removeObjectForKey:key];
if (conn==nil) {
[networkPlayers removeObjectForKey:key];
}
else {
[networkPlayers setObject:conn forKey:key];
}
}
-(void)makePlayerHuman:(int)pnum {
[self setAIName:nil forPlayer:pnum];
[self setNetConnection:nil forPlayer:pnum];
}
-(void)makeAllPlayersHuman {
[aiPlayers removeAllObjects];
[networkPlayers removeAllObjects];
}
-(int)playerNumberForNetConnection:(NetConnection *)conn {
NSArray *keys = [networkPlayers allKeysForObject:conn];
return ([keys count] ? [[keys lastObject] intValue] : 0);
}
-(NetConnection *)netConnectionForPlayer:(int)pnum {
return [networkPlayers objectForKey:[NSNumber numberWithInt:pnum]];
}
-(BOOL)hasNetConnections {
return [networkPlayers count]>0;
}
-(NSArray *)allNetConnections {
return [networkPlayers allValues];
}
-(id)propertyList {
NSMutableDictionary *plist = [NSMutableDictionary dictionary];
[plist setObject:[[self gameHistory] propertyList] forKey:@"history"];
{
NSMutableDictionary *aiDict = [NSMutableDictionary dictionary];
int i;
for(i=1; i<=[[self currentGame] numberOfPlayers]; i++) {
NSString *aiName = [self aiNameForPlayer:i];
if ([aiName length]>0) {
[aiDict setObject:aiName forKey:[[NSNumber numberWithInt:i] stringValue]];
}
}
[plist setObject:aiDict forKey:@"AIs"];
}
return plist;
}
@end
|