merge
authorkoda
Tue, 19 Apr 2011 00:43:39 +0200
changeset 5159 cb19cb531435
parent 5158 4941df038b95 (diff)
parent 5153 c1df8a73f916 (current diff)
child 5160 37f0cd541c0a
merge
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.h	Tue Apr 19 00:43:39 2011 +0200
@@ -0,0 +1,59 @@
+/*
+ * Hedgewars-iOS, a Hedgewars port for iOS devices
+ * Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * File created on 10/01/2010.
+ */
+
+
+#import <Foundation/Foundation.h>
+#import "SDL_net.h"
+
+@protocol EngineProtocolDelegate <NSObject>
+
+-(void) gameHasEndedWithStats:(NSArray *)stats;
+
+@end
+
+@interface EngineProtocolNetwork : NSObject {
+    id<EngineProtocolDelegate> delegate;
+
+    NSOutputStream *stream;
+    NSInteger ipcPort;              // Port on which engine will listen
+    TCPsocket csd;                  // Client socket descriptor
+}
+
+@property (nonatomic,assign) id<EngineProtocolDelegate> delegate;
+@property (nonatomic,retain) NSOutputStream *stream;
+@property (assign) NSInteger ipcPort;
+@property (assign) TCPsocket csd;
+
+
+-(id)   init;
+-(id)   initOnPort:(NSInteger) port;
+
+-(void) spawnThread:(NSString *)onSaveFile;
+-(void) spawnThread:(NSString *)onSaveFile withOptions:(NSDictionary *)dictionary;
+-(void) engineProtocol:(id) object;
+-(void) gameHasEndedWithStats:(NSArray *)stats;
+
+-(int)  sendToEngine:(NSString *)string;
+-(int)  sendToEngineNoSave:(NSString *)string;
+-(void) provideTeamData:(NSString *)teamName forHogs:(NSInteger) numberOfPlayingHogs withHealth:(NSInteger) initialHealth ofColor:(NSNumber *)teamColor;
+-(void) provideAmmoData:(NSString *)ammostoreName forPlayingTeams:(NSInteger) numberOfTeams;
+-(NSInteger) provideScheme:(NSString *)schemeName;
+
+@end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.m	Tue Apr 19 00:43:39 2011 +0200
@@ -0,0 +1,421 @@
+/*
+ * Hedgewars-iOS, a Hedgewars port for iOS devices
+ * Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * File created on 10/01/2010.
+ */
+
+
+#import "EngineProtocolNetwork.h"
+#import "PascalImports.h"
+#import "CommodityFunctions.h"
+#import "OverlayViewController.h"
+
+#define BUFFER_SIZE 255     // like in original frontend
+
+@implementation EngineProtocolNetwork
+@synthesize delegate, stream, ipcPort, csd;
+
+-(id) init {
+    if (self = [super init]) {
+        self.delegate = nil;
+
+        self.ipcPort = 0;
+        self.csd = NULL;
+        self.stream = nil;
+    }
+    return self;
+}
+
+-(id) initOnPort:(NSInteger) port {
+    if (self = [self init])
+        self.ipcPort = port;
+    return self;
+}
+
+-(void) gameHasEndedWithStats:(NSArray *)stats {
+    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(gameHasEndedWithStats:)])
+        [self.delegate gameHasEndedWithStats:stats];
+    else
+        DLog(@"Error! delegate == nil");
+}
+
+-(void) dealloc {
+    self.delegate = nil;
+    releaseAndNil(stream);
+    [super dealloc];
+}
+
+#pragma mark -
+#pragma mark Spawner functions
+-(void) spawnThread:(NSString *)onSaveFile withOptions:(NSDictionary *)dictionary {
+    self.stream = [[NSOutputStream alloc] initToFileAtPath:onSaveFile append:YES];
+    [self.stream open];
+
+    [NSThread detachNewThreadSelector:@selector(engineProtocol:)
+                             toTarget:self
+                           withObject:dictionary];
+}
+
+-(void) spawnThread:(NSString *)onSaveFile {
+    [self spawnThread:onSaveFile withOptions:nil];
+}
+
+#pragma mark -
+#pragma mark Provider functions
+// unpacks team data from the selected team.plist to a sequence of engine commands
+-(void) provideTeamData:(NSString *)teamName forHogs:(NSInteger) numberOfPlayingHogs withHealth:(NSInteger) initialHealth ofColor:(NSNumber *)teamColor {
+    /*
+     addteam <32charsMD5hash> <color> <team name>
+     addhh <level> <health> <hedgehog name>
+     <level> is 0 for human, 1-5 for bots (5 is the most stupid)
+    */
+
+    NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@", TEAMS_DIRECTORY(), teamName];
+    NSDictionary *teamData = [[NSDictionary alloc] initWithContentsOfFile:teamFile];
+    [teamFile release];
+
+    NSString *teamHashColorAndName = [[NSString alloc] initWithFormat:@"eaddteam %@ %@ %@",
+                                      [teamData objectForKey:@"hash"], [teamColor stringValue], [teamName stringByDeletingPathExtension]];
+    [self sendToEngine: teamHashColorAndName];
+    [teamHashColorAndName release];
+
+    NSString *grave = [[NSString alloc] initWithFormat:@"egrave %@", [teamData objectForKey:@"grave"]];
+    [self sendToEngine: grave];
+    [grave release];
+
+    NSString *fort = [[NSString alloc] initWithFormat:@"efort %@", [teamData objectForKey:@"fort"]];
+    [self sendToEngine: fort];
+    [fort release];
+
+    NSString *voicepack = [[NSString alloc] initWithFormat:@"evoicepack %@", [teamData objectForKey:@"voicepack"]];
+    [self sendToEngine: voicepack];
+    [voicepack release];
+
+    NSString *flag = [[NSString alloc] initWithFormat:@"eflag %@", [teamData objectForKey:@"flag"]];
+    [self sendToEngine: flag];
+    [flag release];
+
+    NSArray *hogs = [teamData objectForKey:@"hedgehogs"];
+    for (int i = 0; i < numberOfPlayingHogs; i++) {
+        NSDictionary *hog = [hogs objectAtIndex:i];
+
+        NSString *hogLevelHealthAndName = [[NSString alloc] initWithFormat:@"eaddhh %@ %d %@",
+                                           [hog objectForKey:@"level"], initialHealth, [hog objectForKey:@"hogname"]];
+        [self sendToEngine: hogLevelHealthAndName];
+        [hogLevelHealthAndName release];
+
+        NSString *hogHat = [[NSString alloc] initWithFormat:@"ehat %@", [hog objectForKey:@"hat"]];
+        [self sendToEngine: hogHat];
+        [hogHat release];
+    }
+
+    [teamData release];
+}
+
+// unpacks ammostore data from the selected ammo.plist to a sequence of engine commands
+-(void) provideAmmoData:(NSString *)ammostoreName forPlayingTeams:(NSInteger) numberOfTeams {
+    NSString *weaponPath = [[NSString alloc] initWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),ammostoreName];
+    NSDictionary *ammoData = [[NSDictionary alloc] initWithContentsOfFile:weaponPath];
+    [weaponPath release];
+
+    // if we're loading an older version of ammos fill the engine message with 0s
+    int diff = HW_getNumberOfWeapons() - [[ammoData objectForKey:@"ammostore_initialqt"] length];
+    NSString *update = @"";
+    while ([update length] < diff)
+        update = [update stringByAppendingString:@"0"];
+
+    NSString *ammloadt = [[NSString alloc] initWithFormat:@"eammloadt %@%@", [ammoData objectForKey:@"ammostore_initialqt"], update];
+    [self sendToEngine: ammloadt];
+    [ammloadt release];
+
+    NSString *ammprob = [[NSString alloc] initWithFormat:@"eammprob %@%@", [ammoData objectForKey:@"ammostore_probability"], update];
+    [self sendToEngine: ammprob];
+    [ammprob release];
+
+    NSString *ammdelay = [[NSString alloc] initWithFormat:@"eammdelay %@%@", [ammoData objectForKey:@"ammostore_delay"], update];
+    [self sendToEngine: ammdelay];
+    [ammdelay release];
+
+    NSString *ammreinf = [[NSString alloc] initWithFormat:@"eammreinf %@%@", [ammoData objectForKey:@"ammostore_crate"], update];
+    [self sendToEngine: ammreinf];
+    [ammreinf release];
+
+    // send this for each team so it applies the same ammostore to all teams
+    NSString *ammstore = [[NSString alloc] initWithString:@"eammstore"];
+    for (int i = 0; i < numberOfTeams; i++)
+        [self sendToEngine: ammstore];
+    [ammstore release];
+
+    [ammoData release];
+}
+
+// unpacks scheme data from the selected scheme.plist to a sequence of engine commands
+-(NSInteger) provideScheme:(NSString *)schemeName {
+    NSString *schemePath = [[NSString alloc] initWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),schemeName];
+    NSDictionary *schemeDictionary = [[NSDictionary alloc] initWithContentsOfFile:schemePath];
+    [schemePath release];
+    NSArray *basicArray = [schemeDictionary objectForKey:@"basic"];
+    NSArray *gamemodArray = [schemeDictionary objectForKey:@"gamemod"];
+    int result = 0;
+    int mask = 0x00000004;
+
+    // pack the gameflags in a single var and send it
+    for (NSNumber *value in gamemodArray) {
+        if ([value boolValue] == YES)
+            result |= mask;
+        mask <<= 1;
+    }
+    NSString *flags = [[NSString alloc] initWithFormat:@"e$gmflags %d",result];
+    [self sendToEngine:flags];
+    [flags release];
+
+    /* basic game flags */
+    NSString *path = [[NSString alloc] initWithFormat:@"%@/basicFlags_en.plist",IFRONTEND_DIRECTORY()];
+    NSArray *mods = [[NSArray alloc] initWithContentsOfFile:path];
+    [path release];
+
+    result = [[basicArray objectAtIndex:0] intValue];
+
+    for (int i = 1; i < [basicArray count]; i++) {
+        NSDictionary *dict = [mods objectAtIndex:i];
+        NSString *command = [dict objectForKey:@"command"];
+        NSInteger value = [[basicArray objectAtIndex:i] intValue];
+        if ([[dict objectForKey:@"checkOverMax"] boolValue] && value >= [[dict objectForKey:@"max"] intValue])
+            value = 9999;
+        if ([[dict objectForKey:@"times1000"] boolValue])
+            value = value * 1000;
+        NSString *strToSend = [[NSString alloc] initWithFormat:@"%@ %d",command,value];
+        [self sendToEngine:strToSend];
+        [strToSend release];
+    }
+    [mods release];
+
+    [schemeDictionary release];
+    return result;
+}
+
+#pragma mark -
+#pragma mark Network relevant code
+-(void) dumpRawData:(const char *)buffer ofSize:(uint8_t) length {
+    [self.stream write:&length maxLength:1];
+    [self.stream write:(const uint8_t *)buffer maxLength:length];
+}
+
+// wrapper that computes the length of the message and then sends the command string, saving the command on a file
+-(int) sendToEngine:(NSString *)string {
+    uint8_t length = [string length];
+
+    [self dumpRawData:[string UTF8String] ofSize:length];
+    SDLNet_TCP_Send(csd, &length, 1);
+    return SDLNet_TCP_Send(csd, [string UTF8String], length);
+}
+
+// wrapper that computes the length of the message and then sends the command string, skipping file writing
+-(int) sendToEngineNoSave:(NSString *)string {
+    uint8_t length = [string length];
+
+    SDLNet_TCP_Send(csd, &length, 1);
+    return SDLNet_TCP_Send(csd, [string UTF8String], length);
+}
+
+// this is launched as thread and handles all IPC with engine
+-(void) engineProtocol:(id) object {
+    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+    NSDictionary *gameConfig = (NSDictionary *)object;
+    NSMutableArray *statsArray = nil;
+    TCPsocket sd;
+    IPaddress ip;
+    int eProto;
+    BOOL clientQuit;
+    char const buffer[BUFFER_SIZE];
+    uint8_t msgSize;
+
+    clientQuit = NO;
+    csd = NULL;
+
+    if (SDLNet_Init() < 0) {
+        DLog(@"SDLNet_Init: %s", SDLNet_GetError());
+        clientQuit = YES;
+    }
+
+    // Resolving the host using NULL make network interface to listen
+    if (SDLNet_ResolveHost(&ip, NULL, ipcPort) < 0 && !clientQuit) {
+        DLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError());
+        clientQuit = YES;
+    }
+
+    // Open a connection with the IP provided (listen on the host's port)
+    if (!(sd = SDLNet_TCP_Open(&ip)) && !clientQuit) {
+        DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), ipcPort);
+        clientQuit = YES;
+    }
+
+    DLog(@"Waiting for a client on port %d", ipcPort);
+    while (csd == NULL)
+        csd = SDLNet_TCP_Accept(sd);
+    SDLNet_TCP_Close(sd);
+
+    while (!clientQuit) {
+        msgSize = 0;
+        memset((void *)buffer, '\0', BUFFER_SIZE);
+        if (SDLNet_TCP_Recv(csd, &msgSize, sizeof(uint8_t)) <= 0)
+            break;
+        if (SDLNet_TCP_Recv(csd, (void *)buffer, msgSize) <= 0)
+            break;
+
+        switch (buffer[0]) {
+            case 'C':
+                DLog(@"Sending game config...\n%@", gameConfig);
+
+                /*if (isNetGame == YES)
+                    [self sendToEngineNoSave:@"TN"];
+                else*/
+                    [self sendToEngineNoSave:@"TL"];
+                NSString *saveHeader = @"TS";
+                [self dumpRawData:[saveHeader UTF8String] ofSize:[saveHeader length]];
+
+                // seed info
+                [self sendToEngine:[gameConfig objectForKey:@"seed_command"]];
+
+                // dimension of the map
+                [self sendToEngine:[gameConfig objectForKey:@"templatefilter_command"]];
+                [self sendToEngine:[gameConfig objectForKey:@"mapgen_command"]];
+                [self sendToEngine:[gameConfig objectForKey:@"mazesize_command"]];
+
+                // static land (if set)
+                NSString *staticMap = [gameConfig objectForKey:@"staticmap_command"];
+                if ([staticMap length] != 0)
+                    [self sendToEngine:staticMap];
+
+                // lua script (if set)
+                NSString *script = [gameConfig objectForKey:@"mission_command"];
+                if ([script length] != 0)
+                    [self sendToEngine:script];
+
+                // theme info
+                [self sendToEngine:[gameConfig objectForKey:@"theme_command"]];
+
+                // scheme (returns initial health)
+                NSInteger health = [self provideScheme:[gameConfig objectForKey:@"scheme"]];
+
+                // send an ammostore for each team
+                NSArray *teamsConfig = [gameConfig objectForKey:@"teams_list"];
+                [self provideAmmoData:[gameConfig objectForKey:@"weapon"] forPlayingTeams:[teamsConfig count]];
+
+                // finally add hogs
+                for (NSDictionary *teamData in teamsConfig) {
+                    [self provideTeamData:[teamData objectForKey:@"team"]
+                                  forHogs:[[teamData objectForKey:@"number"] intValue]
+                               withHealth:health
+                                  ofColor:[teamData objectForKey:@"color"]];
+                }
+                break;
+            case '?':
+                DLog(@"Ping? Pong!");
+                [self sendToEngine:@"!"];
+                break;
+            case 'E':
+                DLog(@"ERROR - last console line: [%s]", &buffer[1]);
+                clientQuit = YES;
+                break;
+            case 'e':
+                [self dumpRawData:buffer ofSize:msgSize];
+
+                sscanf((char *)buffer, "%*s %d", &eProto);
+                int netProto;
+                char *versionStr;
+
+                HW_versionInfo(&netProto, &versionStr);
+                if (netProto == eProto) {
+                    DLog(@"Setting protocol version %d (%s)", eProto, versionStr);
+                } else {
+                    DLog(@"ERROR - wrong protocol number: %d (expecting %d)", netProto, eProto);
+                    clientQuit = YES;
+                }
+                break;
+            case 'i':
+                if (statsArray == nil) {
+                    statsArray = [[NSMutableArray alloc] initWithCapacity:10 - 2];
+                    NSMutableArray *ranking = [[NSMutableArray alloc] initWithCapacity:4];
+                    [statsArray insertObject:ranking atIndex:0];
+                    [ranking release];
+                }
+                NSString *tempStr = [NSString stringWithUTF8String:&buffer[2]];
+                NSArray *info = [tempStr componentsSeparatedByString:@" "];
+                NSString *arg = [info objectAtIndex:0];
+                int index = [arg length] + 3;
+                switch (buffer[1]) {
+                    case 'r':           // winning team
+                        [statsArray insertObject:[NSString stringWithUTF8String:&buffer[2]] atIndex:1];
+                        break;
+                    case 'D':           // best shot
+                        [statsArray addObject:[NSString stringWithFormat:@"The best shot award won by %s (with %@ points)", &buffer[index], arg]];
+                        break;
+                    case 'k':           // best hedgehog
+                        [statsArray addObject:[NSString stringWithFormat:@"The best killer is %s with %@ kills in a turn", &buffer[index], arg]];
+                        break;
+                    case 'K':           // number of hogs killed
+                        [statsArray addObject:[NSString stringWithFormat:@"%@ hedgehog(s) were killed during this round", arg]];
+                        break;
+                    case 'H':           // team health/graph
+                        break;
+                    case 'T':           // local team stats
+                        // still WIP in statsPage.cpp
+                        break;
+                    case 'P':           // teams ranking
+                        [[statsArray objectAtIndex:0] addObject:tempStr];
+                        break;
+                    case 's':           // self damage
+                        [statsArray addObject:[NSString stringWithFormat:@"%s thought it's good to shoot his own hedgehogs with %@ points", &buffer[index], arg]];
+                        break;
+                    case 'S':           // friendly fire
+                        [statsArray addObject:[NSString stringWithFormat:@"%s killed %@ of his own hedgehogs", &buffer[index], arg]];
+                        break;
+                    case 'B':           // turn skipped
+                        [statsArray addObject:[NSString stringWithFormat:@"%s was scared and skipped turn %@ times", &buffer[index], arg]];
+                        break;
+                    default:
+                        DLog(@"Unhandled stat message, see statsPage.cpp");
+                        break;
+                }
+                break;
+            case 'q':
+                // game ended, can remove the savefile and the trailing overlay (when dualhead)
+                [self gameHasEndedWithStats:statsArray];
+                break;
+            case 'Q':
+                // game exited but not completed, nothing to do (just don't save the message)
+                break;
+            default:
+                [self dumpRawData:buffer ofSize:msgSize];
+                break;
+        }
+    }
+    DLog(@"Engine exited, ending thread");
+    [self.stream close];
+
+    // Close the client socket
+    SDLNet_TCP_Close(csd);
+    SDLNet_Quit();
+
+    [pool release];
+    // 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.
+    //[NSThread exit];
+}
+
+@end
--- a/project_files/HedgewarsMobile/Classes/GameConfigViewController.m	Sun Apr 17 23:44:54 2011 -0400
+++ b/project_files/HedgewarsMobile/Classes/GameConfigViewController.m	Tue Apr 19 00:43:39 2011 +0200
@@ -24,7 +24,7 @@
 #import "TeamConfigViewController.h"
 #import "SchemeWeaponConfigViewController.h"
 #import "HelpPageViewController.h"
