project_files/HedgewarsMobile/Classes/otherSrc/CommodityFunctions.m
author koda
Thu, 17 Jun 2010 20:30:39 +0200
changeset 3514 59dbd31e9953
parent 3513 cocoaTouch/otherSrc/CommodityFunctions.m@f589230fa21b
child 3522 156c04c6a3d8
permissions -rw-r--r--
move stuff around and update iphone project
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     1
//
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     2
//  CommodityFunctions.m
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     3
//  HedgewarsMobile
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     4
//
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     5
//  Created by Vittorio on 08/04/10.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     6
//  Copyright 2010 __MyCompanyName__. All rights reserved.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     7
//
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     8
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     9
#import "CommodityFunctions.h"
3490
016b3172b645 a bunch of minor stuff
koda
parents: 3479
diff changeset
    10
#import <mach/mach.h>
016b3172b645 a bunch of minor stuff
koda
parents: 3479
diff changeset
    11
#import <mach/mach_host.h>
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    12
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    13
void createTeamNamed (NSString *nameWithoutExt) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    14
    NSString *teamsDirectory = TEAMS_DIRECTORY();
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    15
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    16
    if (![[NSFileManager defaultManager] fileExistsAtPath: teamsDirectory]) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    17
        [[NSFileManager defaultManager] createDirectoryAtPath:teamsDirectory 
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    18
                                  withIntermediateDirectories:NO 
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    19
                                                   attributes:nil 
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    20
                                                        error:NULL];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    21
    }
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    22
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    23
    NSMutableArray *hedgehogs = [[NSMutableArray alloc] initWithCapacity: MAX_HOGS];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    24
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    25
    for (int i = 0; i < MAX_HOGS; i++) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    26
        NSString *hogName = [[NSString alloc] initWithFormat:@"hedgehog %d",i];
