project_files/frontlib/model/weapon.c
author Medo <smaxein@googlemail.com>
Tue, 10 Jul 2012 19:33:57 +0200
changeset 7320 e704706008d4
parent 7316 f7b49b2c5d84
child 7497 7e1d72fc03c7
permissions -rw-r--r--
frontlib: Renamed cfg to scheme, Un-refcounted some types, small API adjustments
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7314
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     1
/*
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     2
 * Hedgewars, a free turn based strategy game
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     4
 *
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     5
 * This program is free software; you can redistribute it and/or
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     6
 * modify it under the terms of the GNU General Public License
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     8
 * of the License, or (at your option) any later version.
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     9
 *
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    13
 * GNU General Public License for more details.
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    14
 *
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    15
 * You should have received a copy of the GNU General Public License
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    16
 * along with this program; if not, write to the Free Software
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    18
 */
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    19
7177
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
#include "weapon.h"
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents: 7177
diff changeset
    22
#include "../util/inihelper.h"
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents: 7177
diff changeset
    23
#include "../util/logging.h"
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents: 7177
diff changeset
    24
#include "../util/util.h"
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    25
#include "../util/refcounter.h"
7269
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7230
diff changeset
    26
#include "../util/list.h"
7177
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
#include <stdlib.h>
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
#include <ctype.h>
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    30
#include <string.h>
7177
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    32
static void flib_weaponset_destroy(flib_weaponset *cfg) {
7177
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
	if(cfg) {
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
		free(cfg->name);
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
		free(cfg);
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
	}
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
}
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    39
static void setField(char field[WEAPONS_COUNT+1], const char *line, int lineLen, bool no9) {
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    40
	if(lineLen>WEAPONS_COUNT) {
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    41
		lineLen = WEAPONS_COUNT;
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    42
	}
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    43
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    44
	char min = '0';
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    45
	char max = no9 ? '8' : '9';
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    46
	for(int i=0; i<lineLen; i++) {
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    47
		if(line[i] >= min && line[i] <= max) {
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    48
			field[i] = line[i];
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    49
		} else {
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    50
			flib_log_w("Invalid character in weapon config string \"%.*s\", position %i", lineLen, line, i);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    51
			field[i] = '0';
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    52
		}
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    53
	}
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    54
	for(int i=lineLen; i<WEAPONS_COUNT; i++) {
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    55
		field[i] = '0';
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    56
	}
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    57
	field[WEAPONS_COUNT] = 0;
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    58
}
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    59
7177
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
flib_weaponset *flib_weaponset_create(const char *name) {
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    61
	flib_weaponset *result = NULL;
7316
f7b49b2c5d84 frontlib: Improved and unified parameter checking, moved the cmdlineClient out
Medo <smaxein@googlemail.com>
parents: 7314
diff changeset
    62
	if(!log_badargs_if(name==NULL)) {
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    63
		flib_weaponset *newSet = flib_weaponset_retain(flib_calloc(1, sizeof(flib_weaponset)));
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    64
		if(newSet) {
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    65
			newSet->name = flib_strdupnull(name);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    66
			if(newSet->name) {
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    67
				setField(newSet->loadout, "", 0, false);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    68
				setField(newSet->crateprob, "", 0, false);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    69
				setField(newSet->crateammo, "", 0, false);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    70
				setField(newSet->delay, "", 0, false);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    71
				result = flib_weaponset_retain(newSet);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    72
			}
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    73
		}
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    74
		flib_weaponset_release(newSet);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    75
	}
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    76
	return result;
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    77
}
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    78
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    79
flib_weaponset *flib_weaponset_retain(flib_weaponset *weaponset) {
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    80
	if(weaponset) {
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    81
		flib_retain(&weaponset->_referenceCount, "flib_weaponset");
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    82
	}
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    83
	return weaponset;
7177
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
    84
}
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
    85
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    86
void flib_weaponset_release(flib_weaponset *weaponset) {
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    87
	if(weaponset && flib_release(&weaponset->_referenceCount, "flib_weaponset")) {
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    88
		flib_weaponset_destroy(weaponset);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    89
	}
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    90
}
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    91
7275
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
    92
