# HG changeset patch # User koda # Date 1303066376 -7200 # Node ID 851f36579ed45b1b1f64e0ba3a66ce930cb349b7 # Parent 589f69a9665c9b46d015a88d5bc6e31f5d0b6cf1 initial refactoring for interfacing the game engine from the ios frontend (game doesn't run yet) diff -r 589f69a9665c -r 851f36579ed4 project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.h Sun Apr 17 20:52:56 2011 +0200 @@ -0,0 +1,50 @@ +/* + * Hedgewars-iOS, a Hedgewars port for iOS devices + * Copyright (c) 2009-2011 Vittorio Giovara + * + * 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 +#import "SDL_net.h" + +@interface EngineProtocolNetwork : NSObject { + NSMutableArray *statsArray; + NSString *savePath; + NSDictionary *gameConfig; + + NSInteger ipcPort; // Port on which engine will listen + TCPsocket csd; // Client socket descriptor +} + +@property (nonatomic,retain) NSMutableArray *statsArray; +@property (nonatomic,retain) NSString *savePath; +@property (nonatomic,retain) NSDictionary *gameConfig; +@property (assign) NSInteger ipcPort; +@property (assign) TCPsocket csd; + + +-(id) init; +-(void) engineProtocol; +-(void) spawnThreadOnPort:(NSInteger) port; +-(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 diff -r 589f69a9665c -r 851f36579ed4 project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.m Sun Apr 17 20:52:56 2011 +0200 @@ -0,0 +1,405 @@ +/* + * Hedgewars-iOS, a Hedgewars port for iOS devices + * Copyright (c) 2009-2011 Vittorio Giovara + * + * 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 statsArray, savePath, gameConfig, ipcPort, csd; + +-(id) init { + if (self = [super init]) { + self.savePath = nil; + self.statsArray = nil; + self.gameConfig = nil; + self.ipcPort = 0; + } + return self; +} + +-(void) dealloc { + [statsArray release]; + [savePath release]; + [gameConfig release]; + [super dealloc]; +} + +-(void) spawnThreadOnPort:(NSInteger) port { + self.ipcPort = port; + + [NSThread detachNewThreadSelector:@selector(engineProtocol) + toTarget:self + withObject: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> + addhh + 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); +} + +// this is launched as thread and handles all IPC with engine +-(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]; +} + +@end diff -r 589f69a9665c -r 851f36579ed4 project_files/HedgewarsMobile/Classes/GameInterfaceBridge.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/project_files/HedgewarsMobile/Classes/GameInterfaceBridge.h Sun Apr 17 20:52:56 2011 +0200 @@ -0,0 +1,73 @@ +/* + * Hedgewars-iOS, a Hedgewars port for iOS devices + * Copyright (c) 2009-2011 Vittorio Giovara + * + * 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 + +typedef enum {gtNone, gtLocal, gtSave, gtNet} TGameType; + +@class OverlayViewController; +@class EngineProtocolNetwork; + +@interface GameInterfaceBridge : NSObject { + 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; +-(const char **)gatherGameSettings; +-(void) startGameEngine; + +/* +@property (nonatomic, retain) NSDictionary *systemSettings; +@property (nonatomic, retain) NSMutableArray *statsArray; +@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 diff -r 589f69a9665c -r 851f36579ed4 project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m Sun Apr 17 20:52:56 2011 +0200 @@ -0,0 +1,187 @@ +/* + * Hedgewars-iOS, a Hedgewars port for iOS devices + * Copyright (c) 2009-2011 Vittorio Giovara + * + * 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" + +#define BUFFER_SIZE 255 // like in original frontend + +@implementation GameInterfaceBridge +@synthesize parentController, systemSettings, savePath, overlayController, ipcPort, gameType, engineProtocol; + +-(id) initWithController:(id) viewController { + if (self = [super init]) { + self.parentController = (UIViewController *)viewController; + self.engineProtocol = [[EngineProtocolNetwork alloc] init]; +; + self.savePath = nil; + + self.systemSettings = [NSDictionary dictionaryWithContentsOfFile:SETTINGS_FILE()]; + self.overlayController = [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil]; + self.ipcPort = randomPort(); + + self.gameType = gtNone; + } + 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 { + self.parentController.view.opaque = YES; + self.parentController.view.backgroundColor = [UIColor blackColor]; + self.parentController.view.alpha = 0; + + [UIView beginAnimations:@"fade out to black" context:NULL]; + [UIView setAnimationDuration:1]; + self.parentController.view.alpha = 1; + [UIView commitAnimations]; + + self.engineProtocol.savePath = self.savePath; + [self.engineProtocol spawnThreadOnPort:self.ipcPort]; + + NSDictionary *overlayOptions = [[NSDictionary alloc] initWithObjectsAndKeys: + [NSNumber numberWithInt:self.parentController.interfaceOrientation],@"orientation", + [self.systemSettings objectForKey:@"menu"],@"menu", + nil]; + [self performSelector:@selector(displayOverlayLater:) withObject:overlayOptions afterDelay:1]; + [overlayOptions release]; + + // this is the pascal fuction that starts the game, wrapped around isInGame + [HedgewarsAppDelegate sharedAppDelegate].isInGame = YES; + Game([self gatherGameSettings]); + [HedgewarsAppDelegate sharedAppDelegate].isInGame = NO; + + [UIView beginAnimations:@"fade in" context:NULL]; + [UIView setAnimationDuration:1]; + self.parentController.view.alpha = 0; + [UIView commitAnimations]; +} + +-(void) startLocalGame:(NSDictionary *)withDictionary { + self.gameType = gtLocal; + [self.engineProtocol setGameConfig:withDictionary]; + + 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]; + + [self startGameEngine]; +} + +-(void) startSaveGame:(NSString *)atPath { + self.gameType = gtSave; + self.savePath = atPath; + [self.engineProtocol setGameConfig:nil]; + + [self startGameEngine]; +} + +#pragma mark - +-(const char **)gatherGameSettings { + const char *gameArgs[10]; + 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]; + return gameArgs; +} + + +@end diff -r 589f69a9665c -r 851f36579ed4 project_files/HedgewarsMobile/Classes/GameSetup.h --- a/project_files/HedgewarsMobile/Classes/GameSetup.h Sun Apr 17 13:33:46 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 - * - * 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 -#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 diff -r 589f69a9665c -r 851f36579ed4 project_files/HedgewarsMobile/Classes/GameSetup.m --- a/project_files/HedgewarsMobile/Classes/GameSetup.m Sun Apr 17 13:33:46 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 - * - * 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> - addhh - 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 diff -r 589f69a9665c -r 851f36579ed4 project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.h --- a/project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.h Sun Apr 17 13:33:46 2011 -0400 +++ b/project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.h Sun Apr 17 20:52:56 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 diff -r 589f69a9665c -r 851f36579ed4 project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.m --- a/project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.m Sun Apr 17 13:33:46 2011 -0400 +++ b/project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.m Sun Apr 17 20:52:56 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 @@ -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]; diff -r 589f69a9665c -r 851f36579ed4 project_files/HedgewarsMobile/Classes/OverlayViewController.h --- a/project_files/HedgewarsMobile/Classes/OverlayViewController.h Sun Apr 17 13:33:46 2011 -0400 +++ b/project_files/HedgewarsMobile/Classes/OverlayViewController.h Sun Apr 17 20:52:56 2011 +0200 @@ -49,7 +49,6 @@ BOOL isAttacking; // stuff initialized externally - BOOL isNetGame; BOOL useClassicMenu; NSInteger initialOrientation; diff -r 589f69a9665c -r 851f36579ed4 project_files/HedgewarsMobile/Classes/OverlayViewController.m --- a/project_files/HedgewarsMobile/Classes/OverlayViewController.m Sun Apr 17 13:33:46 2011 -0400 +++ b/project_files/HedgewarsMobile/Classes/OverlayViewController.m Sun Apr 17 20:52:56 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 diff -r 589f69a9665c -r 851f36579ed4 project_files/HedgewarsMobile/Hedgewars.xcodeproj/project.pbxproj --- a/project_files/HedgewarsMobile/Hedgewars.xcodeproj/project.pbxproj Sun Apr 17 13:33:46 2011 -0400 +++ b/project_files/HedgewarsMobile/Hedgewars.xcodeproj/project.pbxproj Sun Apr 17 20:52:56 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 = ""; }; 616591E411CA9BA200D6E256 /* GameConfigViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GameConfigViewController.h; sourceTree = ""; }; 616591E511CA9BA200D6E256 /* GameConfigViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GameConfigViewController.m; sourceTree = ""; }; - 616591E611CA9BA200D6E256 /* GameSetup.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GameSetup.h; sourceTree = ""; }; - 616591E711CA9BA200D6E256 /* GameSetup.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GameSetup.m; sourceTree = ""; }; + 616591E611CA9BA200D6E256 /* EngineProtocolNetwork.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EngineProtocolNetwork.h; sourceTree = ""; }; + 616591E711CA9BA200D6E256 /* EngineProtocolNetwork.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EngineProtocolNetwork.m; sourceTree = ""; }; 616591E811CA9BA200D6E256 /* GeneralSettingsViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GeneralSettingsViewController.h; sourceTree = ""; }; 616591E911CA9BA200D6E256 /* GeneralSettingsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneralSettingsViewController.m; sourceTree = ""; }; 616591EA11CA9BA200D6E256 /* GravesViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GravesViewController.h; sourceTree = ""; }; @@ -985,6 +986,8 @@ 61E2F7431283752C00E12521 /* tw.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = tw.png; path = Resources/Icons/tw.png; sourceTree = ""; }; 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 = ""; }; + 61EDB5AE135B3F97009B29A6 /* GameInterfaceBridge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GameInterfaceBridge.h; sourceTree = ""; }; + 61EDB5AF135B3F97009B29A6 /* GameInterfaceBridge.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GameInterfaceBridge.m; sourceTree = ""; }; 61EF920511DF57AC003441C4 /* arrowDown.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = arrowDown.png; path = Resources/Overlay/arrowDown.png; sourceTree = ""; }; 61EF920611DF57AC003441C4 /* arrowLeft.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = arrowLeft.png; path = Resources/Overlay/arrowLeft.png; sourceTree = ""; }; 61EF920711DF57AC003441C4 /* arrowRight.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = arrowRight.png; path = Resources/Overlay/arrowRight.png; sourceTree = ""; }; @@ -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; };