-#import "StatsPageViewController.h"
+#import "GameInterfaceBridge.h"
 #import "CommodityFunctions.h"
 #import "UIImageExtra.h"
 #import "PascalImports.h"
@@ -224,40 +224,13 @@
                                     self.teamConfigViewController.listOfSelectedTeams,@"teams_list",
                                     self.schemeWeaponConfigViewController.selectedScheme,@"scheme",
                                     self.schemeWeaponConfigViewController.selectedWeapon,@"weapon",
-                                    [NSNumber numberWithInt:self.interfaceOrientation],@"orientation",
                                     nil];
 
-    NSDictionary *allDataNecessary = [NSDictionary dictionaryWithObjectsAndKeys:
-                                      gameDictionary,@"game_dictionary",
-                                      [NSNumber numberWithBool:NO],@"netgame",
-                                      @"",@"savefile",
-                                      nil];
-
-    // also modify SavedGamesViewController.m
-    StatsPageViewController *statsPage = [[StatsPageViewController alloc] initWithStyle:UITableViewStyleGrouped];
-    statsPage.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
-    if ([statsPage respondsToSelector:@selector(setModalPresentationStyle:)])
-        statsPage.modalPresentationStyle = UIModalPresentationPageSheet;
+    GameInterfaceBridge *bridge = [[GameInterfaceBridge alloc] initWithController:self];
 
