cocoaTouch/GameSetup.m
author unc0rr
Mon, 10 May 2010 17:48:06 +0000
changeset 3458 11cd56019f00
parent 3395 095273ad0e08
child 3479 972ae3ec178a
permissions -rw-r--r--
Make some more protocol commands work
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
     1
//
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
     2
//  gameSetup.m
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
     3
//  hwengine
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
     4
//
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
     5
//  Created by Vittorio on 10/01/10.
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
     6
//  Copyright 2010 __MyCompanyName__. All rights reserved.
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
     7
//
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
     8
3335
2520ee7a5484 new manager for handling (future) rotations
koda
parents: 3332
diff changeset
     9
#include <sys/types.h>
2520ee7a5484 new manager for handling (future) rotations
koda
parents: 3332
diff changeset
    10
#include <sys/sysctl.h>
2520ee7a5484 new manager for handling (future) rotations
koda
parents: 3332
diff changeset
    11
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
    12
#import "GameSetup.h"
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
    13
#import "SDL_uikitappdelegate.h"
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
    14
#import "SDL_net.h"
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
    15
#import "PascalImports.h"
3335
2520ee7a5484 new manager for handling (future) rotations
koda
parents: 3332
diff changeset
    16
#import "CommodityFunctions.h"
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
    17
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
    18
#define BUFFER_SIZE 256
3221
3e6586c1ab4f koda's changelog turn
koda
parents: 3199
diff changeset
    19
#define debug(format, ...) CFShow([NSString stringWithFormat:format, ## __VA_ARGS__]);
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
    20
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
    21
@implementation GameSetup
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
    22
3365
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3364
diff changeset
    23
@synthesize systemSettings, gameConfig;
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
    24
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
    25
