project_files/HedgewarsMobile/Classes/GameSetup.m
changeset 3697 d5b30d6373fc
parent 3670 4c673e57f0d7
child 3752 73c2d7d5643b
equal deleted inserted replaced
3695:c11abf387a7d 3697:d5b30d6373fc
    19 @synthesize systemSettings, gameConfig;
    19 @synthesize systemSettings, gameConfig;
    20 
    20 
    21 -(id) initWithDictionary:(NSDictionary *)gameDictionary {
    21 -(id) initWithDictionary:(NSDictionary *)gameDictionary {
    22     if (self = [super init]) {
    22     if (self = [super init]) {
    23         ipcPort = randomPort();
    23         ipcPort = randomPort();
    24         
    24 
    25         // should check they exist and throw and exection if not
    25         // should check they exist and throw and exection if not
    26         NSDictionary *dictSett = [[NSDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()];
    26         NSDictionary *dictSett = [[NSDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()];
    27         self.systemSettings = dictSett;
    27         self.systemSettings = dictSett;
    28         [dictSett release];
    28         [dictSett release];
    29         
    29 
    30         self.gameConfig = gameDictionary;
    30         self.gameConfig = gameDictionary;
    31     } 
    31     }
    32     return self;
    32     return self;
    33 }
    33 }
    34 
    34 
    35 -(void) dealloc {
    35 -(void) dealloc {
    36     [gameConfig release];
    36     [gameConfig release];
    45     /*
    45     /*
    46      addteam <32charsMD5hash> <color> <team name>
    46      addteam <32charsMD5hash> <color> <team name>
    47      addhh <level> <health> <hedgehog name>
    47      addhh <level> <health> <hedgehog name>
    48      <level> is 0 for human, 1-5 for bots (5 is the most stupid)
    48      <level> is 0 for human, 1-5 for bots (5 is the most stupid)
    49     */
    49     */
    50     
    50 
    51     NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@", TEAMS_DIRECTORY(), teamName];
    51     NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@", TEAMS_DIRECTORY(), teamName];
    52     NSDictionary *teamData = [[NSDictionary alloc] initWithContentsOfFile:teamFile];
    52     NSDictionary *teamData = [[NSDictionary alloc] initWithContentsOfFile:teamFile];
    53     [teamFile release];
    53     [teamFile release];
    54     
    54 
    55     NSString *teamHashColorAndName = [[NSString alloc] initWithFormat:@"eaddteam %@ %@ %@", 
    55     NSString *teamHashColorAndName = [[NSString alloc] initWithFormat:@"eaddteam %@ %@ %@",
    56                                       [teamData objectForKey:@"hash"], [teamColor stringValue], [teamName stringByDeletingPathExtension]];
    56                                       [teamData objectForKey:@"hash"], [teamColor stringValue], [teamName stringByDeletingPathExtension]];
    57     [self sendToEngine: teamHashColorAndName];
    57     [self sendToEngine: teamHashColorAndName];
    58     [teamHashColorAndName release];
    58     [teamHashColorAndName release];
    59     
    59 
    60     NSString *grave = [[NSString alloc] initWithFormat:@"egrave %@", [teamData objectForKey:@"grave"]];
    60     NSString *grave = [[NSString alloc] initWithFormat:@"egrave %@", [teamData objectForKey:@"grave"]];
    61     [self sendToEngine: grave];
    61     [self sendToEngine: grave];
    62     [grave release];
    62     [grave release];
    63     
    63 
    64     NSString *fort = [[NSString alloc] initWithFormat:@"efort %@", [teamData objectForKey:@"fort"]];
    64     NSString *fort = [[NSString alloc] initWithFormat:@"efort %@", [teamData objectForKey:@"fort"]];
    65     [self sendToEngine: fort];
    65     [self sendToEngine: fort];
    66     [fort release];
    66     [fort release];
    67     
    67 
    68     NSString *voicepack = [[NSString alloc] initWithFormat:@"evoicepack %@", [teamData objectForKey:@"voicepack"]];
    68     NSString *voicepack = [[NSString alloc] initWithFormat:@"evoicepack %@", [teamData objectForKey:@"voicepack"]];
    69     [self sendToEngine: voicepack];
    69     [self sendToEngine: voicepack];
    70     [voicepack release];
    70     [voicepack release];
    71     
    71 
    72     NSString *flag = [[NSString alloc] initWithFormat:@"eflag %@", [teamData objectForKey:@"flag"]];
    72     NSString *flag = [[NSString alloc] initWithFormat:@"eflag %@", [teamData objectForKey:@"flag"]];
    73     [self sendToEngine: flag];
    73     [self sendToEngine: flag];
    74     [flag release];
    74     [flag release];
    75     
    75 
    76     NSArray *hogs = [teamData objectForKey:@"hedgehogs"];
    76     NSArray *hogs = [teamData objectForKey:@"hedgehogs"];
    77     for (int i = 0; i < numberOfPlayingHogs; i++) {
    77     for (int i = 0; i < numberOfPlayingHogs; i++) {
    78         NSDictionary *hog = [hogs objectAtIndex:i];
    78         NSDictionary *hog = [hogs objectAtIndex:i];
    79         
    79 
    80         NSString *hogLevelHealthAndName = [[NSString alloc] initWithFormat:@"eaddhh %@ %d %@", 
    80         NSString *hogLevelHealthAndName = [[NSString alloc] initWithFormat:@"eaddhh %@ %d %@",
    81                                            [hog objectForKey:@"level"], initialHealth, [hog objectForKey:@"hogname"]];
    81                                            [hog objectForKey:@"level"], initialHealth, [hog objectForKey:@"hogname"]];
    82         [self sendToEngine: hogLevelHealthAndName];
    82         [self sendToEngine: hogLevelHealthAndName];
    83         [hogLevelHealthAndName release];
    83         [hogLevelHealthAndName release];
    84         
    84 
    85         NSString *hogHat = [[NSString alloc] initWithFormat:@"ehat %@", [hog objectForKey:@"hat"]];
    85         NSString *hogHat = [[NSString alloc] initWithFormat:@"ehat %@", [hog objectForKey:@"hat"]];
    86         [self sendToEngine: hogHat];
    86         [self sendToEngine: hogHat];
    87         [hogHat release];
    87         [hogHat release];
    88     }
    88     }
    89     
    89 
    90     [teamData release];
    90     [teamData release];
    91 }
    91 }
    92 
    92 
    93 // unpacks ammostore data from the selected ammo.plist to a sequence of engine commands
    93 // unpacks ammostore data from the selected ammo.plist to a sequence of engine commands
    94 -(void) provideAmmoData:(NSString *)ammostoreName forPlayingTeams:(NSInteger) numberOfTeams {
    94 -(void) provideAmmoData:(NSString *)ammostoreName forPlayingTeams:(NSInteger) numberOfTeams {
    95     NSString *weaponPath = [[NSString alloc] initWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),ammostoreName];
    95     NSString *weaponPath = [[NSString alloc] initWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),ammostoreName];
    96     NSDictionary *ammoData = [[NSDictionary alloc] initWithContentsOfFile:weaponPath];
    96     NSDictionary *ammoData = [[NSDictionary alloc] initWithContentsOfFile:weaponPath];
    97     [weaponPath release];
    97     [weaponPath release];
    98     NSString *update = @"";
    98     NSString *update = @"";
    99     
    99 
   100     // if we're loading an older version of ammos fill the engine message with 0s
   100     // if we're loading an older version of ammos fill the engine message with 0s
   101     int diff = CURRENT_AMMOSIZE - [[ammoData objectForKey:@"version"] intValue];
   101     int diff = CURRENT_AMMOSIZE - [[ammoData objectForKey:@"version"] intValue];
   102     if (diff != 0)
   102     if (diff != 0)
   103         update = [NSString stringWithCharacters:(const unichar*)"0000000000000000000000000000000000" length:diff];
   103         update = [NSString stringWithCharacters:(const unichar*)"0000000000000000000000000000000000" length:diff];
   104     
   104 
   105     NSString *ammloadt = [[NSString alloc] initWithFormat:@"eammloadt %@%@", [ammoData objectForKey:@"ammostore_initialqt"], update];
   105     NSString *ammloadt = [[NSString alloc] initWithFormat:@"eammloadt %@%@", [ammoData objectForKey:@"ammostore_initialqt"], update];
   106     [self sendToEngine: ammloadt];
   106     [self sendToEngine: ammloadt];
   107     [ammloadt release];
   107     [ammloadt release];
   108     
   108 
   109     NSString *ammprob = [[NSString alloc] initWithFormat:@"eammprob %@%@", [ammoData objectForKey:@"ammostore_probability"], update];
   109     NSString *ammprob = [[NSString alloc] initWithFormat:@"eammprob %@%@", [ammoData objectForKey:@"ammostore_probability"], update];
   110     [self sendToEngine: ammprob];
   110     [self sendToEngine: ammprob];
   111     [ammprob release];
   111     [ammprob release];
   112     
   112 
   113     NSString *ammdelay = [[NSString alloc] initWithFormat:@"eammdelay %@%@", [ammoData objectForKey:@"ammostore_delay"], update];
   113     NSString *ammdelay = [[NSString alloc] initWithFormat:@"eammdelay %@%@", [ammoData objectForKey:@"ammostore_delay"], update];
   114     [self sendToEngine: ammdelay];
   114     [self sendToEngine: ammdelay];
   115     [ammdelay release];
   115     [ammdelay release];
   116     
   116 
   117     NSString *ammreinf = [[NSString alloc] initWithFormat:@"eammreinf %@%@", [ammoData objectForKey:@"ammostore_crate"], update];
   117     NSString *ammreinf = [[NSString alloc] initWithFormat:@"eammreinf %@%@", [ammoData objectForKey:@"ammostore_crate"], update];
   118     [self sendToEngine: ammreinf];
   118     [self sendToEngine: ammreinf];
   119     [ammreinf release];
   119     [ammreinf release];
   120     
   120 
   121     // sent twice so it applies to both teams
   121     // sent twice so it applies to both teams
   122     NSString *ammstore = [[NSString alloc] initWithString:@"eammstore"];
   122     NSString *ammstore = [[NSString alloc] initWithString:@"eammstore"];
   123     for (int i = 0; i < numberOfTeams; i++)
   123     for (int i = 0; i < numberOfTeams; i++)
   124         [self sendToEngine: ammstore];
   124         [self sendToEngine: ammstore];
   125     [ammstore release];
   125     [ammstore release];
   126     
   126 
   127     [ammoData release];
   127     [ammoData release];
   128 }
   128 }
   129 
   129 
   130 // unpacks scheme data from the selected scheme.plist to a sequence of engine commands
   130 // unpacks scheme data from the selected scheme.plist to a sequence of engine commands
   131 -(NSInteger) provideScheme:(NSString *)schemeName {
   131 -(NSInteger) provideScheme:(NSString *)schemeName {
   136     int i = 0;
   136     int i = 0;
   137 
   137 
   138     if ([[scheme objectAtIndex:i++] boolValue])
   138     if ([[scheme objectAtIndex:i++] boolValue])
   139         result |= 0x01;
   139         result |= 0x01;
   140     if ([[scheme objectAtIndex:i++] boolValue])
   140     if ([[scheme objectAtIndex:i++] boolValue])
   141         result |= 0x10;    
   141         result |= 0x10;
   142     if ([[scheme objectAtIndex:i++] boolValue])
   142     if ([[scheme objectAtIndex:i++] boolValue])
   143         result |= 0x04;
   143         result |= 0x04;
   144     if ([[scheme objectAtIndex:i++] boolValue])
   144     if ([[scheme objectAtIndex:i++] boolValue])
   145         result |= 0x08;    
   145         result |= 0x08;
   146     if ([[scheme objectAtIndex:i++] boolValue])
   146     if ([[scheme objectAtIndex:i++] boolValue])
   147         result |= 0x20;
   147         result |= 0x20;
   148     if ([[scheme objectAtIndex:i++] boolValue])
   148     if ([[scheme objectAtIndex:i++] boolValue])
   149         result |= 0x40;    
   149         result |= 0x40;
   150     if ([[scheme objectAtIndex:i++] boolValue])
   150     if ([[scheme objectAtIndex:i++] boolValue])
   151         result |= 0x80;
   151         result |= 0x80;
   152     if ([[scheme objectAtIndex:i++] boolValue])
   152     if ([[scheme objectAtIndex:i++] boolValue])
   153         result |= 0x100;    
   153         result |= 0x100;
   154     if ([[scheme objectAtIndex:i++] boolValue])
   154     if ([[scheme objectAtIndex:i++] boolValue])
   155         result |= 0x200;
   155         result |= 0x200;
   156     if ([[scheme objectAtIndex:i++] boolValue])
   156     if ([[scheme objectAtIndex:i++] boolValue])
   157         result |= 0x400;    
   157         result |= 0x400;
   158     if ([[scheme objectAtIndex:i++] boolValue])
   158     if ([[scheme objectAtIndex:i++] boolValue])
   159         result |= 0x800;
   159         result |= 0x800;
   160     if ([[scheme objectAtIndex:i++] boolValue])
   160     if ([[scheme objectAtIndex:i++] boolValue])
   161         result |= 0x2000;    
   161         result |= 0x2000;
   162     if ([[scheme objectAtIndex:i++] boolValue])
   162     if ([[scheme objectAtIndex:i++] boolValue])
   163         result |= 0x4000;
   163         result |= 0x4000;
   164     if ([[scheme objectAtIndex:i++] boolValue])
   164     if ([[scheme objectAtIndex:i++] boolValue])
   165         result |= 0x8000;    
   165         result |= 0x8000;
   166     if ([[scheme objectAtIndex:i++] boolValue])
   166     if ([[scheme objectAtIndex:i++] boolValue])
   167         result |= 0x10000;
   167         result |= 0x10000;
   168     if ([[scheme objectAtIndex:i++] boolValue])
   168     if ([[scheme objectAtIndex:i++] boolValue])
   169         result |= 0x20000;
   169         result |= 0x20000;
   170     if ([[scheme objectAtIndex:i++] boolValue])
   170     if ([[scheme objectAtIndex:i++] boolValue])
   171         result |= 0x80000;    
   171         result |= 0x80000;
   172 
   172 
   173     NSString *flags = [[NSString alloc] initWithFormat:@"e$gmflags %d",result];
   173     NSString *flags = [[NSString alloc] initWithFormat:@"e$gmflags %d",result];
   174     [self sendToEngine:flags];
   174     [self sendToEngine:flags];
   175     [flags release];
   175     [flags release];
   176     
   176 
   177     NSString *dmgMod = [[NSString alloc] initWithFormat:@"e$damagepct %d",[[scheme objectAtIndex:i++] intValue]];
   177     NSString *dmgMod = [[NSString alloc] initWithFormat:@"e$damagepct %d",[[scheme objectAtIndex:i++] intValue]];
   178     [self sendToEngine:dmgMod];
   178     [self sendToEngine:dmgMod];
   179     [dmgMod release];
   179     [dmgMod release];
   180     
   180 
   181     NSString *turnTime = [[NSString alloc] initWithFormat:@"e$turntime %d",[[scheme objectAtIndex:i++] intValue] * 1000];
   181     NSString *turnTime = [[NSString alloc] initWithFormat:@"e$turntime %d",[[scheme objectAtIndex:i++] intValue] * 1000];
   182     [self sendToEngine:turnTime];
   182     [self sendToEngine:turnTime];
   183     [turnTime release];
   183     [turnTime release];
   184     
   184 
   185     result = [[scheme objectAtIndex:i++] intValue]; // initial health
   185     result = [[scheme objectAtIndex:i++] intValue]; // initial health
   186     
   186 
   187     NSString *sdTime = [[NSString alloc] initWithFormat:@"e$sd_turns %d",[[scheme objectAtIndex:i++] intValue]];
   187     NSString *sdTime = [[NSString alloc] initWithFormat:@"e$sd_turns %d",[[scheme objectAtIndex:i++] intValue]];
   188     [self sendToEngine:sdTime];
   188     [self sendToEngine:sdTime];
   189     [sdTime release];
   189     [sdTime release];
   190     
   190 
   191     NSString *crateDrops = [[NSString alloc] initWithFormat:@"e$casefreq %d",[[scheme objectAtIndex:i++] intValue]];
   191     NSString *crateDrops = [[NSString alloc] initWithFormat:@"e$casefreq %d",[[scheme objectAtIndex:i++] intValue]];
   192     [self sendToEngine:crateDrops];
   192     [self sendToEngine:crateDrops];
   193     [crateDrops release];
   193     [crateDrops release];
   194     
   194 
   195     NSString *minesTime = [[NSString alloc] initWithFormat:@"e$minestime %d",[[scheme objectAtIndex:i++] intValue] * 1000];
   195     NSString *minesTime = [[NSString alloc] initWithFormat:@"e$minestime %d",[[scheme objectAtIndex:i++] intValue] * 1000];
   196     [self sendToEngine:minesTime];
   196     [self sendToEngine:minesTime];
   197     [minesTime release];
   197     [minesTime release];
   198     
   198 
   199     NSString *minesNumber = [[NSString alloc] initWithFormat:@"e$landadds %d",[[scheme objectAtIndex:i++] intValue]];
   199     NSString *minesNumber = [[NSString alloc] initWithFormat:@"e$landadds %d",[[scheme objectAtIndex:i++] intValue]];
   200     [self sendToEngine:minesNumber];
   200     [self sendToEngine:minesNumber];
   201     [minesNumber release];
   201     [minesNumber release];
   202     
   202 
   203     NSString *dudMines = [[NSString alloc] initWithFormat:@"e$minedudpct %d",[[scheme objectAtIndex:i++] intValue]];
   203     NSString *dudMines = [[NSString alloc] initWithFormat:@"e$minedudpct %d",[[scheme objectAtIndex:i++] intValue]];
   204     [self sendToEngine:dudMines];
   204     [self sendToEngine:dudMines];
   205     [dudMines release];
   205     [dudMines release];
   206     
   206 
   207     NSString *explosives = [[NSString alloc] initWithFormat:@"e$explosives %d",[[scheme objectAtIndex:i++] intValue]];
   207     NSString *explosives = [[NSString alloc] initWithFormat:@"e$explosives %d",[[scheme objectAtIndex:i++] intValue]];
   208     [self sendToEngine:explosives];
   208     [self sendToEngine:explosives];
   209     [explosives release];
   209     [explosives release];
   210     
   210 
   211     [scheme release];
   211     [scheme release];
   212     return result;
   212     return result;
   213 }
   213 }
   214 
   214 
   215 #pragma mark -
   215 #pragma mark -
   221 }
   221 }
   222 
   222 
   223 // wrapper that computes the length of the message and then sends the command string
   223 // wrapper that computes the length of the message and then sends the command string
   224 -(int) sendToEngine: (NSString *)string {
   224 -(int) sendToEngine: (NSString *)string {
   225     uint8_t length = [string length];
   225     uint8_t length = [string length];
   226     
   226 
   227     SDLNet_TCP_Send(csd, &length , 1);
   227     SDLNet_TCP_Send(csd, &length , 1);
   228     return SDLNet_TCP_Send(csd, [string UTF8String], length);
   228     return SDLNet_TCP_Send(csd, [string UTF8String], length);
   229 }
   229 }
   230 
   230 
   231 // method that handles net setup with engine and keeps connection alive
   231 // method that handles net setup with engine and keeps connection alive
   236     int eProto;
   236     int eProto;
   237     BOOL clientQuit;
   237     BOOL clientQuit;
   238     char buffer[BUFFER_SIZE];
   238     char buffer[BUFFER_SIZE];
   239     uint8_t msgSize;
   239     uint8_t msgSize;
   240     uint16_t gameTicks;
   240     uint16_t gameTicks;
   241     
   241 
   242     clientQuit = NO;
   242     clientQuit = NO;
   243     csd = NULL;
   243     csd = NULL;
   244 
   244 
   245     if (SDLNet_Init() < 0) {
   245     if (SDLNet_Init() < 0) {
   246         DLog(@"SDLNet_Init: %s", SDLNet_GetError());
   246         DLog(@"SDLNet_Init: %s", SDLNet_GetError());
   247         clientQuit = YES;
   247         clientQuit = YES;
   248     }
   248     }
   249     
   249 
   250     // Resolving the host using NULL make network interface to listen
   250     // Resolving the host using NULL make network interface to listen
   251     if (SDLNet_ResolveHost(&ip, NULL, ipcPort) < 0 && !clientQuit) {
   251     if (SDLNet_ResolveHost(&ip, NULL, ipcPort) < 0 && !clientQuit) {
   252         DLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError());
   252         DLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError());
   253         clientQuit = YES;
   253         clientQuit = YES;
   254     }
   254     }
   255     
   255 
   256     // Open a connection with the IP provided (listen on the host's port) 
   256     // Open a connection with the IP provided (listen on the host's port)
   257     if (!(sd = SDLNet_TCP_Open(&ip)) && !clientQuit) {
   257     if (!(sd = SDLNet_TCP_Open(&ip)) && !clientQuit) {
   258         DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), ipcPort);
   258         DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), ipcPort);
   259         clientQuit = YES;
   259         clientQuit = YES;
   260     }
   260     }
   261     
   261 
   262     DLog(@"Waiting for a client on port %d", ipcPort);
   262     DLog(@"Waiting for a client on port %d", ipcPort);
   263     while (csd == NULL) 
   263     while (csd == NULL)
   264         csd = SDLNet_TCP_Accept(sd);
   264         csd = SDLNet_TCP_Accept(sd);
   265     SDLNet_TCP_Close(sd);
   265     SDLNet_TCP_Close(sd);
   266     
   266 
   267     while (!clientQuit) {
   267     while (!clientQuit) {
   268         msgSize = 0;
   268         msgSize = 0;
   269         memset(buffer, 0, BUFFER_SIZE);
   269         memset(buffer, 0, BUFFER_SIZE);
   270         if (SDLNet_TCP_Recv(csd, &msgSize, sizeof(uint8_t)) <= 0)
   270         if (SDLNet_TCP_Recv(csd, &msgSize, sizeof(uint8_t)) <= 0)
   271             clientQuit = YES;
   271             clientQuit = YES;
   272         if (SDLNet_TCP_Recv(csd, buffer, msgSize) <=0)
   272         if (SDLNet_TCP_Recv(csd, buffer, msgSize) <=0)
   273             clientQuit = YES;
   273             clientQuit = YES;
   274         
   274 
   275         switch (buffer[0]) {
   275         switch (buffer[0]) {
   276             case 'C':
   276             case 'C':
   277                 DLog(@"sending game config...\n%@",self.gameConfig);
   277                 DLog(@"sending game config...\n%@",self.gameConfig);
   278                 
   278 
   279                 // local game
   279                 // local game
   280                 [self sendToEngine:@"TL"];
   280                 [self sendToEngine:@"TL"];
   281                 
   281 
   282                 // seed info
   282                 // seed info
   283                 [self sendToEngine:[self.gameConfig objectForKey:@"seed_command"]];
   283                 [self sendToEngine:[self.gameConfig objectForKey:@"seed_command"]];
   284                 
   284 
   285                 // dimension of the map
   285                 // dimension of the map
   286                 [self sendToEngine:[self.gameConfig objectForKey:@"templatefilter_command"]];
   286                 [self sendToEngine:[self.gameConfig objectForKey:@"templatefilter_command"]];
   287                 [self sendToEngine:[self.gameConfig objectForKey:@"mapgen_command"]];
   287                 [self sendToEngine:[self.gameConfig objectForKey:@"mapgen_command"]];
   288                 [self sendToEngine:[self.gameConfig objectForKey:@"mazesize_command"]];
   288                 [self sendToEngine:[self.gameConfig objectForKey:@"mazesize_command"]];
   289                 
   289 
   290                 // static land (if set)
   290                 // static land (if set)
   291                 NSString *staticMap = [self.gameConfig objectForKey:@"staticmap_command"];
   291                 NSString *staticMap = [self.gameConfig objectForKey:@"staticmap_command"];
   292                 if ([staticMap length] != 0)
   292                 if ([staticMap length] != 0)
   293                     [self sendToEngine:staticMap];
   293                     [self sendToEngine:staticMap];
   294                 
   294 
   295                 // theme info
   295                 // theme info
   296                 [self sendToEngine:[self.gameConfig objectForKey:@"theme_command"]];
   296                 [self sendToEngine:[self.gameConfig objectForKey:@"theme_command"]];
   297                 
   297 
   298                 // scheme (returns initial health)
   298                 // scheme (returns initial health)
   299                 NSInteger health = [self provideScheme:[self.gameConfig objectForKey:@"scheme"]];
   299                 NSInteger health = [self provideScheme:[self.gameConfig objectForKey:@"scheme"]];
   300                 
   300 
   301                 NSArray *teamsConfig = [self.gameConfig objectForKey:@"teams_list"];
   301                 NSArray *teamsConfig = [self.gameConfig objectForKey:@"teams_list"];
   302                 for (NSDictionary *teamData in teamsConfig) {
   302                 for (NSDictionary *teamData in teamsConfig) {
   303                     [self provideTeamData:[teamData objectForKey:@"team"] 
   303                     [self provideTeamData:[teamData objectForKey:@"team"]
   304                                   forHogs:[[teamData objectForKey:@"number"] intValue]
   304                                   forHogs:[[teamData objectForKey:@"number"] intValue]
   305                                withHealth:health
   305                                withHealth:health
   306                                   ofColor:[teamData objectForKey:@"color"]];
   306                                   ofColor:[teamData objectForKey:@"color"]];
   307                 }
   307                 }
   308                 
   308 
   309                 [self provideAmmoData:[self.gameConfig objectForKey:@"weapon"] forPlayingTeams:[teamsConfig count]];
   309                 [self provideAmmoData:[self.gameConfig objectForKey:@"weapon"] forPlayingTeams:[teamsConfig count]];
   310                 break;
   310                 break;
   311             case '?':
   311             case '?':
   312                 DLog(@"Ping? Pong!");
   312                 DLog(@"Ping? Pong!");
   313                 [self sendToEngine:@"!"];
   313                 [self sendToEngine:@"!"];
   318                 break;
   318                 break;
   319             case 'e':
   319             case 'e':
   320                 sscanf(buffer, "%*s %d", &eProto);
   320                 sscanf(buffer, "%*s %d", &eProto);
   321                 short int netProto = 0;
   321                 short int netProto = 0;
   322                 char *versionStr;
   322                 char *versionStr;
   323                 
   323 
   324                 HW_versionInfo(&netProto, &versionStr);
   324                 HW_versionInfo(&netProto, &versionStr);
   325                 if (netProto == eProto) {
   325                 if (netProto == eProto) {
   326                     DLog(@"Setting protocol version %d (%s)", eProto, versionStr);
   326                     DLog(@"Setting protocol version %d (%s)", eProto, versionStr);
   327                 } else {
   327                 } else {
   328                     DLog(@"ERROR - wrong protocol number: [%s] - expecting %d", &buffer[1], eProto);
   328                     DLog(@"ERROR - wrong protocol number: [%s] - expecting %d", &buffer[1], eProto);
   329                     clientQuit = YES;
   329                     clientQuit = YES;
   330                 }
   330                 }
   331                 
   331 
   332                 break;
   332                 break;
   333             case 'i':
   333             case 'i':
   334                 switch (buffer[1]) {
   334                 switch (buffer[1]) {
   335                     case 'r':
   335                     case 'r':
   336                         NSLog(@"Winning team: %s", &buffer[2]);
   336                         NSLog(@"Winning team: %s", &buffer[2]);
   349     }
   349     }
   350     DLog(@"Engine exited, closing server");
   350     DLog(@"Engine exited, closing server");
   351     // wait a little to let the client close cleanly
   351     // wait a little to let the client close cleanly
   352     [NSThread sleepForTimeInterval:2];
   352     [NSThread sleepForTimeInterval:2];
   353     // Close the client socket
   353     // Close the client socket
   354     SDLNet_TCP_Close(csd);    
   354     SDLNet_TCP_Close(csd);
   355     SDLNet_Quit();
   355     SDLNet_Quit();
   356         
   356 
   357     [pool release];
   357     [pool release];
   358     //Invoking this method should be avoided as it does not give your thread a chance to clean up any resources it allocated during its execution.
   358     //Invoking this method should be avoided as it does not give your thread a chance to clean up any resources it allocated during its execution.
   359     //[NSThread exit];
   359     //[NSThread exit];
   360 }
   360 }
   361 
   361 
   368     CGRect screenBounds = [[UIScreen mainScreen] bounds];
   368     CGRect screenBounds = [[UIScreen mainScreen] bounds];
   369     NSString *wSize = [[NSString alloc] initWithFormat:@"%d", (int) screenBounds.size.width];
   369     NSString *wSize = [[NSString alloc] initWithFormat:@"%d", (int) screenBounds.size.width];
   370     NSString *hSize = [[NSString alloc] initWithFormat:@"%d", (int) screenBounds.size.height];
   370     NSString *hSize = [[NSString alloc] initWithFormat:@"%d", (int) screenBounds.size.height];
   371     const char **gameArgs = (const char**) malloc(sizeof(char *) * 10);
   371     const char **gameArgs = (const char**) malloc(sizeof(char *) * 10);
   372     NSInteger tmpQuality;
   372     NSInteger tmpQuality;
   373     
   373 
   374     NSString *modelId = modelType();
   374     NSString *modelId = modelType();
   375     if ([modelId hasPrefix:@"iPhone1"] ||                                   // = iPhone or iPhone 3G
   375     if ([modelId hasPrefix:@"iPhone1"] ||                                   // = iPhone or iPhone 3G
   376         [modelId hasPrefix:@"iPod1,1"] || [modelId hasPrefix:@"iPod2,1"])   // = iPod Touch or iPod Touch 2G
   376         [modelId hasPrefix:@"iPod1,1"] || [modelId hasPrefix:@"iPod2,1"])   // = iPod Touch or iPod Touch 2G
   377         tmpQuality = 0x00000001 | 0x00000002 | 0x00000040;  // rqLowRes | rqBlurryLand | rqKillFlakes
   377         tmpQuality = 0x00000001 | 0x00000002 | 0x00000040;  // rqLowRes | rqBlurryLand | rqKillFlakes
   378     else if ([modelId hasPrefix:@"iPhone2"] ||                              // = iPhone 3GS
   378     else if ([modelId hasPrefix:@"iPhone2"] ||                              // = iPhone 3GS
   379              [modelId hasPrefix:@"iPod3"])                                  // = iPod Touch 3G
   379              [modelId hasPrefix:@"iPod3"])                                  // = iPod Touch 3G
   380             tmpQuality = 0x00000002 | 0x00000040;           // rqBlurryLand | rqKillFlakes 
   380             tmpQuality = 0x00000002 | 0x00000040;           // rqBlurryLand | rqKillFlakes
   381         else if ([modelId hasPrefix:@"iPad1"])                              // = iPad
   381         else if ([modelId hasPrefix:@"iPad1"])                              // = iPad
   382                 tmpQuality = 0x00000002;                    // rqBlurryLand
   382                 tmpQuality = 0x00000002;                    // rqBlurryLand
   383             else                                                            // = everything else
   383             else                                                            // = everything else
   384                 tmpQuality = 0;                             // full quality
   384                 tmpQuality = 0;                             // full quality
   385     if (![modelId hasPrefix:@"iPad"])                                       // = disable tooltips unless iPad
   385     if (![modelId hasPrefix:@"iPad"])                                       // = disable tooltips unless iPad
   386         tmpQuality = tmpQuality | 0x00000400;
   386         tmpQuality = tmpQuality | 0x00000400;
   387     
   387 
   388     gameArgs[9] = [[[NSNumber numberWithInteger:tmpQuality] stringValue] UTF8String];
   388     gameArgs[9] = [[[NSNumber numberWithInteger:tmpQuality] stringValue] UTF8String];
   389 
   389 
   390     // prevents using an empty nickname
   390     // prevents using an empty nickname
   391     NSString *username;
   391     NSString *username;
   392     NSString *originalUsername = [self.systemSettings objectForKey:@"username"];
   392     NSString *originalUsername = [self.systemSettings objectForKey:@"username"];
   393     if ([originalUsername length] == 0)
   393     if ([originalUsername length] == 0)
   394         username = [[NSString alloc] initWithFormat:@"MobileUser-%@",ipcString];
   394         username = [[NSString alloc] initWithFormat:@"MobileUser-%@",ipcString];
   395     else
   395     else
   396         username = [[NSString alloc] initWithString:originalUsername];
   396         username = [[NSString alloc] initWithString:originalUsername];
   397     
   397 
   398     gameArgs[0] = [username UTF8String];                                                        //UserNick
   398     gameArgs[0] = [username UTF8String];                                                        //UserNick
   399     gameArgs[1] = [ipcString UTF8String];                                                       //ipcPort
   399     gameArgs[1] = [ipcString UTF8String];                                                       //ipcPort
   400     gameArgs[2] = [[[self.systemSettings objectForKey:@"sound"] stringValue] UTF8String];       //isSoundEnabled
   400     gameArgs[2] = [[[self.systemSettings objectForKey:@"sound"] stringValue] UTF8String];       //isSoundEnabled
   401     gameArgs[3] = [[[self.systemSettings objectForKey:@"music"] stringValue] UTF8String];       //isMusicEnabled
   401     gameArgs[3] = [[[self.systemSettings objectForKey:@"music"] stringValue] UTF8String];       //isMusicEnabled
   402     gameArgs[4] = [localeString UTF8String];                                                    //cLocaleFName
   402     gameArgs[4] = [localeString UTF8String];                                                    //cLocaleFName
   403     gameArgs[5] = [[[self.systemSettings objectForKey:@"alternate"] stringValue] UTF8String];   //cAltDamage
   403     gameArgs[5] = [[[self.systemSettings objectForKey:@"alternate"] stringValue] UTF8String];   //cAltDamage
   404     gameArgs[6] = [wSize UTF8String];                                                           //cScreenHeight
   404     gameArgs[6] = [wSize UTF8String];                                                           //cScreenHeight
   405     gameArgs[7] = [hSize UTF8String];                                                           //cScreenWidth
   405     gameArgs[7] = [hSize UTF8String];                                                           //cScreenWidth
   406     gameArgs[8] = NULL;                                                                         //recordFileName
   406     gameArgs[8] = NULL;                                                                         //recordFileName
   407     
   407 
   408     [wSize release];
   408     [wSize release];
   409     [hSize release];
   409     [hSize release];
   410     [localeString release];
   410     [localeString release];
   411     [ipcString release];
   411     [ipcString release];
   412     [username release];
   412     [username release];