3479
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    27
        NSDictionary *hog = [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithInt:0],@"level",
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    28
                             hogName,@"hogname", @"NoHat",@"hat", nil];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    29
        [hogName release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    30
        [hedgehogs addObject:hog];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    31
        [hog release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    32
    }
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    33
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    34
    NSDictionary *theTeam = [[NSDictionary alloc] initWithObjectsAndKeys:@"0",@"hash", nameWithoutExt,@"teamname",
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    35
                             @"Statue",@"grave", @"Plane",@"fort", @"Default",@"voicepack",
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    36
                             @"hedgewars",@"flag", hedgehogs,@"hedgehogs", nil];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    37
    [hedgehogs release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    38
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    39
    NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist", teamsDirectory, nameWithoutExt];
3352
ac5d14a35482 complete previews on the team settings
koda
parents: 3340
diff changeset
    40
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    41
    [theTeam writeToFile:teamFile atomically:YES];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    42
    [teamFile release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    43
    [theTeam release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    44
}
3330
987ec27b6042 add support for modifying the name of the team
koda
parents: 3325
diff changeset
    45
3479
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    46
void createSchemeNamed (NSString *nameWithoutExt) {
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    47
    NSString *schemesDirectory = SCHEMES_DIRECTORY();
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    48
    
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    49
    if (![[NSFileManager defaultManager] fileExistsAtPath: schemesDirectory]) {
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    50
        [[NSFileManager defaultManager] createDirectoryAtPath:schemesDirectory 
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    51
                                  withIntermediateDirectories:NO 
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    52
                                                   attributes:nil 
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    53
                                                        error:NULL];
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    54
    }
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    55
    
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    56
    NSArray *theScheme = [[NSArray alloc] initWithObjects:
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    57
                          [NSNumber numberWithBool:NO],    //fortmode
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    58
                          [NSNumber numberWithBool:NO],    //divideteam
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    59
                          [NSNumber numberWithBool:NO],    //solidland
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    60
                          [NSNumber numberWithBool:NO],    //addborder
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    61
                          [NSNumber numberWithBool:NO],    //lowgravity
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    62
                          [NSNumber numberWithBool:NO],    //lasersight
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    63
                          [NSNumber numberWithBool:NO],    //invulnerable
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    64
                          [NSNumber numberWithBool:NO],    //addmines
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    65
                          [NSNumber numberWithBool:NO],    //vampirism
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    66
                          [NSNumber numberWithBool:NO],    //karma
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    67
                          [NSNumber numberWithBool:NO],    //artillery
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    68
                          [NSNumber numberWithBool:YES],   //randomorder
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    69
                          [NSNumber numberWithBool:NO],    //king
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    70
                          [NSNumber numberWithBool:NO],    //placehedgehogs
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    71
                          [NSNumber numberWithBool:NO],    //clansharesammo
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    72
                          [NSNumber numberWithBool:NO],    //disablegirders
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    73
                          [NSNumber numberWithBool:NO],    //disablelandobjects
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    74
                          [NSNumber numberWithInt:100],    //damagemodifier
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    75
                          [NSNumber numberWithInt:45],     //turntime
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    76
                          [NSNumber numberWithInt:100],    //initialhealth
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    77
                          [NSNumber numberWithInt:15],     //suddendeathtimeout
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    78
                          [NSNumber numberWithInt:5],      //cratedrops
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    79
                          [NSNumber numberWithInt:3],      //minestime
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    80
                          [NSNumber numberWithInt:4],      //mines
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    81
                          [NSNumber numberWithInt:0],      //dudmines
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    82
                          [NSNumber numberWithInt:2],      //explosives
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    83
                          nil];
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    84
    
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    85
    NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist", schemesDirectory, nameWithoutExt];
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    86
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    87
    [theScheme writeToFile:schemeFile atomically:YES];
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    88
    [schemeFile release];
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    89
    [theScheme release];
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    90
}
972ae3ec178a initial support for game modifiers (schemes)
koda
parents: 3405
diff changeset
    91
3335
2520ee7a5484 new manager for handling (future) rotations
koda
parents: 3330
diff changeset
    92
BOOL rotationManager (UIInterfaceOrientation interfaceOrientation) {
2520ee7a5484 new manager for handling (future) rotations
koda
parents: 3330
diff changeset
    93
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
3405
8fdb08497bf1 js' patch that restores Vsync on snow leopard
koda
parents: 3385
diff changeset
    94
        return YES;
3335
2520ee7a5484 new manager for handling (future) rotations
koda
parents: 3330
diff changeset
    95
    else
3513
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3495
diff changeset
    96
        return (interfaceOrientation == UIInterfaceOrientationLandscapeRight) ||
f589230fa21b now it's possible to select the scheme file in the ifrontendfix a type about loading an image (iphone file system IS case senstive)
koda
parents: 3495
diff changeset
    97
               (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);  
3335
2520ee7a5484 new manager for handling (future) rotations
koda
parents: 3330
diff changeset
    98
}
3365
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3352
diff changeset
    99
3373
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3365
diff changeset
   100
NSInteger randomPort () {
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3365
diff changeset
   101
    srandom(time(NULL));
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3365
diff changeset
   102
    return (random() % 64511) + 1024;
3365
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3352
diff changeset
   103
}
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3352
diff changeset
   104
3373
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3365
diff changeset
   105
void popError (const char *title, const char *message) {
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3365
diff changeset
   106
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithUTF8String:title]
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3365
diff changeset
   107
                                                    message:[NSString stringWithUTF8String:message]
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3365
diff changeset
   108
                                                   delegate:nil
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3365
diff changeset
   109
                                          cancelButtonTitle:@"Ok"
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3365
diff changeset
   110
                                          otherButtonTitles:nil];
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3365
diff changeset
   111
    [alert show];
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3365
diff changeset
   112
    [alert release];
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3365
diff changeset
   113
}
3490
016b3172b645 a bunch of minor stuff
koda
parents: 3479
diff changeset
   114
016b3172b645 a bunch of minor stuff
koda
parents: 3479
diff changeset
   115
// by http://landonf.bikemonkey.org/code/iphone/Determining_Available_Memory.20081203.html
016b3172b645 a bunch of minor stuff
koda
parents: 3479
diff changeset
   116
void print_free_memory () {
016b3172b645 a bunch of minor stuff
koda
parents: 3479
diff changeset
   117
    mach_port_t host_port;
016b3172b645 a bunch of minor stuff
koda
parents: 3479
diff changeset
   118
    mach_msg_type_number_t host_size;
016b3172b645 a bunch of minor stuff
koda
parents: 3479
diff changeset
   119
    vm_size_t pagesize;
3373
c1ff724a5c34 use a proper bundle identifier
koda
parents: 3365
diff changeset
   120
    
3490
016b3172b645 a bunch of minor stuff
koda
parents: 3479
diff changeset
   121
    host_port = mach_host_self();
016b3172b645 a bunch of minor stuff
koda
parents: 3479
diff changeset
   122
    host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
016b3172b645 a bunch of minor stuff
koda
parents: 3479
diff changeset
   123
    host_page_size(host_port, &pagesize);        
016b3172b645 a bunch of minor stuff
koda
parents: 3479
diff changeset
   124
 
016b3172b645 a bunch of minor stuff
koda
parents: 3479
diff changeset
   125
    vm_statistics_data_t vm_stat;
016b3172b645 a bunch of minor stuff
koda
parents: 3479
diff changeset
   126
              
016b3172b645 a bunch of minor stuff
koda
parents: 3479
diff changeset
   127
    if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS)
016b3172b645 a bunch of minor stuff
koda
parents: 3479
diff changeset
   128
        DLog(@"Failed to fetch vm statistics");
016b3172b645 a bunch of minor stuff
koda
parents: 3479
diff changeset
   129
 
016b3172b645 a bunch of minor stuff
koda
parents: 3479
diff changeset
   130
    /* Stats in bytes */ 
016b3172b645 a bunch of minor stuff
koda
parents: 3479
diff changeset
   131
    natural_t mem_used = (vm_stat.active_count +
016b3172b645 a bunch of minor stuff
koda
parents: 3479
diff changeset
   132
                          vm_stat.inactive_count +
016b3172b645 a bunch of minor stuff
koda
parents: 3479
diff changeset
   133
                          vm_stat.wire_count) * pagesize;
016b3172b645 a bunch of minor stuff
koda
parents: 3479
diff changeset
   134
    natural_t mem_free = vm_stat.free_count * pagesize;
016b3172b645 a bunch of minor stuff
koda
parents: 3479
diff changeset
   135
    natural_t mem_total = mem_used + mem_free;
016b3172b645 a bunch of minor stuff
koda
parents: 3479
diff changeset
   136
    DLog(@"used: %u free: %u total: %u", mem_used, mem_free, mem_total);
016b3172b645 a bunch of minor stuff
koda
parents: 3479
diff changeset
   137
}