project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m
branchui-scaling
changeset 15288 c4fd2813b127
parent 13204 e17efd663125
equal deleted inserted replaced
13395:0135e64c6c66 15288:c4fd2813b127
    28 @synthesize blackView, savePath, port;
    28 @synthesize blackView, savePath, port;
    29 
    29 
    30 #pragma mark -
    30 #pragma mark -
    31 #pragma mark Instance methods for engine interaction
    31 #pragma mark Instance methods for engine interaction
    32 // prepares the controllers for hosting a game
    32 // prepares the controllers for hosting a game
    33 -(void) earlyEngineLaunch:(NSDictionary *)optionsOrNil {
    33 - (void)earlyEngineLaunch:(NSDictionary *)optionsOrNil {
    34     [self retain];
       
    35     [[AudioManagerController mainManager] fadeOutBackgroundMusic];
    34     [[AudioManagerController mainManager] fadeOutBackgroundMusic];
    36 
    35 
    37     EngineProtocolNetwork *engineProtocol = [[EngineProtocolNetwork alloc] init];
    36     EngineProtocolNetwork *engineProtocol = [[EngineProtocolNetwork alloc] init];
    38     self.port = engineProtocol.enginePort;
    37     self.port = engineProtocol.enginePort;
    39     engineProtocol.delegate = self;
    38     engineProtocol.delegate = self;
    40     [engineProtocol spawnThread:self.savePath withOptions:optionsOrNil];
    39     [engineProtocol spawnThread:self.savePath withOptions:optionsOrNil];
    41     [engineProtocol release];
       
    42 
    40 
    43     // add a black view hiding the background
    41     // add a black view hiding the background
    44     UIWindow *thisWindow = [[HedgewarsAppDelegate sharedAppDelegate] uiwindow];
    42     UIWindow *thisWindow = [[HedgewarsAppDelegate sharedAppDelegate] uiwindow];
    45     self.blackView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    43     self.blackView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    46     self.blackView.opaque = YES;
    44     self.blackView.opaque = YES;
    50     [UIView beginAnimations:@"fade out" context:NULL];
    48     [UIView beginAnimations:@"fade out" context:NULL];
    51     [UIView setAnimationDuration:1];
    49     [UIView setAnimationDuration:1];
    52     self.blackView.alpha = 1;
    50     self.blackView.alpha = 1;
    53     [UIView commitAnimations];
    51     [UIView commitAnimations];
    54     [thisWindow addSubview:self.blackView];
    52     [thisWindow addSubview:self.blackView];
    55     [self.blackView release];
       
    56 
    53 
    57     // keep the point of return for games that completed loading
    54     // keep the point of return for games that completed loading
    58     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    55     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    59     [userDefaults setObject:self.savePath forKey:@"savedGamePath"];
    56     [userDefaults setObject:self.savePath forKey:@"savedGamePath"];
    60     [userDefaults setObject:[NSNumber numberWithBool:NO] forKey:@"saveIsValid"];
    57     [userDefaults setObject:[NSNumber numberWithBool:NO] forKey:@"saveIsValid"];
    63     // let's launch the engine using this -perfomSelector so that the runloop can deal with queued messages first
    60     // let's launch the engine using this -perfomSelector so that the runloop can deal with queued messages first
    64     [self performSelector:@selector(engineLaunch) withObject:nil afterDelay:0.1f];
    61     [self performSelector:@selector(engineLaunch) withObject:nil afterDelay:0.1f];
    65 }
    62 }
    66 
    63 
    67 // cleans up everything
    64 // cleans up everything
    68 -(void) lateEngineLaunch {
    65 - (void)lateEngineLaunch {
    69     // notify views below that they are getting the spotlight again
    66     // notify views below that they are getting the spotlight again
    70     [[[HedgewarsAppDelegate sharedAppDelegate] uiwindow] makeKeyAndVisible];
    67     [[[HedgewarsAppDelegate sharedAppDelegate] uiwindow] makeKeyAndVisible];
    71     [callingController viewWillAppear:YES];
    68     [callingController viewWillAppear:YES];
    72 
    69 
    73     // remove completed games notification
    70     // remove completed games notification
    89 
    86 
    90     // restart music and we're done
    87     // restart music and we're done
    91     [[AudioManagerController mainManager] fadeInBackgroundMusic];
    88     [[AudioManagerController mainManager] fadeInBackgroundMusic];
    92     [HWUtils setGameStatus:gsNone];
    89     [HWUtils setGameStatus:gsNone];
    93     [HWUtils setGameType:gtNone];
    90     [HWUtils setGameType:gtNone];
    94     [self release];
       
    95 }
    91 }
    96 
    92 
    97 // main routine for calling the actual game engine
    93 // main routine for calling the actual game engine
    98 -(void) engineLaunch {
    94 - (void)engineLaunch {
    99     CGFloat width, height;
    95     CGFloat width, height;
   100     CGFloat screenScale = [[UIScreen mainScreen] safeScale];
    96     CGFloat screenScale = [[UIScreen mainScreen] safeScale];
   101     NSString *ipcString = [[NSString alloc] initWithFormat:@"%d",self.port];
    97     NSString *ipcString = [[NSString alloc] initWithFormat:@"%d",self.port];
   102     
    98     
   103     NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [HWUtils languageID]];
    99     NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [HWUtils languageID]];
   139                                       @"--raw-quality", rawQuality,
   135                                       @"--raw-quality", rawQuality,
   140                                       @"--locale", localeString,
   136                                       @"--locale", localeString,
   141                                       @"--prefix", resourcePath,
   137                                       @"--prefix", resourcePath,
   142                                       @"--user-prefix", documentsDirectory,
   138                                       @"--user-prefix", documentsDirectory,
   143                                       nil];
   139                                       nil];
   144     [verticalSize release];
       
   145     [horizontalSize release];
       
   146     [resourcePath release];
       
   147     [localeString release];
       
   148     [ipcString release];
       
   149 
   140 
   150     NSString *username = [settings objectForKey:@"username"];
   141     NSString *username = [settings objectForKey:@"username"];
   151     if ([username length] > 0) {
   142     if ([username length] > 0) {
   152         [gameParameters addObject:@"--nick"];
   143         [gameParameters addObject:@"--nick"];
   153         [gameParameters addObject: username];
   144         [gameParameters addObject: username];
   173 
   164 
   174     int argc = [gameParameters count];
   165     int argc = [gameParameters count];
   175     const char **argv = (const char **)malloc(sizeof(const char*)*argc);
   166     const char **argv = (const char **)malloc(sizeof(const char*)*argc);
   176     for (int i = 0; i < argc; i++)
   167     for (int i = 0; i < argc; i++)
   177         argv[i] = strdup([[gameParameters objectAtIndex:i] UTF8String]);
   168         argv[i] = strdup([[gameParameters objectAtIndex:i] UTF8String]);
   178     [gameParameters release];
       
   179 
   169 
   180     // this is the pascal function that starts the game
   170     // this is the pascal function that starts the game
   181     RunEngine(argc, argv);
   171     RunEngine(argc, argv);
   182 
   172 
   183     // cleanup
   173     // cleanup
   187 
   177 
   188     // moar cleanup
   178     // moar cleanup
   189     [self lateEngineLaunch];
   179     [self lateEngineLaunch];
   190 }
   180 }
   191 
   181 
   192 -(void) dealloc {
       
   193     releaseAndNil(blackView);
       
   194     releaseAndNil(savePath);
       
   195     [super dealloc];
       
   196 }
       
   197 
   182 
   198 #pragma mark -
   183 #pragma mark -
   199 #pragma mark EngineProtocolDelegate methods
   184 #pragma mark EngineProtocolDelegate methods
   200 -(void) gameEndedWithStatistics:(NSArray *)stats {
   185 - (void)gameEndedWithStatistics:(NSArray *)stats {
       
   186     [self performSelectorOnMainThread:@selector(presentStats:) withObject:stats waitUntilDone:NO];
       
   187 }
       
   188 
       
   189 - (void)presentStats:(NSArray *)stats {
   201     if (stats != nil) {
   190     if (stats != nil) {
   202         StatsPageViewController *statsPage = [[StatsPageViewController alloc] init];
   191         StatsPageViewController *statsPage = [[StatsPageViewController alloc] init];
   203         statsPage.statsArray = stats;
   192         statsPage.statsArray = stats;
   204         statsPage.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
   193         statsPage.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
   205 
   194 
   206         [callingController presentViewController:statsPage animated:YES completion:nil];
   195         [callingController presentViewController:statsPage animated:YES completion:nil];
   207         [statsPage release];
       
   208     }
   196     }
   209 }
   197 }
   210 
   198 
   211 #pragma mark -
   199 #pragma mark -
   212 #pragma mark Class methods for setting up the engine from outsite
   200 #pragma mark Class methods for setting up the engine from outsite
   213 +(void) registerCallingController:(UIViewController *)controller {
   201 + (void)registerCallingController:(UIViewController *)controller {
   214     callingController = controller;
   202     callingController = controller;
   215 }
   203 }
   216 
   204 
   217 +(void) startGame:(TGameType) type atPath:(NSString *)path withOptions:(NSDictionary *)config {
   205 + (void)startGame:(TGameType)type atPath:(NSString *)path withOptions:(NSDictionary *)config {
   218     [HWUtils setGameType:type];
   206     [HWUtils setGameType:type];
   219     id bridge = [[self alloc] init];
   207     id bridge = [[self alloc] init];
   220     [bridge setSavePath:path];
   208     [bridge setSavePath:path];
   221     [bridge earlyEngineLaunch:config];
   209     [bridge earlyEngineLaunch:config];
   222     [bridge release];
   210 }
   223 }
   211 
   224 
   212 + (void)startLocalGame:(NSDictionary *)withOptions {
   225 +(void) startLocalGame:(NSDictionary *)withOptions {
       
   226     NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
   213     NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
   227     [outputFormatter setDateFormat:@"yyyy-MM-dd '@' HH.mm"];
   214     [outputFormatter setDateFormat:@"yyyy-MM-dd '@' HH.mm"];
   228     NSString *savePath = [[NSString alloc] initWithFormat:@"%@%@.hws",SAVES_DIRECTORY(),[outputFormatter stringFromDate:[NSDate date]]];
   215     NSString *savePath = [[NSString alloc] initWithFormat:@"%@%@.hws",SAVES_DIRECTORY(),[outputFormatter stringFromDate:[NSDate date]]];
   229     [outputFormatter release];
       
   230 
   216 
   231     // in the rare case in which a savefile with the same name exists the older one must be removed (otherwise it gets corrupted)
   217     // in the rare case in which a savefile with the same name exists the older one must be removed (otherwise it gets corrupted)
   232     if ([[NSFileManager defaultManager] fileExistsAtPath:savePath])
   218     if ([[NSFileManager defaultManager] fileExistsAtPath:savePath])
   233         [[NSFileManager defaultManager] removeItemAtPath:savePath error:nil];
   219         [[NSFileManager defaultManager] removeItemAtPath:savePath error:nil];
   234 
   220 
   235     [self startGame:gtLocal atPath:savePath withOptions:withOptions];
   221     [self startGame:gtLocal atPath:savePath withOptions:withOptions];
   236     [savePath release];
   222 }
   237 }
   223 
   238 
   224 + (void)startSaveGame:(NSString *)atPath {
   239 +(void) startSaveGame:(NSString *)atPath {
       
   240     [self startGame:gtSave atPath:atPath withOptions:nil];
   225     [self startGame:gtSave atPath:atPath withOptions:nil];
   241 }
   226 }
   242 
   227 
   243 +(void) startMissionGame:(NSString *)withScript {
   228 + (void)startMissionGame:(NSString *)withSubPath {
   244     NSString *seedCmd = [self seedCommand];
   229     NSString *seedCmd = [self seedCommand];
   245     NSString *missionPath = [[NSString alloc] initWithFormat:@"escript Missions/Training/%@.lua",withScript];
   230     NSString *missionPath = [[NSString alloc] initWithFormat:@"escript Missions/%@.lua", withSubPath];
   246     NSDictionary *missionDict = [[NSDictionary alloc] initWithObjectsAndKeys:missionPath, @"mission_command", seedCmd, @"seed_command", nil];
   231     NSDictionary *missionDict = [[NSDictionary alloc] initWithObjectsAndKeys:missionPath, @"mission_command", seedCmd, @"seed_command", nil];
   247     [missionPath release];
       
   248     [seedCmd release];
       
   249 
   232 
   250     [self startGame:gtMission atPath:nil withOptions:missionDict];
   233     [self startGame:gtMission atPath:nil withOptions:missionDict];
   251     [missionDict release];
   234 }
   252 }
   235 
   253 
   236 + (NSString *)seedCommand {
   254 +(NSString *) seedCommand {
       
   255     // generate a seed
   237     // generate a seed
   256     NSString *seed = [HWUtils seed];
   238     NSString *seed = [HWUtils seed];
   257     NSString *seedCmd = [[NSString alloc] initWithFormat:@"eseed {%@}", seed];
   239     NSString *seedCmd = [[NSString alloc] initWithFormat:@"eseed {%@}", seed];
   258     [seed release];
       
   259     return seedCmd;
   240     return seedCmd;
   260 }
   241 }
   261 
   242 
   262 +(void) startCampaignMissionGameWithScript:(NSString *)missionScriptName forCampaign:(NSString *)campaignName {
   243 + (void)startCampaignMissionGameWithScript:(NSString *)missionScriptName forCampaign:(NSString *)campaignName {
   263     NSString *seedCmd = [self seedCommand];
   244     NSString *seedCmd = [self seedCommand];
   264     NSString *campaignMissionPath = [[NSString alloc] initWithFormat:@"escript Missions/Campaign/%@/%@", campaignName, missionScriptName];
   245     NSString *campaignMissionPath = [[NSString alloc] initWithFormat:@"escript Missions/Campaign/%@/%@", campaignName, missionScriptName];
   265     NSDictionary *campaignMissionDict = [[NSDictionary alloc] initWithObjectsAndKeys:campaignMissionPath, @"mission_command", seedCmd, @"seed_command", nil];
   246     NSDictionary *campaignMissionDict = [[NSDictionary alloc] initWithObjectsAndKeys:campaignMissionPath, @"mission_command", seedCmd, @"seed_command", nil];
   266     [campaignMissionPath release];
       
   267     [seedCmd release];
       
   268     
   247     
   269     [self startGame:gtCampaign atPath:nil withOptions:campaignMissionDict];
   248     [self startGame:gtCampaign atPath:nil withOptions:campaignMissionDict];
   270     [campaignMissionDict release];
   249 }
   271 }
   250 
   272 
   251 + (void)startSimpleGame {
   273 +(void) startSimpleGame {
       
   274     NSString *seedCmd = [self seedCommand];
   252     NSString *seedCmd = [self seedCommand];
   275 
   253 
   276     // pick a random static map
   254     // pick a random static map
   277     NSArray *listOfMaps = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:MAPS_DIRECTORY() error:NULL];
   255     NSArray *listOfMaps = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:MAPS_DIRECTORY() error:NULL];
   278     NSString *mapName = [listOfMaps objectAtIndex:arc4random_uniform((int)[listOfMaps count])];
   256     NSString *mapName = [listOfMaps objectAtIndex:arc4random_uniform((int)[listOfMaps count])];
   279     NSString *fileCfg = [[NSString alloc] initWithFormat:@"%@/%@/map.cfg",MAPS_DIRECTORY(),mapName];
   257     NSString *fileCfg = [[NSString alloc] initWithFormat:@"%@/%@/map.cfg",MAPS_DIRECTORY(),mapName];
   280     NSString *contents = [[NSString alloc] initWithContentsOfFile:fileCfg encoding:NSUTF8StringEncoding error:NULL];
   258     NSString *contents = [[NSString alloc] initWithContentsOfFile:fileCfg encoding:NSUTF8StringEncoding error:NULL];
   281     [fileCfg release];
       
   282     NSArray *split = [contents componentsSeparatedByString:@"\n"];
   259     NSArray *split = [contents componentsSeparatedByString:@"\n"];
   283     [contents release];
       
   284     NSString *themeCommand = [[NSString alloc] initWithFormat:@"etheme %@", [split objectAtIndex:0]];
   260     NSString *themeCommand = [[NSString alloc] initWithFormat:@"etheme %@", [split objectAtIndex:0]];
   285     NSString *staticMapCommand = [[NSString alloc] initWithFormat:@"emap %@", mapName];
   261     NSString *staticMapCommand = [[NSString alloc] initWithFormat:@"emap %@", mapName];
   286 
   262 
   287     // select teams with two different colors
   263     // select teams with two different colors
   288     NSArray *colorArray = [HWUtils teamColors];
   264     NSArray *colorArray = [HWUtils teamColors];
   299                                                                            @"Ninjas.plist",@"team",nil];
   275                                                                            @"Ninjas.plist",@"team",nil];
   300     NSDictionary *secondTeam = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:4],@"number",
   276     NSDictionary *secondTeam = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:4],@"number",
   301                                                                             [NSNumber numberWithUnsignedInt:secondColor],@"color",
   277                                                                             [NSNumber numberWithUnsignedInt:secondColor],@"color",
   302                                                                             @"Robots.plist",@"team",nil];
   278                                                                             @"Robots.plist",@"team",nil];
   303     NSArray *listOfTeams = [[NSArray alloc] initWithObjects:firstTeam,secondTeam,nil];
   279     NSArray *listOfTeams = [[NSArray alloc] initWithObjects:firstTeam,secondTeam,nil];
   304     [firstTeam release];
       
   305     [secondTeam release];
       
   306 
   280 
   307     // create the configuration
   281     // create the configuration
   308     NSDictionary *gameDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
   282     NSDictionary *gameDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
   309                                     seedCmd,@"seed_command",
   283                                     seedCmd,@"seed_command",
   310                                     @"e$template_filter 0",@"templatefilter_command",
   284                                     @"e$template_filter 0",@"templatefilter_command",
   315                                     listOfTeams,@"teams_list",
   289                                     listOfTeams,@"teams_list",
   316                                     @"Default.plist",@"scheme",
   290                                     @"Default.plist",@"scheme",
   317                                     @"Default.plist",@"weapon",
   291                                     @"Default.plist",@"weapon",
   318                                     @"",@"mission_command",
   292                                     @"",@"mission_command",
   319                                     nil];
   293                                     nil];
   320     [listOfTeams release];
       
   321     [staticMapCommand release];
       
   322     [themeCommand release];
       
   323     [seedCmd release];
       
   324 
   294 
   325     // launch game
   295     // launch game
   326     [GameInterfaceBridge startLocalGame:gameDictionary];
   296     [GameInterfaceBridge startLocalGame:gameDictionary];
   327     [gameDictionary release];
       
   328 }
   297 }
   329 
   298 
   330 @end
   299 @end