-(id) init {
3170
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
    26
	if (self = [super init]) {
3365
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3364
diff changeset
    27
        ipcPort = randomPort();
3170
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
    28
        
3373
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3369
diff changeset
    29
        // should check they exist and throw and exection if not
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3369
diff changeset
    30
        NSDictionary *dictSett = [[NSDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()];
3365
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3364
diff changeset
    31
        self.systemSettings = dictSett;
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3364
diff changeset
    32
        [dictSett release];
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3364
diff changeset
    33
        
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3364
diff changeset
    34
        NSDictionary *dictGame = [[NSDictionary alloc] initWithContentsOfFile:GAMECONFIG_FILE()];
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3364
diff changeset
    35
        self.gameConfig = dictGame;
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3364
diff changeset
    36
        [dictGame release];
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3356
diff changeset
    37
    } 
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3356
diff changeset
    38
    return self;
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
    39
}
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
    40
3221
3e6586c1ab4f koda's changelog turn
koda
parents: 3199
diff changeset
    41
-(NSString *)description {
3365
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3364
diff changeset
    42
    return [NSString stringWithFormat:@"ipcport: %d\nsockets: %d,%d\n teams: %@\n systemSettings: %@",ipcPort,sd,csd,gameConfig,systemSettings];
3221
3e6586c1ab4f koda's changelog turn
koda
parents: 3199
diff changeset
    43
}
3e6586c1ab4f koda's changelog turn
koda
parents: 3199
diff changeset
    44
2696
41aa7b56c17b settings are applied to game launch
koda
parents: 2694
diff changeset
    45
-(void) dealloc {
3365
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3364
diff changeset
    46
    [gameConfig release];
3029
67483e87590c a couple of smaller cleanups that didn't get in previous commit
koda
parents: 3021
diff changeset
    47
    [systemSettings release];
2696
41aa7b56c17b settings are applied to game launch
koda
parents: 2694
diff changeset
    48
	[super dealloc];
41aa7b56c17b settings are applied to game launch
koda
parents: 2694
diff changeset
    49
}
41aa7b56c17b settings are applied to game launch
koda
parents: 2694
diff changeset
    50
41aa7b56c17b settings are applied to game launch
koda
parents: 2694
diff changeset
    51
#pragma mark -
41aa7b56c17b settings are applied to game launch
koda
parents: 2694
diff changeset
    52
#pragma mark Thread/Network relevant code
3373
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3369
diff changeset
    53
// select one of GameSetup method and execute it in a seprate thread
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
    54
-(void) startThread: (NSString *) selector {
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
    55
	SEL usage = NSSelectorFromString(selector);
2702
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2699
diff changeset
    56
	[NSThread detachNewThreadSelector:usage toTarget:self withObject:nil];
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
    57
}
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
    58
3373
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3369
diff changeset
    59
// wrapper that computes the length of the message and then sends the command string
2696
41aa7b56c17b settings are applied to game launch
koda
parents: 2694
diff changeset
    60
-(int) sendToEngine: (NSString *)string {
3199
1a9938a27677 fixes to the mac build system
koda
parents: 3170
diff changeset
    61
	unsigned char length = [string length];
2696
41aa7b56c17b settings are applied to game launch
koda
parents: 2694
diff changeset
    62
	
41aa7b56c17b settings are applied to game launch
koda
parents: 2694
diff changeset
    63
	SDLNet_TCP_Send(csd, &length , 1);
41aa7b56c17b settings are applied to game launch
koda
parents: 2694
diff changeset
    64
	return SDLNet_TCP_Send(csd, [string UTF8String], length);
41aa7b56c17b settings are applied to game launch
koda
parents: 2694
diff changeset
    65
}
41aa7b56c17b settings are applied to game launch
koda
parents: 2694
diff changeset
    66
3373
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3369
diff changeset
    67
// unpacks team data from the team.plist to a sequence of commands for engine
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3356
diff changeset
    68
-(void) sendTeamData:(NSString *)fileName withPlayingHogs:(NSInteger) playingHogs ofColor:(NSNumber *)color{    
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3356
diff changeset
    69
    NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@", TEAMS_DIRECTORY(), fileName];
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3356
diff changeset
    70
    NSDictionary *teamData = [[NSDictionary alloc] initWithContentsOfFile:teamFile];
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3356
diff changeset
    71
    [teamFile release];
3199
1a9938a27677 fixes to the mac build system
koda
parents: 3170
diff changeset
    72
    
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3356
diff changeset
    73
    NSString *teamHashColorAndName = [[NSString alloc] initWithFormat:@"eaddteam %@ %@ %@", [teamData objectForKey:@"hash"], [color stringValue], [teamData objectForKey:@"teamname"]];
3170
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
    74
    [self sendToEngine: teamHashColorAndName];
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
    75
    [teamHashColorAndName release];
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
    76
    
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
    77
    NSString *grave = [[NSString alloc] initWithFormat:@"egrave %@", [teamData objectForKey:@"grave"]];
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
    78
    [self sendToEngine: grave];
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
    79
    [grave release];
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
    80
    
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
    81
    NSString *fort = [[NSString alloc] initWithFormat:@"efort %@", [teamData objectForKey:@"fort"]];
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
    82
    [self sendToEngine: fort];
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
    83
    [fort release];
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
    84
    
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
    85
    NSString *voicepack = [[NSString alloc] initWithFormat:@"evoicepack %@", [teamData objectForKey:@"voicepack"]];
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
    86
    [self sendToEngine: voicepack];
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
    87
    [voicepack release];
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
    88
    
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
    89
    NSString *flag = [[NSString alloc] initWithFormat:@"eflag %@", [teamData objectForKey:@"flag"]];
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
    90
    [self sendToEngine: flag];
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
    91
    [flag release];
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
    92
    
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
    93
    NSArray *hogs = [teamData objectForKey:@"hedgehogs"];
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3356
diff changeset
    94
    for (int i = 0; i < playingHogs; i++) {
3199
1a9938a27677 fixes to the mac build system
koda
parents: 3170
diff changeset
    95
        NSDictionary *hog = [hogs objectAtIndex:i];
1a9938a27677 fixes to the mac build system
koda
parents: 3170
diff changeset
    96
        
3170
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
    97
        NSString *hogLevelHealthAndName = [[NSString alloc] initWithFormat:@"eaddhh %@ %@ %@", [hog objectForKey:@"level"], [hog objectForKey:@"health"], [hog objectForKey:@"hogname"]];
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
    98
        [self sendToEngine: hogLevelHealthAndName];
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
    99
        [hogLevelHealthAndName release];
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
   100
        
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
   101
        NSString *hogHat = [[NSString alloc] initWithFormat:@"ehat %@", [hog objectForKey:@"hat"]];
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
   102
        [self sendToEngine: hogHat];
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
   103
        [hogHat release];
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3356
diff changeset
   104
    }
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3356
diff changeset
   105
    
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3356
diff changeset
   106
    [teamData release];
3349
5571592f10a8 update ifrontend to latest ammostore protocol
koda
parents: 3335
diff changeset
   107
}
5571592f10a8 update ifrontend to latest ammostore protocol
koda
parents: 3335
diff changeset
   108
3373
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3369
diff changeset
   109
// unpacks ammodata from the ammo.plist to a sequence of commands for engine
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3356
diff changeset
   110
-(void) sendAmmoData:(NSDictionary *)ammoData forTeams: (NSInteger)numberPlaying {
3349
5571592f10a8 update ifrontend to latest ammostore protocol
koda
parents: 3335
diff changeset
   111
    NSString *ammloadt = [[NSString alloc] initWithFormat:@"eammloadt %@", [ammoData objectForKey:@"ammostore_initialqt"]];
5571592f10a8 update ifrontend to latest ammostore protocol
koda
parents: 3335
diff changeset
   112
    [self sendToEngine: ammloadt];
5571592f10a8 update ifrontend to latest ammostore protocol
koda
parents: 3335
diff changeset
   113
    [ammloadt release];
5571592f10a8 update ifrontend to latest ammostore protocol
koda
parents: 3335
diff changeset
   114
    
5571592f10a8 update ifrontend to latest ammostore protocol
koda
parents: 3335
diff changeset
   115
    NSString *ammdelay = [[NSString alloc] initWithFormat:@"eammprob %@", [ammoData objectForKey:@"ammostore_probability"]];
5571592f10a8 update ifrontend to latest ammostore protocol
koda
parents: 3335
diff changeset
   116
    [self sendToEngine: ammdelay];
5571592f10a8 update ifrontend to latest ammostore protocol
koda
parents: 3335
diff changeset
   117
    [ammdelay release];
3170
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
   118
    
3349
5571592f10a8 update ifrontend to latest ammostore protocol
koda
parents: 3335
diff changeset
   119
    NSString *ammprob = [[NSString alloc] initWithFormat:@"eammdelay %@", [ammoData objectForKey:@"ammostore_delay"]];
5571592f10a8 update ifrontend to latest ammostore protocol
koda
parents: 3335
diff changeset
   120
    [self sendToEngine: ammprob];
5571592f10a8 update ifrontend to latest ammostore protocol
koda
parents: 3335
diff changeset
   121
    [ammprob release];
5571592f10a8 update ifrontend to latest ammostore protocol
koda
parents: 3335
diff changeset
   122
    
5571592f10a8 update ifrontend to latest ammostore protocol
koda
parents: 3335
diff changeset
   123
    NSString *ammreinf = [[NSString alloc] initWithFormat:@"eammreinf %@", [ammoData objectForKey:@"ammostore_crate"]];
5571592f10a8 update ifrontend to latest ammostore protocol
koda
parents: 3335
diff changeset
   124
    [self sendToEngine: ammreinf];
5571592f10a8 update ifrontend to latest ammostore protocol
koda
parents: 3335
diff changeset
   125
    [ammreinf release];
5571592f10a8 update ifrontend to latest ammostore protocol
koda
parents: 3335
diff changeset
   126
    
5571592f10a8 update ifrontend to latest ammostore protocol
koda
parents: 3335
diff changeset
   127
    // sent twice so it applies to both teams
5571592f10a8 update ifrontend to latest ammostore protocol
koda
parents: 3335
diff changeset
   128
    NSString *ammstore = [[NSString alloc] initWithString:@"eammstore"];
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3356
diff changeset
   129
    for (int i = 0; i < numberPlaying; i++)
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3356
diff changeset
   130
        [self sendToEngine: ammstore];
3349
5571592f10a8 update ifrontend to latest ammostore protocol
koda
parents: 3335
diff changeset
   131
    [ammstore release];
3170
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
   132
}
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
   133
3373
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3369
diff changeset
   134
// method that handles net setup with engine and keeps connection alive
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   135
-(void) engineProtocol {
2702
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2699
diff changeset
   136
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   137
	IPaddress ip;
2705
2b5625c4ec16 fix a nasty 196 bytes memory leak in engine, plus other stuff for iphone frontend
koda
parents: 2702
diff changeset
   138
	int eProto;
2702
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2699
diff changeset
   139
	BOOL clientQuit, serverQuit;
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   140
	char buffer[BUFFER_SIZE], string[BUFFER_SIZE];
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   141
	Uint8 msgSize;
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   142
	Uint16 gameTicks;
3365
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3364
diff changeset
   143
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3364
diff changeset
   144
    serverQuit = NO;
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3364
diff changeset
   145
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   146
	if (SDLNet_Init() < 0) {
2702
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2699
diff changeset
   147
		NSLog(@"SDLNet_Init: %s", SDLNet_GetError());
3365
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3364
diff changeset
   148
        serverQuit = YES;
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   149
	}
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   150
	
3373
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3369
diff changeset
   151
	// Resolving the host using NULL make network interface to listen
2694
dcd248e04f3d can use latest sdlimage, work on setting panel and option when launching the game, minor fixes
koda
parents: 2693
diff changeset
   152
	if (SDLNet_ResolveHost(&ip, NULL, ipcPort) < 0) {
2702
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2699
diff changeset
   153
		NSLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError());
3365
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3364
diff changeset
   154
        serverQuit = YES;
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   155
	}
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   156
	
3373
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3369
diff changeset
   157
	// Open a connection with the IP provided (listen on the host's port) 
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   158
	if (!(sd = SDLNet_TCP_Open(&ip))) {
2716
b9ca1bfca24f complete the replacement of init/free wrappers for every unit
koda
parents: 2705
diff changeset
   159
		NSLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), ipcPort);