-    NSArray *stats;
-    if (IS_DUALHEAD()) {
-        stats = [[HedgewarsAppDelegate sharedAppDelegate] startSDLgame:allDataNecessary];
-        [self presentModalViewController:statsPage animated:NO];
-    } else {
-        [self performSelector:@selector(presentModalViewController:animated:) withObject:statsPage afterDelay:3];
-        stats = [[HedgewarsAppDelegate sharedAppDelegate] startSDLgame:allDataNecessary];
-    }
+    [bridge startLocalGame:gameDictionary];
 
-    if ([stats count] <= 1) {
-        DLog(@"%@",stats);
-        [statsPage dismissModalViewControllerAnimated:NO];
-    } else {
-        statsPage.statsArray = stats;
-        [statsPage.tableView reloadData];
-        [statsPage viewWillAppear:YES];
-    }
-
-    [statsPage release];
+    [bridge release];
 }
 
 -(void) loadNiceHogs {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/project_files/HedgewarsMobile/Classes/GameInterfaceBridge.h	Tue Apr 19 00:43:39 2011 +0200
@@ -0,0 +1,60 @@
+/*
+ * Hedgewars-iOS, a Hedgewars port for iOS devices
+ * Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * File created on 18/04/2011.
+ */
+
+
+#import <Foundation/Foundation.h>
+#import "EngineProtocolNetwork.h"
+
+typedef enum {gtNone, gtLocal, gtSave, gtNet} TGameType;
+typedef enum {gsNone, gsInGame, gsEnded, gsInterrupted} TGameStatus;
+
+@class OverlayViewController;
+
+@interface GameInterfaceBridge : NSObject <EngineProtocolDelegate> {
+    UIViewController *parentController;
+    OverlayViewController *overlayController;
+
+    NSDictionary *systemSettings;
+    NSString *savePath;
+    EngineProtocolNetwork *engineProtocol;
+
+    NSInteger ipcPort;  // Port on which engine will listen
+    TGameType gameType;
+}
+
+@property (nonatomic,retain) UIViewController *parentController;
+@property (nonatomic,retain) NSDictionary *systemSettings;
+@property (nonatomic,retain) NSString *savePath;
+
+@property (nonatomic,retain) OverlayViewController *overlayController;
+@property (nonatomic,retain) EngineProtocolNetwork *engineProtocol;
+
+@property (assign) NSInteger ipcPort;
+@property (assign) TGameType gameType;
+
+
+-(id)   initWithController:(id) viewController;
+-(void) startLocalGame:(NSDictionary *)withDictionary;
+-(void) startSaveGame:(NSString *)atPath;
+-(void) prepareEngineLaunch;
+-(void) startGameEngine;
+-(void) gameHasEndedWithStats:(NSArray *)stats;
+
+@end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m	Tue Apr 19 00:43:39 2011 +0200
@@ -0,0 +1,225 @@
+/*
+ * Hedgewars-iOS, a Hedgewars port for iOS devices
+ * Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * File created on 18/04/2011.
+ */
+
+
+#import "GameInterfaceBridge.h"
+#import "PascalImports.h"
+#import "EngineProtocolNetwork.h"
+#import "OverlayViewController.h"
+#import "StatsPageViewController.h"
+
+@implementation GameInterfaceBridge
+@synthesize parentController, systemSettings, savePath, overlayController, engineProtocol, ipcPort, gameType;
+
+-(id) initWithController:(id) viewController {
+    if (self = [super init]) {
+        self.ipcPort = randomPort();
+        self.gameType = gtNone;
+        self.savePath = nil;
+
+        self.parentController = (UIViewController *)viewController;
+        self.engineProtocol = [[EngineProtocolNetwork alloc] initOnPort:self.ipcPort];
+        self.engineProtocol.delegate = self;
+
+        self.systemSettings = [NSDictionary dictionaryWithContentsOfFile:SETTINGS_FILE()];
+        self.overlayController = [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil];
+    }
+    return self;
+}
+
+-(void) dealloc {
+    releaseAndNil(parentController);
+    releaseAndNil(engineProtocol);
+    releaseAndNil(systemSettings);
+    releaseAndNil(savePath);
+    releaseAndNil(overlayController);
+    [super dealloc];
+}
+
+#pragma mark -
+// overlay with controls, become visible later, with a transparency effect since the sdlwindow is not yet created
+-(void) displayOverlayLater:(id) object {
+    NSDictionary *dict = (NSDictionary *)object;
+
+    [self.overlayController setUseClassicMenu:[[dict objectForKey:@"menu"] boolValue]];
+    [self.overlayController setInitialOrientation:[[dict objectForKey:@"orientation"] intValue]];
+
+    UIWindow *gameWindow = (IS_DUALHEAD() ? [HedgewarsAppDelegate sharedAppDelegate].uiwindow : [[UIApplication sharedApplication] keyWindow]);
+    [gameWindow addSubview:self.overlayController.view];
+}
+
+// main routine for calling the actual game engine
+-(void) startGameEngine {
+    const char *gameArgs[11];
+    NSInteger width, height, orientation;
+    NSString *ipcString = [[NSString alloc] initWithFormat:@"%d", self.ipcPort];
+    NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]];
+
+    if (IS_DUALHEAD()) {
+        CGRect screenBounds = [[[UIScreen screens] objectAtIndex:1] bounds];
+        width = (int) screenBounds.size.width;
+        height = (int) screenBounds.size.height;
+        orientation = 0;
+    } else {
+        CGRect screenBounds = [[UIScreen mainScreen] bounds];
+        width = (int) screenBounds.size.height;
+        height = (int) screenBounds.size.width;
+        orientation = (self.parentController.interfaceOrientation == UIDeviceOrientationLandscapeLeft) ? -90 : 90;
+    }
+
+    NSString *horizontalSize = [[NSString alloc] initWithFormat:@"%d", width];
+    NSString *verticalSize = [[NSString alloc] initWithFormat:@"%d", height];
+    NSString *rotation = [[NSString alloc] initWithFormat:@"%d", orientation];
+    BOOL enhanced = [[self.systemSettings objectForKey:@"enhanced"] boolValue];
+
+    NSString *modelId = modelType();
+    NSInteger tmpQuality;
+    if ([modelId hasPrefix:@"iPhone1"] || [modelId hasPrefix:@"iPod1,1"] || [modelId hasPrefix:@"iPod2,1"])     // = iPhone and iPhone 3G or iPod Touch or iPod Touch 2G
+        tmpQuality = 0x00000001 | 0x00000002 | 0x00000008 | 0x00000040;                 // rqLowRes | rqBlurryLand | rqSimpleRope | rqKillFlakes
+    else if ([modelId hasPrefix:@"iPhone2"] || [modelId hasPrefix:@"iPod3"])                                    // = iPhone 3GS or iPod Touch 3G
+        tmpQuality = 0x00000002 | 0x00000040;                                           // rqBlurryLand | rqKillFlakes
+    else if ([modelId hasPrefix:@"iPad1"] || [modelId hasPrefix:@"iPod4"] || enhanced == NO)                    // = iPad 1G or iPod Touch 4G or not enhanced mode
+        tmpQuality = 0x00000002;                                                        // rqBlurryLand
+    else                                                                                                        // = everything else
+        tmpQuality = 0;                                                                 // full quality
+
+    // disable tooltips on iPhone
+    if (IS_IPAD() == NO)
+        tmpQuality = tmpQuality | 0x00000400;
+
+    // prevents using an empty nickname
+    NSString *username = [self.systemSettings objectForKey:@"username"];
+    if ([username length] == 0)
+        username = [NSString stringWithFormat:@"MobileUser-%@",ipcString];
+
+    gameArgs[ 0] = [ipcString UTF8String];                                                      //ipcPort
+    gameArgs[ 1] = [horizontalSize UTF8String];                                                 //cScreenWidth
+    gameArgs[ 2] = [verticalSize UTF8String];                                                   //cScreenHeight
+    gameArgs[ 3] = [[NSString stringWithFormat:@"%d",tmpQuality] UTF8String];                   //quality
+    gameArgs[ 4] = "en.txt";//[localeString UTF8String];                                        //cLocaleFName
+    gameArgs[ 5] = [username UTF8String];                                                       //UserNick
+    gameArgs[ 6] = [[[self.systemSettings objectForKey:@"sound"] stringValue] UTF8String];      //isSoundEnabled
+    gameArgs[ 7] = [[[self.systemSettings objectForKey:@"music"] stringValue] UTF8String];      //isMusicEnabled
+    gameArgs[ 8] = [[[self.systemSettings objectForKey:@"alternate"] stringValue] UTF8String];  //cAltDamage
+    gameArgs[ 9] = [rotation UTF8String];                                                       //rotateQt
+    gameArgs[10] = (self.gameType == gtSave) ? [self.savePath UTF8String] : NULL;               //recordFileName
+
+    [verticalSize release];
+    [horizontalSize release];
+    [rotation release];
+    [localeString release];
+    [ipcString release];
+
+    // this is the pascal fuction that starts the game, wrapped around isInGame
+    [HedgewarsAppDelegate sharedAppDelegate].isInGame = YES;
+    Game(gameArgs);
+    [HedgewarsAppDelegate sharedAppDelegate].isInGame = NO;
+}
+
+// prepares the controllers for hosting a game
+-(void) prepareEngineLaunch {
+    // we add a black view hiding the background
+    CGRect theFrame = CGRectMake(0, 0, self.parentController.view.frame.size.height, self.parentController.view.frame.size.width);
+    UIView *blackView = [[UIView alloc] initWithFrame:theFrame];
+    [self.parentController.view addSubview:blackView];
+    blackView.opaque = YES;
+    blackView.backgroundColor = [UIColor blackColor];
+    blackView.alpha = 0;
+    // when dual screen we apply a little transition
+    if (IS_DUALHEAD()) {
+        [UIView beginAnimations:@"fade out" context:NULL];
+        [UIView setAnimationDuration:1];
+        blackView.alpha = 1;
+        [UIView commitAnimations];
+    }
+
+    // prepare options for overlay and add it to the future sdl uiwindow
+    NSDictionary *overlayOptions = [[NSDictionary alloc] initWithObjectsAndKeys:
+                                    [NSNumber numberWithInt:self.parentController.interfaceOrientation],@"orientation",
+                                    [self.systemSettings objectForKey:@"menu"],@"menu",
+                                    nil];
+    [self performSelector:@selector(displayOverlayLater:) withObject:overlayOptions afterDelay:3];
+    [overlayOptions release];
+
+    // SYSTEMS ARE GO!!
+    [self startGameEngine];
+
+    // now we can remove the cover with a transition
+    blackView.alpha = 1;
+    [UIView beginAnimations:@"fade in" context:NULL];
+    [UIView setAnimationDuration:1];
+    blackView.alpha = 0;
+    [UIView commitAnimations];
+    [blackView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1];
+    [blackView release];
+
+    // the overlay is not needed any more and can be removed
+    [self.overlayController removeOverlay];
+
+    // warn our host that it's going to be visible again
+    [self.parentController viewWillAppear:YES];
+}
+
+// set up variables for a local game
+-(void) startLocalGame:(NSDictionary *)withDictionary {
+    self.gameType = gtLocal;
+
+    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
+    [outputFormatter setDateFormat:@"yyyy-MM-dd '@' HH.mm"];
+    NSString *newDateString = [outputFormatter stringFromDate:[NSDate date]];
+    self.savePath = [SAVES_DIRECTORY() stringByAppendingFormat:@"%@.hws", newDateString];
+    [outputFormatter release];
+
+    // in the rare case in which a savefile with the same name exists the older one must be removed (or it gets corrupted)
+    if ([[NSFileManager defaultManager] fileExistsAtPath:self.savePath])
+        [[NSFileManager defaultManager] removeItemAtPath:self.savePath error:nil];
+
+    [self.engineProtocol spawnThread:self.savePath withOptions:withDictionary];
+    [self prepareEngineLaunch];
+}
+
+// set up variables for a save game
+-(void) startSaveGame:(NSString *)atPath {
+    self.gameType = gtSave;
+    self.savePath = atPath;
+
+    [self.engineProtocol spawnThread:self.savePath];
+    [self prepareEngineLaunch];
+}
+
+-(void) gameHasEndedWithStats:(NSArray *)stats {
+    // display stats page
+    if (stats != nil) {
+        StatsPageViewController *statsPage = [[StatsPageViewController alloc] initWithStyle:UITableViewStyleGrouped];
+        statsPage.statsArray = stats;
+        statsPage.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
+        if ([statsPage respondsToSelector:@selector(setModalPresentationStyle:)])
+            statsPage.modalPresentationStyle = UIModalPresentationPageSheet;
+
+        [self.parentController presentModalViewController:statsPage animated:YES];
+        [statsPage release];
+    }
+
+    // can remove the savefile if the replay has ended
+    if (self.gameType == gtSave)
+        [[NSFileManager defaultManager] removeItemAtPath:self.savePath error:nil];
+}
+
+@end
--- a/project_files/HedgewarsMobile/Classes/GameSetup.h	Sun Apr 17 23:44:54 2011 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,55 +0,0 @@
-/*
- * Hedgewars-iOS, a Hedgewars port for iOS devices
- * Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- *
- * File created on 10/01/2010.
- */
-
-
-#import <Foundation/Foundation.h>
-#import "SDL_net.h"
-
-@interface GameSetup : NSObject {
-    NSDictionary *systemSettings;
-    NSDictionary *gameConfig;
-    NSMutableArray *statsArray;
-
-    NSInteger ipcPort;  // Port on which engine will listen
-    TCPsocket csd;      // Client socket descriptor
-    TCPsocket esd;      // External socket descriptor
-
-    NSString *savePath;
-    BOOL isNetGame;
-    BOOL menuStyle;
-}
-
-@property (nonatomic, retain) NSDictionary *systemSettings;
-@property (nonatomic, retain) NSDictionary *gameConfig;
-@property (nonatomic, retain) NSMutableArray *statsArray;
-@property (nonatomic, retain) NSString *savePath;
-@property (assign) BOOL menuStyle;
-
--(id) initWithDictionary:(NSDictionary *)gameDictionary;
--(void) engineProtocol;
--(int) sendToEngine:(NSString *)string;
--(int) sendToEngineNoSave:(NSString *)string;
--(void) provideTeamData:(NSString *)teamName forHogs:(NSInteger) numberOfPlayingHogs withHealth:(NSInteger) initialHealth ofColor:(NSNumber *)teamColor;
--(void) provideAmmoData:(NSString *)ammostoreName forPlayingTeams:(NSInteger) numberOfTeams;
--(NSInteger) provideScheme:(NSString *)schemeName;
-
--(const char **)getGameSettings:(NSString *)recordFile;
-
-@end
--- a/project_files/HedgewarsMobile/Classes/GameSetup.m	Sun Apr 17 23:44:54 2011 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,493 +0,0 @@
-/*
- * Hedgewars-iOS, a Hedgewars port for iOS devices
- * Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- *
- * File created on 10/01/2010.
- */
-
-
-#import "GameSetup.h"
-#import "PascalImports.h"
-#import "CommodityFunctions.h"
-#import "OverlayViewController.h"
-
-#define BUFFER_SIZE 255     // like in original frontend
-
-@implementation GameSetup
-@synthesize systemSettings, gameConfig, statsArray, savePath, menuStyle;
-
--(id) initWithDictionary:(NSDictionary *)gameDictionary {
-    if (self = [super init]) {
-        ipcPort = randomPort();
-
-        // the general settings file + menu style (read by the overlay)
-        NSDictionary *dictSett = [[NSDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()];
-        self.menuStyle = [[dictSett objectForKey:@"menu"] boolValue];
-        self.systemSettings = dictSett;
-        [dictSett release];
-
-        // this game run settings
-        self.gameConfig = [gameDictionary objectForKey:@"game_dictionary"];
-
-        // is it a netgame?
-        isNetGame = [[gameDictionary objectForKey:@"netgame"] boolValue];
-
-        // is it a Save?
-        NSString *path = [gameDictionary objectForKey:@"savefile"];
-        // if path is empty it means that you have to create a new file, otherwise read from that file
-        if ([path isEqualToString:@""] == YES) {
-            NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
-            [outputFormatter setDateFormat:@"yyyy-MM-dd '@' HH.mm"];
-            NSString *newDateString = [outputFormatter stringFromDate:[NSDate date]];
-            self.savePath = [SAVES_DIRECTORY() stringByAppendingFormat:@"%@.hws", newDateString];
-            [outputFormatter release];
-        } else
-            self.savePath = path;
-
-        self.statsArray = nil;
-    }
-    return self;
-}
-
--(void) dealloc {
-    [statsArray release];
-    [gameConfig release];
-    [systemSettings release];
-    [savePath release];
-    [super dealloc];
-}
-
-#pragma mark -
-#pragma mark Provider functions
-// unpacks team data from the selected team.plist to a sequence of engine commands
--(void) provideTeamData:(NSString *)teamName forHogs:(NSInteger) numberOfPlayingHogs withHealth:(NSInteger) initialHealth ofColor:(NSNumber *)teamColor {
-    /*
-     addteam <32charsMD5hash> <color> <team name>
-     addhh <level> <health> <hedgehog name>
-     <level> is 0 for human, 1-5 for bots (5 is the most stupid)
-    */
-
-    NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@", TEAMS_DIRECTORY(), teamName];
-    NSDictionary *teamData = [[NSDictionary alloc] initWithContentsOfFile:teamFile];
-    [teamFile release];
-
-    NSString *teamHashColorAndName = [[NSString alloc] initWithFormat:@"eaddteam %@ %@ %@",
-                                      [teamData objectForKey:@"hash"], [teamColor stringValue], [teamName stringByDeletingPathExtension]];
-    [self sendToEngine: teamHashColorAndName];
-    [teamHashColorAndName release];
-
-    NSString *grave = [[NSString alloc] initWithFormat:@"egrave %@", [teamData objectForKey:@"grave"]];
-    [self sendToEngine: grave];
-    [grave release];
-
-    NSString *fort = [[NSString alloc] initWithFormat:@"efort %@", [teamData objectForKey:@"fort"]];
-    [self sendToEngine: fort];
-    [fort release];
-
-    NSString *voicepack = [[NSString alloc] initWithFormat:@"evoicepack %@", [teamData objectForKey:@"voicepack"]];
-    [self sendToEngine: voicepack];
-    [voicepack release];
-
-    NSString *flag = [[NSString alloc] initWithFormat:@"eflag %@", [teamData objectForKey:@"flag"]];
-    [self sendToEngine: flag];
-    [flag release];
-
-    NSArray *hogs = [teamData objectForKey:@"hedgehogs"];
-    for (int i = 0; i < numberOfPlayingHogs; i++) {
-        NSDictionary *hog = [hogs objectAtIndex:i];
-
-        NSString *hogLevelHealthAndName = [[NSString alloc] initWithFormat:@"eaddhh %@ %d %@",
-                                           [hog objectForKey:@"level"], initialHealth, [hog objectForKey:@"hogname"]];
-        [self sendToEngine: hogLevelHealthAndName];
-        [hogLevelHealthAndName release];
-
-        NSString *hogHat = [[NSString alloc] initWithFormat:@"ehat %@", [hog objectForKey:@"hat"]];
-        [self sendToEngine: hogHat];
-        [hogHat release];
-    }
-
-    [teamData release];
-}
-
-// unpacks ammostore data from the selected ammo.plist to a sequence of engine commands
--(void) provideAmmoData:(NSString *)ammostoreName forPlayingTeams:(NSInteger) numberOfTeams {
-    NSString *weaponPath = [[NSString alloc] initWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),ammostoreName];
-    NSDictionary *ammoData = [[NSDictionary alloc] initWithContentsOfFile:weaponPath];
-    [weaponPath release];
-
-    // if we're loading an older version of ammos fill the engine message with 0s
-    int diff = HW_getNumberOfWeapons() - [[ammoData objectForKey:@"ammostore_initialqt"] length];
-    NSString *update = @"";
-    while ([update length] < diff)
-        update = [update stringByAppendingString:@"0"];
-
-    NSString *ammloadt = [[NSString alloc] initWithFormat:@"eammloadt %@%@", [ammoData objectForKey:@"ammostore_initialqt"], update];
-    [self sendToEngine: ammloadt];
-    [ammloadt release];
-
-    NSString *ammprob = [[NSString alloc] initWithFormat:@"eammprob %@%@", [ammoData objectForKey:@"ammostore_probability"], update];
-    [self sendToEngine: ammprob];
-    [ammprob release];
-
-    NSString *ammdelay = [[NSString alloc] initWithFormat:@"eammdelay %@%@", [ammoData objectForKey:@"ammostore_delay"], update];
-    [self sendToEngine: ammdelay];
-    [ammdelay release];
-
-    NSString *ammreinf = [[NSString alloc] initWithFormat:@"eammreinf %@%@", [ammoData objectForKey:@"ammostore_crate"], update];
-    [self sendToEngine: ammreinf];
-    [ammreinf release];
-
-    // send this for each team so it applies the same ammostore to all teams
-    NSString *ammstore = [[NSString alloc] initWithString:@"eammstore"];
-    for (int i = 0; i < numberOfTeams; i++)
-        [self sendToEngine: ammstore];
-    [ammstore release];
-
-    [ammoData release];
-}
-
-// unpacks scheme data from the selected scheme.plist to a sequence of engine commands
--(NSInteger) provideScheme:(NSString *)schemeName {
-    NSString *schemePath = [[NSString alloc] initWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),schemeName];
-    NSDictionary *schemeDictionary = [[NSDictionary alloc] initWithContentsOfFile:schemePath];
-    [schemePath release];
-    NSArray *basicArray = [schemeDictionary objectForKey:@"basic"];
-    NSArray *gamemodArray = [schemeDictionary objectForKey:@"gamemod"];
-    int result = 0;
-    int mask = 0x00000004;
-
-    // pack the gameflags in a single var and send it
-    for (NSNumber *value in gamemodArray) {
-        if ([value boolValue] == YES)
-            result |= mask;
-        mask <<= 1;
-    }
-    NSString *flags = [[NSString alloc] initWithFormat:@"e$gmflags %d",result];
-    [self sendToEngine:flags];
-    [flags release];
-
-    /* basic game flags */
-    NSString *path = [[NSString alloc] initWithFormat:@"%@/basicFlags_en.plist",IFRONTEND_DIRECTORY()];
-    NSArray *mods = [[NSArray alloc] initWithContentsOfFile:path];
-    [path release];
-
-    result = [[basicArray objectAtIndex:0] intValue];
-
-    for (int i = 1; i < [basicArray count]; i++) {
-        NSDictionary *dict = [mods objectAtIndex:i];
-        NSString *command = [dict objectForKey:@"command"];
-        NSInteger value = [[basicArray objectAtIndex:i] intValue];
-        if ([[dict objectForKey:@"checkOverMax"] boolValue] && value >= [[dict objectForKey:@"max"] intValue])
-            value = 9999;
-        if ([[dict objectForKey:@"times1000"] boolValue])
-            value = value * 1000;
-        NSString *strToSend = [[NSString alloc] initWithFormat:@"%@ %d",command,value];
-        [self sendToEngine:strToSend];
-        [strToSend release];
-    }
-    [mods release];
-
-    [schemeDictionary release];
-    return result;
-}
-
-#pragma mark -
-#pragma mark Network relevant code
--(void) dumpRawData:(const char *)buffer ofSize:(uint8_t) length {
-    // is it performant to reopen the stream every time?
-    NSOutputStream *os = [[NSOutputStream alloc] initToFileAtPath:self.savePath append:YES];
-    [os open];
-    [os write:&length maxLength:1];
-    [os write:(const uint8_t *)buffer maxLength:length];
-    [os close];
-    [os release];
-}
-
-// wrapper that computes the length of the message and then sends the command string, saving the command on a file
--(int) sendToEngine:(NSString *)string {
-    uint8_t length = [string length];
-
-    [self dumpRawData:[string UTF8String] ofSize:length];
-    SDLNet_TCP_Send(csd, &length, 1);
-    return SDLNet_TCP_Send(csd, [string UTF8String], length);
-}
-
-// wrapper that computes the length of the message and then sends the command string, skipping file writing
--(int) sendToEngineNoSave:(NSString *)string {
-    uint8_t length = [string length];
-
-    SDLNet_TCP_Send(csd, &length, 1);
-    return SDLNet_TCP_Send(csd, [string UTF8String], length);
-}
-
-// method that handles net setup with engine and keeps connection alive
--(void) engineProtocol {
-    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-    TCPsocket sd;
-    IPaddress ip;
-    int eProto;
-    BOOL clientQuit;
-    char const buffer[BUFFER_SIZE];
-    uint8_t msgSize;
-
-    clientQuit = NO;
-    csd = NULL;
-
-    if (SDLNet_Init() < 0) {
-        DLog(@"SDLNet_Init: %s", SDLNet_GetError());
-        clientQuit = YES;
-    }
-
-    // Resolving the host using NULL make network interface to listen
-    if (SDLNet_ResolveHost(&ip, NULL, ipcPort) < 0 && !clientQuit) {
-        DLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError());
-        clientQuit = YES;
-    }
-
-    // Open a connection with the IP provided (listen on the host's port)
-    if (!(sd = SDLNet_TCP_Open(&ip)) && !clientQuit) {
-        DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), ipcPort);
-        clientQuit = YES;
-    }
-
-    DLog(@"Waiting for a client on port %d", ipcPort);
-    while (csd == NULL)
-        csd = SDLNet_TCP_Accept(sd);
-    SDLNet_TCP_Close(sd);
-
-    while (!clientQuit) {
-        msgSize = 0;
-        memset((void *)buffer, '\0', BUFFER_SIZE);
-        if (SDLNet_TCP_Recv(csd, &msgSize, sizeof(uint8_t)) <= 0)
-            break;
-        if (SDLNet_TCP_Recv(csd, (void *)buffer, msgSize) <= 0)
-            break;
-
-        switch (buffer[0]) {
-            case 'C':
-                DLog(@"sending game config...\n%@",self.gameConfig);
-
-                if (isNetGame == YES)
-                    [self sendToEngineNoSave:@"TN"];
-                else
-                    [self sendToEngineNoSave:@"TL"];
-                NSString *saveHeader = @"TS";
-                [self dumpRawData:[saveHeader UTF8String] ofSize:[saveHeader length]];
-
-                // seed info
-                [self sendToEngine:[self.gameConfig objectForKey:@"seed_command"]];
-
-                // dimension of the map
-                [self sendToEngine:[self.gameConfig objectForKey:@"templatefilter_command"]];
-                [self sendToEngine:[self.gameConfig objectForKey:@"mapgen_command"]];
-                [self sendToEngine:[self.gameConfig objectForKey:@"mazesize_command"]];
-
-                // static land (if set)
-                NSString *staticMap = [self.gameConfig objectForKey:@"staticmap_command"];
-                if ([staticMap length] != 0)
-                    [self sendToEngine:staticMap];
-
-                // lua script (if set)
-                NSString *script = [self.gameConfig objectForKey:@"mission_command"];
-                if ([script length] != 0)
-                    [self sendToEngine:script];
-                
-                // theme info
-                [self sendToEngine:[self.gameConfig objectForKey:@"theme_command"]];
-
-                // scheme (returns initial health)
-                NSInteger health = [self provideScheme:[self.gameConfig objectForKey:@"scheme"]];
-
-                // send an ammostore for each team
-                NSArray *teamsConfig = [self.gameConfig objectForKey:@"teams_list"];
-                [self provideAmmoData:[self.gameConfig objectForKey:@"weapon"] forPlayingTeams:[teamsConfig count]];
-
-                // finally add hogs
-                for (NSDictionary *teamData in teamsConfig) {
-                    [self provideTeamData:[teamData objectForKey:@"team"]
-                                  forHogs:[[teamData objectForKey:@"number"] intValue]
-                               withHealth:health
-                                  ofColor:[teamData objectForKey:@"color"]];
-                }
-                break;
-            case '?':
-                DLog(@"Ping? Pong!");
-                [self sendToEngine:@"!"];
-                break;
-            case 'E':
-                DLog(@"ERROR - last console line: [%s]", &buffer[1]);
-                clientQuit = YES;
-                break;
-            case 'e':
-                [self dumpRawData:buffer ofSize:msgSize];
-
-                sscanf((char *)buffer, "%*s %d", &eProto);
-                int netProto;
-                char *versionStr;
-
-                HW_versionInfo(&netProto, &versionStr);
-                if (netProto == eProto) {
-                    DLog(@"Setting protocol version %d (%s)", eProto, versionStr);
-                } else {
-                    DLog(@"ERROR - wrong protocol number: %d (expecting %d)", netProto, eProto);
-                    clientQuit = YES;
-                }
-                break;
-            case 'i':
-                if (self.statsArray == nil) {
-                    self.statsArray = [[NSMutableArray alloc] initWithCapacity:10 - 2];
-                    NSMutableArray *ranking = [[NSMutableArray alloc] initWithCapacity:4];
-                    [self.statsArray insertObject:ranking atIndex:0];
-                    [ranking release];
-                }
-                NSString *tempStr = [NSString stringWithUTF8String:&buffer[2]];
-                NSArray *info = [tempStr componentsSeparatedByString:@" "];
-                NSString *arg = [info objectAtIndex:0];
-                int index = [arg length] + 3;
-                switch (buffer[1]) {
-                    case 'r':           // winning team
-                        [self.statsArray insertObject:[NSString stringWithUTF8String:&buffer[2]] atIndex:1];
-                        break;
-                    case 'D':           // best shot
-                        [self.statsArray addObject:[NSString stringWithFormat:@"The best shot award won by %s (with %@ points)", &buffer[index], arg]];
-                        break;
-                    case 'k':           // best hedgehog
-                        [self.statsArray addObject:[NSString stringWithFormat:@"The best killer is %s with %@ kills in a turn", &buffer[index], arg]];
-                        break;
-                    case 'K':           // number of hogs killed
-                        [self.statsArray addObject:[NSString stringWithFormat:@"%@ hedgehog(s) were killed during this round", arg]];
-                        break;
-                    case 'H':           // team health/graph
-                        break;
-                    case 'T':           // local team stats
-                        // still WIP in statsPage.cpp
-                        break;
-                    case 'P':           // teams ranking
-                        [[self.statsArray objectAtIndex:0] addObject:tempStr];
-                        break;
-                    case 's':           // self damage
-                        [self.statsArray addObject:[NSString stringWithFormat:@"%s thought it's good to shoot his own hedgehogs with %@ points", &buffer[index], arg]];
-                        break;
-                    case 'S':           // friendly fire
-                        [self.statsArray addObject:[NSString stringWithFormat:@"%s killed %@ of his own hedgehogs", &buffer[index], arg]];
-                        break;
-                    case 'B':           // turn skipped
-                        [self.statsArray addObject:[NSString stringWithFormat:@"%s was scared and skipped turn %@ times", &buffer[index], arg]];
-                        break;
-                    default:
-                        DLog(@"Unhandled stat message, see statsPage.cpp");
-                        break;
-                }
-                break;
-            case 'q':
-                // game ended, can remove the savefile and the trailing overlay (when dualhead)
-                [[NSFileManager defaultManager] removeItemAtPath:self.savePath error:nil];
-                if (IS_DUALHEAD())
-                    [[NSNotificationCenter defaultCenter] postNotificationName:@"remove overlay" object:nil];
-                break;
-            case 'Q':
-                // game exited but not completed, nothing to do (just don't save the message)
-                break;
-            default:
-                [self dumpRawData:buffer ofSize:msgSize];
-                break;
-        }
-    }
-    DLog(@"Engine exited, ending thread");
-
-    // Close the client socket
-    SDLNet_TCP_Close(csd);
-    SDLNet_Quit();
-
-    [pool release];
-    // 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.
-    //[NSThread exit];
-}
-
-#pragma mark -
-#pragma mark Setting methods
-// returns an array of c-strings that are read by engine at startup
--(const char **)getGameSettings:(NSString *)recordFile {
-    NSInteger width, height;
-    NSString *ipcString = [[NSString alloc] initWithFormat:@"%d", ipcPort];
-    NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]];
-    NSString *rotation;
-    if (IS_DUALHEAD()) {
-        CGRect screenBounds = [[[UIScreen screens] objectAtIndex:1] bounds];
-        width = (int) screenBounds.size.width;
-        height = (int) screenBounds.size.height;
-        rotation = @"0";
-    } else {
-        CGRect screenBounds = [[UIScreen mainScreen] bounds];
-        width = (int) screenBounds.size.height;
-        height = (int) screenBounds.size.width;
-        UIDeviceOrientation orientation = (UIDeviceOrientation) [[self.gameConfig objectForKey:@"orientation"] intValue];
-        if (orientation == UIDeviceOrientationLandscapeLeft)
-            rotation = @"-90";
-        else
-            rotation = @"90";
-    }
-        
-    NSString *horizontalSize = [[NSString alloc] initWithFormat:@"%d", width];
-    NSString *verticalSize = [[NSString alloc] initWithFormat:@"%d", height];
-    const char **gameArgs = (const char **)malloc(sizeof(char *) * 10);
-    BOOL enhanced = [[self.systemSettings objectForKey:@"enhanced"] boolValue];
-
-    NSString *modelId = modelType();
-    NSInteger tmpQuality;
-    if ([modelId hasPrefix:@"iPhone1"] || [modelId hasPrefix:@"iPod1,1"] || [modelId hasPrefix:@"iPod2,1"])     // = iPhone and iPhone 3G or iPod Touch or iPod Touch 2G
-        tmpQuality = 0x00000001 | 0x00000002 | 0x00000008 | 0x00000040;                 // rqLowRes | rqBlurryLand | rqSimpleRope | rqKillFlakes
-    else if ([modelId hasPrefix:@"iPhone2"] || [modelId hasPrefix:@"iPod3"])                                    // = iPhone 3GS or iPod Touch 3G
-        tmpQuality = 0x00000002 | 0x00000040;                                           // rqBlurryLand | rqKillFlakes
-    else if ([modelId hasPrefix:@"iPad1"] || [modelId hasPrefix:@"iPod4"] || enhanced == NO)                    // = iPad 1G or iPod Touch 4G or not enhanced mode
-        tmpQuality = 0x00000002;                                                        // rqBlurryLand
-    else                                                                                                        // = everything else
-        tmpQuality = 0;                                                                 // full quality
-
-    if (IS_IPAD() == NO)                                                                                        // = disable tooltips on phone
-        tmpQuality = tmpQuality | 0x00000400;
-
-    // prevents using an empty nickname
-    NSString *username;
-    NSString *originalUsername = [self.systemSettings objectForKey:@"username"];
-    if ([originalUsername length] == 0)
-        username = [[NSString alloc] initWithFormat:@"MobileUser-%@",ipcString];
-    else
-        username = [[NSString alloc] initWithString:originalUsername];
-
-    gameArgs[ 0] = [ipcString UTF8String];                                                       //ipcPort
-    gameArgs[ 1] = [horizontalSize UTF8String];                                                  //cScreenWidth
-    gameArgs[ 2] = [verticalSize UTF8String];                                                    //cScreenHeight
-    gameArgs[ 3] = [[[NSNumber numberWithInteger:tmpQuality] stringValue] UTF8String];           //quality
-    gameArgs[ 4] = "en.txt";//[localeString UTF8String];                                                    //cLocaleFName
-    gameArgs[ 5] = [username UTF8String];                                                        //UserNick
-    gameArgs[ 6] = [[[self.systemSettings objectForKey:@"sound"] stringValue] UTF8String];       //isSoundEnabled
-    gameArgs[ 7] = [[[self.systemSettings objectForKey:@"music"] stringValue] UTF8String];       //isMusicEnabled
-    gameArgs[ 8] = [[[self.systemSettings objectForKey:@"alternate"] stringValue] UTF8String];   //cAltDamage
-    gameArgs[ 9] = [rotation UTF8String];                                                        //rotateQt
-    gameArgs[10] = [recordFile UTF8String];                                                      //recordFileName
-
-    [verticalSize release];
-    [horizontalSize release];
-    [localeString release];
-    [ipcString release];
-    [username release];
-    return gameArgs;
-}
-
-
-@end
--- a/project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.h	Sun Apr 17 23:44:54 2011 -0400
+++ b/project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.h	Tue Apr 19 00:43:39 2011 +0200
@@ -23,11 +23,9 @@
 #import "SDL_uikitappdelegate.h"
 
 @class MainMenuViewController;
