cocoaTouch/GameSetup.m
changeset 2696 41aa7b56c17b
parent 2694 dcd248e04f3d
child 2697 75880595a9f1
equal deleted inserted replaced
2695:ed789a7ef68d 2696:41aa7b56c17b
     9 #import "GameSetup.h"
     9 #import "GameSetup.h"
    10 #import "SDL_uikitappdelegate.h"
    10 #import "SDL_uikitappdelegate.h"
    11 #import "SDL_net.h"
    11 #import "SDL_net.h"
    12 #import "PascalImports.h"
    12 #import "PascalImports.h"
    13 
    13 
    14 
       
    15 #define BUFFER_SIZE 256
    14 #define BUFFER_SIZE 256
    16 
       
    17 
       
    18 // they should go in the interface
       
    19 TCPsocket sd, csd; /* Socket descriptor, Client socket descriptor */
       
    20 NSInteger ipcPort;
       
    21 
       
    22 int sendToEngine (NSString * string) {
       
    23 	Uint8 length = [string length];
       
    24 	
       
    25 	SDLNet_TCP_Send(csd, &length , 1);
       
    26 	return SDLNet_TCP_Send(csd, [string UTF8String], length);
       
    27 }
       
    28 
       
    29 
    15 
    30 @implementation GameSetup
    16 @implementation GameSetup
    31 
    17 
    32 @synthesize localeString, systemSettings;
    18 @synthesize localeString, systemSettings;
    33 
    19 
    38 	engineProtocolStarted = NO;
    24 	engineProtocolStarted = NO;
    39 	ipcPort = 51342;
    25 	ipcPort = 51342;
    40 	return self;
    26 	return self;
    41 }
    27 }
    42 
    28 
       
    29 -(void) dealloc {
       
    30 	[self.systemSettings release];
       
    31 	[self.localeString autorelease];
       
    32 	[super dealloc];
       
    33 }
       
    34 
       
    35 #pragma mark -
       
    36 #pragma mark Thread/Network relevant code
    43 -(void) startThread: (NSString *) selector {
    37 -(void) startThread: (NSString *) selector {
    44 	SEL usage = NSSelectorFromString(selector);
    38 	SEL usage = NSSelectorFromString(selector);
    45 	
    39 	
    46 	// do not start the server thread because the port is already bound
    40 	// do not start the server thread because the port is already bound
    47 	if (NO == engineProtocolStarted) {
    41 	if (NO == engineProtocolStarted) {
    48 		engineProtocolStarted = YES;
    42 		engineProtocolStarted = YES;
    49 		[NSThread detachNewThreadSelector:usage toTarget:self withObject:nil];
    43 		[NSThread detachNewThreadSelector:usage toTarget:self withObject:nil];
    50 	}
    44 	}
       
    45 }
       
    46 
       
    47 -(int) sendToEngine: (NSString *)string {
       
    48 	Uint8 length = [string length];
       
    49 	
       
    50 	SDLNet_TCP_Send(csd, &length , 1);
       
    51 	return SDLNet_TCP_Send(csd, [string UTF8String], length);
    51 }
    52 }
    52 
    53 
    53 -(void) engineProtocol {
    54 -(void) engineProtocol {
    54 	IPaddress ip;
    55 	IPaddress ip;
    55 	int idx, eProto;
    56 	int idx, eProto;
    98 				NSLog(@"engineProtocol - sending game config");
    99 				NSLog(@"engineProtocol - sending game config");
    99 				
   100 				
   100 				// send config data data
   101 				// send config data data
   101 				
   102 				
   102 				// local game
   103 				// local game
   103 				sendToEngine(@"TL");
   104 				[self sendToEngine:@"TL"];
   104 				
   105 				
   105 				// seed info
   106 				// seed info
   106 				sendToEngine(@"eseed {232c1b42-7d39-4ee6-adf8-4240e1f1efb8}");
   107 				[self sendToEngine:@"eseed {232c1b42-7d39-4ee6-adf8-4240e1f1efb8}"];
   107 				
   108 				
   108 				// various flags
   109 				// various flags
   109 				sendToEngine(@"e$gmflags 256"); 
   110 				[self sendToEngine:@"e$gmflags 256"]; 
   110 
   111 
   111 				// various flags
   112 				// various flags
   112 				sendToEngine(@"e$damagepct 100");
   113 				[self sendToEngine:@"e$damagepct 100"];
   113 				
   114 				
   114 				// various flags
   115 				// various flags
   115 				sendToEngine(@"e$turntime 45000");
   116 				[self sendToEngine:@"e$turntime 45000"];
   116 				
   117 				
   117 				// various flags
   118 				// various flags
   118 				sendToEngine(@"e$minestime 3000");
   119 				[self sendToEngine:@"e$minestime 3000"];
   119 				
   120 				
   120 				// various flags
   121 				// various flags
   121 				sendToEngine(@"e$landadds 4");
   122 				[self sendToEngine:@"e$landadds 4"];
   122 				
   123 				
   123 				// various flags
   124 				// various flags
   124 				sendToEngine(@"e$sd_turns 15");
   125 				[self sendToEngine:@"e$sd_turns 15"];
   125 												
   126 												
   126 				// various flags
   127 				// various flags
   127 				sendToEngine(@"e$casefreq 5");
   128 				[self sendToEngine:@"e$casefreq 5"];
   128 				
   129 				
   129 				// various flags
   130 				// various flags
   130 				sendToEngine(@"e$template_filter 1");
   131 				[self sendToEngine:@"e$template_filter 1"];
   131 								
   132 								
   132 				// theme info
   133 				// theme info
   133 				sendToEngine(@"etheme Freeway");
   134 				[self sendToEngine:@"etheme Freeway"];
   134 				
   135 				
   135 				// team 1 info
   136 				// team 1 info
   136 				sendToEngine(@"eaddteam 4421353 System Cats");
   137 				[self sendToEngine:@"eaddteam 4421353 System Cats"];
   137 				
   138 				
   138 				// team 1 grave info
   139 				// team 1 grave info
   139 				sendToEngine(@"egrave star");
   140 				[self sendToEngine:@"egrave star"];
   140 				
   141 				
   141 				// team 1 fort info
   142 				// team 1 fort info
   142 				sendToEngine(@"efort  Earth");
   143 				[self sendToEngine:@"efort  Earth"];
   143 								
   144 								
   144 				// team 1 voicepack info
   145 				// team 1 voicepack info
   145 				sendToEngine(@"evoicepack Classic");
   146 				[self sendToEngine:@"evoicepack Classic"];
   146 				
   147 				
   147 				// team 1 binds (skipped)				
   148 				// team 1 binds (skipped)			
   148 				// team 1 members info
   149 				// team 1 members info
   149 				sendToEngine(@"eaddhh 0 100 Snow Leopard");
   150 				[self sendToEngine:@"eaddhh 0 100 Snow Leopard"];
   150 				sendToEngine(@"ehat NoHat");
   151 				[self sendToEngine:@"ehat NoHat"];
   151 				
   152 				
   152 				// team 1 ammostore
   153 				// team 1 ammostore
   153 				sendToEngine(@"eammstore 93919294221991210322351110012010000002110404000441400444645644444774776112211144");
   154 				[self sendToEngine:@"eammstore 93919294221991210322351110012010000002110404000441400444645644444774776112211144"];
   154 
   155 
   155 				// team 2 info
   156 				// team 2 info
   156 				sendToEngine(@"eaddteam 4100897 Poke-MAN");
   157 				[self sendToEngine:@"eaddteam 4100897 Poke-MAN"];
   157 				
   158 				
   158 				// team 2 grave info
   159 				// team 2 grave info
   159 				sendToEngine(@"egrave Badger");
   160 				[self sendToEngine:@"egrave Badger"];
   160 				
   161 				
   161 				// team 2 fort info
   162 				// team 2 fort info
   162 				sendToEngine(@"efort UFO");
   163 				[self sendToEngine:@"efort UFO"];
   163 				
   164 				
   164 				// team 2 voicepack info
   165 				// team 2 voicepack info
   165 				sendToEngine(@"evoicepack Classic");
   166 				[self sendToEngine:@"evoicepack Classic"];
   166 				
   167 				
   167 				// team 2 binds (skipped)
   168 				// team 2 binds (skipped)
   168 				// team 2 members info
   169 				// team 2 members info
   169 				sendToEngine(@"eaddhh 0 100 Raichu");
   170 				[self sendToEngine:@"eaddhh 0 100 Raichu"];
   170 				sendToEngine(@"ehat Bunny");
   171 				[self sendToEngine:@"ehat Bunny"];
   171 				
   172 				
   172 				// team 2 ammostore
   173 				// team 2 ammostore
   173 				sendToEngine(@"eammstore 93919294221991210322351110012010000002110404000441400444645644444774776112211144");
   174 				[self sendToEngine:@"eammstore 93919294221991210322351110012010000002110404000441400444645644444774776112211144"];
   174 				
   175 				
   175 				clientQuit = NO;
   176 				clientQuit = NO;
   176 			} else {
   177 			} else {
   177 				NSLog(@"engineProtocolThread - wrong message, closing connection");
   178 				NSLog(@"engineProtocolThread - wrong message, closing connection");
   178 				clientQuit = YES;
   179 				clientQuit = YES;
   194 				//NSLog(@"engineProtocolThread - %d: received [%s]", gameTicks, buffer);
   195 				//NSLog(@"engineProtocolThread - %d: received [%s]", gameTicks, buffer);
   195 				
   196 				
   196 				switch (buffer[0]) {
   197 				switch (buffer[0]) {
   197 					case '?':
   198 					case '?':
   198 						NSLog(@"Ping? Pong!");
   199 						NSLog(@"Ping? Pong!");
   199 						sendToEngine(@"!");
   200 						[self sendToEngine:@"!"];
   200 						break;
   201 						break;
   201 					case 'E':
   202 					case 'E':
   202 						NSLog(@"ERROR - last console line: [%s]", buffer);
   203 						NSLog(@"ERROR - last console line: [%s]", buffer);
   203 						clientQuit = YES;
   204 						clientQuit = YES;
   204 						break;
   205 						break;
   238 
   239 
   239 	[pool release];
   240 	[pool release];
   240 	[NSThread exit];
   241 	[NSThread exit];
   241 }
   242 }
   242 
   243 
       
   244 #pragma mark -
       
   245 #pragma mark Settings setup methods
       
   246 -(void) loadSettingsFromFile:(NSString *)fileName forKey:(NSString *)objName {
       
   247 	NSString *filePath = [SDLUIKitDelegate dataFilePath:fileName];
       
   248 	
       
   249 	if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {	
       
   250 		NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
       
   251 		[self setValue:dict forKey:objName];
       
   252 		[dict release];
       
   253 	} else {
       
   254 		//TODO create it
       
   255 		[NSException raise:@"File NOT found" format:@"The file %@ was not found at %@", fileName, filePath];
       
   256 	}
       
   257 
       
   258 }
       
   259 
       
   260 -(void) unloadSettings {
       
   261 	for (id obj in self)
       
   262 		if ([obj isKindOfClass:[NSDictionary class]]) {
       
   263 			[obj release];
       
   264 		}
       
   265 }
       
   266 
   243 -(void) setArgsForLocalPlay {
   267 -(void) setArgsForLocalPlay {
   244 	NSString *portNumber = [[NSString alloc] initWithFormat:@"%d",ipcPort];
   268 	NSString *portNumber = [[NSString alloc] initWithFormat:@"%d", ipcPort];
       
   269 	//NSString *username = [[NSString alloc] initWithString:[systemSettings objectForKey:@"username"]];
   245 	/*for (NSString *theString in [NSLocale ISOLanguageCodes]) {
   270 	/*for (NSString *theString in [NSLocale ISOLanguageCodes]) {
   246 		NSLog(theString);
   271 		NSLog(theString);
   247 	}*/
   272 	}*/
   248 	
       
   249 	
   273 	
   250 	memset(forward_argv, 0, forward_argc);
   274 	memset(forward_argv, 0, forward_argc);
   251 	
   275 	
   252 	forward_argc = 18;
   276 	forward_argc = 18;
   253 	forward_argv = (char **)realloc(forward_argv, forward_argc * sizeof(char *));
   277 	forward_argv = (char **)realloc(forward_argv, forward_argc * sizeof(char *));
   256 	forward_argv[ 2] = "320";							// cScreenWidth
   280 	forward_argv[ 2] = "320";							// cScreenWidth
   257 	forward_argv[ 3] = "480";							// cScreenHeight
   281 	forward_argv[ 3] = "480";							// cScreenHeight
   258 	forward_argv[ 4] = "16";							// cBitsStr
   282 	forward_argv[ 4] = "16";							// cBitsStr
   259 	forward_argv[ 5] = [portNumber UTF8String];			// ipcPort;
   283 	forward_argv[ 5] = [portNumber UTF8String];			// ipcPort;
   260 	forward_argv[ 6] = "1";								// cFullScreen (NO EFFECT)
   284 	forward_argv[ 6] = "1";								// cFullScreen (NO EFFECT)
   261 	forward_argv[ 7] = "0";				// isSoundEnabled (TOSET)
   285 	forward_argv[ 7] = [[systemSettings objectForKey:@"effects"] UTF8String];	// isSoundEnabled
   262 	forward_argv[ 8] = "1";								// cVSyncInUse (UNUSED)
   286 	forward_argv[ 8] = "1";								// cVSyncInUse (UNUSED)
   263 	forward_argv[ 9] = [localeString UTF8String];		// cLocaleFName
   287 	forward_argv[ 9] = [localeString UTF8String];		// cLocaleFName
   264 	forward_argv[10] = "100";			// cInitVolume (TOSET)
   288 	forward_argv[10] = [[systemSettings objectForKey:@"volume"] UTF8String];	// cInitVolume
   265 	forward_argv[11] = "8";								// cTimerInterval
   289 	forward_argv[11] = "8";								// cTimerInterval
   266 	forward_argv[12] = "Data";							// PathPrefix
   290 	forward_argv[12] = "Data";							// PathPrefix
   267 	forward_argv[13] = "1";				// cShowFPS (TOSET?)
   291 	forward_argv[13] = "1";								// cShowFPS (TOSET?)
   268 	forward_argv[14] = "0";				// cAltDamage (TOSET)
   292 	forward_argv[14] = [[systemSettings objectForKey:@"alternate"] UTF8String];	// cAltDamage (TOSET)
   269 	forward_argv[15] = "Koda";			// UserNick (DecodeBase64(ParamStr(15)) FTW) <- TODO
   293 	forward_argv[15] = "Koda";				// UserNick (DecodeBase64(ParamStr(15)) FTW) <- TODO
   270 	forward_argv[16] = "0";				// isMusicEnabled (TOSET)
   294 	forward_argv[16] = [[systemSettings objectForKey:@"music"] UTF8String];		// isMusicEnabled
   271 	forward_argv[17] = "0";								// cReducedQuality
   295 	forward_argv[17] = "0";								// cReducedQuality
   272 
   296 
   273 	[portNumber release];
   297 	[portNumber release];
   274 	return;
   298 	return;
   275 }
   299 }
   276 
   300 
   277 
   301 
   278 
   302 
   279 -(void) dealloc {
       
   280 	[self.systemSettings release];
       
   281 	[self.localeString autorelease];
       
   282 	[super dealloc];
       
   283 }
       
   284 
   303 
   285 
   304 
   286 
   305 
   287 @end
   306 @end