project_files/frontlib/model/team.c
author Medo <smaxein@googlemail.com>
Tue, 12 Jun 2012 11:25:05 +0200
changeset 7224 5143861c83bd
parent 7179 f84805e6df03
child 7227 1c859f572d72
permissions -rw-r--r--
Cleanup, refactoring and generally more development in the frontlib
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
#include "team.h"
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
     2
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
     3
#include "../util/inihelper.h"
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
     4
#include "../util/util.h"
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
     5
#include "../util/logging.h"
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
     6
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
     7
static flib_team *from_ini_handleError(flib_team *result, dictionary *settingfile, char **bindingKeys) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
     8
	if(settingfile) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
     9
		iniparser_freedict(settingfile);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    10
	}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    11
	flib_team_destroy(result);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    12
	free(bindingKeys);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    13
	return NULL;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    14
}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    15
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    16
flib_team *flib_team_from_ini(const char *filename) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    17
	flib_team *result = flib_calloc(1, sizeof(flib_team));
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    18
	dictionary *settingfile = NULL;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    19
	char **bindingKeys = NULL;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    20
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    21
	if(!filename) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    22
		flib_log_e("null parameter in flib_team_from_ini");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    23
		return from_ini_handleError(result, settingfile, bindingKeys);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    24
	}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    25
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    26
	if(!result) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    27
		return from_ini_handleError(result, settingfile, bindingKeys);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    28
	}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    29
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    30
	settingfile = iniparser_load(filename);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    31
	if(!settingfile) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    32
		flib_log_e("Error loading team file %s", filename);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    33
		return from_ini_handleError(result, settingfile, bindingKeys);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    34
	}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    35
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    36
	bool error = false;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    37
	result->name = inihelper_getstringdup(settingfile, &error, "team", "name");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    38
	result->grave = inihelper_getstringdup(settingfile, &error, "team", "grave");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    39
	result->fort = inihelper_getstringdup(settingfile, &error, "team", "fort");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    40
	result->voicepack = inihelper_getstringdup(settingfile, &error, "team", "voicepack");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    41
	result->flag = inihelper_getstringdup(settingfile, &error, "team", "flag");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    42
	result->rounds = inihelper_getint(settingfile, &error, "team", "rounds");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    43
	result->wins = inihelper_getint(settingfile, &error, "team", "wins");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    44
	result->campaignProgress = inihelper_getint(settingfile, &error, "team", "campaignprogress");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    45
	int difficulty = inihelper_getint(settingfile, &error, "team", "difficulty");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    46
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    47
	char sectionName[10];
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    48
	strcpy(sectionName, "hedgehog0");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    49
	for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    50
		sectionName[8] = '0'+i;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    51
		result->hogs[i].name = inihelper_getstringdup(settingfile, &error, sectionName, "name");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    52
		result->hogs[i].hat = inihelper_getstringdup(settingfile, &error, sectionName, "hat");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    53
		result->hogs[i].rounds = inihelper_getint(settingfile, &error, sectionName, "rounds");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    54
		result->hogs[i].kills = inihelper_getint(settingfile, &error, sectionName, "kills");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    55
		result->hogs[i].deaths = inihelper_getint(settingfile, &error, sectionName, "deaths");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    56
		result->hogs[i].suicides = inihelper_getint(settingfile, &error, sectionName, "suicides");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    57
		result->hogs[i].difficulty = difficulty;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    58
		result->hogs[i].initialHealth = TEAM_DEFAULT_HEALTH;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    59
	}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    60
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    61
	result->bindingCount = iniparser_getsecnkeys(settingfile, "binds");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    62
	result->bindings = flib_calloc(result->bindingCount, sizeof(flib_binding));
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    63
	bindingKeys = iniparser_getseckeys(settingfile, "binds");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    64
	if(!result->bindings || !bindingKeys) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    65
		return from_ini_handleError(result, settingfile, bindingKeys);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    66
	}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    67
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    68
	for(int i=0; i<result->bindingCount; i++) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    69
		result->bindings[i].binding = flib_strdupnull(iniparser_getstring(settingfile, bindingKeys[i], NULL));
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    70
		// The key names all start with "binds:", so we skip that.
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    71
		result->bindings[i].action = inihelper_urldecode(bindingKeys[i]+strlen("binds:"));
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    72
		if(!result->bindings[i].action || !result->bindings[i].binding) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    73
			error = true;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    74
		}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    75
	}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    76
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    77
	if(error) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    78
		flib_log_e("Error reading team file %s", filename);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    79
		return from_ini_handleError(result, settingfile, bindingKeys);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    80
	}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    81
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    82
	iniparser_freedict(settingfile);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    83
	free(bindingKeys);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    84
	return result;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    85
}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    86
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    87
void flib_team_destroy(flib_team *team) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    88
	if(team) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    89
		for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    90
			free(team->hogs[i].name);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    91
			free(team->hogs[i].hat);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    92
		}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    93
		free(team->name);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    94
		free(team->grave);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    95
		free(team->fort);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    96
		free(team->voicepack);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    97
		free(team->flag);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    98
		if(team->bindings) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    99
			for(int i=0; i<team->bindingCount; i++) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   100
				free(team->bindings[i].action);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   101
				free(team->bindings[i].binding);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   102
			}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   103
		}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   104
		free(team->bindings);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   105
		free(team->hash);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   106
		flib_weaponset_destroy(team->weaponset);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   107
		free(team);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   108
	}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   109
}