-@class OverlayViewController;
 
-@interface HedgewarsAppDelegate:SDLUIKitDelegate {
+@interface HedgewarsAppDelegate : SDLUIKitDelegate {
     MainMenuViewController *mainViewController;
-    OverlayViewController *overlayController;
     UIWindow *uiwindow;
     UIWindow *secondWindow;
     BOOL isInGame;
@@ -35,13 +33,10 @@
 
 @property (assign) BOOL isInGame;
 @property (nonatomic,retain) MainMenuViewController *mainViewController;
-@property (nonatomic,retain) OverlayViewController *overlayController;
 @property (nonatomic,retain) UIWindow *uiwindow;
 @property (nonatomic,retain) UIWindow *secondWindow;
 
 +(HedgewarsAppDelegate *)sharedAppDelegate;
--(NSArray *)startSDLgame:(NSDictionary *)gameDictionary;
--(void) displayOverlayLater:(id) object;
 
 @end
 
--- a/project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.m	Sun Apr 17 23:44:54 2011 -0400
+++ b/project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.m	Tue Apr 19 00:43:39 2011 +0200
@@ -23,9 +23,7 @@
 #import "PascalImports.h"
 #import "ObjcExports.h"
 #import "CommodityFunctions.h"
-#import "GameSetup.h"
 #import "MainMenuViewController.h"
-#import "OverlayViewController.h"
 #import "Appirater.h"
 #include <unistd.h>
 
@@ -45,7 +43,7 @@
 @end
 
 @implementation HedgewarsAppDelegate
-@synthesize mainViewController, overlayController, uiwindow, secondWindow, isInGame;
+@synthesize mainViewController, uiwindow, secondWindow, isInGame;
 
 // convenience method
 +(HedgewarsAppDelegate *)sharedAppDelegate {
@@ -64,108 +62,11 @@
 
 -(void) dealloc {
     [mainViewController release];
-    [overlayController release];
     [uiwindow release];
     [secondWindow release];
     [super dealloc];
 }
 
-// main routine for calling the actual game engine
--(NSArray *)startSDLgame:(NSDictionary *)gameDictionary {
-    UIWindow *gameWindow;
-    if (IS_DUALHEAD())
-        gameWindow = self.secondWindow;
-    else
-        gameWindow = self.uiwindow;
-
-    UIView *blackView = [[UIView alloc] initWithFrame:gameWindow.frame];
-    blackView.backgroundColor = [UIColor blackColor];
-    blackView.opaque = YES;
-    blackView.tag = BLACKVIEW_TAG;
-    [gameWindow addSubview:blackView];
-    if (IS_DUALHEAD()) {
-        blackView.alpha = 0;
-        [UIView beginAnimations:@"fading to game first" context:NULL];
-        [UIView setAnimationDuration:1];
-        blackView.alpha = 1;
-        [UIView commitAnimations];
-
-        UIView *secondBlackView = [[UIView alloc] initWithFrame:self.uiwindow.frame];
-        secondBlackView.backgroundColor = [UIColor blackColor];
-        secondBlackView.opaque = YES;
-        secondBlackView.tag = SECONDBLACKVIEW_TAG;
-        secondBlackView.alpha = 0;
-        [self.uiwindow addSubview:secondBlackView];
-        [UIView beginAnimations:@"fading to game second" context:NULL];
-        [UIView setAnimationDuration:1];
-        secondBlackView.alpha = 1;
-        [UIView commitAnimations];
-        [secondBlackView release];
-    }
-    [blackView release];
-
-
-    // pull out useful configuration info from various files
-    GameSetup *setup = [[GameSetup alloc] initWithDictionary:gameDictionary];
-    NSNumber *isNetGameNum = [gameDictionary objectForKey:@"netgame"];
-
-    [NSThread detachNewThreadSelector:@selector(engineProtocol)
-                             toTarget:setup
-                           withObject:nil];
-
-    NSNumber *menuStyle = [NSNumber numberWithBool:setup.menuStyle];
-    NSNumber *orientation = [[gameDictionary objectForKey:@"game_dictionary"] objectForKey:@"orientation"];
-    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
-                          isNetGameNum,@"net",
-                          menuStyle,@"menu",
-                          orientation,@"orientation",
-                          nil];
-    [self performSelector:@selector(displayOverlayLater:) withObject:dict afterDelay:1];
-
-    // need to set again [gameDictionary objectForKey:@"savefile"] because if it's empty it means it's a normal game
-    const char **gameArgs = [setup getGameSettings:[gameDictionary objectForKey:@"savefile"]];
-    self.isInGame = YES;
-    // this is the pascal fuction that starts the game
-    Game(gameArgs);
-    self.isInGame = NO;
-    free(gameArgs);
-
-    NSArray *stats = setup.statsArray;
-    [setup release];
-
-
-    [self.uiwindow makeKeyAndVisible];
-    [self.uiwindow bringSubviewToFront:self.mainViewController.view];
-
-    UIView *refBlackView = [gameWindow viewWithTag:BLACKVIEW_TAG];
-    UIView *refSecondBlackView = [self.uiwindow viewWithTag:SECONDBLACKVIEW_TAG];
-    [UIView beginAnimations:@"fading in from ingame" context:NULL];
-    [UIView setAnimationDuration:1];
-    refBlackView.alpha = 0;
-    refSecondBlackView.alpha = 0;
-    [UIView commitAnimations];
-    [refBlackView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1];
-    [refSecondBlackView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:2];
-
-    return stats;
-}
-
-// overlay with controls, become visible later, with a transparency effect since the sdlwindow is not yet created
--(void) displayOverlayLater:(id) object {
-    NSDictionary *dict = (NSDictionary *)object;
-    self.overlayController = [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil];
-    self.overlayController.isNetGame = [[dict objectForKey:@"net"] boolValue];
-    self.overlayController.useClassicMenu = [[dict objectForKey:@"menu"] boolValue];
-    self.overlayController.initialOrientation = [[dict objectForKey:@"orientation"] intValue];
-
-    UIWindow *gameWindow;
-    if (IS_DUALHEAD())
-        gameWindow = self.uiwindow;
-    else
-        gameWindow = [[UIApplication sharedApplication] keyWindow];
-    [gameWindow addSubview:self.overlayController.view];
-}
-
 // override the direct execution of SDL_main to allow us to implement our own frontend
 -(void) postFinishLaunch {
     [[UIApplication sharedApplication] setStatusBarHidden:YES];
@@ -173,10 +74,8 @@
 
     self.uiwindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 
-    if (IS_IPAD())
-        self.mainViewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController-iPad" bundle:nil];
-    else
-        self.mainViewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController-iPhone" bundle:nil];
+    NSString *controllerName = (IS_IPAD() ? @"MainMenuViewController-iPad" : @"MainMenuViewController-iPhone");
+    self.mainViewController = [[MainMenuViewController alloc] initWithNibName:controllerName bundle:nil];
 
     [self.uiwindow addSubview:self.mainViewController.view];
     [self.mainViewController release];
@@ -185,7 +84,7 @@
 
     // check for dual monitor support
     if (IS_DUALHEAD()) {
-        DLog(@"dual head mode ftw");
+        DLog(@"Dualhead mode");
         self.secondWindow = [[UIWindow alloc] initWithFrame:[[[UIScreen screens] objectAtIndex:1] bounds]];
         self.secondWindow.backgroundColor = [UIColor blackColor];
         self.secondWindow.screen = [[UIScreen screens] objectAtIndex:1];
--- a/project_files/HedgewarsMobile/Classes/InGameMenuViewController.m	Sun Apr 17 23:44:54 2011 -0400
+++ b/project_files/HedgewarsMobile/Classes/InGameMenuViewController.m	Tue Apr 19 00:43:39 2011 +0200
@@ -88,8 +88,8 @@
         [self.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.35];
     }
 
-    HW_chatEnd();
-    SDL_iPhoneKeyboardHide((SDL_Window *)HW_getSDLWindow());
+//    HW_chatEnd();
+//    SDL_iPhoneKeyboardHide((SDL_Window *)HW_getSDLWindow());
 
     if (shouldTakeScreenshot) {
         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please wait"
--- a/project_files/HedgewarsMobile/Classes/OverlayViewController.h	Sun Apr 17 23:44:54 2011 -0400
+++ b/project_files/HedgewarsMobile/Classes/OverlayViewController.h	Tue Apr 19 00:43:39 2011 +0200
@@ -49,7 +49,6 @@
     BOOL isAttacking;
     
     // stuff initialized externally
-    BOOL isNetGame;
     BOOL useClassicMenu;
     NSInteger initialOrientation;
     
@@ -62,7 +61,6 @@
 @property (nonatomic,retain) InGameMenuViewController *popupMenu;
 @property (nonatomic,retain) HelpPageViewController *helpPage;
 @property (nonatomic,retain) AmmoMenuViewController *amvc;
-@property (assign) BOOL isNetGame;
 @property (assign) BOOL useClassicMenu;
 @property (assign) NSInteger initialOrientation;
 
@@ -78,5 +76,6 @@
 -(void) dismissPopover;
 -(void) dimOverlay;
 -(void) activateOverlay;
+-(void) removeOverlay;
 
 @end
--- a/project_files/HedgewarsMobile/Classes/OverlayViewController.m	Sun Apr 17 23:44:54 2011 -0400
+++ b/project_files/HedgewarsMobile/Classes/OverlayViewController.m	Tue Apr 19 00:43:39 2011 +0200
@@ -39,7 +39,7 @@
                             [[self.view viewWithTag:GRENADE_TAG] removeFromSuperview];
 
 @implementation OverlayViewController
-@synthesize popoverController, popupMenu, helpPage, amvc, isNetGame, useClassicMenu, initialOrientation;
+@synthesize popoverController, popupMenu, helpPage, amvc, useClassicMenu, initialOrientation;
 
 #pragma mark -
 #pragma mark rotation
@@ -188,12 +188,6 @@
                                                  name:@"show help ingame"
                                                object:nil];
 
-    // remove the view, required by the dual head version
-    [[NSNotificationCenter defaultCenter] addObserver:self
-                                             selector:@selector(removeOverlay:)
-                                                 name:@"remove overlay"
-                                               object:nil];
-
     // for iOS >= 3.2
     if ([UIScreen respondsToSelector:@selector(screens)]) {
         [[NSNotificationCenter defaultCenter] addObserver:self
@@ -243,12 +237,8 @@
 
 -(void) showHelp:(id) sender {
     if (self.helpPage == nil) {
-        NSString *xib;
-        if (IS_IPAD())
-            xib = @"HelpPageInGameViewController-iPad";
-        else
-            xib = @"HelpPageInGameViewController-iPhone";
-        self.helpPage = [[HelpPageViewController alloc] initWithNibName:xib bundle:nil];
+        NSString *xibName = (IS_IPAD() ? @"HelpPageInGameViewController-iPad" : @"HelpPageInGameViewController-iPhone");
+        self.helpPage = [[HelpPageViewController alloc] initWithNibName:xibName bundle:nil];
     }
     self.helpPage.view.alpha = 0;
     [self.view addSubview:helpPage.view];
@@ -258,11 +248,10 @@
     doNotDim();
 }
 
--(void) removeOverlay:(id) sender {
+-(void) removeOverlay {
     [self.popupMenu performSelectorOnMainThread:@selector(dismiss) withObject:nil waitUntilDone:YES];
     [self.popoverController performSelectorOnMainThread:@selector(dismissPopoverAnimated:) withObject:nil waitUntilDone:YES];
     [self.view performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:YES];
-    HW_terminate(NO);
 }
 
 -(void) didReceiveMemoryWarning {
@@ -275,7 +264,7 @@
     if (IS_IPAD())
         if (((UIPopoverController *)self.popoverController).contentViewController.view.superview == nil)
             self.popoverController = nil;
-    
+
     MSG_MEMCLEAN();
     [super didReceiveMemoryWarning];
 }
--- a/project_files/HedgewarsMobile/Classes/SavedGamesViewController.m	Sun Apr 17 23:44:54 2011 -0400
+++ b/project_files/HedgewarsMobile/Classes/SavedGamesViewController.m	Tue Apr 19 00:43:39 2011 +0200
@@ -20,7 +20,7 @@
 
 
 #import "SavedGamesViewController.h"
-#import "StatsPageViewController.h"
+#import "GameInterfaceBridge.h"
 #import "CommodityFunctions.h"
 
 @implementation SavedGamesViewController
@@ -208,42 +208,14 @@
         [self updateTable];
 
     [(EditableCellView *)[self.tableView cellForRowAtIndexPath:indexPath] save:nil];
-    
-    NSString *filePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),[self.listOfSavegames objectAtIndex:[indexPath row]]];
-    
-    NSDictionary *allDataNecessary = [NSDictionary dictionaryWithObjectsAndKeys:
-                                      filePath,@"savefile",
-                                      [NSNumber numberWithBool:NO],@"netgame",
-                                      [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:self.interfaceOrientation] forKey:@"orientation"],@"game_dictionary",
-                                      nil];
 
-    // also modify GameConfigViewController.m
-    StatsPageViewController *statsPage = [[StatsPageViewController alloc] initWithStyle:UITableViewStyleGrouped];
-    statsPage.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
-    if ([statsPage respondsToSelector:@selector(setModalPresentationStyle:)])
-        statsPage.modalPresentationStyle = UIModalPresentationPageSheet;
-    // avoid showing the stat page immediately, but wait for 3 seconds
-    [self performSelector:@selector(presentModalViewController:animated:) withObject:statsPage afterDelay:3];
+    GameInterfaceBridge *bridge = [[GameInterfaceBridge alloc] initWithController:self];
 
-    NSArray *stats;
-    if (IS_DUALHEAD()) {
-        stats = [[HedgewarsAppDelegate sharedAppDelegate] startSDLgame:allDataNecessary];
-        [self presentModalViewController:statsPage animated:NO];
-    } else {
-        [self performSelector:@selector(presentModalViewController:animated:) withObject:statsPage afterDelay:3];
-        stats = [[HedgewarsAppDelegate sharedAppDelegate] startSDLgame:allDataNecessary];
-    }
+    NSString *filePath = [[NSString alloc] initWithFormat:@"%@/%@",SAVES_DIRECTORY(),[self.listOfSavegames objectAtIndex:[indexPath row]]];
+    [bridge startSaveGame:filePath];
+    [filePath release];
 
-    if ([stats count] <= 1) {
-        DLog(@"%@",stats);
-        [statsPage dismissModalViewControllerAnimated:NO];
-    } else {
-        statsPage.statsArray = stats;
-        [statsPage.tableView reloadData];
-        [statsPage viewWillAppear:YES];
-    }
-    // reload needed because when ending game the entry remains there
-    [self.tableView reloadData];
+    [bridge release];
 }
 
 #pragma mark -
--- a/project_files/HedgewarsMobile/Hedgewars.xcodeproj/project.pbxproj	Sun Apr 17 23:44:54 2011 -0400
+++ b/project_files/HedgewarsMobile/Hedgewars.xcodeproj/project.pbxproj	Tue Apr 19 00:43:39 2011 +0200
@@ -85,7 +85,7 @@
 		6165920D11CA9BA200D6E256 /* FlagsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 616591E111CA9BA200D6E256 /* FlagsViewController.m */; };
 		6165920E11CA9BA200D6E256 /* FortsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 616591E311CA9BA200D6E256 /* FortsViewController.m */; };
 		6165920F11CA9BA200D6E256 /* GameConfigViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 616591E511CA9BA200D6E256 /* GameConfigViewController.m */; };
