project_files/frontlib/model/team.c
author alfadur
Sun, 06 Sep 2020 00:21:37 +0300
changeset 15738 027a56b3895e
parent 10017 de822cd3df3a
permissions -rw-r--r--
fix vcpkg build
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     1
/*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     2
 * Hedgewars, a free turn based strategy game
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     4
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     5
 * This program is free software; you can redistribute it and/or
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     6
 * modify it under the terms of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     8
 * of the License, or (at your option) any later version.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     9
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    13
 * GNU General Public License for more details.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    14
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    15
 * You should have received a copy of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    16
 * along with this program; if not, write to the Free Software
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    18
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    19
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    20
#include "team.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    21
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    22
#include "../util/inihelper.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    23
#include "../util/util.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    24
#include "../util/logging.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    25
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    26
#include <string.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    27
#include <stdlib.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    28
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    29
static flib_team *from_ini_handleError(flib_team *result, flib_ini *settingfile) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    30
    flib_ini_destroy(settingfile);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    31
    flib_team_destroy(result);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    32
    return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    33
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    34
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    35
flib_team *flib_team_from_ini(const char *filename) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    36
    if(log_badargs_if(filename==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    37
        return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    38
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    39
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    40
    flib_team *result = flib_calloc(1, sizeof(flib_team));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    41
    flib_ini *ini = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    42
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    43
    if(!result) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    44
        return from_ini_handleError(result, ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    45
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    46
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    47
    ini = flib_ini_load(filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    48
    if(!ini) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    49
        flib_log_e("Error loading team file %s", filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    50
        return from_ini_handleError(result, ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    51
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    52
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    53
    if(flib_ini_enter_section(ini, "team")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    54
        flib_log_e("Missing section \"Team\" in team file %s", filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    55
        return from_ini_handleError(result, ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    56
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    57
    bool error = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    58
    error |= flib_ini_get_str(ini, &result->name, "name");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    59
    error |= flib_ini_get_str(ini, &result->grave, "grave");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    60
    error |= flib_ini_get_str(ini, &result->fort, "fort");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    61
    error |= flib_ini_get_str(ini, &result->voicepack, "voicepack");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    62
    error |= flib_ini_get_str(ini, &result->flag, "flag");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    63
    error |= flib_ini_get_int(ini, &result->rounds, "rounds");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    64
    error |= flib_ini_get_int(ini, &result->wins, "wins");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    65
    error |= flib_ini_get_int(ini, &result->campaignProgress, "campaignprogress");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    66
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    67
    int difficulty = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    68
    error |= flib_ini_get_int(ini, &difficulty, "difficulty");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    69
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    70
    if(error) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    71
        flib_log_e("Missing or malformed entry in section \"Team\" in file %s", filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    72
        return from_ini_handleError(result, ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    73
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    74
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    75
    for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    76
        char sectionName[32];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    77
        if(snprintf(sectionName, sizeof(sectionName), "hedgehog%i", i) <= 0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    78
            return from_ini_handleError(result, ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    79
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    80
        if(flib_ini_enter_section(ini, sectionName)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    81
            flib_log_e("Missing section \"%s\" in team file %s", sectionName, filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    82
            return from_ini_handleError(result, ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    83
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    84
        flib_hog *hog = &result->hogs[i];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    85
        error |= flib_ini_get_str(ini, &hog->name, "name");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    86
        error |= flib_ini_get_str(ini, &hog->hat, "hat");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    87
        error |= flib_ini_get_int(ini, &hog->rounds, "rounds");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    88
        error |= flib_ini_get_int(ini, &hog->kills, "kills");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    89
        error |= flib_ini_get_int(ini, &hog->deaths, "deaths");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    90
        error |= flib_ini_get_int(ini, &hog->suicides, "suicides");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    91
        result->hogs[i].difficulty = difficulty;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    92
        result->hogs[i].initialHealth = TEAM_DEFAULT_HEALTH;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    93
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    94
        if(error) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    95
            flib_log_e("Missing or malformed entry in section \"%s\" in file %s", sectionName, filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    96
            return from_ini_handleError(result, ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    97
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    98
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    99
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   100
    if(!flib_ini_enter_section(ini, "binds")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   101
        result->bindingCount = flib_ini_get_keycount(ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   102
        if(result->bindingCount<0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   103
            flib_log_e("Error reading bindings from file %s", filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   104
            result->bindingCount = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   105
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   106
        result->bindings = flib_calloc(result->bindingCount, sizeof(flib_binding));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   107
        if(!result->bindings) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   108
            return from_ini_handleError(result, ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   109
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   110
        for(int i=0; i<result->bindingCount; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   111
            char *keyname = flib_ini_get_keyname(ini, i);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   112
            if(!keyname) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   113
                error = true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   114
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   115
                result->bindings[i].action = flib_urldecode(keyname);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   116
                error |= !result->bindings[i].action;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   117
                error |= flib_ini_get_str(ini, &result->bindings[i].binding, keyname);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   118
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   119
            free(keyname);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   120
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   121
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   122
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   123
    if(error) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   124
        flib_log_e("Error reading team file %s", filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   125
        return from_ini_handleError(result, ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   126
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   127
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   128
    flib_ini_destroy(ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   129
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   130
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   131
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   132
void flib_team_destroy(flib_team *team) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   133
    if(team) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   134
        for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   135
            free(team->hogs[i].name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   136
            free(team->hogs[i].hat);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   137
            flib_weaponset_destroy(team->hogs[i].weaponset);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   138
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   139
        free(team->name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   140
        free(team->grave);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   141
        free(team->fort);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   142
        free(team->voicepack);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   143
        free(team->flag);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   144
        if(team->bindings) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   145
            for(int i=0; i<team->bindingCount; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   146
                free(team->bindings[i].action);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   147
                free(team->bindings[i].binding);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   148
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   149
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   150
        free(team->bindings);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   151
        free(team->ownerName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   152
        free(team);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   153
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   154
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   155
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   156
static int writeTeamSection(const flib_team *team, flib_ini *ini) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   157
    if(flib_ini_create_section(ini, "team")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   158
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   159
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   160
    bool error = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   161
    error |= flib_ini_set_str(ini, "name",  team->name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   162
    error |= flib_ini_set_str(ini, "grave", team->grave);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   163
    error |= flib_ini_set_str(ini, "fort", team->fort);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   164
    error |= flib_ini_set_str(ini, "voicepack", team->voicepack);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   165
    error |= flib_ini_set_str(ini, "flag", team->flag);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   166
    error |= flib_ini_set_int(ini, "rounds", team->rounds);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   167
    error |= flib_ini_set_int(ini, "wins", team->wins);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   168
    error |= flib_ini_set_int(ini, "campaignprogress", team->campaignProgress);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   169
    error |= flib_ini_set_int(ini, "difficulty", team->hogs[0].difficulty);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   170
    return error;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   171
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   172
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   173
static int writeHogSections(const flib_team *team, flib_ini *ini) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   174
    for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   175
        const flib_hog *hog = &team->hogs[i];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   176
        char sectionName[32];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   177
        if(snprintf(sectionName, sizeof(sectionName), "hedgehog%i", i) <= 0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   178
            return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   179
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   180
        if(flib_ini_create_section(ini, sectionName)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   181
            return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   182
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   183
        bool error = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   184
        error |= flib_ini_set_str(ini, "name", hog->name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   185
        error |= flib_ini_set_str(ini, "hat", hog->hat);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   186
        error |= flib_ini_set_int(ini, "rounds", hog->rounds);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   187
        error |= flib_ini_set_int(ini, "kills", hog->kills);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   188
        error |= flib_ini_set_int(ini, "deaths", hog->deaths);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   189
        error |= flib_ini_set_int(ini, "suicides", hog->suicides);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   190
        if(error) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   191
            return error;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   192
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   193
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   194
    return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   195
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   196
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   197
static int writeBindingSection(const flib_team *team, flib_ini *ini) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   198
    if(team->bindingCount == 0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   199
        return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   200
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   201
    if(flib_ini_create_section(ini, "binds")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   202
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   203
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   204
    for(int i=0; i<team->bindingCount; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   205
        bool error = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   206
        char *action = flib_urlencode(team->bindings[i].action);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   207
        if(action) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   208
            error |= flib_ini_set_str(ini, action, team->bindings[i].binding);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   209
            free(action);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   210
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   211
            error = true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   212
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   213
        if(error) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   214
            return error;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   215
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   216
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   217
    return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   218
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   219
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   220
int flib_team_to_ini(const char *filename, const flib_team *team) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   221
    int result = -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   222
    if(!log_badargs_if2(filename==NULL, team==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   223
        flib_ini *ini = flib_ini_create(filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   224
        bool error = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   225
        error |= writeTeamSection(team, ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   226
        error |= writeHogSections(team, ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   227
        error |= writeBindingSection(team, ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   228
        if(!error) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   229
            result = flib_ini_save(ini, filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   230
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   231
        flib_ini_destroy(ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   232
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   233
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   234
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   235
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   236
int flib_team_set_weaponset(flib_team *team, const flib_weaponset *set) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   237
    if(team) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   238
        for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   239
            flib_weaponset_destroy(team->hogs[i].weaponset);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   240
            team->hogs[i].weaponset = flib_weaponset_copy(set);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   241
            if(set && !team->hogs[i].weaponset) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   242
                return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   243
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   244
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   245
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   246
    return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   247
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   248
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   249
void flib_team_set_health(flib_team *team, int health) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   250
    if(team) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   251
        for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   252
            team->hogs[i].initialHealth = health;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   253
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   254
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   255
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   256
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   257
static char *strdupWithError(const char *in, bool *error) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   258
    char *out = flib_strdupnull(in);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   259
    if(in && !out) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   260
        *error = true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   261
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   262
    return out;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   263
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   264
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   265
flib_team *flib_team_copy(const flib_team *team) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   266
    flib_team *result = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   267
    if(team) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   268
        flib_team *tmpTeam = flib_calloc(1, sizeof(flib_team));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   269
        if(tmpTeam) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   270
            bool error = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   271
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   272
            for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   273
                tmpTeam->hogs[i].name = strdupWithError(team->hogs[i].name, &error);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   274
                tmpTeam->hogs[i].hat = strdupWithError(team->hogs[i].hat, &error);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   275
                tmpTeam->hogs[i].rounds = team->hogs[i].rounds;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   276
                tmpTeam->hogs[i].kills = team->hogs[i].kills;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   277
                tmpTeam->hogs[i].deaths = team->hogs[i].deaths;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   278
                tmpTeam->hogs[i].suicides = team->hogs[i].suicides;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   279
                tmpTeam->hogs[i].difficulty = team->hogs[i].difficulty;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   280
                tmpTeam->hogs[i].initialHealth = team->hogs[i].initialHealth;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   281
                tmpTeam->hogs[i].weaponset = flib_weaponset_copy(team->hogs[i].weaponset);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   282
                if(team->hogs[i].weaponset && !tmpTeam->hogs[i].weaponset) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   283
                    error = true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   284
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   285
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   286
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   287
            tmpTeam->name = strdupWithError(team->name, &error);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   288
            tmpTeam->grave = strdupWithError(team->grave, &error);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   289
            tmpTeam->fort = strdupWithError(team->fort, &error);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   290
            tmpTeam->voicepack = strdupWithError(team->voicepack, &error);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   291
            tmpTeam->flag = strdupWithError(team->flag, &error);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   292
            tmpTeam->ownerName = strdupWithError(team->ownerName, &error);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   293
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   294
            tmpTeam->bindingCount = team->bindingCount;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   295
            if(team->bindings) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   296
                tmpTeam->bindings = flib_calloc(team->bindingCount, sizeof(flib_binding));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   297
                if(tmpTeam->bindings) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   298
                    for(int i=0; i<tmpTeam->bindingCount; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   299
                        tmpTeam->bindings[i].action = strdupWithError(team->bindings[i].action, &error);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   300
                        tmpTeam->bindings[i].binding = strdupWithError(team->bindings[i].binding, &error);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   301
                    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   302
                } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   303
                    error = true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   304
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   305
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   306
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   307
            tmpTeam->rounds = team->rounds;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   308
            tmpTeam->wins = team->wins;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   309
            tmpTeam->campaignProgress = team->campaignProgress;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   310
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   311
            tmpTeam->colorIndex = team->colorIndex;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   312
            tmpTeam->hogsInGame = team->hogsInGame;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   313
            tmpTeam->remoteDriven = team->remoteDriven;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   314
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   315
            if(!error) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   316
                result = tmpTeam;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   317
                tmpTeam = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   318
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   319
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   320
        flib_team_destroy(tmpTeam);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   321
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   322
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   323
}