cocoaTouch/GameSetup.m
changeset 3365 37ac593e9027
parent 3364 e5403e2bf02c
child 3369 c7289e42f0ee
equal deleted inserted replaced
3364:e5403e2bf02c 3365:37ac593e9027
    18 #define BUFFER_SIZE 256
    18 #define BUFFER_SIZE 256
    19 #define debug(format, ...) CFShow([NSString stringWithFormat:format, ## __VA_ARGS__]);
    19 #define debug(format, ...) CFShow([NSString stringWithFormat:format, ## __VA_ARGS__]);
    20 
    20 
    21 @implementation GameSetup
    21 @implementation GameSetup
    22 
    22 
    23 @synthesize systemSettings, teamsConfig;
    23 @synthesize systemSettings, gameConfig;
    24 
    24 
    25 -(id) init {
    25 -(id) init {
    26 	if (self = [super init]) {
    26 	if (self = [super init]) {
    27     	srandom(time(NULL));
    27     	srandom(time(NULL));
    28         ipcPort = (random() % 64541) + 1025;
    28         ipcPort = randomPort();
    29         
    29         
    30         self.systemSettings = [[NSDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()]; //should check it exists
    30         NSDictionary *dictSett = [[NSDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()]; //should check it exists
    31         self.teamsConfig = [[NSArray alloc] initWithContentsOfFile:GAMECONFIG_FILE()];
    31         self.systemSettings = dictSett;
       
    32         [dictSett release];
       
    33         
       
    34         NSDictionary *dictGame = [[NSDictionary alloc] initWithContentsOfFile:GAMECONFIG_FILE()];
       
    35         self.gameConfig = dictGame;
       
    36         [dictGame release];
    32     } 
    37     } 
    33     return self;
    38     return self;
    34 }
    39 }
    35 
    40 
    36 -(NSString *)description {
    41 -(NSString *)description {
    37     return [NSString stringWithFormat:@"ipcport: %d\nsockets: %d,%d\n teams: %@\n systemSettings: %@",ipcPort,sd,csd,teamsConfig,systemSettings];
    42     return [NSString stringWithFormat:@"ipcport: %d\nsockets: %d,%d\n teams: %@\n systemSettings: %@",ipcPort,sd,csd,gameConfig,systemSettings];
    38 }
    43 }
    39 
    44 
    40 -(void) dealloc {
    45 -(void) dealloc {
    41     [teamsConfig release];
    46     [gameConfig release];
    42     [systemSettings release];
    47     [systemSettings release];
    43 	[super dealloc];
    48 	[super dealloc];
    44 }
    49 }
    45 
    50 
    46 #pragma mark -
    51 #pragma mark -
   128 	int eProto;
   133 	int eProto;
   129 	BOOL clientQuit, serverQuit;
   134 	BOOL clientQuit, serverQuit;
   130 	char buffer[BUFFER_SIZE], string[BUFFER_SIZE];
   135 	char buffer[BUFFER_SIZE], string[BUFFER_SIZE];
   131 	Uint8 msgSize;
   136 	Uint8 msgSize;
   132 	Uint16 gameTicks;
   137 	Uint16 gameTicks;
   133 	
   138 
       
   139     serverQuit = NO;
       
   140 
   134 	if (SDLNet_Init() < 0) {
   141 	if (SDLNet_Init() < 0) {
   135 		NSLog(@"SDLNet_Init: %s", SDLNet_GetError());
   142 		NSLog(@"SDLNet_Init: %s", SDLNet_GetError());
   136 		exit(EXIT_FAILURE);
   143         serverQuit = YES;
   137 	}
   144 	}
   138 	
   145 	
   139 	/* Resolving the host using NULL make network interface to listen */
   146 	/* Resolving the host using NULL make network interface to listen */
   140 	if (SDLNet_ResolveHost(&ip, NULL, ipcPort) < 0) {
   147 	if (SDLNet_ResolveHost(&ip, NULL, ipcPort) < 0) {
   141 		NSLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError());
   148 		NSLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError());
   142 		exit(EXIT_FAILURE);
   149         serverQuit = YES;
   143 	}
   150 	}
   144 	
   151 	
   145 	/* Open a connection with the IP provided (listen on the host's port) */
   152 	/* Open a connection with the IP provided (listen on the host's port) */
   146 	if (!(sd = SDLNet_TCP_Open(&ip))) {
   153 	if (!(sd = SDLNet_TCP_Open(&ip))) {
   147 		NSLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), ipcPort);
   154 		NSLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), ipcPort);
   148 		exit(EXIT_FAILURE);
   155         serverQuit = YES;
   149 	}
   156 	}
   150 	
   157 	
   151 	NSLog(@"engineProtocol - Waiting for a client on port %d", ipcPort);
   158 	NSLog(@"engineProtocol - Waiting for a client on port %d", ipcPort);
   152 	serverQuit = NO;
       
   153 	while (!serverQuit) {
   159 	while (!serverQuit) {
   154 		
   160 		
   155 		/* This check the sd if there is a pending connection.
   161 		/* This check the sd if there is a pending connection.
   156 		 * If there is one, accept that, and open a new socket for communicating */
   162 		 * If there is one, accept that, and open a new socket for communicating */
   157 		csd = SDLNet_TCP_Accept(sd);
   163 		csd = SDLNet_TCP_Accept(sd);
   173 				/*
   179 				/*
   174 				seed is arbitrary string
   180 				seed is arbitrary string
   175 				addteam <32charsMD5hash> <color> <team name>
   181 				addteam <32charsMD5hash> <color> <team name>
   176 				addhh <level> <health> <hedgehog name>
   182 				addhh <level> <health> <hedgehog name>
   177 				  <level> is 0 for human, 1-5 for bots (5 is the most stupid)
   183 				  <level> is 0 for human, 1-5 for bots (5 is the most stupid)
   178 				ammostore is one byte/number for each ammocount then one for each probability or so
       
   179 				*/
   184 				*/
   180 				// local game
   185 				// local game
   181 				[self sendToEngine:@"TL"];
   186 				[self sendToEngine:@"TL"];
   182 				
   187 				
   183 				// seed info
   188 				// seed info
   184 				[self sendToEngine:@"eseed {232c1b42-7d39-4ee6-adf8-4240e1f1efb8}"];
   189 				[self sendToEngine:[self.gameConfig objectForKey:@"seed_command"]];
   185 				
   190 				
   186 				// various flags
   191 				// various flags
   187 				[self sendToEngine:@"e$gmflags 256"]; 
   192 				[self sendToEngine:@"e$gmflags 256"]; 
   188 				[self sendToEngine:@"e$damagepct 100"];
   193 				[self sendToEngine:@"e$damagepct 100"];
   189 				[self sendToEngine:@"e$turntime 45000"];
   194 				[self sendToEngine:@"e$turntime 45000"];
   196 				[self sendToEngine:@"e$template_filter 1"];
   201 				[self sendToEngine:@"e$template_filter 1"];
   197 								
   202 								
   198 				// theme info
   203 				// theme info
   199 				[self sendToEngine:@"etheme Compost"];
   204 				[self sendToEngine:@"etheme Compost"];
   200 				
   205 				
   201                 for (NSDictionary *teamData in self.teamsConfig) {
   206                 NSArray *teamsConfig = [self.gameConfig objectForKey:@"teams_list"];
       
   207                 for (NSDictionary *teamData in teamsConfig) {
   202                     [self sendTeamData:[teamData objectForKey:@"team"] 
   208                     [self sendTeamData:[teamData objectForKey:@"team"] 
   203                        withPlayingHogs:[[teamData objectForKey:@"number"] intValue]
   209                        withPlayingHogs:[[teamData objectForKey:@"number"] intValue]
   204                                ofColor:[teamData objectForKey:@"color"]];
   210                                ofColor:[teamData objectForKey:@"color"]];
   205                     NSLog(@"teamData sent");
   211                     NSLog(@"teamData sent");
   206                 }
   212                 }
   208                 NSDictionary *ammoData = [[NSDictionary alloc] initWithObjectsAndKeys:
   214                 NSDictionary *ammoData = [[NSDictionary alloc] initWithObjectsAndKeys:
   209                                           @"9391929422199121032235111001201000000211190",@"ammostore_initialqt",
   215                                           @"9391929422199121032235111001201000000211190",@"ammostore_initialqt",
   210                                           @"0405040541600655546554464776576666666155501",@"ammostore_probability",
   216                                           @"0405040541600655546554464776576666666155501",@"ammostore_probability",
   211                                           @"0000000000000205500000040007004000000000200",@"ammostore_delay",
   217                                           @"0000000000000205500000040007004000000000200",@"ammostore_delay",
   212                                           @"1311110312111111123114111111111111111211101",@"ammostore_crate", nil];
   218                                           @"1311110312111111123114111111111111111211101",@"ammostore_crate", nil];
   213                 [self sendAmmoData:ammoData forTeams:[self.teamsConfig count]];
   219                 [self sendAmmoData:ammoData forTeams:[teamsConfig count]];
   214                 [ammoData release];
   220                 [ammoData release];
   215                 
   221                 
   216                 clientQuit = NO;
   222                 clientQuit = NO;
   217 			} else {
   223 			} else {
   218 				NSLog(@"engineProtocolThread - wrong message or client closed connection");
   224 				NSLog(@"engineProtocolThread - wrong message or client closed connection");