-		6165921011CA9BA200D6E256 /* GameSetup.m in Sources */ = {isa = PBXBuildFile; fileRef = 616591E711CA9BA200D6E256 /* GameSetup.m */; };
+		6165921011CA9BA200D6E256 /* EngineProtocolNetwork.m in Sources */ = {isa = PBXBuildFile; fileRef = 616591E711CA9BA200D6E256 /* EngineProtocolNetwork.m */; };
 		6165921111CA9BA200D6E256 /* GeneralSettingsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 616591E911CA9BA200D6E256 /* GeneralSettingsViewController.m */; };
 		6165921211CA9BA200D6E256 /* GravesViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 616591EB11CA9BA200D6E256 /* GravesViewController.m */; };
 		6165921311CA9BA200D6E256 /* HogHatViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 616591ED11CA9BA200D6E256 /* HogHatViewController.m */; };
@@ -199,6 +199,7 @@
 		61E2F7451283752C00E12521 /* tw.png in Resources */ = {isa = PBXBuildFile; fileRef = 61E2F7431283752C00E12521 /* tw.png */; };
 		61E5D68D12AB006F00566F29 /* uLandPainted.pas in Sources */ = {isa = PBXBuildFile; fileRef = 61E5D68C12AB006F00566F29 /* uLandPainted.pas */; };
 		61EBA62A11DFF2BC0048B68A /* title.png in Resources */ = {isa = PBXBuildFile; fileRef = 61EBA62811DFF2BC0048B68A /* title.png */; };