flib_weaponset *flib_weaponset_copy(const flib_weaponset *weaponset) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
    93
	if(!weaponset) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
    94
		return NULL;
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
    95
	}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
    96
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
    97
	flib_weaponset *result = flib_weaponset_create(weaponset->name);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
    98
	if(result) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
    99
		memcpy(result->loadout, weaponset->loadout, WEAPONS_COUNT+1);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   100
		memcpy(result->crateprob, weaponset->crateprob, WEAPONS_COUNT+1);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   101
		memcpy(result->delay, weaponset->delay, WEAPONS_COUNT+1);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   102
		memcpy(result->crateammo, weaponset->crateammo, WEAPONS_COUNT+1);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   103
	}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   104
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   105
	return result;
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   106
}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   107
7320
e704706008d4 frontlib: Renamed cfg to scheme, Un-refcounted some types, small API adjustments
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   108
void flib_weaponsetlist_destroy(flib_weaponsetlist *list) {
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   109
	if(list) {
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   110
		for(int i=0; i<list->weaponsetCount; i++) {
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   111
			flib_weaponset_release(list->weaponsets[i]);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   112
		}
7275
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   113
		free(list->weaponsets);
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   114
		free(list);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   115
	}
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   116
}
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   117
7271
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   118
flib_weaponset *flib_weaponset_from_ammostring(const char *name, const char *ammostring) {
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   119
	flib_weaponset *result = NULL;
7316
f7b49b2c5d84 frontlib: Improved and unified parameter checking, moved the cmdlineClient out
Medo <smaxein@googlemail.com>
parents: 7314
diff changeset
   120
	if(!log_badargs_if2(name==NULL, ammostring==NULL)) {
7271
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   121
		result = flib_weaponset_create(name);
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   122
		if(result) {
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   123
			int fieldlen = strlen(ammostring)/4;
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   124
			setField(result->loadout, ammostring, fieldlen, false);
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   125
			setField(result->crateprob, ammostring + fieldlen, fieldlen, true);
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   126
			setField(result->delay, ammostring + 2*fieldlen, fieldlen, true);
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   127
			setField(result->crateammo, ammostring + 3*fieldlen, fieldlen, true);
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   128
		}
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   129
	}
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   130
	return result;
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   131
}
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   132
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   133
static int fillWeaponsetFromIni(flib_weaponsetlist *list, flib_ini *ini, int index) {
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   134
	int result = -1;
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   135
	char *keyname = flib_ini_get_keyname(ini, index);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   136
	char *decodedKeyname = flib_urldecode(keyname);
7271
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   137
	char *ammostring = NULL;
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   138
	if(decodedKeyname && !flib_ini_get_str(ini, &ammostring, keyname)) {
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   139
		flib_weaponset *set = flib_weaponset_from_ammostring(decodedKeyname, ammostring);
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   140
		if(set) {
7271
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   141
			result = flib_weaponsetlist_insert(list, set, list->weaponsetCount);
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   142
		}
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   143
		flib_weaponset_release(set);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   144
	}
7271
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   145
	free(ammostring);
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   146
	free(decodedKeyname);
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   147
	free(keyname);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   148
	return result;
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   149
}
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   150
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   151
static int fillWeaponsetsFromIni(flib_weaponsetlist *list, flib_ini *ini) {
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   152
	bool error = false;
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   153
	int weaponsets = flib_ini_get_keycount(ini);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   154
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   155
	for(int i=0; i<weaponsets && !error; i++) {
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   156
		error |= fillWeaponsetFromIni(list, ini, i);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   157
	}
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   158
	return error;
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   159
}
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   160
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   161
flib_weaponsetlist *flib_weaponsetlist_from_ini(const char *filename) {
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   162
	flib_weaponsetlist *result = NULL;
7316
f7b49b2c5d84 frontlib: Improved and unified parameter checking, moved the cmdlineClient out
Medo <smaxein@googlemail.com>
parents: 7314
diff changeset
   163
	if(!log_badargs_if(filename==NULL)) {
7227
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   164
		flib_ini *ini = flib_ini_load(filename);
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   165
		if(!ini) {
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   166
			flib_log_e("Missing file %s.", filename);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   167
		} else if(flib_ini_enter_section(ini, "General")) {
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   168
			flib_log_e("Missing section \"General\" in file %s.", filename);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   169
		} else {
7320
e704706008d4 frontlib: Renamed cfg to scheme, Un-refcounted some types, small API adjustments
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   170
			flib_weaponsetlist *tmpList = flib_weaponsetlist_create();
e704706008d4 frontlib: Renamed cfg to scheme, Un-refcounted some types, small API adjustments
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   171
			if(tmpList && !fillWeaponsetsFromIni(tmpList, ini)) {
e704706008d4 frontlib: Renamed cfg to scheme, Un-refcounted some types, small API adjustments
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   172
				result = tmpList;
e704706008d4 frontlib: Renamed cfg to scheme, Un-refcounted some types, small API adjustments
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   173
				tmpList = NULL;
7177
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
   174
			}
7320
e704706008d4 frontlib: Renamed cfg to scheme, Un-refcounted some types, small API adjustments
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   175
			flib_weaponsetlist_destroy(tmpList);
7177
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
   176
		}
7227
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   177
		flib_ini_destroy(ini);
7177
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
   178
	}
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
   179
	return result;
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
   180
}
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
   181
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   182
static bool needsEscape(char c) {
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   183
	return !((c>='0' && c<='9') || (c>='a' && c <='z'));
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   184
}
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   185
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   186
static int writeWeaponsetToIni(flib_ini *ini, flib_weaponset *set) {
7177
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
   187
	int result = -1;
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   188
	char weaponstring[WEAPONS_COUNT*4+1];
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   189
	strcpy(weaponstring, set->loadout);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   190
	strcat(weaponstring, set->crateprob);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   191
	strcat(weaponstring, set->delay);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   192
	strcat(weaponstring, set->crateammo);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   193
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   194
	char *escapedname = flib_urlencode_pred(set->name, needsEscape);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   195
	if(escapedname) {
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   196
		result = flib_ini_set_str(ini, escapedname, weaponstring);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   197
	}
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   198
	free(escapedname);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   199
	return result;
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   200
}
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   201
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   202
int flib_weaponsetlist_to_ini(const char *filename, const flib_weaponsetlist *list) {
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   203
	int result = -1;
7316
f7b49b2c5d84 frontlib: Improved and unified parameter checking, moved the cmdlineClient out
Medo <smaxein@googlemail.com>
parents: 7314
diff changeset
   204
	if(!log_badargs_if2(filename==NULL, list==NULL)) {
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   205
		flib_ini *ini = flib_ini_create(NULL);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   206
		if(ini && !flib_ini_create_section(ini, "General")) {
7177
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
   207
			bool error = false;
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   208
			for(int i=0; i<list->weaponsetCount && !error; i++) {
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   209
				error |= writeWeaponsetToIni(ini, list->weaponsets[i]);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   210
			}
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   211
7177
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
   212
			if(!error) {
7227
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   213
				result = flib_ini_save(ini, filename);
7177
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
   214
			}
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
   215
		}
7227
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   216
		flib_ini_destroy(ini);
7177
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
   217
	}
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
   218
	return result;
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff changeset
   219
}
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   220
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   221
flib_weaponsetlist *flib_weaponsetlist_create() {
7320
e704706008d4 frontlib: Renamed cfg to scheme, Un-refcounted some types, small API adjustments
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   222
	return flib_calloc(1, sizeof(flib_weaponsetlist));
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   223
}
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   224
7275
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   225
GENERATE_STATIC_LIST_INSERT(insertWeaponset, flib_weaponset*)
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   226
GENERATE_STATIC_LIST_DELETE(deleteWeaponset, flib_weaponset*)
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   227
7269
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7230
diff changeset
   228
