project_files/HedgewarsMobile/Classes/CommodityFunctions.m
author koda
Tue, 20 Jul 2010 03:14:43 +0200
changeset 3659 f8d5ac50e307
parent 3621 a8ddf681ba7d
child 3660 bc125bea5849
permissions -rw-r--r--
half reworking of the settings page with delegation some graphical enhancements, leaks and fixes
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
//  CommodityFunctions.m
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     3
//  HedgewarsMobile
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     4
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     5
//  Created by Vittorio on 08/04/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
#import "CommodityFunctions.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    10
#import <mach/mach.h>
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    11
#import <mach/mach_host.h>
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    12
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    13
void createTeamNamed (NSString *nameWithoutExt) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    14
    NSString *teamsDirectory = TEAMS_DIRECTORY();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    15
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    16
    if (![[NSFileManager defaultManager] fileExistsAtPath: teamsDirectory]) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    17
        [[NSFileManager defaultManager] createDirectoryAtPath:teamsDirectory 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    18
                                  withIntermediateDirectories:NO 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    19
                                                   attributes:nil 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    20
                                                        error:NULL];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    21
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    23
    NSMutableArray *hedgehogs = [[NSMutableArray alloc] initWithCapacity: MAX_HOGS];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    24
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
    for (int i = 0; i < MAX_HOGS; i++) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    26
        NSString *hogName = [[NSString alloc] initWithFormat:@"hedgehog %d",i];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    27
        NSDictionary *hog = [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithInt:0],@"level",
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    28
                             hogName,@"hogname", @"NoHat",@"hat", nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    29
        [hogName release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    30
        [hedgehogs addObject:hog];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    31
        [hog release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    32
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    33
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    34
    NSDictionary *theTeam = [[NSDictionary alloc] initWithObjectsAndKeys:@"0",@"hash", nameWithoutExt,@"teamname",
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    35
                             @"Statue",@"grave", @"Plane",@"fort", @"Default",@"voicepack",
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    36
                             @"hedgewars",@"flag", hedgehogs,@"hedgehogs", nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    37
    [hedgehogs release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    38
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    39
    NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@.plist", teamsDirectory, nameWithoutExt];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    40
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    41
    [theTeam writeToFile:teamFile atomically:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    42
    [teamFile release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    43
    [theTeam release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    45
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
void createWeaponNamed (NSString *nameWithoutExt) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    47
    NSString *weaponsDirectory = WEAPONS_DIRECTORY();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    48
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
    if (![[NSFileManager defaultManager] fileExistsAtPath: weaponsDirectory]) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    50
        [[NSFileManager defaultManager] createDirectoryAtPath:weaponsDirectory 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    51
                                  withIntermediateDirectories:NO 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
                                                   attributes:nil 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    53
                                                        error:NULL];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    56
    NSDictionary *theWeapon = [[NSDictionary alloc] initWithObjectsAndKeys:
3621
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3598
diff changeset
    57
                               [NSNumber numberWithInt:CURRENT_AMMOSIZE],@"version",
3598
a8aa06bae895 tiy new overlay graphics
koda
parents: 3573
diff changeset
    58
                               @"9391929422199121032235111001201000000211110111",@"ammostore_initialqt",
a8aa06bae895 tiy new overlay graphics
koda
parents: 3573
diff changeset
    59
                               @"0405040541600655546554464776576666666155510111",@"ammostore_probability",
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    60
                               @"0000000000000205500000040007004000000000200000",@"ammostore_delay",
3598
a8aa06bae895 tiy new overlay graphics
koda
parents: 3573
diff changeset
    61
                               @"1311110312111111123114111111111111111211111111",@"ammostore_crate", nil];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    63
    NSString *weaponFile = [[NSString alloc] initWithFormat:@"%@/%@.plist", weaponsDirectory, nameWithoutExt];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    64
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    65
    [theWeapon writeToFile:weaponFile atomically:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    66
    [weaponFile release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
    [theWeapon release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    68
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    69
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    70
void createSchemeNamed (NSString *nameWithoutExt) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    71
    NSString *schemesDirectory = SCHEMES_DIRECTORY();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    72
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    73
    if (![[NSFileManager defaultManager] fileExistsAtPath: schemesDirectory]) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    74
        [[NSFileManager defaultManager] createDirectoryAtPath:schemesDirectory 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    75
                                  withIntermediateDirectories:NO 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    76
                                                   attributes:nil 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    77
                                                        error:NULL];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    78
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    79
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    80
    NSArray *theScheme = [[NSArray alloc] initWithObjects:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    81
                          [NSNumber numberWithBool:NO],    //fortmode
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    82
                          [NSNumber numberWithBool:NO],    //divideteam
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    83
                          [NSNumber numberWithBool:NO],    //solidland
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    84
                          [NSNumber numberWithBool:NO],    //addborder
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    85
                          [NSNumber numberWithBool:NO],    //lowgravity
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    86
                          [NSNumber numberWithBool:NO],    //lasersight
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    87
                          [NSNumber numberWithBool:NO],    //invulnerable
3573
c84067629035 complete configuration for schemes
koda
parents: 3551
diff changeset
    88
                          [NSNumber numberWithBool:YES],   //addmines
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    89
                          [NSNumber numberWithBool:NO],    //vampirism
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    90
                          [NSNumber numberWithBool:NO],    //karma
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    91
                          [NSNumber numberWithBool:NO],    //artillery
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    92
                          [NSNumber numberWithBool:YES],   //randomorder
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    93
                          [NSNumber numberWithBool:NO],    //king
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    94
                          [NSNumber numberWithBool:NO],    //placehedgehogs
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    95
                          [NSNumber numberWithBool:NO],    //clansharesammo
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    96
                          [NSNumber numberWithBool:NO],    //disablegirders
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    97
                          [NSNumber numberWithBool:NO],    //disablelandobjects
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    98
                          [NSNumber numberWithInt:100],    //damagemodifier
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    99
                          [NSNumber numberWithInt:45],     //turntime
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   100
                          [NSNumber numberWithInt:100],    //initialhealth
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   101
                          [NSNumber numberWithInt:15],     //suddendeathtimeout
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   102
                          [NSNumber numberWithInt:5],      //cratedrops
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   103
                          [NSNumber numberWithInt:3],      //minestime
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   104
                          [NSNumber numberWithInt:4],      //mines
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   105
                          [NSNumber numberWithInt:0],      //dudmines
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   106
                          [NSNumber numberWithInt:2],      //explosives
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   107
                          nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   108
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   109
    NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist", schemesDirectory, nameWithoutExt];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   110
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   111
    [theScheme writeToFile:schemeFile atomically:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   112
    [schemeFile release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   113
    [theScheme release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   114
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   115
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   116
BOOL rotationManager (UIInterfaceOrientation interfaceOrientation) {
3551
d4de36b3801a moar zoom, fixed fort mode, other glitches
koda
parents: 3547
diff changeset
   117
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight) ||
d4de36b3801a moar zoom, fixed fort mode, other glitches
koda
parents: 3547
diff changeset
   118
           (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);  
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   119
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   120
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   121
NSInteger randomPort () {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   122
    srandom(time(NULL));
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   123
    return (random() % 64511) + 1024;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   124
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   125
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   126
void popError (const char *title, const char *message) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   127
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithUTF8String:title]
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   128
                                                    message:[NSString stringWithUTF8String:message]
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   129
                                                   delegate:nil
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   130
                                          cancelButtonTitle:@"Ok"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   131
                                          otherButtonTitles:nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   132
    [alert show];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   133
    [alert release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   134
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   135
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   136
// by http://landonf.bikemonkey.org/code/iphone/Determining_Available_Memory.20081203.html
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   137
void print_free_memory () {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   138
    mach_port_t host_port;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   139
    mach_msg_type_number_t host_size;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   140
    vm_size_t pagesize;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   141
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   142
    host_port = mach_host_self();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   143
    host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   144
    host_page_size(host_port, &pagesize);        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   145
 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   146
    vm_statistics_data_t vm_stat;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   147
              
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   148
    if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   149
        DLog(@"Failed to fetch vm statistics");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   150
 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   151
    /* Stats in bytes */ 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   152
    natural_t mem_used = (vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count) * pagesize;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   153
    natural_t mem_free = vm_stat.free_count * pagesize;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   154
    natural_t mem_total = mem_used + mem_free;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   155
    DLog(@"used: %u free: %u total: %u", mem_used, mem_free, mem_total);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   156
}