project_files/HedgewarsMobile/Classes/GameSetup.m
changeset 3522 156c04c6a3d8
parent 3514 59dbd31e9953
child 3523 6592fbb969da
equal deleted inserted replaced
3520:c3dcb80bdb1d 3522:156c04c6a3d8
   100     [teamData release];
   100     [teamData release];
   101 }
   101 }
   102 
   102 
   103 // unpacks ammostore data from the selected ammo.plist to a sequence of engine commands
   103 // unpacks ammostore data from the selected ammo.plist to a sequence of engine commands
   104 -(void) provideAmmoData:(NSString *)ammostoreName forPlayingTeams:(NSInteger) numberOfTeams {
   104 -(void) provideAmmoData:(NSString *)ammostoreName forPlayingTeams:(NSInteger) numberOfTeams {
   105     
   105     NSString *weaponPath = [[NSString alloc] initWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),ammostoreName];
   106     //NSDictionary *ammoData = [[NSDictionary alloc] initWithContentsOfFile:ammoDataFile];
   106     NSDictionary *ammoData = [[NSDictionary alloc] initWithContentsOfFile:ammoDataFile];
   107     NSDictionary *ammoData = [[NSDictionary alloc] initWithObjectsAndKeys:
   107     [weaponPath release];
   108                               @"9391929422199121032235111001201000000211190911",@"ammostore_initialqt",
       
   109                               @"0405040541600655546554464776576666666155501000",@"ammostore_probability",
       
   110                               @"0000000000000205500000040007004000000000200000",@"ammostore_delay",
       
   111                               @"1311110312111111123114111111111111111211101111",@"ammostore_crate", nil];
       
   112     
       
   113     
   108     
   114     NSString *ammloadt = [[NSString alloc] initWithFormat:@"eammloadt %@", [ammoData objectForKey:@"ammostore_initialqt"]];
   109     NSString *ammloadt = [[NSString alloc] initWithFormat:@"eammloadt %@", [ammoData objectForKey:@"ammostore_initialqt"]];
   115     [self sendToEngine: ammloadt];
   110     [self sendToEngine: ammloadt];
   116     [ammloadt release];
   111     [ammloadt release];
   117     
   112     
   248 	uint16_t gameTicks;
   243 	uint16_t gameTicks;
   249 
   244 
   250     serverQuit = NO;
   245     serverQuit = NO;
   251 
   246 
   252 	if (SDLNet_Init() < 0) {
   247 	if (SDLNet_Init() < 0) {
   253 		NSLog(@"SDLNet_Init: %s", SDLNet_GetError());
   248 		DLog(@"SDLNet_Init: %s", SDLNet_GetError());
   254         serverQuit = YES;
   249         serverQuit = YES;
   255 	}
   250 	}
   256 	
   251 	
   257 	// Resolving the host using NULL make network interface to listen
   252 	// Resolving the host using NULL make network interface to listen
   258 	if (SDLNet_ResolveHost(&ip, NULL, ipcPort) < 0) {
   253 	if (SDLNet_ResolveHost(&ip, NULL, ipcPort) < 0) {
   259 		NSLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError());
   254 		DLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError());
   260         serverQuit = YES;
   255         serverQuit = YES;
   261 	}
   256 	}
   262 	
   257 	
   263 	// Open a connection with the IP provided (listen on the host's port) 
   258 	// Open a connection with the IP provided (listen on the host's port) 
   264 	if (!(sd = SDLNet_TCP_Open(&ip))) {
   259 	if (!(sd = SDLNet_TCP_Open(&ip))) {
   265 		NSLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), ipcPort);
   260 		DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), ipcPort);
   266         serverQuit = YES;
   261         serverQuit = YES;
   267 	}
   262 	}
   268 	
   263 	
   269 	NSLog(@"engineProtocol - Waiting for a client on port %d", ipcPort);
   264 	DLog(@"Waiting for a client on port %d", ipcPort);
   270 	while (!serverQuit) {
   265 	while (!serverQuit) {
   271 		// This check the sd if there is a pending connection.
   266 		// This check the sd if there is a pending connection.
   272         // If there is one, accept that, and open a new socket for communicating
   267         // If there is one, accept that, and open a new socket for communicating
   273 		csd = SDLNet_TCP_Accept(sd);
   268 		csd = SDLNet_TCP_Accept(sd);
   274 		if (NULL != csd) {
   269 		if (NULL != csd) {
   275 			// Now we can communicate with the client using csd socket
   270 			// Now we can communicate with the client using csd socket
   276 			// sd will remain opened waiting other connections
   271 			// sd will remain opened waiting other connections
   277 			NSLog(@"engineProtocol - Client found");
   272 			DLog(@"client found");
   278 			
   273 			
   279 			//first byte of the command alwayas contain the size of the command
   274 			//first byte of the command alwayas contain the size of the command
   280 			SDLNet_TCP_Recv(csd, &msgSize, sizeof(uint8_t));
   275 			SDLNet_TCP_Recv(csd, &msgSize, sizeof(uint8_t));
   281 			
   276 			
   282 			SDLNet_TCP_Recv(csd, buffer, msgSize);
   277 			SDLNet_TCP_Recv(csd, buffer, msgSize);
   283 			gameTicks = SDLNet_Read16 (&buffer[msgSize - 2]);
   278 			gameTicks = SDLNet_Read16 (&buffer[msgSize - 2]);
   284 			//NSLog(@"engineProtocol - %d: received [%s]", gameTicks, buffer);
   279 			//DLog(@"engineProtocol - %d: received [%s]", gameTicks, buffer);
   285 			
   280 			
   286 			if ('C' == buffer[0]) {
   281 			if ('C' == buffer[0]) {
   287 				NSLog(@"engineProtocol - sending game config");
   282 				DLog(@"sending game config");
   288                 
   283                 
   289 				// local game
   284 				// local game
   290 				[self sendToEngine:@"TL"];
   285 				[self sendToEngine:@"TL"];
   291 				
   286 				
   292 				// seed info
   287 				// seed info
   309                                   forHogs:[[teamData objectForKey:@"number"] intValue]
   304                                   forHogs:[[teamData objectForKey:@"number"] intValue]
   310                                withHealth:health
   305                                withHealth:health
   311                                   ofColor:[teamData objectForKey:@"color"]];
   306                                   ofColor:[teamData objectForKey:@"color"]];
   312                 }
   307                 }
   313                 
   308                 
   314                 [self provideAmmoData:nil forPlayingTeams:[teamsConfig count]];
   309                 [self provideAmmoData:@"Default" forPlayingTeams:[teamsConfig count]];
   315                 
   310                 
   316                 clientQuit = NO;
   311                 clientQuit = NO;
   317 			} else {
   312 			} else {
   318 				NSLog(@"engineProtocolThread - wrong message or client closed connection");
   313 				DLog(@"wrong message or client closed connection");
   319 				clientQuit = YES;
   314 				clientQuit = YES;
   320 			}
   315 			}
   321 			
   316 			
   322 			while (!clientQuit){
   317 			while (!clientQuit){
   323 				msgSize = 0;
   318 				msgSize = 0;
   327 					clientQuit = YES;
   322 					clientQuit = YES;
   328 				if (SDLNet_TCP_Recv(csd, buffer, msgSize) <=0)
   323 				if (SDLNet_TCP_Recv(csd, buffer, msgSize) <=0)
   329 					clientQuit = YES;
   324 					clientQuit = YES;
   330 				
   325 				
   331 				gameTicks = SDLNet_Read16(&buffer[msgSize - 2]);
   326 				gameTicks = SDLNet_Read16(&buffer[msgSize - 2]);
   332 				//NSLog(@"engineProtocolThread - %d: received [%s]", gameTicks, buffer);
   327 				//DLog(@"engineProtocolThread - %d: received [%s]", gameTicks, buffer);
   333 				
   328 				
   334 				switch (buffer[0]) {
   329 				switch (buffer[0]) {
   335 					case '?':
   330 					case '?':
   336 						NSLog(@"Ping? Pong!");
   331 						DLog(@"Ping? Pong!");
   337 						[self sendToEngine:@"!"];
   332 						[self sendToEngine:@"!"];
   338 						break;
   333 						break;
   339 					case 'E':
   334 					case 'E':
   340 						NSLog(@"ERROR - last console line: [%s]", buffer);
   335 						DLog(@"ERROR - last console line: [%s]", buffer);
   341 						clientQuit = YES;
   336 						clientQuit = YES;
   342 						break;
   337 						break;
   343 					case 'e':
   338 					case 'e':
   344 						sscanf(buffer, "%*s %d", &eProto);
   339 						sscanf(buffer, "%*s %d", &eProto);
   345 						short int netProto = 0;
   340 						short int netProto = 0;
   346 						char *versionStr;
   341 						char *versionStr;
   347 						
   342 						
   348                         HW_versionInfo(&netProto, &versionStr);
   343                         HW_versionInfo(&netProto, &versionStr);
   349 						if (netProto == eProto) {
   344 						if (netProto == eProto) {
   350 							NSLog(@"Setting protocol version %d (%s)", eProto, versionStr);
   345 							DLog(@"Setting protocol version %d (%s)", eProto, versionStr);
   351 						} else {
   346 						} else {
   352 							NSLog(@"ERROR - wrong protocol number: [%s] - expecting %d", buffer, eProto);
   347 							DLog(@"ERROR - wrong protocol number: [%s] - expecting %d", buffer, eProto);
   353 							clientQuit = YES;
   348 							clientQuit = YES;
   354 						}
   349 						}
   355                         
   350                         
   356 						break;
   351 						break;
   357 					case 'i':
   352 					case 'i':
   358 						switch (buffer[1]) {
   353 						switch (buffer[1]) {
   359 							case 'r':
   354 							case 'r':
   360 								NSLog(@"Winning team: %s", &buffer[2]);
   355 								NSLog(@"Winning team: %s", &buffer[2]);
   361 								break;
   356 								break;
   362 							case 'k':
   357 							case 'k':
   363 								NSLog(@"Best Hedgehog: %s", &buffer[2]);
   358                                 NSLog(@"Best Hedgehog: %s", &buffer[2]);
   364 								break;
   359 								break;
   365 						}
   360 						}
   366 						break;
   361 						break;
   367 					default:
   362 					default:
   368 						// empty packet or just statistics
   363 						// empty packet or just statistics
   369 						break;
   364 						break;
   370 					// missing case for exiting right away
   365 					// missing case for exiting right away
   371 				}
   366 				}
   372 			}
   367 			}
   373 			NSLog(@"Engine exited, closing server");
   368 			DLog(@"Engine exited, closing server");
   374 			// wait a little to let the client close cleanly
   369 			// wait a little to let the client close cleanly
   375 			[NSThread sleepForTimeInterval:2];
   370 			[NSThread sleepForTimeInterval:2];
   376 			// Close the client socket
   371 			// Close the client socket
   377 			SDLNet_TCP_Close(csd);
   372 			SDLNet_TCP_Close(csd);
   378 			serverQuit = YES;
   373 			serverQuit = YES;