+		61EDB5B0135B3F97009B29A6 /* GameInterfaceBridge.m in Sources */ = {isa = PBXBuildFile; fileRef = 61EDB5AF135B3F97009B29A6 /* GameInterfaceBridge.m */; };
 		61EF920E11DF57AC003441C4 /* arrowDown.png in Resources */ = {isa = PBXBuildFile; fileRef = 61EF920511DF57AC003441C4 /* arrowDown.png */; };
 		61EF920F11DF57AC003441C4 /* arrowLeft.png in Resources */ = {isa = PBXBuildFile; fileRef = 61EF920611DF57AC003441C4 /* arrowLeft.png */; };
 		61EF921011DF57AC003441C4 /* arrowRight.png in Resources */ = {isa = PBXBuildFile; fileRef = 61EF920711DF57AC003441C4 /* arrowRight.png */; };
@@ -826,8 +827,8 @@
 		616591E311CA9BA200D6E256 /* FortsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FortsViewController.m; sourceTree = "<group>"; };
 		616591E411CA9BA200D6E256 /* GameConfigViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GameConfigViewController.h; sourceTree = "<group>"; };
 		616591E511CA9BA200D6E256 /* GameConfigViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GameConfigViewController.m; sourceTree = "<group>"; };
-		616591E611CA9BA200D6E256 /* GameSetup.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GameSetup.h; sourceTree = "<group>"; };
-		616591E711CA9BA200D6E256 /* GameSetup.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GameSetup.m; sourceTree = "<group>"; };
+		616591E611CA9BA200D6E256 /* EngineProtocolNetwork.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EngineProtocolNetwork.h; sourceTree = "<group>"; };
+		616591E711CA9BA200D6E256 /* EngineProtocolNetwork.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EngineProtocolNetwork.m; sourceTree = "<group>"; };
 		616591E811CA9BA200D6E256 /* GeneralSettingsViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GeneralSettingsViewController.h; sourceTree = "<group>"; };
 		616591E911CA9BA200D6E256 /* GeneralSettingsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneralSettingsViewController.m; sourceTree = "<group>"; };
 		616591EA11CA9BA200D6E256 /* GravesViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GravesViewController.h; sourceTree = "<group>"; };
@@ -985,6 +986,8 @@
 		61E2F7431283752C00E12521 /* tw.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = tw.png; path = Resources/Icons/tw.png; sourceTree = "<group>"; };
 		61E5D68C12AB006F00566F29 /* uLandPainted.pas */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.pascal; name = uLandPainted.pas; path = ../../hedgewars/uLandPainted.pas; sourceTree = SOURCE_ROOT; };
 		61EBA62811DFF2BC0048B68A /* title.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = title.png; path = Resources/Frontend/title.png; sourceTree = "<group>"; };
+		61EDB5AE135B3F97009B29A6 /* GameInterfaceBridge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GameInterfaceBridge.h; sourceTree = "<group>"; };
+		61EDB5AF135B3F97009B29A6 /* GameInterfaceBridge.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GameInterfaceBridge.m; sourceTree = "<group>"; };
 		61EF920511DF57AC003441C4 /* arrowDown.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = arrowDown.png; path = Resources/Overlay/arrowDown.png; sourceTree = "<group>"; };
 		61EF920611DF57AC003441C4 /* arrowLeft.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = arrowLeft.png; path = Resources/Overlay/arrowLeft.png; sourceTree = "<group>"; };
 		61EF920711DF57AC003441C4 /* arrowRight.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = arrowRight.png; path = Resources/Overlay/arrowRight.png; sourceTree = "<group>"; };
@@ -1054,8 +1057,10 @@
 				616591F111CA9BA200D6E256 /* MainMenuViewController.m */,
 				6165924B11CA9CB400D6E256 /* MainMenuViewController-iPad.xib */,
 				6165924C11CA9CB400D6E256 /* MainMenuViewController-iPhone.xib */,