3365
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3364
diff changeset
   160
        serverQuit = YES;
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   161
	}
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   162
	
2702
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2699
diff changeset
   163
	NSLog(@"engineProtocol - Waiting for a client on port %d", ipcPort);
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   164
	while (!serverQuit) {
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   165
		
3373
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3369
diff changeset
   166
		// This check the sd if there is a pending connection.
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3369
diff changeset
   167
        // If there is one, accept that, and open a new socket for communicating
2697
75880595a9f1 code cleanup and opengles optimizations
koda
parents: 2696
diff changeset
   168
		csd = SDLNet_TCP_Accept(sd);
75880595a9f1 code cleanup and opengles optimizations
koda
parents: 2696
diff changeset
   169
		if (NULL != csd) {
3373
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3369
diff changeset
   170
			// Now we can communicate with the client using csd socket
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3369
diff changeset
   171
			// sd will remain opened waiting other connections
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   172
			NSLog(@"engineProtocol - Client found");
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   173
			
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   174
			//first byte of the command alwayas contain the size of the command
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   175
			SDLNet_TCP_Recv(csd, &msgSize, sizeof(Uint8));
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   176
			
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   177
			SDLNet_TCP_Recv(csd, buffer, msgSize);
3373
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3369
diff changeset
   178
			gameTicks = SDLNet_Read16 (&buffer[msgSize - 2]);
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   179
			//NSLog(@"engineProtocol - %d: received [%s]", gameTicks, buffer);
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   180
			
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   181
			if ('C' == buffer[0]) {
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   182
				NSLog(@"engineProtocol - sending game config");
3199
1a9938a27677 fixes to the mac build system
koda
parents: 3170
diff changeset
   183
                
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   184
				// send config data data
2702
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2699
diff changeset
   185
				/*
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2699
diff changeset
   186
				seed is arbitrary string
3006
da6023c2745b restore compilation on simulator and device
koda
parents: 2799
diff changeset
   187
				addteam <32charsMD5hash> <color> <team name>
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   188
				addhh <level> <health> <hedgehog name>
2702
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2699
diff changeset
   189
				  <level> is 0 for human, 1-5 for bots (5 is the most stupid)
2734
fb9ad1587054 smaller improvements in mouse handling
koda
parents: 2716
diff changeset
   190
				*/
2702
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2699
diff changeset
   191
				// local game
2696
41aa7b56c17b settings are applied to game launch
koda
parents: 2694
diff changeset
   192
				[self sendToEngine:@"TL"];
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   193
				
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   194
				// seed info
3365
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3364
diff changeset
   195
				[self sendToEngine:[self.gameConfig objectForKey:@"seed_command"]];
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   196
				
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   197
				// various flags
3395
095273ad0e08 adding a somewhat working autoration for ipad (uStore.pas and uWorld.pas got mixed in the last two commits)
koda
parents: 3373
diff changeset
   198
				[self sendToEngine:@"e$gmflags 256"]; 
2696
41aa7b56c17b settings are applied to game launch
koda
parents: 2694
diff changeset
   199
				[self sendToEngine:@"e$damagepct 100"];
41aa7b56c17b settings are applied to game launch
koda
parents: 2694
diff changeset
   200
				[self sendToEngine:@"e$turntime 45000"];
41aa7b56c17b settings are applied to game launch
koda
parents: 2694
diff changeset
   201
				[self sendToEngine:@"e$minestime 3000"];
41aa7b56c17b settings are applied to game launch
koda
parents: 2694
diff changeset
   202
				[self sendToEngine:@"e$landadds 4"];
41aa7b56c17b settings are applied to game launch
koda
parents: 2694
diff changeset
   203
				[self sendToEngine:@"e$sd_turns 15"];
41aa7b56c17b settings are applied to game launch
koda
parents: 2694
diff changeset
   204
				[self sendToEngine:@"e$casefreq 5"];
3373
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3369
diff changeset
   205
				[self sendToEngine:@"e$explosives 2"];
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3369
diff changeset
   206
				[self sendToEngine:@"e$minedudpct 0"];
2753
2305bb5dc5f2 update iphone port to new code from Smaxx
koda
parents: 2743
diff changeset
   207
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   208
				// dimension of the map
3369
c7289e42f0ee add other controls for map preview, also fix a bug in digest
koda
parents: 3365
diff changeset
   209
				[self sendToEngine:[self.gameConfig objectForKey:@"templatefilter_command"]];
c7289e42f0ee add other controls for map preview, also fix a bug in digest
koda
parents: 3365
diff changeset
   210
				[self sendToEngine:[self.gameConfig objectForKey:@"mapgen_command"]];
c7289e42f0ee add other controls for map preview, also fix a bug in digest
koda
parents: 3365
diff changeset
   211
				[self sendToEngine:[self.gameConfig objectForKey:@"mazesize_command"]];
3373
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3369
diff changeset
   212
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   213
				// theme info
3373
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3369
diff changeset
   214
				[self sendToEngine:[self.gameConfig objectForKey:@"theme_command"]];
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   215
				
3365
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3364
diff changeset
   216
                NSArray *teamsConfig = [self.gameConfig objectForKey:@"teams_list"];
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3364
diff changeset
   217
                for (NSDictionary *teamData in teamsConfig) {
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3356
diff changeset
   218
                    [self sendTeamData:[teamData objectForKey:@"team"] 
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3356
diff changeset
   219
                       withPlayingHogs:[[teamData objectForKey:@"number"] intValue]
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3356
diff changeset
   220
                               ofColor:[teamData objectForKey:@"color"]];
3170
1dbf4f8eaac0 ifrontend: predispone teams in a proper data structure
koda
parents: 3165
diff changeset
   221
                }
3349
5571592f10a8 update ifrontend to latest ammostore protocol
koda
parents: 3335
diff changeset
   222
                
5571592f10a8 update ifrontend to latest ammostore protocol
koda
parents: 3335
diff changeset
   223
                NSDictionary *ammoData = [[NSDictionary alloc] initWithObjectsAndKeys:
3395
095273ad0e08 adding a somewhat working autoration for ipad (uStore.pas and uWorld.pas got mixed in the last two commits)
koda
parents: 3373
diff changeset
   224
                                          @"939192942219912103223511100120100000021119091",@"ammostore_initialqt",
095273ad0e08 adding a somewhat working autoration for ipad (uStore.pas and uWorld.pas got mixed in the last two commits)
koda
parents: 3373
diff changeset
   225
                                          @"040504054160065554655446477657666666615550100",@"ammostore_probability",
095273ad0e08 adding a somewhat working autoration for ipad (uStore.pas and uWorld.pas got mixed in the last two commits)
koda
parents: 3373
diff changeset
   226
                                          @"000000000000020550000004000700400000000020000",@"ammostore_delay",
095273ad0e08 adding a somewhat working autoration for ipad (uStore.pas and uWorld.pas got mixed in the last two commits)
koda
parents: 3373
diff changeset
   227
                                          @"131111031211111112311411111111111111121110111",@"ammostore_crate", nil];
3365
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3364
diff changeset
   228
                [self sendAmmoData:ammoData forTeams:[teamsConfig count]];
3349
5571592f10a8 update ifrontend to latest ammostore protocol
koda
parents: 3335
diff changeset
   229
                [ammoData release];
5571592f10a8 update ifrontend to latest ammostore protocol
koda
parents: 3335
diff changeset
   230
                
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3356
diff changeset
   231
                clientQuit = NO;
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   232
			} else {
2697
75880595a9f1 code cleanup and opengles optimizations
koda
parents: 2696
diff changeset
   233
				NSLog(@"engineProtocolThread - wrong message or client closed connection");
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   234
				clientQuit = YES;
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   235
			}
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   236
			
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   237
			while (!clientQuit){
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   238
				msgSize = 0;
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   239
				memset(buffer, 0, BUFFER_SIZE);
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   240
				memset(string, 0, BUFFER_SIZE);
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   241
				if (SDLNet_TCP_Recv(csd, &msgSize, sizeof(Uint8)) <= 0)
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   242
					clientQuit = YES;
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   243
				if (SDLNet_TCP_Recv(csd, buffer, msgSize) <=0)
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   244
					clientQuit = YES;
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   245
				
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   246
				gameTicks = SDLNet_Read16(&buffer[msgSize - 2]);
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   247
				//NSLog(@"engineProtocolThread - %d: received [%s]", gameTicks, buffer);
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   248
				
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   249
				switch (buffer[0]) {
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   250
					case '?':
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   251
						NSLog(@"Ping? Pong!");
2696
41aa7b56c17b settings are applied to game launch
koda
parents: 2694
diff changeset
   252
						[self sendToEngine:@"!"];
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   253
						break;
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   254
					case 'E':
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   255
						NSLog(@"ERROR - last console line: [%s]", buffer);
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   256
						clientQuit = YES;
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   257
						break;
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   258
					case 'e':
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   259
						sscanf(buffer, "%*s %d", &eProto);
3328
fe87c2242984 fix the save of the team and rearrange hat/name modification
koda
parents: 3312
diff changeset
   260
						short int netProto = 0;
2799
558b29bf00c5 add a new way to fetch version info from pascal to c
koda
parents: 2753
diff changeset
   261
						char *versionStr;
3199
1a9938a27677 fixes to the mac build system
koda
parents: 3170
diff changeset
   262
						
3006
da6023c2745b restore compilation on simulator and device
koda
parents: 2799
diff changeset
   263
                        HW_versionInfo(&netProto, &versionStr);
2799
558b29bf00c5 add a new way to fetch version info from pascal to c
koda
parents: 2753
diff changeset
   264
						if (netProto == eProto) {
558b29bf00c5 add a new way to fetch version info from pascal to c
koda
parents: 2753
diff changeset
   265
							NSLog(@"Setting protocol version %d (%s)", eProto, versionStr);
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   266
						} else {
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   267
							NSLog(@"ERROR - wrong protocol number: [%s] - expecting %d", buffer, eProto);
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   268
							clientQuit = YES;
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   269
						}
3199
1a9938a27677 fixes to the mac build system
koda
parents: 3170
diff changeset
   270
                        
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   271
						break;
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   272
					case 'i':
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   273
						switch (buffer[1]) {
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   274
							case 'r':
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   275
								NSLog(@"Winning team: %s", &buffer[2]);
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   276
								break;
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   277
							case 'k':
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   278
								NSLog(@"Best Hedgehog: %s", &buffer[2]);
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   279
								break;
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   280
						}
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   281
						break;
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   282
					default:
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   283
						// empty packet or just statistics
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   284
						break;
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   285
					// missing case for exiting right away
2697
75880595a9f1 code cleanup and opengles optimizations
koda
parents: 2696
diff changeset
   286
				}
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   287
			}
2702
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2699
diff changeset
   288
			NSLog(@"Engine exited, closing server");
2697
75880595a9f1 code cleanup and opengles optimizations
koda
parents: 2696
diff changeset
   289
			// wait a little to let the client close cleanly
2702
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2699
diff changeset
   290
			[NSThread sleepForTimeInterval:2];
2697
75880595a9f1 code cleanup and opengles optimizations
koda
parents: 2696
diff changeset
   291
			// Close the client socket
75880595a9f1 code cleanup and opengles optimizations
koda
parents: 2696
diff changeset
   292
			SDLNet_TCP_Close(csd);
2702
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2699
diff changeset
   293
			serverQuit = YES;
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   294
		}
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   295
	}
