cocoaTouch/GameSetup.m
changeset 3513 f589230fa21b
parent 3495 a6b4f351d400
equal deleted inserted replaced
3512:6a8b5f313190 3513:f589230fa21b
    47     [systemSettings release];
    47     [systemSettings release];
    48 	[super dealloc];
    48 	[super dealloc];
    49 }
    49 }
    50 
    50 
    51 #pragma mark -
    51 #pragma mark -
    52 #pragma mark Thread/Network relevant code
    52 #pragma mark Provider functions
    53 // select one of GameSetup method and execute it in a seprate thread
       
    54 -(void) startThread: (NSString *) selector {
       
    55 	SEL usage = NSSelectorFromString(selector);
       
    56 	[NSThread detachNewThreadSelector:usage toTarget:self withObject:nil];
       
    57 }
       
    58 
       
    59 // wrapper that computes the length of the message and then sends the command string
       
    60 -(int) sendToEngine: (NSString *)string {
       
    61 	uint8_t length = [string length];
       
    62 	
       
    63 	SDLNet_TCP_Send(csd, &length , 1);
       
    64 	return SDLNet_TCP_Send(csd, [string UTF8String], length);
       
    65 }
       
    66 
       
    67 // unpacks team data from the selected team.plist to a sequence of engine commands
    53 // unpacks team data from the selected team.plist to a sequence of engine commands
    68 -(void) provideTeamData:(NSString *)teamName forHogs:(NSInteger) numberOfPlayingHogs withHealth:(NSInteger) initialHealth ofColor:(NSNumber *)teamColor {
    54 -(void) provideTeamData:(NSString *)teamName forHogs:(NSInteger) numberOfPlayingHogs withHealth:(NSInteger) initialHealth ofColor:(NSNumber *)teamColor {
    69     /*
    55     /*
    70      addteam <32charsMD5hash> <color> <team name>
    56      addteam <32charsMD5hash> <color> <team name>
    71      addhh <level> <health> <hedgehog name>
    57      addhh <level> <health> <hedgehog name>
   150     [ammoData release];
   136     [ammoData release];
   151 }
   137 }
   152 
   138 
   153 // unpacks scheme data from the selected scheme.plist to a sequence of engine commands
   139 // unpacks scheme data from the selected scheme.plist to a sequence of engine commands
   154 -(NSInteger) provideScheme:(NSString *)schemeName {
   140 -(NSInteger) provideScheme:(NSString *)schemeName {
   155     NSString *schemePath = [[NSString alloc] initWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),schemeName];
   141     NSString *schemePath = [[NSString alloc] initWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),schemeName];
   156     NSArray *scheme = [[NSArray alloc] initWithContentsOfFile:schemePath];
   142     NSArray *scheme = [[NSArray alloc] initWithContentsOfFile:schemePath];
   157     [schemePath release];
   143     [schemePath release];
   158     int result = 0;
   144     int result = 0;
   159     int i = 0;
   145     int i = 0;
   160     
   146 
   161     if ([[scheme objectAtIndex:i++] boolValue])
   147     if ([[scheme objectAtIndex:i++] boolValue])
   162         result |= 0x01;
   148         result |= 0x01;
   163     if ([[scheme objectAtIndex:i++] boolValue])
   149     if ([[scheme objectAtIndex:i++] boolValue])
   164         result |= 0x10;    
   150         result |= 0x10;    
   165     if ([[scheme objectAtIndex:i++] boolValue])
   151     if ([[scheme objectAtIndex:i++] boolValue])
   221     
   207     
   222     NSString *minesNumber = [[NSString alloc] initWithFormat:@"e$landadds %d",[[scheme objectAtIndex:i++] intValue]];
   208     NSString *minesNumber = [[NSString alloc] initWithFormat:@"e$landadds %d",[[scheme objectAtIndex:i++] intValue]];
   223     [self sendToEngine:minesNumber];
   209     [self sendToEngine:minesNumber];
   224     [minesNumber release];
   210     [minesNumber release];
   225     
   211     
   226 
       
   227     NSString *dudMines = [[NSString alloc] initWithFormat:@"e$minedudpct %d",[[scheme objectAtIndex:i++] intValue]];
   212     NSString *dudMines = [[NSString alloc] initWithFormat:@"e$minedudpct %d",[[scheme objectAtIndex:i++] intValue]];
   228     [self sendToEngine:dudMines];
   213     [self sendToEngine:dudMines];
   229     [dudMines release];
   214     [dudMines release];
   230     
   215     
   231     NSString *explosives = [[NSString alloc] initWithFormat:@"e$explosives %d",[[scheme objectAtIndex:i++] intValue]];
   216     NSString *explosives = [[NSString alloc] initWithFormat:@"e$explosives %d",[[scheme objectAtIndex:i++] intValue]];
   232     [self sendToEngine:explosives];
   217     [self sendToEngine:explosives];
   233     [explosives release];
   218     [explosives release];
   234     
   219     
   235     [scheme release];
   220     [scheme release];
   236     return result;
   221     return result;
       
   222 }
       
   223 
       
   224 #pragma mark -
       
   225 #pragma mark Thread/Network relevant code
       
   226 // select one of GameSetup method and execute it in a seprate thread
       
   227 -(void) startThread: (NSString *) selector {
       
   228 	SEL usage = NSSelectorFromString(selector);
       
   229 	[NSThread detachNewThreadSelector:usage toTarget:self withObject:nil];
       
   230 }
       
   231 
       
   232 // wrapper that computes the length of the message and then sends the command string
       
   233 -(int) sendToEngine: (NSString *)string {
       
   234 	uint8_t length = [string length];
       
   235 	
       
   236 	SDLNet_TCP_Send(csd, &length , 1);
       
   237 	return SDLNet_TCP_Send(csd, [string UTF8String], length);
   237 }
   238 }
   238 
   239 
   239 // method that handles net setup with engine and keeps connection alive
   240 // method that handles net setup with engine and keeps connection alive
   240 -(void) engineProtocol {
   241 -(void) engineProtocol {
   241 	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   242 	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   290 				
   291 				
   291 				// seed info
   292 				// seed info
   292 				[self sendToEngine:[self.gameConfig objectForKey:@"seed_command"]];
   293 				[self sendToEngine:[self.gameConfig objectForKey:@"seed_command"]];
   293 				
   294 				
   294                 // scheme (returns initial health)
   295                 // scheme (returns initial health)
   295                 NSInteger health = [self provideScheme:@"Scheme 0"];
   296                 NSInteger health = [self provideScheme:[self.gameConfig objectForKey:@"scheme"]];
   296 
   297 
   297 				// dimension of the map
   298 				// dimension of the map
   298 				[self sendToEngine:[self.gameConfig objectForKey:@"templatefilter_command"]];
   299 				[self sendToEngine:[self.gameConfig objectForKey:@"templatefilter_command"]];
   299 				[self sendToEngine:[self.gameConfig objectForKey:@"mapgen_command"]];
   300 				[self sendToEngine:[self.gameConfig objectForKey:@"mapgen_command"]];
   300 				[self sendToEngine:[self.gameConfig objectForKey:@"mazesize_command"]];
   301 				[self sendToEngine:[self.gameConfig objectForKey:@"mazesize_command"]];