-				616591E611CA9BA200D6E256 /* GameSetup.h */,
-				616591E711CA9BA200D6E256 /* GameSetup.m */,
+				61EDB5AE135B3F97009B29A6 /* GameInterfaceBridge.h */,
+				61EDB5AF135B3F97009B29A6 /* GameInterfaceBridge.m */,
+				616591E611CA9BA200D6E256 /* EngineProtocolNetwork.h */,
+				616591E711CA9BA200D6E256 /* EngineProtocolNetwork.m */,
 				61E2E12C12BAAEE30051B659 /* ServerSetup.h */,
 				61E2E12D12BAAEE30051B659 /* ServerSetup.m */,
 			);
@@ -2382,7 +2387,7 @@
 				6165920D11CA9BA200D6E256 /* FlagsViewController.m in Sources */,
 				6165920E11CA9BA200D6E256 /* FortsViewController.m in Sources */,
 				6165920F11CA9BA200D6E256 /* GameConfigViewController.m in Sources */,
-				6165921011CA9BA200D6E256 /* GameSetup.m in Sources */,
+				6165921011CA9BA200D6E256 /* EngineProtocolNetwork.m in Sources */,
 				6165921111CA9BA200D6E256 /* GeneralSettingsViewController.m in Sources */,
 				6165921211CA9BA200D6E256 /* GravesViewController.m in Sources */,
 				6165921311CA9BA200D6E256 /* HogHatViewController.m in Sources */,
@@ -2438,6 +2443,7 @@
 				61AC067412B2E32D000B52A2 /* Appirater.m in Sources */,
 				61E2E12E12BAAEE30051B659 /* ServerSetup.m in Sources */,
 				61B7A33812CC21080086B604 /* StatsPageViewController.m in Sources */,
+				61EDB5B0135B3F97009B29A6 /* GameInterfaceBridge.m in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};