2702
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2699
diff changeset
   296
	
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   297
	SDLNet_TCP_Close(sd);
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   298
	SDLNet_Quit();
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   299
3369
c7289e42f0ee add other controls for map preview, also fix a bug in digest
koda
parents: 3365
diff changeset
   300
    [[NSFileManager defaultManager] removeItemAtPath:GAMECONFIG_FILE() error:NULL];
c7289e42f0ee add other controls for map preview, also fix a bug in digest
koda
parents: 3365
diff changeset
   301
    
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   302
	[pool release];
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   303
	[NSThread exit];
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   304
}
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   305
2696
41aa7b56c17b settings are applied to game launch
koda
parents: 2694
diff changeset
   306
#pragma mark -
2702
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2699
diff changeset
   307
#pragma mark Setting methods
3373
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3369
diff changeset
   308
// returns an array of c-strings that are read by engine at startup
2702
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2699
diff changeset
   309
-(const char **)getSettings {
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2699
diff changeset
   310
	NSString *ipcString = [[NSString alloc] initWithFormat:@"%d", ipcPort];
3006
da6023c2745b restore compilation on simulator and device
koda
parents: 2799
diff changeset
   311
	NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]];
3021
1d3302ac371f move the buttons a little
koda
parents: 3006
diff changeset
   312
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
3029
67483e87590c a couple of smaller cleanups that didn't get in previous commit
koda
parents: 3021
diff changeset
   313
    NSString *wSize = [[NSString alloc] initWithFormat:@"%d", (int) screenBounds.size.width];
