project_files/HedgewarsMobile/Classes/GameSetup.m
author koda
Wed, 23 Jun 2010 22:03:56 +0200
changeset 3547 02875b1145b7
parent 3546 ccf4854df294
child 3548 4d220ee7c75f
permissions -rw-r--r--
i <3 mercurial
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     1
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     2
//  gameSetup.m
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     3
//  hwengine
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     4
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     5
//  Created by Vittorio on 10/01/10.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     6
//  Copyright 2010 __MyCompanyName__. All rights reserved.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     7
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     8
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     9
#include <sys/types.h>
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    10
#include <sys/sysctl.h>
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    11
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    12
#import "GameSetup.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    13
#import "SDL_uikitappdelegate.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    14
#import "SDL_net.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    15
#import "PascalImports.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    16
#import "CommodityFunctions.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    17
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    18
#define BUFFER_SIZE 256
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    19
#define debug(format, ...) CFShow([NSString stringWithFormat:format, ## __VA_ARGS__]);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    20
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    21
@implementation GameSetup
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    23
@synthesize systemSettings, gameConfig;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    24
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
-(id) init {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    26
    if (self = [super init]) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    27
        ipcPort = randomPort();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    28
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    29
        // should check they exist and throw and exection if not
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    30
        NSDictionary *dictSett = [[NSDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    31
        self.systemSettings = dictSett;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    32
        [dictSett release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    33
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    34
        NSDictionary *dictGame = [[NSDictionary alloc] initWithContentsOfFile:GAMECONFIG_FILE()];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    35
        self.gameConfig = dictGame;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    36
        [dictGame release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    37
    } 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    38
    return self;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    39
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    40
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    41
-(NSString *)description {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    42
    return [NSString stringWithFormat:@"ipcport: %d\nsockets: %d,%d\n teams: %@\n systemSettings: %@",ipcPort,sd,csd,gameConfig,systemSettings];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    43
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    45
-(void) dealloc {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
    [gameConfig release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    47
    [systemSettings release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    48
    [super dealloc];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    50
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    51
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
#pragma mark Provider functions
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    53
// unpacks team data from the selected team.plist to a sequence of engine commands
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
-(void) provideTeamData:(NSString *)teamName forHogs:(NSInteger) numberOfPlayingHogs withHealth:(NSInteger) initialHealth ofColor:(NSNumber *)teamColor {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
    /*
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    56
     addteam <32charsMD5hash> <color> <team name>
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    57
     addhh <level> <health> <hedgehog name>
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    58
     <level> is 0 for human, 1-5 for bots (5 is the most stupid)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    59
    */
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    60
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    61
    NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@", TEAMS_DIRECTORY(), teamName];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
    NSDictionary *teamData = [[NSDictionary alloc] initWithContentsOfFile:teamFile];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    63
    [teamFile release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    64
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    65
    NSString *teamHashColorAndName = [[NSString alloc] initWithFormat:@"eaddteam %@ %@ %@", 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    66
                                      [teamData objectForKey:@"hash"], [teamColor stringValue], [teamData objectForKey:@"teamname"]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
    [self sendToEngine: teamHashColorAndName];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    68
    [teamHashColorAndName release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    69
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    70
    NSString *grave = [[NSString alloc] initWithFormat:@"egrave %@", [teamData objectForKey:@"grave"]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    71
    [self sendToEngine: grave];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    72
    [grave release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    73
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    74
    NSString *fort = [[NSString alloc] initWithFormat:@"efort %@", [teamData objectForKey:@"fort"]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    75
    [self sendToEngine: fort];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    76
    [fort release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    77
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    78
    NSString *voicepack = [[NSString alloc] initWithFormat:@"evoicepack %@", [teamData objectForKey:@"voicepack"]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    79
    [self sendToEngine: voicepack];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    80
    [voicepack release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    81
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    82
    NSString *flag = [[NSString alloc] initWithFormat:@"eflag %@", [teamData objectForKey:@"flag"]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    83
    [self sendToEngine: flag];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    84
    [flag release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    85
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    86
    NSArray *hogs = [teamData objectForKey:@"hedgehogs"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    87
    for (int i = 0; i < numberOfPlayingHogs; i++) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    88
        NSDictionary *hog = [hogs objectAtIndex:i];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    89
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    90
        NSString *hogLevelHealthAndName = [[NSString alloc] initWithFormat:@"eaddhh %@ %d %@", 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    91
                                           [hog objectForKey:@"level"], initialHealth, [hog objectForKey:@"hogname"]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    92
        [self sendToEngine: hogLevelHealthAndName];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    93
        [hogLevelHealthAndName release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    94
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    95
        NSString *hogHat = [[NSString alloc] initWithFormat:@"ehat %@", [hog objectForKey:@"hat"]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    96
        [self sendToEngine: hogHat];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    97
        [hogHat release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    98
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    99
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   100
    [teamData release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   101
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   102
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   103
// unpacks ammostore data from the selected ammo.plist to a sequence of engine commands
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   104
-(void) provideAmmoData:(NSString *)ammostoreName forPlayingTeams:(NSInteger) numberOfTeams {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   105
    NSString *weaponPath = [[NSString alloc] initWithFormat:@"%@/%@",WEAPONS_DIRECTORY(),ammostoreName];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   106
    NSDictionary *ammoData = [[NSDictionary alloc] initWithContentsOfFile:weaponPath];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   107
    [weaponPath release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   108
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   109
    NSString *ammloadt = [[NSString alloc] initWithFormat:@"eammloadt %@", [ammoData objectForKey:@"ammostore_initialqt"]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   110
    [self sendToEngine: ammloadt];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   111
    [ammloadt release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   112
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   113
    NSString *ammprob = [[NSString alloc] initWithFormat:@"eammprob %@", [ammoData objectForKey:@"ammostore_probability"]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   114
    [self sendToEngine: ammprob];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   115
    [ammprob release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   116
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   117
    NSString *ammdelay = [[NSString alloc] initWithFormat:@"eammdelay %@", [ammoData objectForKey:@"ammostore_delay"]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   118
    [self sendToEngine: ammdelay];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   119
    [ammdelay release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   120
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   121
    NSString *ammreinf = [[NSString alloc] initWithFormat:@"eammreinf %@", [ammoData objectForKey:@"ammostore_crate"]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   122
    [self sendToEngine: ammreinf];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   123
    [ammreinf release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   124
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   125
    // sent twice so it applies to both teams
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   126
    NSString *ammstore = [[NSString alloc] initWithString:@"eammstore"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   127
    for (int i = 0; i < numberOfTeams; i++)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   128
        [self sendToEngine: ammstore];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   129
    [ammstore release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   130
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   131
    [ammoData release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   132
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   133
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   134
// unpacks scheme data from the selected scheme.plist to a sequence of engine commands
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   135
-(NSInteger) provideScheme:(NSString *)schemeName {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   136
    NSString *schemePath = [[NSString alloc] initWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),schemeName];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   137
    NSArray *scheme = [[NSArray alloc] initWithContentsOfFile:schemePath];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   138
    [schemePath release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   139
    int result = 0;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   140
    int i = 0;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   141
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   142
    if ([[scheme objectAtIndex:i++] boolValue])
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   143
        result |= 0x01;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   144
    if ([[scheme objectAtIndex:i++] boolValue])
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   145
        result |= 0x10;    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   146
    if ([[scheme objectAtIndex:i++] boolValue])
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   147
        result |= 0x04;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   148
    if ([[scheme objectAtIndex:i++] boolValue])
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   149
        result |= 0x08;    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   150
    if ([[scheme objectAtIndex:i++] boolValue])
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   151
        result |= 0x20;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   152
    if ([[scheme objectAtIndex:i++] boolValue])
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   153
        result |= 0x40;    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   154
    if ([[scheme objectAtIndex:i++] boolValue])
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   155
        result |= 0x80;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   156
    if ([[scheme objectAtIndex:i++] boolValue])
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   157
        result |= 0x100;    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   158
    if ([[scheme objectAtIndex:i++] boolValue])
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   159
        result |= 0x200;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   160
    if ([[scheme objectAtIndex:i++] boolValue])
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   161
        result |= 0x400;    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   162
    if ([[scheme objectAtIndex:i++] boolValue])
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   163
        result |= 0x800;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   164
    if ([[scheme objectAtIndex:i++] boolValue])
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   165
        result |= 0x2000;    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   166
    if ([[scheme objectAtIndex:i++] boolValue])
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   167
        result |= 0x4000;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   168
    if ([[scheme objectAtIndex:i++] boolValue])
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   169
        result |= 0x8000;    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   170
    if ([[scheme objectAtIndex:i++] boolValue])
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   171
        result |= 0x10000;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   172
    if ([[scheme objectAtIndex:i++] boolValue])
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   173
        result |= 0x20000;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   174
    if ([[scheme objectAtIndex:i++] boolValue])
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   175
        result |= 0x80000;    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   176
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   177
    NSString *flags = [[NSString alloc] initWithFormat:@"e$gmflags %d",result];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   178
    [self sendToEngine:flags];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   179
    [flags release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   180
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   181
    NSString *dmgMod = [[NSString alloc] initWithFormat:@"e$damagepct %d",[[scheme objectAtIndex:i++] intValue]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   182
    [self sendToEngine:dmgMod];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   183
    [dmgMod release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   184
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   185
    NSString *turnTime = [[NSString alloc] initWithFormat:@"e$turntime %d",[[scheme objectAtIndex:i++] intValue] * 1000];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   186
    [self sendToEngine:turnTime];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   187
    [turnTime release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   188
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   189
    result = [[scheme objectAtIndex:i++] intValue]; // initial health
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   190
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   191
    NSString *sdTime = [[NSString alloc] initWithFormat:@"e$sd_turns %d",[[scheme objectAtIndex:i++] intValue]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   192
    [self sendToEngine:sdTime];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   193
    [sdTime release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   194
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   195
    NSString *crateDrops = [[NSString alloc] initWithFormat:@"e$casefreq %d",[[scheme objectAtIndex:i++] intValue]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   196
    [self sendToEngine:crateDrops];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   197
    [crateDrops release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   198
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   199
    NSString *minesTime = [[NSString alloc] initWithFormat:@"e$minestime %d",[[scheme objectAtIndex:i++] intValue] * 1000];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   200
    [self sendToEngine:minesTime];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   201
    [minesTime release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   202
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   203
    NSString *minesNumber = [[NSString alloc] initWithFormat:@"e$landadds %d",[[scheme objectAtIndex:i++] intValue]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   204
    [self sendToEngine:minesNumber];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   205
    [minesNumber release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   206
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   207
    NSString *dudMines = [[NSString alloc] initWithFormat:@"e$minedudpct %d",[[scheme objectAtIndex:i++] intValue]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   208
    [self sendToEngine:dudMines];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   209
    [dudMines release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   210
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   211
    NSString *explosives = [[NSString alloc] initWithFormat:@"e$explosives %d",[[scheme objectAtIndex:i++] intValue]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   212
    [self sendToEngine:explosives];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   213
    [explosives release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   214
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   215
    [scheme release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   216
    return result;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   217
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   218
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   219
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   220
#pragma mark Thread/Network relevant code
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   221
// select one of GameSetup method and execute it in a seprate thread
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   222
-(void) startThread: (NSString *) selector {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   223
    SEL usage = NSSelectorFromString(selector);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   224
    [NSThread detachNewThreadSelector:usage toTarget:self withObject:nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   225
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   226
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   227
// wrapper that computes the length of the message and then sends the command string
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   228
-(int) sendToEngine: (NSString *)string {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   229
    uint8_t length = [string length];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   230
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   231
    SDLNet_TCP_Send(csd, &length , 1);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   232
    return SDLNet_TCP_Send(csd, [string UTF8String], length);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   233
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   234
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   235
// method that handles net setup with engine and keeps connection alive
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   236
-(void) engineProtocol {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   237
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   238
    IPaddress ip;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   239
    int eProto;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   240
    BOOL clientQuit, serverQuit;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   241
    char buffer[BUFFER_SIZE], string[BUFFER_SIZE];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   242
    uint8_t msgSize;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   243
    uint16_t gameTicks;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   244
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   245
    serverQuit = NO;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   246
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   247
    if (SDLNet_Init() < 0) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   248
        DLog(@"SDLNet_Init: %s", SDLNet_GetError());
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   249
        serverQuit = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   250
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   251
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   252
    // Resolving the host using NULL make network interface to listen
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   253
    if (SDLNet_ResolveHost(&ip, NULL, ipcPort) < 0) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   254
        DLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError());
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   255
        serverQuit = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   256
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   257
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   258
    // Open a connection with the IP provided (listen on the host's port) 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   259
    if (!(sd = SDLNet_TCP_Open(&ip))) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   260
        DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), ipcPort);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   261
        serverQuit = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   262
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   263
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   264
    DLog(@"Waiting for a client on port %d", ipcPort);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   265
    while (!serverQuit) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   266
        // This check the sd if there is a pending connection.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   267
        // If there is one, accept that, and open a new socket for communicating
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   268
        csd = SDLNet_TCP_Accept(sd);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   269
        if (NULL != csd) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   270
            // Now we can communicate with the client using csd socket
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   271
            // sd will remain opened waiting other connections
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   272
            DLog(@"client found");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   273
            
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   274
            //first byte of the command alwayas contain the size of the command
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   275
            SDLNet_TCP_Recv(csd, &msgSize, sizeof(uint8_t));
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   276
            
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   277
            SDLNet_TCP_Recv(csd, buffer, msgSize);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   278
            gameTicks = SDLNet_Read16 (&buffer[msgSize - 2]);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   279
            //DLog(@"engineProtocol - %d: received [%s]", gameTicks, buffer);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   280
            
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   281
            if ('C' == buffer[0]) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   282
                DLog(@"sending game config");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   283
                
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   284
                // local game
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   285
                [self sendToEngine:@"TL"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   286
                
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   287
                // seed info
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   288
                [self sendToEngine:[self.gameConfig objectForKey:@"seed_command"]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   289
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   290
                // dimension of the map
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   291
                [self sendToEngine:[self.gameConfig objectForKey:@"templatefilter_command"]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   292
                [self sendToEngine:[self.gameConfig objectForKey:@"mapgen_command"]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   293
                [self sendToEngine:[self.gameConfig objectForKey:@"mazesize_command"]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   294
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   295
                // theme info
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   296
                [self sendToEngine:[self.gameConfig objectForKey:@"theme_command"]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   297
                
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   298
                // scheme (returns initial health)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   299
                NSInteger health = [self provideScheme:[self.gameConfig objectForKey:@"scheme"]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   300
                
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   301
                NSArray *teamsConfig = [self.gameConfig objectForKey:@"teams_list"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   302
                for (NSDictionary *teamData in teamsConfig) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   303
                    [self provideTeamData:[teamData objectForKey:@"team"] 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   304
                                  forHogs:[[teamData objectForKey:@"number"] intValue]
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   305
                               withHealth:health
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   306
                                  ofColor:[teamData objectForKey:@"color"]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   307
                }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   308
                
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   309
                [self provideAmmoData:@"Default.plist" forPlayingTeams:[teamsConfig count]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   310
                
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   311
                clientQuit = NO;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   312
            } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   313
                DLog(@"wrong message or client closed connection");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   314
                clientQuit = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   315
            }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   316
            
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   317
            while (!clientQuit){
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   318
                msgSize = 0;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   319
                memset(buffer, 0, BUFFER_SIZE);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   320
                memset(string, 0, BUFFER_SIZE);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   321
                if (SDLNet_TCP_Recv(csd, &msgSize, sizeof(uint8_t)) <= 0)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   322
                    clientQuit = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   323
                if (SDLNet_TCP_Recv(csd, buffer, msgSize) <=0)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   324
                    clientQuit = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   325
                
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   326
                gameTicks = SDLNet_Read16(&buffer[msgSize - 2]);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   327
                //DLog(@"engineProtocolThread - %d: received [%s]", gameTicks, buffer);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   328
                
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   329
                switch (buffer[0]) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   330
                    case '?':
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   331
                        DLog(@"Ping? Pong!");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   332
                        [self sendToEngine:@"!"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   333
                        break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   334
                    case 'E':
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   335
                        DLog(@"ERROR - last console line: [%s]", buffer);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   336
                        clientQuit = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   337
                        break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   338
                    case 'e':
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   339
                        sscanf(buffer, "%*s %d", &eProto);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   340
                        short int netProto = 0;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   341
                        char *versionStr;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   342
                        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   343
                        HW_versionInfo(&netProto, &versionStr);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   344
                        if (netProto == eProto) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   345
                            DLog(@"Setting protocol version %d (%s)", eProto, versionStr);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   346
                        } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   347
                            DLog(@"ERROR - wrong protocol number: [%s] - expecting %d", buffer, eProto);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   348
                            clientQuit = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   349
                        }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   350
                        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   351
                        break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   352
                    case 'i':
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   353
                        switch (buffer[1]) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   354
                            case 'r':
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   355
                                NSLog(@"Winning team: %s", &buffer[2]);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   356
                                break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   357
                            case 'k':
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   358
                                NSLog(@"Best Hedgehog: %s", &buffer[2]);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   359
                                break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   360
                        }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   361
                        break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   362
                    default:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   363
                        // empty packet or just statistics
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   364
                        break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   365
                    // missing case for exiting right away
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   366
                }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   367
            }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   368
            DLog(@"Engine exited, closing server");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   369
            // wait a little to let the client close cleanly
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   370
            [NSThread sleepForTimeInterval:2];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   371
            // Close the client socket
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   372
            SDLNet_TCP_Close(csd);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   373
            serverQuit = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   374
        }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   375
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   376
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   377
    SDLNet_TCP_Close(sd);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   378
    SDLNet_Quit();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   379
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   380
    [[NSFileManager defaultManager] removeItemAtPath:GAMECONFIG_FILE() error:NULL];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   381
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   382
    [pool release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   383
    //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.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   384
    //[NSThread exit];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   385
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   386
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   387
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   388
#pragma mark Setting methods
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   389
// returns an array of c-strings that are read by engine at startup
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   390
-(const char **)getSettings {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   391
    NSString *ipcString = [[NSString alloc] initWithFormat:@"%d", ipcPort];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   392
    NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   393
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   394
    NSString *wSize = [[NSString alloc] initWithFormat:@"%d", (int) screenBounds.size.width];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   395
    NSString *hSize = [[NSString alloc] initWithFormat:@"%d", (int) screenBounds.size.height];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   396
    const char **gameArgs = (const char**) malloc(sizeof(char *) * 9);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   397
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   398
    /*
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   399
    size_t size;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   400
    // Set 'oldp' parameter to NULL to get the size of the data returned so we can allocate appropriate amount of space
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   401
    sysctlbyname("hw.machine", NULL, &size, NULL, 0); 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   402
    char *name = malloc(size);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   403
    // Get the platform name
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   404
    sysctlbyname("hw.machine", name, &size, NULL, 0);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   405
    NSString *machine = [[NSString alloc] initWithUTF8String:name];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   406
    free(name);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   407
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   408
    const char **gameArgs = (const char**) malloc(sizeof(char*) * 9);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   409
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   410
    // if the machine is less than iphone 3gs or less than ipod touch 3g use reduced graphics (land array)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   411
    if ([machine hasPrefix:@"iPhone1"] || ([machine hasPrefix:@"iPod"] && ([machine hasSuffix:@"1,1"] || [machine hasSuffix:@"2,1"])))
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   412
        gameArgs[8] = "1";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   413
    else
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   414
        gameArgs[8] = "0";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   415
    [machine release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   416
    */
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   417
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   418
    // prevents using an empty nickname
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   419
    NSString *username;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   420
    NSString *originalUsername = [self.systemSettings objectForKey:@"username"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   421
    if ([originalUsername length] == 0)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   422
        username = [[NSString alloc] initWithFormat:@"MobileUser-%@",ipcString];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   423
    else
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   424
        username = [[NSString alloc] initWithString:originalUsername];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   425
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   426
    gameArgs[0] = [username UTF8String];                                                        //UserNick
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   427
    gameArgs[1] = [ipcString UTF8String];                                                       //ipcPort
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   428
    gameArgs[2] = [[[self.systemSettings objectForKey:@"sound"] stringValue] UTF8String];       //isSoundEnabled
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   429
    gameArgs[3] = [[[self.systemSettings objectForKey:@"music"] stringValue] UTF8String];       //isMusicEnabled
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   430
    gameArgs[4] = [localeString UTF8String];                                                    //cLocaleFName
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   431
    gameArgs[5] = [[[self.systemSettings objectForKey:@"alternate"] stringValue] UTF8String];   //cAltDamage
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   432
    gameArgs[6] = [wSize UTF8String];                                                           //cScreenHeight
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   433
    gameArgs[7] = [hSize UTF8String];                                                           //cScreenWidth
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   434
    gameArgs[8] = NULL;                                                                         //recordFileName
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   435
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   436
    [wSize release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   437
    [hSize release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   438
    [localeString release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   439
    [ipcString release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   440
    [username release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   441
    return gameArgs;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   442
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   443
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   444
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   445
@end