int flib_weaponsetlist_insert(flib_weaponsetlist *list, flib_weaponset *set, int pos) {
7316
f7b49b2c5d84 frontlib: Improved and unified parameter checking, moved the cmdlineClient out
Medo <smaxein@googlemail.com>
parents: 7314
diff changeset
   229
	if(!log_badargs_if2(list==NULL, set==NULL)
f7b49b2c5d84 frontlib: Improved and unified parameter checking, moved the cmdlineClient out
Medo <smaxein@googlemail.com>
parents: 7314
diff changeset
   230
			&& !insertWeaponset(&list->weaponsets, &list->weaponsetCount, set, pos)) {
7269
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7230
diff changeset
   231
		flib_weaponset_retain(set);
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7230
diff changeset
   232
		return 0;
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   233
	}
7275
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   234
	return -1;
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   235
}
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   236
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   237
int flib_weaponsetlist_delete(flib_weaponsetlist *list, int pos) {
7316
f7b49b2c5d84 frontlib: Improved and unified parameter checking, moved the cmdlineClient out
Medo <smaxein@googlemail.com>
parents: 7314
diff changeset
   238
	if(!log_badargs_if(list==NULL)) {
7269
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7230
diff changeset
   239
		flib_weaponset *elem = list->weaponsets[pos];
7275
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   240
		if(!deleteWeaponset(&list->weaponsets, &list->weaponsetCount, pos)) {
7269
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7230
diff changeset
   241
			flib_weaponset_release(elem);
7275
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   242
			return 0;
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   243
		}
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   244
	}
7275
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   245
	return -1;
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   246
}