67483e87590c a couple of smaller cleanups that didn't get in previous commit
koda
parents: 3021
diff changeset
   314
    NSString *hSize = [[NSString alloc] initWithFormat:@"%d", (int) screenBounds.size.height];
3369
c7289e42f0ee add other controls for map preview, also fix a bug in digest
koda
parents: 3365
diff changeset
   315
	const char **gameArgs = (const char**) malloc(sizeof(char *) * 8);
3165
3ec07a7d8456 just some very sane stuff for the iphone port (plus some macro on pascal files)
koda
parents: 3090
diff changeset
   316
3ec07a7d8456 just some very sane stuff for the iphone port (plus some macro on pascal files)
koda
parents: 3090
diff changeset
   317
    /*
3ec07a7d8456 just some very sane stuff for the iphone port (plus some macro on pascal files)
koda
parents: 3090
diff changeset
   318
    size_t size;
3ec07a7d8456 just some very sane stuff for the iphone port (plus some macro on pascal files)
koda
parents: 3090
diff changeset
   319
    // Set 'oldp' parameter to NULL to get the size of the data returned so we can allocate appropriate amount of space
3ec07a7d8456 just some very sane stuff for the iphone port (plus some macro on pascal files)
koda
parents: 3090
diff changeset
   320
    sysctlbyname("hw.machine", NULL, &size, NULL, 0); 
3ec07a7d8456 just some very sane stuff for the iphone port (plus some macro on pascal files)
koda
parents: 3090
diff changeset
   321
    char *name = malloc(size);
3ec07a7d8456 just some very sane stuff for the iphone port (plus some macro on pascal files)
koda
parents: 3090
diff changeset
   322
    // Get the platform name
3ec07a7d8456 just some very sane stuff for the iphone port (plus some macro on pascal files)
koda
parents: 3090
diff changeset
   323
    sysctlbyname("hw.machine", name, &size, NULL, 0);
3ec07a7d8456 just some very sane stuff for the iphone port (plus some macro on pascal files)
koda
parents: 3090
diff changeset
   324
    NSString *machine = [[NSString alloc] initWithUTF8String:name];
3ec07a7d8456 just some very sane stuff for the iphone port (plus some macro on pascal files)
koda
parents: 3090
diff changeset
   325
    free(name);
3ec07a7d8456 just some very sane stuff for the iphone port (plus some macro on pascal files)
koda
parents: 3090
diff changeset
   326
    
3ec07a7d8456 just some very sane stuff for the iphone port (plus some macro on pascal files)
koda
parents: 3090
diff changeset
   327
   	const char **gameArgs = (const char**) malloc(sizeof(char*) * 9);
3ec07a7d8456 just some very sane stuff for the iphone port (plus some macro on pascal files)
koda
parents: 3090
diff changeset
   328
3ec07a7d8456 just some very sane stuff for the iphone port (plus some macro on pascal files)
koda
parents: 3090
diff changeset
   329
    // if the machine is less than iphone 3gs or less than ipod touch 3g use reduced graphics (land array)
3ec07a7d8456 just some very sane stuff for the iphone port (plus some macro on pascal files)
koda
parents: 3090
diff changeset
   330
    if ([machine hasPrefix:@"iPhone1"] || ([machine hasPrefix:@"iPod"] && ([machine hasSuffix:@"1,1"] || [machine hasSuffix:@"2,1"])))
3ec07a7d8456 just some very sane stuff for the iphone port (plus some macro on pascal files)
koda
parents: 3090
diff changeset
   331
        gameArgs[8] = "1";
3ec07a7d8456 just some very sane stuff for the iphone port (plus some macro on pascal files)
koda
parents: 3090
diff changeset
   332
    else
3ec07a7d8456 just some very sane stuff for the iphone port (plus some macro on pascal files)
koda
parents: 3090
diff changeset
   333
        gameArgs[8] = "0";
3ec07a7d8456 just some very sane stuff for the iphone port (plus some macro on pascal files)
koda
parents: 3090
diff changeset
   334
    [machine release];
3ec07a7d8456 just some very sane stuff for the iphone port (plus some macro on pascal files)
koda
parents: 3090
diff changeset
   335
    */
