cocoaTouch/GameSetup.m
changeset 3364 e5403e2bf02c
parent 3356 3ae3fccb439e
child 3365 37ac593e9027
equal deleted inserted replaced
3363:bcd6d76db4f7 3364:e5403e2bf02c
    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, teams;
    23 @synthesize systemSettings, teamsConfig;
    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 = (random() % 64541) + 1025;
    29         
    29         
    30         systemSettings = [[NSDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()]; //should check it exists
    30         self.systemSettings = [[NSDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()]; //should check it exists
    31         return self;
    31         self.teamsConfig = [[NSArray alloc] initWithContentsOfFile:GAMECONFIG_FILE()];
    32     } else
    32     } 
    33         return nil;
    33     return self;
    34 }
    34 }
    35 
    35 
    36 -(NSString *)description {
    36 -(NSString *)description {
    37     return [NSString stringWithFormat:@"ipcport: %d\nsockets: %d,%d\n teams: %@\n systemSettings: %@",ipcPort,sd,csd,teams,systemSettings];
    37     return [NSString stringWithFormat:@"ipcport: %d\nsockets: %d,%d\n teams: %@\n systemSettings: %@",ipcPort,sd,csd,teamsConfig,systemSettings];
    38 }
    38 }
    39 
    39 
    40 -(void) dealloc {
    40 -(void) dealloc {
    41     [teams release];
    41     [teamsConfig release];
    42     [systemSettings release];
    42     [systemSettings release];
    43 	[super dealloc];
    43 	[super dealloc];
    44 }
    44 }
    45 
    45 
    46 #pragma mark -
    46 #pragma mark -
    55 	
    55 	
    56 	SDLNet_TCP_Send(csd, &length , 1);
    56 	SDLNet_TCP_Send(csd, &length , 1);
    57 	return SDLNet_TCP_Send(csd, [string UTF8String], length);
    57 	return SDLNet_TCP_Send(csd, [string UTF8String], length);
    58 }
    58 }
    59 
    59 
    60 -(void) initTeam:(NSArray *)teamLists {
    60 -(void) sendTeamData:(NSString *)fileName withPlayingHogs:(NSInteger) playingHogs ofColor:(NSNumber *)color{    
    61     teams = [[NSMutableArray alloc] initWithObjects:nil];
    61     NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@", TEAMS_DIRECTORY(), fileName];
    62     
    62     NSDictionary *teamData = [[NSDictionary alloc] initWithContentsOfFile:teamFile];
    63     for (NSString *teamString in teamLists) {
    63     [teamFile release];
    64         //NSString *teamFile = [[NSString alloc] initWithFormat:@"%@.plist", teamString];
    64     
    65         
    65     NSString *teamHashColorAndName = [[NSString alloc] initWithFormat:@"eaddteam %@ %@ %@", [teamData objectForKey:@"hash"], [color stringValue], [teamData objectForKey:@"teamname"]];
    66         //NSDictionary *theTeam = [[NSDictionary alloc] initWithContentsOfFile:filePath];
       
    67         //[teams addObject:theTeam];
       
    68         //[theTeam release];
       
    69     }
       
    70     
       
    71     NSDictionary *hogA1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"100",@"health",@"0",@"level",@"Snow Leopard",@"hogname",@"NoHat",@"hat",nil];
       
    72     NSDictionary *hogA2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"100",@"health",@"0",@"level",@"Leopard",@"hogname",@"NoHat",@"hat",nil];
       
    73     NSArray *hedgehogs1 = [[NSArray alloc] initWithObjects:hogA1,hogA2,nil];
       
    74     [hogA1 release];
       
    75     [hogA2 release];
       
    76 
       
    77     NSDictionary *firstTeam = [[NSDictionary alloc] initWithObjectsAndKeys:@"4421353",@"color",@"0",@"hash",@"System Cats",@"teamname",
       
    78                                @"star",@"grave",@"Earth",@"fort",@"Classic",@"voicepack",@"hedgewars",@"flag",hedgehogs1,@"hedgehogs",
       
    79                                nil];
       
    80     [hedgehogs1 release];
       
    81     [teams addObject:firstTeam];
       
    82     [firstTeam release];
       
    83     
       
    84     NSDictionary *hogB1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"100",@"health",@"0",@"level",@"Raichu",@"hogname",@"Bunny",@"hat",nil];
       
    85     NSDictionary *hogB2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"100",@"health",@"0",@"level",@"Pikachu",@"hogname",@"Bunny",@"hat",nil];
       
    86     NSArray *hedgehogs2 = [[NSArray alloc] initWithObjects:hogB1,hogB2,nil];
       
    87     [hogB1 release];
       
    88     [hogB2 release];
       
    89     NSDictionary *secondTeam = [[NSDictionary alloc] initWithObjectsAndKeys:@"4100897",@"color",@"0",@"hash",@"Poke-MAN",@"teamname",
       
    90                                 @"Badger",@"grave",@"UFO",@"fort",@"Default",@"voicepack",@"hedgewars",@"flag",hedgehogs2,@"hedgehogs",
       
    91                                 nil];
       
    92     [hedgehogs2 release];
       
    93     [teams addObject:secondTeam];
       
    94     [secondTeam release];
       
    95 }
       
    96 
       
    97 -(void) sendTeamData:(NSDictionary *)teamData withPlayingHogs:(int) playingHogs{
       
    98     int i;
       
    99     
       
   100     NSString *teamHashColorAndName = [[NSString alloc] initWithFormat:@"eaddteam %@ %@ %@", [teamData objectForKey:@"hash"], [teamData objectForKey:@"color"], [teamData objectForKey:@"teamname"]];
       
   101     [self sendToEngine: teamHashColorAndName];
    66     [self sendToEngine: teamHashColorAndName];
   102     [teamHashColorAndName release];
    67     [teamHashColorAndName release];
   103     
    68     
   104     NSString *grave = [[NSString alloc] initWithFormat:@"egrave %@", [teamData objectForKey:@"grave"]];
    69     NSString *grave = [[NSString alloc] initWithFormat:@"egrave %@", [teamData objectForKey:@"grave"]];
   105     [self sendToEngine: grave];
    70     [self sendToEngine: grave];
   116     NSString *flag = [[NSString alloc] initWithFormat:@"eflag %@", [teamData objectForKey:@"flag"]];
    81     NSString *flag = [[NSString alloc] initWithFormat:@"eflag %@", [teamData objectForKey:@"flag"]];
   117     [self sendToEngine: flag];
    82     [self sendToEngine: flag];
   118     [flag release];
    83     [flag release];
   119     
    84     
   120     NSArray *hogs = [teamData objectForKey:@"hedgehogs"];
    85     NSArray *hogs = [teamData objectForKey:@"hedgehogs"];
   121     for (i = 0; i < playingHogs; i++) {
    86     for (int i = 0; i < playingHogs; i++) {
   122         NSDictionary *hog = [hogs objectAtIndex:i];
    87         NSDictionary *hog = [hogs objectAtIndex:i];
   123         
    88         
   124         NSString *hogLevelHealthAndName = [[NSString alloc] initWithFormat:@"eaddhh %@ %@ %@", [hog objectForKey:@"level"], [hog objectForKey:@"health"], [hog objectForKey:@"hogname"]];
    89         NSString *hogLevelHealthAndName = [[NSString alloc] initWithFormat:@"eaddhh %@ %@ %@", [hog objectForKey:@"level"], [hog objectForKey:@"health"], [hog objectForKey:@"hogname"]];
   125         [self sendToEngine: hogLevelHealthAndName];
    90         [self sendToEngine: hogLevelHealthAndName];
   126         [hogLevelHealthAndName release];
    91         [hogLevelHealthAndName release];
   127         
    92         
   128         NSString *hogHat = [[NSString alloc] initWithFormat:@"ehat %@", [hog objectForKey:@"hat"]];
    93         NSString *hogHat = [[NSString alloc] initWithFormat:@"ehat %@", [hog objectForKey:@"hat"]];
   129         [self sendToEngine: hogHat];
    94         [self sendToEngine: hogHat];
   130         [hogHat release];
    95         [hogHat release];
   131     }         
    96     }
   132 }
    97     
   133 
    98     [teamData release];
   134 -(void) sendAmmoData:(NSDictionary *)ammoData {
    99 }
       
   100 
       
   101 -(void) sendAmmoData:(NSDictionary *)ammoData forTeams: (NSInteger)numberPlaying {
   135     NSString *ammloadt = [[NSString alloc] initWithFormat:@"eammloadt %@", [ammoData objectForKey:@"ammostore_initialqt"]];
   102     NSString *ammloadt = [[NSString alloc] initWithFormat:@"eammloadt %@", [ammoData objectForKey:@"ammostore_initialqt"]];
   136     [self sendToEngine: ammloadt];
   103     [self sendToEngine: ammloadt];
   137     [ammloadt release];
   104     [ammloadt release];
   138     
   105     
   139     NSString *ammdelay = [[NSString alloc] initWithFormat:@"eammprob %@", [ammoData objectForKey:@"ammostore_probability"]];
   106     NSString *ammdelay = [[NSString alloc] initWithFormat:@"eammprob %@", [ammoData objectForKey:@"ammostore_probability"]];
   148     [self sendToEngine: ammreinf];
   115     [self sendToEngine: ammreinf];
   149     [ammreinf release];
   116     [ammreinf release];
   150     
   117     
   151     // sent twice so it applies to both teams
   118     // sent twice so it applies to both teams
   152     NSString *ammstore = [[NSString alloc] initWithString:@"eammstore"];
   119     NSString *ammstore = [[NSString alloc] initWithString:@"eammstore"];
   153     [self sendToEngine: ammstore];
   120     for (int i = 0; i < numberPlaying; i++)
   154     [self sendToEngine: ammstore];
   121         [self sendToEngine: ammstore];
   155     [ammstore release];
   122     [ammstore release];
   156 }
   123 }
   157 
   124 
   158 -(void) engineProtocol {
   125 -(void) engineProtocol {
   159 	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   126 	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   199 			gameTicks = SDLNet_Read16(&buffer[msgSize - 2]);
   166 			gameTicks = SDLNet_Read16(&buffer[msgSize - 2]);
   200 			//NSLog(@"engineProtocol - %d: received [%s]", gameTicks, buffer);
   167 			//NSLog(@"engineProtocol - %d: received [%s]", gameTicks, buffer);
   201 			
   168 			
   202 			if ('C' == buffer[0]) {
   169 			if ('C' == buffer[0]) {
   203 				NSLog(@"engineProtocol - sending game config");
   170 				NSLog(@"engineProtocol - sending game config");
   204                 
       
   205 				NSArray *teamlist = [[NSArray alloc] initWithObjects:@"this",@"is",@"test",nil];
       
   206                 [self initTeam:teamlist];
       
   207                 [teamlist release];
       
   208                 
   171                 
   209 				// send config data data
   172 				// send config data data
   210 				/*
   173 				/*
   211 				seed is arbitrary string
   174 				seed is arbitrary string
   212 				addteam <32charsMD5hash> <color> <team name>
   175 				addteam <32charsMD5hash> <color> <team name>
   233 				[self sendToEngine:@"e$template_filter 1"];
   196 				[self sendToEngine:@"e$template_filter 1"];
   234 								
   197 								
   235 				// theme info
   198 				// theme info
   236 				[self sendToEngine:@"etheme Compost"];
   199 				[self sendToEngine:@"etheme Compost"];
   237 				
   200 				
   238                 for (NSDictionary *teamData in teams) {
   201                 for (NSDictionary *teamData in self.teamsConfig) {
   239                     [self sendTeamData:teamData withPlayingHogs:2];
   202                     [self sendTeamData:[teamData objectForKey:@"team"] 
       
   203                        withPlayingHogs:[[teamData objectForKey:@"number"] intValue]
       
   204                                ofColor:[teamData objectForKey:@"color"]];
   240                     NSLog(@"teamData sent");
   205                     NSLog(@"teamData sent");
   241                 }
   206                 }
   242                 
   207                 
   243                 NSDictionary *ammoData = [[NSDictionary alloc] initWithObjectsAndKeys:
   208                 NSDictionary *ammoData = [[NSDictionary alloc] initWithObjectsAndKeys:
   244                                           @"9391929422199121032235111001201000000211190",@"ammostore_initialqt",
   209                                           @"9391929422199121032235111001201000000211190",@"ammostore_initialqt",
   245                                           @"0405040541600655546554464776576666666155501",@"ammostore_probability",
   210                                           @"0405040541600655546554464776576666666155501",@"ammostore_probability",
   246                                           @"0000000000000205500000040007004000000000200",@"ammostore_delay",
   211                                           @"0000000000000205500000040007004000000000200",@"ammostore_delay",
   247                                           @"1311110312111111123114111111111111111211101",@"ammostore_crate", nil];
   212                                           @"1311110312111111123114111111111111111211101",@"ammostore_crate", nil];
   248                 [self sendAmmoData: ammoData];
   213                 [self sendAmmoData:ammoData forTeams:[self.teamsConfig count]];
   249                 [ammoData release];
   214                 [ammoData release];
   250                 
   215                 
   251 			clientQuit = NO;
   216                 clientQuit = NO;
   252 			} else {
   217 			} else {
   253 				NSLog(@"engineProtocolThread - wrong message or client closed connection");
   218 				NSLog(@"engineProtocolThread - wrong message or client closed connection");
   254 				clientQuit = YES;
   219 				clientQuit = YES;
   255 			}
   220 			}
   256 			
   221 			
   353     [machine release];
   318     [machine release];
   354     */
   319     */
   355     
   320     
   356     // prevents using an empty nickname
   321     // prevents using an empty nickname
   357     NSString *username;
   322     NSString *username;
   358     NSString *originalUsername = [systemSettings objectForKey:@"username"];
   323     NSString *originalUsername = [self.systemSettings objectForKey:@"username"];
   359     if ([originalUsername length] == 0) {
   324     if ([originalUsername length] == 0) {
   360         username = [[NSString alloc] initWithFormat:@"MobileUser-%@",ipcString];
   325         username = [[NSString alloc] initWithFormat:@"MobileUser-%@",ipcString];
   361     } else {
   326     } else {
   362         username = [[NSString alloc] initWithString:originalUsername];
   327         username = [[NSString alloc] initWithString:originalUsername];
   363     }
   328     }
   364     
   329     
   365 	gameArgs[0] = [username UTF8String];                                                    //UserNick
   330 	gameArgs[0] = [username UTF8String];                                                    //UserNick
   366 	gameArgs[1] = [ipcString UTF8String];                                                   //ipcPort
   331 	gameArgs[1] = [ipcString UTF8String];                                                   //ipcPort
   367 	gameArgs[2] = [[[systemSettings objectForKey:@"sound"] stringValue] UTF8String];        //isSoundEnabled
   332 	gameArgs[2] = [[[self.systemSettings objectForKey:@"sound"] stringValue] UTF8String];        //isSoundEnabled
   368 	gameArgs[3] = [[[systemSettings objectForKey:@"music"] stringValue] UTF8String];        //isMusicEnabled
   333 	gameArgs[3] = [[[self.systemSettings objectForKey:@"music"] stringValue] UTF8String];        //isMusicEnabled
   369 	gameArgs[4] = [localeString UTF8String];                                                //cLocaleFName
   334 	gameArgs[4] = [localeString UTF8String];                                                //cLocaleFName
   370 	gameArgs[5] = [[[systemSettings objectForKey:@"alternate"] stringValue] UTF8String];	//cAltDamage
   335 	gameArgs[5] = [[[self.systemSettings objectForKey:@"alternate"] stringValue] UTF8String];	//cAltDamage
   371 	gameArgs[6] = [wSize UTF8String];                                                       //cScreenHeight
   336 	gameArgs[6] = [wSize UTF8String];                                                       //cScreenHeight
   372     gameArgs[7] = [hSize UTF8String];                                                       //cScreenWidth
   337     gameArgs[7] = [hSize UTF8String];                                                       //cScreenWidth
   373     
   338     
   374     [wSize release];
   339     [wSize release];
   375     [hSize release];
   340     [hSize release];