4
Bygfoot Football Manager -- a small and simple GTK2-based
5
football management game.
7
http://bygfoot.sourceforge.net
9
Copyright (C) 2005 Gyözö Both (gyboth@bygfoot.com)
11
This program is free software; you can redistribute it and/or
12
modify it under the terms of the GNU General Public License
13
as published by the Free Software Foundation; either version 2
14
of the License, or (at your option) any later version.
16
This program is distributed in the hope that it will be useful,
17
but WITHOUT ANY WARRANTY; without even the implied warranty of
18
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
GNU General Public License for more details.
21
You should have received a copy of the GNU General Public License
22
along with this program; if not, write to the Free Software
23
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30
#include "strategy_struct.h"
31
#include "xml_strategy.h"
33
#define TAG_STRATEGY "strategy"
34
#define TAG_STRATEGY_SID "sid"
35
#define TAG_STRATEGY_DESC "desc"
36
#define TAG_STRATEGY_PRIORITY "priority"
38
#define TAG_STRATEGY_PREMATCH "prematch"
39
#define TAG_STRATEGY_PREMATCH_FORMATION "formation"
40
#define TAG_STRATEGY_PREMATCH_LINEUP "lineup"
41
#define TAG_STRATEGY_PREMATCH_BOOST "prematch_boost"
42
#define TAG_STRATEGY_PREMATCH_STYLE "prematch_style"
44
#define TAG_STRATEGY_MATCH_ACTION "match_action"
45
#define TAG_STRATEGY_MATCH_ACTION_BOOST "boost"
46
#define TAG_STRATEGY_MATCH_ACTION_STYLE "style"
47
#define TAG_STRATEGY_MATCH_ACTION_SUB "sub"
48
#define TAG_STRATEGY_MATCH_ACTION_SUB_IN_POS "in"
49
#define TAG_STRATEGY_MATCH_ACTION_SUB_OUT_POS "out"
51
#define ATT_NAME_MIN_FITNESS "min_fitness"
52
#define ATT_NAME_COND "cond"
53
#define ATT_NAME_SUB_PROPERTY "prop"
55
#define LINEUP_NAME_BEST "best"
56
#define LINEUP_NAME_WEAKEST "weakest"
57
#define LINEUP_NAME_FITTEST "fittest"
58
#define LINEUP_NAME_UNFITTEST "unfittest"
60
#define BOOST_NAME_ON "on"
61
#define BOOST_NAME_OFF "off"
62
#define BOOST_NAME_ANTI "anti"
64
#define STYLE_NAME_ALL_OUT_DEFEND "all-out-defend"
65
#define STYLE_NAME_DEFEND "defend"
66
#define STYLE_NAME_BALANCED "balanced"
67
#define STYLE_NAME_ATTACK "attack"
68
#define STYLE_NAME_ALL_OUT_ATTACK "all-out-attack"
70
#define POS_NAME_GOALIE "goalie"
71
#define POS_NAME_DEFENDER "defender"
72
#define POS_NAME_MIDFIELDER "midfielder"
73
#define POS_NAME_FORWARD "forward"
80
STATE_STRATEGY_PRIORITY,
81
STATE_STRATEGY_PREMATCH,
82
STATE_STRATEGY_PREMATCH_FORMATION,
83
STATE_STRATEGY_PREMATCH_LINEUP,
84
STATE_STRATEGY_PREMATCH_BOOST,
85
STATE_STRATEGY_PREMATCH_STYLE,
86
STATE_STRATEGY_MATCH_ACTION,
87
STATE_STRATEGY_MATCH_ACTION_BOOST,
88
STATE_STRATEGY_MATCH_ACTION_STYLE,
89
STATE_STRATEGY_MATCH_ACTION_SUB,
90
STATE_STRATEGY_MATCH_ACTION_SUB_IN_POS,
91
STATE_STRATEGY_MATCH_ACTION_SUB_OUT_POS,
94
gint state, action_id;
96
#define curstrat g_array_index(strategies, Strategy, strategies->len - 1)
97
#define curprematch g_array_index(curstrat.prematch, StrategyPrematch, curstrat.prematch->len - 1)
98
#define curmatchaction g_array_index(curstrat.match_action, StrategyMatchAction, curstrat.match_action->len - 1)
101
xml_strategy_read_start_element (GMarkupParseContext *context,
102
const gchar *element_name,
103
const gchar **attribute_names,
104
const gchar **attribute_values,
110
if(strcmp(element_name, TAG_STRATEGY) == 0)
111
state = STATE_STRATEGY;
112
else if(strcmp(element_name, TAG_STRATEGY_SID) == 0)
113
state = STATE_STRATEGY_SID;
114
else if(strcmp(element_name, TAG_STRATEGY_DESC) == 0)
115
state = STATE_STRATEGY_DESC;
116
else if(strcmp(element_name, TAG_STRATEGY_PRIORITY) == 0)
117
state = STATE_STRATEGY_PRIORITY;
118
else if(strcmp(element_name, TAG_STRATEGY_PREMATCH) == 0)
120
StrategyPrematch new_prematch;
122
state = STATE_STRATEGY_PREMATCH;
124
new_prematch.condition = NULL;
125
new_prematch.lineup = new_prematch.boost =
126
new_prematch.style = -100;
127
new_prematch.min_fitness = 0;
128
new_prematch.formations = g_array_new(FALSE, FALSE, sizeof(gint));
130
while(attribute_names[atidx] != NULL)
132
if(strcmp(attribute_names[atidx], ATT_NAME_COND) == 0 &&
133
new_prematch.condition == NULL)
134
new_prematch.condition =
135
g_strdup(attribute_values[atidx]);
137
g_warning("xml_strategy_read_start_element: unknown attribute %s\n",
138
attribute_names[atidx]);
143
g_array_append_val(curstrat.prematch, new_prematch);
145
else if(strcmp(element_name, TAG_STRATEGY_PREMATCH_FORMATION) == 0)
146
state = STATE_STRATEGY_PREMATCH_FORMATION;
147
else if(strcmp(element_name, TAG_STRATEGY_PREMATCH_LINEUP) == 0)
149
state = STATE_STRATEGY_PREMATCH_LINEUP;
151
while(attribute_names[atidx] != NULL)
153
if(strcmp(attribute_names[atidx], ATT_NAME_MIN_FITNESS) == 0)
154
curprematch.min_fitness =
155
g_ascii_strtod(attribute_values[atidx], NULL) / 100;
157
g_warning("xml_strategy_read_start_element: unknown attribute %s\n",
158
attribute_names[atidx]);
163
else if(strcmp(element_name, TAG_STRATEGY_PREMATCH_BOOST) == 0)
164
state = STATE_STRATEGY_PREMATCH_BOOST;
165
else if(strcmp(element_name, TAG_STRATEGY_PREMATCH_STYLE) == 0)
166
state = STATE_STRATEGY_PREMATCH_STYLE;
167
else if(strcmp(element_name, TAG_STRATEGY_MATCH_ACTION) == 0)
169
StrategyMatchAction new_match_action;
171
state = STATE_STRATEGY_MATCH_ACTION;
172
new_match_action.sub_condition = NULL;
174
new_match_action.condition = NULL;
175
new_match_action.boost =
176
new_match_action.style = -100;
177
new_match_action.sub_in_pos = -1;
178
new_match_action.id = action_id++;
180
while(attribute_names[atidx] != NULL)
182
if(strcmp(attribute_names[atidx], ATT_NAME_COND) == 0 &&
183
new_match_action.condition == NULL)
184
new_match_action.condition =
185
g_strdup(attribute_values[atidx]);
187
g_warning("xml_strategy_read_start_element: unknown attribute %s\n",
188
attribute_names[atidx]);
193
g_array_append_val(curstrat.match_action, new_match_action);
195
else if(strcmp(element_name, TAG_STRATEGY_MATCH_ACTION_BOOST) == 0)
196
state = STATE_STRATEGY_MATCH_ACTION_BOOST;
197
else if(strcmp(element_name, TAG_STRATEGY_MATCH_ACTION_STYLE) == 0)
198
state = STATE_STRATEGY_MATCH_ACTION_STYLE;
199
else if(strcmp(element_name, TAG_STRATEGY_MATCH_ACTION_SUB) == 0)
201
state = STATE_STRATEGY_MATCH_ACTION_SUB;
203
while(attribute_names[atidx] != NULL)
205
if(strcmp(attribute_names[atidx], ATT_NAME_COND) == 0 &&
206
curmatchaction.sub_condition == NULL)
207
curmatchaction.sub_condition =
208
g_strdup(attribute_values[atidx]);
210
g_warning("xml_strategy_read_start_element: unknown attribute %s\n",
211
attribute_names[atidx]);
216
else if(strcmp(element_name, TAG_STRATEGY_MATCH_ACTION_SUB_IN_POS) == 0)
218
state = STATE_STRATEGY_MATCH_ACTION_SUB_IN_POS;
220
while(attribute_names[atidx] != NULL)
222
if(strcmp(attribute_names[atidx], ATT_NAME_SUB_PROPERTY) == 0)
224
if(strcmp(attribute_values[atidx], LINEUP_NAME_BEST) == 0)
225
curmatchaction.sub_in_prop = STRAT_LINEUP_BEST;
226
else if(strcmp(attribute_values[atidx], LINEUP_NAME_WEAKEST) == 0)
227
curmatchaction.sub_in_prop = STRAT_LINEUP_WEAKEST;
228
else if(strcmp(attribute_values[atidx], LINEUP_NAME_FITTEST) == 0)
229
curmatchaction.sub_in_prop = STRAT_LINEUP_FITTEST;
230
else if(strcmp(attribute_values[atidx], LINEUP_NAME_UNFITTEST) == 0)
231
curmatchaction.sub_in_prop = STRAT_LINEUP_UNFITTEST;
233
g_warning("xml_strategy_read_start_element: unknown property value %s\n",
234
attribute_values[atidx]);
237
g_warning("xml_strategy_read_start_element: unknown attribute %s\n",
238
attribute_names[atidx]);
243
else if(strcmp(element_name, TAG_STRATEGY_MATCH_ACTION_SUB_OUT_POS) == 0)
245
state = STATE_STRATEGY_MATCH_ACTION_SUB_OUT_POS;
247
while(attribute_names[atidx] != NULL)
249
if(strcmp(attribute_names[atidx], ATT_NAME_SUB_PROPERTY) == 0)
251
if(strcmp(attribute_values[atidx], LINEUP_NAME_BEST) == 0)
252
curmatchaction.sub_out_prop = STRAT_LINEUP_BEST;
253
else if(strcmp(attribute_values[atidx], LINEUP_NAME_WEAKEST) == 0)
254
curmatchaction.sub_out_prop = STRAT_LINEUP_WEAKEST;
255
else if(strcmp(attribute_values[atidx], LINEUP_NAME_FITTEST) == 0)
256
curmatchaction.sub_out_prop = STRAT_LINEUP_FITTEST;
257
else if(strcmp(attribute_values[atidx], LINEUP_NAME_UNFITTEST) == 0)
258
curmatchaction.sub_out_prop = STRAT_LINEUP_UNFITTEST;
260
g_warning("xml_strategy_read_start_element: unknown property value %s\n",
261
attribute_values[atidx]);
264
g_warning("xml_strategy_read_start_element: unknown attribute %s\n",
265
attribute_names[atidx]);
271
g_warning("xml_strategy_read_start_element: unknown tag: %s; I'm in state %d\n",
272
element_name, state);
276
* The function called by the parser when a closing tag is read.
277
* The state variable is changed in this function.
278
* @see The GLib manual (Simple XML parser).
281
xml_strategy_read_end_element (GMarkupParseContext *context,
282
const gchar *element_name,
286
if(strcmp(element_name, TAG_STRATEGY_SID) == 0 ||
287
strcmp(element_name, TAG_STRATEGY_DESC) == 0 ||
288
strcmp(element_name, TAG_STRATEGY_PRIORITY) == 0 ||
289
strcmp(element_name, TAG_STRATEGY_MATCH_ACTION) == 0 ||
290
strcmp(element_name, TAG_STRATEGY_PREMATCH) == 0)
291
state = STATE_STRATEGY;
292
else if(strcmp(element_name, TAG_STRATEGY_PREMATCH_FORMATION) == 0 ||
293
strcmp(element_name, TAG_STRATEGY_PREMATCH_LINEUP) == 0 ||
294
strcmp(element_name, TAG_STRATEGY_PREMATCH_BOOST) == 0 ||
295
strcmp(element_name, TAG_STRATEGY_PREMATCH_STYLE) == 0)
296
state = STATE_STRATEGY_PREMATCH;
297
else if(strcmp(element_name, TAG_STRATEGY_MATCH_ACTION_SUB) == 0 ||
298
strcmp(element_name, TAG_STRATEGY_MATCH_ACTION_STYLE) == 0 ||
299
strcmp(element_name, TAG_STRATEGY_MATCH_ACTION_BOOST) == 0)
300
state = STATE_STRATEGY_MATCH_ACTION;
301
else if(strcmp(element_name, TAG_STRATEGY_MATCH_ACTION_SUB_IN_POS) == 0 ||
302
strcmp(element_name, TAG_STRATEGY_MATCH_ACTION_SUB_OUT_POS) == 0)
303
state = STATE_STRATEGY_MATCH_ACTION_SUB;
304
else if(strcmp(element_name, TAG_STRATEGY) != 0)
305
g_warning("xml_strategy_read_end_element: unknown tag: %s; I'm in state %d\n",
306
element_name, state);
310
* The function called by the parser when the text between tags is read.
311
* This function is responsible for filling in the variables (e.g. team names)
312
* when a file gets loaded.
313
* @see The GLib manual (Simple XML parser).
316
xml_strategy_read_text (GMarkupParseContext *context,
322
gchar buf[text_len + 1];
325
strncpy(buf, text, text_len);
326
buf[text_len] = '\0';
328
int_value = (gint)g_ascii_strtod(buf, NULL);
330
if(state == STATE_STRATEGY_SID)
331
misc_string_assign(&curstrat.sid, buf);
332
else if(state == STATE_STRATEGY_DESC)
333
misc_string_assign(&curstrat.desc, buf);
334
else if(state == STATE_STRATEGY_PRIORITY)
336
if(strategies->len == 1)
337
curstrat.priority = int_value;
340
g_array_index(strategies, Strategy, strategies->len - 2).priority +
343
else if(state == STATE_STRATEGY_PREMATCH_FORMATION)
344
g_array_append_val(curprematch.formations, int_value);
345
else if(state == STATE_STRATEGY_PREMATCH_LINEUP)
347
if(strcmp(buf, LINEUP_NAME_BEST) == 0)
348
curprematch.lineup = STRAT_LINEUP_BEST;
349
else if(strcmp(buf, LINEUP_NAME_WEAKEST) == 0)
350
curprematch.lineup = STRAT_LINEUP_WEAKEST;
351
else if(strcmp(buf, LINEUP_NAME_FITTEST) == 0)
352
curprematch.lineup = STRAT_LINEUP_FITTEST;
355
"xml_strategy_read_text: unknown lineup type %s\n", buf);
357
else if(state == STATE_STRATEGY_PREMATCH_BOOST)
359
if(strcmp(buf, BOOST_NAME_ON) == 0)
360
curprematch.boost = 1;
361
else if(strcmp(buf, BOOST_NAME_OFF) == 0)
362
curprematch.boost = 0;
363
else if(strcmp(buf, BOOST_NAME_ANTI) == 0)
364
curprematch.boost = -1;
367
"xml_strategy_read_text: unknown boost type %s\n", buf);
369
else if(state == STATE_STRATEGY_PREMATCH_STYLE)
371
if(strcmp(buf, STYLE_NAME_ALL_OUT_DEFEND) == 0)
372
curprematch.style = -2;
373
else if(strcmp(buf, STYLE_NAME_DEFEND) == 0)
374
curprematch.style = -1;
375
else if(strcmp(buf, STYLE_NAME_BALANCED) == 0)
376
curprematch.style = 0;
377
else if(strcmp(buf, STYLE_NAME_ATTACK) == 0)
378
curprematch.style = 1;
379
else if(strcmp(buf, STYLE_NAME_ALL_OUT_ATTACK) == 0)
380
curprematch.style = 2;
383
"xml_strategy_read_text: unknown style type %s\n", buf);
385
else if(state == STATE_STRATEGY_MATCH_ACTION_STYLE)
387
if(strcmp(buf, STYLE_NAME_ALL_OUT_DEFEND) == 0)
388
curmatchaction.style = -2;
389
else if(strcmp(buf, STYLE_NAME_DEFEND) == 0)
390
curmatchaction.style = -1;
391
else if(strcmp(buf, STYLE_NAME_BALANCED) == 0)
392
curmatchaction.style = 0;
393
else if(strcmp(buf, STYLE_NAME_ATTACK) == 0)
394
curmatchaction.style = 1;
395
else if(strcmp(buf, STYLE_NAME_ALL_OUT_ATTACK) == 0)
396
curmatchaction.style = 2;
399
"xml_strategy_read_text: unknown style type %s\n", buf);
401
else if(state == STATE_STRATEGY_MATCH_ACTION_BOOST)
403
if(strcmp(buf, BOOST_NAME_ON) == 0)
404
curmatchaction.boost = 1;
405
else if(strcmp(buf, BOOST_NAME_OFF) == 0)
406
curmatchaction.boost = 0;
407
else if(strcmp(buf, BOOST_NAME_ANTI) == 0)
408
curmatchaction.boost = -1;
411
"xml_strategy_read_text: unknown boost type %s\n", buf);
413
else if(state == STATE_STRATEGY_MATCH_ACTION_SUB_IN_POS ||
414
state == STATE_STRATEGY_MATCH_ACTION_SUB_OUT_POS)
416
GPtrArray *positions = misc_separate_strings(buf);
419
if(state == STATE_STRATEGY_MATCH_ACTION_SUB_IN_POS)
421
if(positions->len > 1)
423
free_gchar_array(&positions);
424
main_exit_program(EXIT_STRATEGY_ERROR,
425
"xml_strategy_read_text: too many sub_in positions: %s\n",
429
pos = &curmatchaction.sub_in_pos;
432
pos = &curmatchaction.sub_out_pos;
436
for(i=0;i<positions->len;i++)
437
if(strcmp((gchar*)g_ptr_array_index(positions, i),
438
POS_NAME_GOALIE) == 0)
440
else if(strcmp((gchar*)g_ptr_array_index(positions, i),
441
POS_NAME_DEFENDER) == 0)
442
*pos = (*pos * 10) + 1;
443
else if(strcmp((gchar*)g_ptr_array_index(positions, i),
444
POS_NAME_MIDFIELDER) == 0)
445
*pos = (*pos * 10) + 2;
446
else if(strcmp((gchar*)g_ptr_array_index(positions, i),
447
POS_NAME_FORWARD) == 0)
448
*pos = (*pos * 10) + 3;
451
"xml_strategy_read_text: unknown position %s\n",
452
(gchar*)g_ptr_array_index(positions, i));
457
free_gchar_array(&positions);
461
/** Add the strategy described in the file
462
to the strategies array. */
464
xml_strategy_read(const gchar *filename)
468
GMarkupParser parser = {xml_strategy_read_start_element,
469
xml_strategy_read_end_element,
470
xml_strategy_read_text, NULL, NULL};
471
GMarkupParseContext *context;
472
gchar *file_contents;
474
GError *error = NULL;
477
g_markup_parse_context_new(&parser, 0, NULL, NULL);
479
if(!g_file_get_contents(filename, &file_contents, &length, &error))
481
g_warning("xml_strategy_read: error reading file %s\n", filename);
482
misc_print_error(&error, TRUE);
486
new_strat.sid = new_strat.desc = NULL;
487
new_strat.priority = 1;
488
new_strat.prematch = g_array_new(FALSE, FALSE, sizeof(StrategyPrematch));
489
new_strat.match_action = g_array_new(FALSE, FALSE, sizeof(StrategyMatchAction));
491
g_array_append_val(strategies, new_strat);
495
if(g_markup_parse_context_parse(context, file_contents, length, &error))
497
g_markup_parse_context_end_parse(context, NULL);
498
g_markup_parse_context_free(context);
499
g_free(file_contents);
503
g_critical("xml_strategy_read: error parsing file %s\n", filename);
504
misc_print_error(&error, TRUE);
508
for(i=0;i<curstrat.match_action->len;i++)
510
if(g_array_index(curstrat.match_action, StrategyMatchAction, i).condition == NULL)
511
main_exit_program(EXIT_STRATEGY_ERROR, "xml_strategy_read: unconditional match action encountered in file %s. match actions MUST have conditions.", filename);
515
/** Load all strategy files found in the appropriate folder. */
517
xml_strategy_load_strategies(void)
520
const gchar *strategydir = file_get_first_support_dir_suffix("strategy");
521
GPtrArray *files = NULL;
524
if(strategydir == NULL)
525
main_exit_program(EXIT_STRATEGY_ERROR,
526
"xml_strategy_load_strategies: strategy directory not found.");
528
files = file_dir_get_contents(strategydir, "strategy_", ".xml");
532
g_ptr_array_free(files, TRUE);
533
main_exit_program(EXIT_STRATEGY_ERROR,
534
"xml_strategy_load_strategies: no CPU strategies found.");
537
for(i=0;i<files->len;i++)
539
sprintf(buf, "%s%s%s", strategydir, G_DIR_SEPARATOR_S,
540
(const gchar*)g_ptr_array_index(files, i));
541
xml_strategy_read(buf);
544
free_gchar_array(&files);
546
/* Fill secondary prematches with default values. */
547
for(i=0;i<strategies->len;i++)
549
StrategyPrematch *prem0 = &g_array_index(
550
g_array_index(strategies, Strategy, i).prematch,
551
StrategyPrematch, 0),
554
if(prem0->formations->len == 0)
555
main_exit_program(EXIT_STRATEGY_ERROR,
556
"xml_strategy_read (%s): The primary prematch should have at least one formation.",
557
g_array_index(strategies, Strategy, i).sid);
559
for(j=1;j<g_array_index(strategies, Strategy, i).prematch->len;j++)
561
premcur = &g_array_index(
562
g_array_index(strategies, Strategy, i).prematch,
563
StrategyPrematch, j);
565
if(premcur->formations->len == 0)
566
for(k=0;k<prem0->formations->len;k++)
567
g_array_append_val(premcur->formations,
568
g_array_index(prem0->formations, gint, k));
571
if(premcur->boost == -100)
572
premcur->boost = prem0->boost;
573
if(premcur->style == -100)
574
premcur->style = prem0->style;
575
if(premcur->lineup == -100)
576
premcur->lineup = prem0->lineup;
577
if(premcur->min_fitness == -100)
578
premcur->min_fitness = prem0->min_fitness;