3021
1d3302ac371f move the buttons a little
koda
parents: 3006
diff changeset
   336
    
3312
6d8f1c76756d restore and update the old general settings
koda
parents: 3221
diff changeset
   337
    // prevents using an empty nickname
6d8f1c76756d restore and update the old general settings
koda
parents: 3221
diff changeset
   338
    NSString *username;
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3356
diff changeset
   339
    NSString *originalUsername = [self.systemSettings objectForKey:@"username"];
3373
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3369
diff changeset
   340
    if ([originalUsername length] == 0)
3312
6d8f1c76756d restore and update the old general settings
koda
parents: 3221
diff changeset
   341
        username = [[NSString alloc] initWithFormat:@"MobileUser-%@",ipcString];
3373
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3369
diff changeset
   342
    else
3312
6d8f1c76756d restore and update the old general settings
koda
parents: 3221
diff changeset
   343
        username = [[NSString alloc] initWithString:originalUsername];
6d8f1c76756d restore and update the old general settings
koda
parents: 3221
diff changeset
   344
    
3369
c7289e42f0ee add other controls for map preview, also fix a bug in digest
koda
parents: 3365
diff changeset
   345
	gameArgs[0] = [username UTF8String];                                                        //UserNick
c7289e42f0ee add other controls for map preview, also fix a bug in digest
koda
parents: 3365
diff changeset
   346
	gameArgs[1] = [ipcString UTF8String];                                                       //ipcPort
