cocoaTouch/GameSetup.m
changeset 2693 3207e0eacd43
child 2694 dcd248e04f3d
equal deleted inserted replaced
2692:ce9992075118 2693:3207e0eacd43
       
     1 //
       
     2 //  gameSetup.m
       
     3 //  hwengine
       
     4 //
       
     5 //  Created by Vittorio on 10/01/10.
       
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
       
     7 //
       
     8 
       
     9 #import "GameSetup.h"
       
    10 #import "SDL_uikitappdelegate.h"
       
    11 #import "SDL_net.h"
       
    12 #import "PascalImports.h"
       
    13 
       
    14 #define IPC_PORT 51342
       
    15 #define IPC_PORT_STR "51342"
       
    16 #define BUFFER_SIZE 256
       
    17 
       
    18 
       
    19 // they should go in the interface
       
    20 TCPsocket sd, csd; /* Socket descriptor, Client socket descriptor */
       
    21 int sendToEngine (NSString * string) {
       
    22 	Uint8 length = [string length];
       
    23 	
       
    24 	SDLNet_TCP_Send(csd, &length , 1);
       
    25 	return SDLNet_TCP_Send(csd, [string UTF8String], length);
       
    26 }
       
    27 
       
    28 
       
    29 @implementation GameSetup
       
    30 
       
    31 @synthesize locale, engineProtocolStarted;
       
    32 
       
    33 -(id) init {
       
    34 	self = [super init];
       
    35 	self.locale = [NSLocale currentLocale];
       
    36 	self.engineProtocolStarted = NO;
       
    37 	return self;
       
    38 }
       
    39 
       
    40 -(void) startThread: (NSString *) selector {
       
    41 	SEL usage = NSSelectorFromString(selector);
       
    42 	
       
    43 	// do not start the server thread because the port is already bound
       
    44 	if (NO == engineProtocolStarted) {
       
    45 		engineProtocolStarted = YES;
       
    46 		[NSThread detachNewThreadSelector:usage toTarget:self withObject:nil];
       
    47 	}
       
    48 }
       
    49 
       
    50 -(void) engineProtocol {
       
    51 	IPaddress ip;
       
    52 	int idx, eProto;
       
    53 	BOOL serverQuit, clientQuit;
       
    54 	char buffer[BUFFER_SIZE], string[BUFFER_SIZE];
       
    55 	Uint8 msgSize;
       
    56 	Uint16 gameTicks;
       
    57 	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
       
    58 	
       
    59 	if (SDLNet_Init() < 0) {
       
    60 		fprintf(stderr, "SDLNet_Init: %s\n", SDLNet_GetError());
       
    61 		exit(EXIT_FAILURE);
       
    62 	}
       
    63 	
       
    64 	/* Resolving the host using NULL make network interface to listen */
       
    65 	if (SDLNet_ResolveHost(&ip, NULL, IPC_PORT) < 0) {
       
    66 		fprintf(stderr, "SDLNet_ResolveHost: %s\n", SDLNet_GetError());
       
    67 		exit(EXIT_FAILURE);
       
    68 	}
       
    69 	
       
    70 	/* Open a connection with the IP provided (listen on the host's port) */
       
    71 	if (!(sd = SDLNet_TCP_Open(&ip))) {
       
    72 		fprintf(stderr, "SDLNet_TCP_Open: %s\n", SDLNet_GetError());
       
    73 		exit(EXIT_FAILURE);
       
    74 	}
       
    75 	
       
    76 	NSLog(@"engineProtocol - Waiting for a client");
       
    77 	
       
    78 	serverQuit = NO;
       
    79 	while (!serverQuit) {
       
    80 		
       
    81 		/* This check the sd if there is a pending connection.
       
    82 		 * If there is one, accept that, and open a new socket for communicating */
       
    83 		if ((csd = SDLNet_TCP_Accept(sd))) {
       
    84 			
       
    85 			NSLog(@"engineProtocol - Client found");
       
    86 			
       
    87 			//first byte of the command alwayas contain the size of the command
       
    88 			SDLNet_TCP_Recv(csd, &msgSize, sizeof(Uint8));
       
    89 			
       
    90 			SDLNet_TCP_Recv(csd, buffer, msgSize);
       
    91 			gameTicks = SDLNet_Read16(&buffer[msgSize - 2]);
       
    92 			//NSLog(@"engineProtocol - %d: received [%s]", gameTicks, buffer);
       
    93 			
       
    94 			if ('C' == buffer[0]) {
       
    95 				NSLog(@"engineProtocol - sending game config");
       
    96 				
       
    97 				// send config data data
       
    98 				
       
    99 				// local game
       
   100 				sendToEngine(@"TL");
       
   101 				
       
   102 				// seed info
       
   103 				sendToEngine(@"eseed {232c1b42-7d39-4ee6-adf8-4240e1f1efb8}");
       
   104 				
       
   105 				// various flags
       
   106 				sendToEngine(@"e$gmflags 256"); 
       
   107 
       
   108 				// various flags
       
   109 				sendToEngine(@"e$damagepct 100");
       
   110 				
       
   111 				// various flags
       
   112 				sendToEngine(@"e$turntime 45000");
       
   113 				
       
   114 				// various flags
       
   115 				sendToEngine(@"e$minestime 3000");
       
   116 				
       
   117 				// various flags
       
   118 				sendToEngine(@"e$landadds 4");
       
   119 				
       
   120 				// various flags
       
   121 				sendToEngine(@"e$sd_turns 15");
       
   122 												
       
   123 				// various flags
       
   124 				sendToEngine(@"e$casefreq 5");
       
   125 				
       
   126 				// various flags
       
   127 				sendToEngine(@"e$template_filter 1");
       
   128 								
       
   129 				// theme info
       
   130 				sendToEngine(@"etheme Freeway");
       
   131 				
       
   132 				// team 1 info
       
   133 				sendToEngine(@"eaddteam 4421353 System Cats");
       
   134 				
       
   135 				// team 1 grave info
       
   136 				sendToEngine(@"egrave star");
       
   137 				
       
   138 				// team 1 fort info
       
   139 				sendToEngine(@"efort  Earth");
       
   140 								
       
   141 				// team 1 voicepack info
       
   142 				sendToEngine(@"evoicepack Classic");
       
   143 				
       
   144 				// team 1 binds (skipped)				
       
   145 				// team 1 members info
       
   146 				sendToEngine(@"eaddhh 0 100 Snow Leopard");
       
   147 				sendToEngine(@"ehat NoHat");
       
   148 				
       
   149 				// team 1 ammostore
       
   150 				sendToEngine(@"eammstore 93919294221991210322351110012010000002110404000441400444645644444774776112211144");
       
   151 
       
   152 				// team 2 info
       
   153 				sendToEngine(@"eaddteam 4100897 Poke-MAN");
       
   154 				
       
   155 				// team 2 grave info
       
   156 				sendToEngine(@"egrave Badger");
       
   157 				
       
   158 				// team 2 fort info
       
   159 				sendToEngine(@"efort UFO");
       
   160 				
       
   161 				// team 2 voicepack info
       
   162 				sendToEngine(@"evoicepack Classic");
       
   163 				
       
   164 				// team 2 binds (skipped)
       
   165 				// team 2 members info
       
   166 				sendToEngine(@"eaddhh 0 100 Raichu");
       
   167 				sendToEngine(@"ehat Bunny");
       
   168 				
       
   169 				// team 2 ammostore
       
   170 				sendToEngine(@"eammstore 93919294221991210322351110012010000002110404000441400444645644444774776112211144");
       
   171 				
       
   172 				clientQuit = NO;
       
   173 			} else {
       
   174 				NSLog(@"engineProtocolThread - wrong message, closing connection");
       
   175 				clientQuit = YES;
       
   176 			}
       
   177 			
       
   178 			while (!clientQuit){
       
   179 				/* Now we can communicate with the client using csd socket
       
   180 				 * sd will remain opened waiting other connections */
       
   181 				idx = 0;
       
   182 				msgSize = 0;
       
   183 				memset(buffer, 0, BUFFER_SIZE);
       
   184 				memset(string, 0, BUFFER_SIZE);
       
   185 				if (SDLNet_TCP_Recv(csd, &msgSize, sizeof(Uint8)) <= 0)
       
   186 					clientQuit = YES;
       
   187 				if (SDLNet_TCP_Recv(csd, buffer, msgSize) <=0)
       
   188 					clientQuit = YES;
       
   189 				
       
   190 				gameTicks = SDLNet_Read16(&buffer[msgSize - 2]);
       
   191 				//NSLog(@"engineProtocolThread - %d: received [%s]", gameTicks, buffer);
       
   192 				
       
   193 				switch (buffer[0]) {
       
   194 					case '?':
       
   195 						NSLog(@"Ping? Pong!");
       
   196 						sendToEngine(@"!");
       
   197 						break;
       
   198 					case 'E':
       
   199 						NSLog(@"ERROR - last console line: [%s]", buffer);
       
   200 						clientQuit = YES;
       
   201 						break;
       
   202 					case 'e':
       
   203 						sscanf(buffer, "%*s %d", &eProto);
       
   204 						if (HW_protoVer() == eProto) {
       
   205 							NSLog(@"Setting protocol version %s", buffer);
       
   206 						} else {
       
   207 							NSLog(@"ERROR - wrong protocol number: [%s] - expecting %d", buffer, eProto);
       
   208 							clientQuit = YES;
       
   209 						}
       
   210 						break;
       
   211 					case 'i':
       
   212 						switch (buffer[1]) {
       
   213 							case 'r':
       
   214 								NSLog(@"Winning team: %s", &buffer[2]);
       
   215 								break;
       
   216 							case 'k':
       
   217 								NSLog(@"Best Hedgehog: %s", &buffer[2]);
       
   218 								break;
       
   219 						}
       
   220 						break;
       
   221 					default:
       
   222 						// empty packet or just statistics
       
   223 						break;
       
   224 					// missing case for exiting right away
       
   225 				} 
       
   226 			}
       
   227 		}
       
   228 		
       
   229 		/* Close the client socket */
       
   230 		SDLNet_TCP_Close(csd);
       
   231 	}
       
   232 
       
   233 	SDLNet_TCP_Close(sd);
       
   234 	SDLNet_Quit();
       
   235 
       
   236 	[pool release];
       
   237 	[NSThread exit];
       
   238 }
       
   239 
       
   240 -(void) setArgsForLocalPlay {
       
   241 	NSString *localeString = [[self.locale localeIdentifier] stringByAppendingString:@".txt"];
       
   242 	NSLog(localeString);
       
   243 	
       
   244 	memset(forward_argv, 0, forward_argc);
       
   245 	
       
   246 	forward_argc = 18;
       
   247 	forward_argv = (char **)realloc(forward_argv, forward_argc * sizeof(char *));
       
   248 	//forward_argv[i] = malloc( (strlen(argv[i])+1) * sizeof(char));
       
   249 	forward_argv[ 1] = forward_argv[0];	// (UNUSED)
       
   250 	forward_argv[ 2] = "320";			// cScreenWidth (NO EFFECT)
       
   251 	forward_argv[ 3] = "480";			// cScreenHeight (NO EFFECT)
       
   252 	forward_argv[ 4] = "32";			// cBitsStr
       
   253 	forward_argv[ 5] = IPC_PORT_STR;	// ipcPort;
       
   254 	forward_argv[ 6] = "1";				// cFullScreen (NO EFFECT)
       
   255 	forward_argv[ 7] = "0";				// isSoundEnabled (TOSET)
       
   256 	forward_argv[ 8] = "1";				// cVSyncInUse (UNUSED)
       
   257 	forward_argv[ 9] = [localeString UTF8String];		// cLocaleFName
       
   258 	forward_argv[10] = "100";			// cInitVolume (TOSET)
       
   259 	forward_argv[11] = "8";				// cTimerInterval
       
   260 	forward_argv[12] = "Data";			// PathPrefix
       
   261 	forward_argv[13] = "1";				// cShowFPS (TOSET?)
       
   262 	forward_argv[14] = "0";				// cAltDamage (TOSET)
       
   263 	forward_argv[15] = "Koda";			// UserNick (DecodeBase64(ParamStr(15)) FTW) <- TODO
       
   264 	forward_argv[16] = "0";				// isMusicEnabled (TOSET)
       
   265 	forward_argv[17] = "0";				// cReducedQuality
       
   266 
       
   267 fprintf(stderr, forward_argv[9]);
       
   268 	return;
       
   269 }
       
   270 
       
   271 
       
   272 /*
       
   273  -(void) dealloc {
       
   274 	[super dealloc];
       
   275 }
       
   276  */
       
   277 
       
   278 
       
   279 @end