c7289e42f0ee add other controls for map preview, also fix a bug in digest
koda
parents: 3365
diff changeset
   347
	gameArgs[2] = [[[self.systemSettings objectForKey:@"sound"] stringValue] UTF8String];       //isSoundEnabled
c7289e42f0ee add other controls for map preview, also fix a bug in digest
koda
parents: 3365
diff changeset
   348
	gameArgs[3] = [[[self.systemSettings objectForKey:@"music"] stringValue] UTF8String];       //isMusicEnabled
c7289e42f0ee add other controls for map preview, also fix a bug in digest
koda
parents: 3365
diff changeset
   349
	gameArgs[4] = [localeString UTF8String];                                                    //cLocaleFName
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3356
diff changeset
   350
	gameArgs[5] = [[[self.systemSettings objectForKey:@"alternate"] stringValue] UTF8String];	//cAltDamage
3369
c7289e42f0ee add other controls for map preview, also fix a bug in digest
koda
parents: 3365
diff changeset
   351
	gameArgs[6] = [wSize UTF8String];                                                           //cScreenHeight
c7289e42f0ee add other controls for map preview, also fix a bug in digest
koda
parents: 3365
diff changeset
   352
    gameArgs[7] = [hSize UTF8String];                                                           //cScreenWidth
3021
1d3302ac371f move the buttons a little
koda
parents: 3006
diff changeset
   353
    
3029
67483e87590c a couple of smaller cleanups that didn't get in previous commit
koda
parents: 3021
diff changeset
   354
    [wSize release];
67483e87590c a couple of smaller cleanups that didn't get in previous commit
koda
parents: 3021
diff changeset
   355
    [hSize release];
2702
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2699
diff changeset
   356
	[localeString release];
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2699
diff changeset
   357
	[ipcString release];
3312
6d8f1c76756d restore and update the old general settings
koda
parents: 3221
diff changeset
   358
    [username release];
2702
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2699
diff changeset
   359
	return gameArgs;
2693
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   360
}
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   361
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   362
3207e0eacd43 GameSetup is now a class, use of NSThread instead of pthreads, game doesn't quit after first execution (but crashes aftewards - the irony)
koda
parents:
diff changeset
   363
@end