bygfoot/src/lg_commentary.c

750 lines
24 KiB
C
Raw Normal View History

2005-10-20 17:45:00 +02:00
/*
2005-11-26 17:52:51 +01:00
lg_commentary.c
2005-10-20 17:45:00 +02:00
Bygfoot Football Manager -- a small and simple GTK2-based
football management game.
http://bygfoot.sourceforge.net
Copyright (C) 2005 Gyözö Both (gyboth@bygfoot.com)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
2005-06-26 13:42:01 +02:00
#include <locale.h>
2005-07-01 22:51:46 +02:00
#include "cup.h"
2005-06-26 13:42:01 +02:00
#include "file.h"
2005-07-01 22:51:46 +02:00
#include "fixture.h"
2005-10-09 11:35:44 +02:00
#include "free.h"
2006-02-28 23:17:08 +01:00
#include "language.h"
2005-07-01 22:51:46 +02:00
#include "league.h"
2005-05-18 18:02:14 +02:00
#include "lg_commentary.h"
#include "live_game.h"
2005-06-17 14:57:05 +02:00
#include "main.h"
2005-07-01 22:51:46 +02:00
#include "maths.h"
2005-05-18 18:02:14 +02:00
#include "misc.h"
#include "option.h"
#include "player.h"
2005-07-01 22:51:46 +02:00
#include "team.h"
2005-05-18 18:02:14 +02:00
#include "variables.h"
2005-06-26 13:42:01 +02:00
#include "xml_lg_commentary.h"
2005-05-18 18:02:14 +02:00
2005-07-01 22:51:46 +02:00
/** The replacement strings for the live game commentary tokens. */
2005-10-24 22:50:48 +02:00
GPtrArray *token_rep[2];
2005-07-05 20:37:26 +02:00
/** Whether there was a commentary repetition rejection. */
gboolean repetition;
2005-07-01 22:51:46 +02:00
2005-05-18 18:02:14 +02:00
/** Generate commentary for the live game event in the unit.
2005-10-25 17:26:53 +02:00
@param live_game The live game being calculated.
@param unit The live game unit we generate the commentary for.
@param event_type The event type we generate the commentary for (needed
when commentary is being tested). */
2005-05-18 18:02:14 +02:00
void
2005-10-25 17:26:53 +02:00
lg_commentary_generate(const LiveGame *live_game, LiveGameUnit *unit,
const gchar *event_name, gint ev_type)
2005-05-18 18:02:14 +02:00
{
#ifdef DEBUG
printf("lg_commentary_generate\n");
#endif
2005-10-25 17:26:53 +02:00
gint i, event_type = -1;
gint commentary_idx = -1;
2005-05-18 18:02:14 +02:00
gchar buf[SMALL];
2005-10-25 17:26:53 +02:00
GArray *commentaries = NULL;
if(unit != NULL)
event_type = unit->event.type;
else
event_type = (event_name == NULL) ? ev_type :
xml_lg_commentary_event_name_to_int(event_name);
if(event_type == -1)
return;
if(live_game != NULL)
{
lg_commentary_set_basic_tokens(unit, live_game->fix);
lg_commentary_set_team_tokens(unit, live_game->fix);
lg_commentary_set_player_tokens(unit, live_game->fix);
2008-11-28 19:10:01 +01:00
lg_commentary_set_stats_tokens(&live_game->stats, token_rep);
2005-10-25 17:26:53 +02:00
}
2005-10-31 17:19:14 +01:00
2005-10-25 17:26:53 +02:00
if(event_type == LIVE_GAME_EVENT_STYLE_CHANGE_ALL_OUT_DEFEND ||
event_type == LIVE_GAME_EVENT_STYLE_CHANGE_DEFEND ||
event_type == LIVE_GAME_EVENT_STYLE_CHANGE_BALANCED ||
event_type == LIVE_GAME_EVENT_STYLE_CHANGE_ATTACK ||
event_type == LIVE_GAME_EVENT_STYLE_CHANGE_ALL_OUT_ATTACK)
commentaries = lg_commentary[LIVE_GAME_EVENT_STYLE_CHANGE_ALL_OUT_DEFEND];
else if(event_type == LIVE_GAME_EVENT_BOOST_CHANGE_ANTI ||
event_type == LIVE_GAME_EVENT_BOOST_CHANGE_OFF ||
event_type == LIVE_GAME_EVENT_BOOST_CHANGE_ON)
commentaries = lg_commentary[LIVE_GAME_EVENT_BOOST_CHANGE_ANTI];
else
commentaries = lg_commentary[event_type];
commentary_idx = lg_commentary_select(commentaries, buf);
if(commentary_idx == -1)
2009-04-29 19:18:54 +02:00
debug_print_message("lg_commentary_generate: didn't find fitting commentary for unit type %d \n",
2005-10-25 17:31:17 +02:00
event_type);
2005-05-18 18:02:14 +02:00
2005-10-25 17:26:53 +02:00
if(live_game != NULL)
{
unit->event.commentary = g_strdup(buf);
unit->event.commentary_id = (commentary_idx == -1) ?
-1 : g_array_index(commentaries, LGCommentary, commentary_idx).id;
for(i=0;i<tokens.list->len;i++)
if(i != option_int("string_token_team_home", &tokens) &&
i != option_int("string_token_team_away", &tokens) &&
i != option_int("string_token_attendance", &tokens) &&
i != option_int("string_token_cup_round_name", &tokens) &&
i != option_int("string_token_league_cup_name", &tokens) &&
i != option_int("string_token_yellow_limit", &tokens) &&
i != option_int("string_token_team_layer0", &tokens) &&
i != option_int("string_token_team_layer1", &tokens))
misc_token_remove(token_rep, i);
}
2005-06-26 13:42:01 +02:00
else
2005-10-25 17:52:44 +02:00
g_print("%s: \"%s\"\n", event_name, buf);
2005-10-25 17:26:53 +02:00
}
2005-05-18 18:02:14 +02:00
2005-10-25 17:26:53 +02:00
/** Select a commentary from the array depending on the tokens
available and write the text into the buffer. */
gint
lg_commentary_select(const GArray *commentaries, gchar *buf)
{
#ifdef DEBUG
printf("lg_commentary_select\n");
#endif
2005-10-25 17:26:53 +02:00
gint i, order[commentaries->len];
lg_commentary_get_order(commentaries, order);
2005-05-18 18:02:14 +02:00
2005-07-05 20:37:26 +02:00
repetition = FALSE;
2005-10-25 17:26:53 +02:00
for(i=0;i<commentaries->len;i++)
if(lg_commentary_check_commentary(&g_array_index(commentaries, LGCommentary, order[i]), buf))
2005-05-18 18:02:14 +02:00
break;
2005-07-05 20:37:26 +02:00
2005-10-25 17:26:53 +02:00
if(i == commentaries->len)
2005-07-05 20:37:26 +02:00
{
repetition = TRUE;
2005-10-25 17:26:53 +02:00
for(i=0;i<commentaries->len;i++)
if(lg_commentary_check_commentary(&g_array_index(commentaries, LGCommentary, order[i]), buf))
2005-07-05 20:37:26 +02:00
break;
2005-05-18 18:02:14 +02:00
}
2005-10-25 17:26:53 +02:00
if(i == commentaries->len)
2005-05-18 18:02:14 +02:00
{
strcpy(buf, "FIXME!");
2005-10-25 17:26:53 +02:00
return -1;
2005-05-18 18:02:14 +02:00
}
2005-10-25 17:26:53 +02:00
return order[i];
2005-07-01 22:51:46 +02:00
}
2005-06-28 04:08:56 +02:00
2005-07-01 22:51:46 +02:00
/** Check whether the commentary conditions are fulfilled and whether
we can replace all tokens in the commentary. */
gboolean
lg_commentary_check_commentary(const LGCommentary *commentary, gchar *dest)
{
#ifdef DEBUG
printf("lg_commentary_check_commentary\n");
#endif
2005-10-09 11:35:44 +02:00
if(strlen(commentary->text) == 0 ||
2005-07-08 11:26:00 +02:00
(commentary->condition != NULL &&
2005-10-24 22:50:48 +02:00
!misc_parse_condition(commentary->condition, token_rep)) ||
2005-07-05 20:37:26 +02:00
(repetition == FALSE && query_lg_commentary_is_repetition(commentary->id)))
2005-07-01 22:51:46 +02:00
return FALSE;
2008-11-28 15:09:08 +01:00
return misc_string_replace_all_tokens(token_rep, commentary->text, dest);
2005-07-01 22:51:46 +02:00
}
2005-07-05 20:37:26 +02:00
/** Check whether a commentary with given id has been used in the last
few commentaries. Return TRUE if so and FALSE otherwise. */
gboolean
query_lg_commentary_is_repetition(gint id)
{
#ifdef DEBUG
printf("query_lg_commentary_is_repetition\n");
#endif
2005-07-05 20:37:26 +02:00
const GArray *units = ((LiveGame*)statp)->units;
gint units_min = units->len - const_int("int_lg_commentary_check_backwards") - 1;
gint i, min = MAX(units_min, 0);
for(i=units->len - 2; i>= min; i--)
if(g_array_index(units, LiveGameUnit, i).event.commentary_id == id)
return TRUE;
return FALSE;
}
2005-07-01 22:51:46 +02:00
/** Write a random order of indices into the integer array
(only depending on the priority values of the commentaries).
I don't like this implementation of ordering the commentaries
according to their priority :-P can't think of a better one, though. */
void
lg_commentary_get_order(const GArray *commentaries, gint *order)
{
#ifdef DEBUG
printf("lg_commentary_get_order\n");
#endif
2005-07-01 22:51:46 +02:00
gint i, j, order_idx = 0;
gint priority_sum = 0, bounds[commentaries->len + 1];
bounds[0] = 0;
for(i=0;i<commentaries->len;i++)
{
priority_sum += g_array_index(commentaries, LGCommentary, i).priority;
bounds[i + 1] = priority_sum;
order[i] = -1;
}
gint permutation[priority_sum];
math_generate_permutation(permutation, 1, priority_sum);
2005-06-28 04:08:56 +02:00
2005-07-01 22:51:46 +02:00
for(i=0;i<priority_sum;i++)
2005-06-28 04:08:56 +02:00
{
2005-07-01 22:51:46 +02:00
if(order_idx == commentaries->len)
break;
for(j=0;j<commentaries->len;j++)
if(bounds[j] < permutation[i] && permutation[i] <= bounds[j + 1])
{
2005-10-13 10:52:33 +02:00
if(!query_integer_is_in_array(j, order, commentaries->len))
2005-07-01 22:51:46 +02:00
{
order[order_idx] = j;
order_idx++;
}
2005-06-28 04:08:56 +02:00
2005-07-01 22:51:46 +02:00
break;
}
2005-06-28 04:08:56 +02:00
}
2005-07-01 22:51:46 +02:00
}
2005-06-28 04:08:56 +02:00
2005-07-01 22:51:46 +02:00
/** Fill the stats tokens. */
void
2008-11-28 19:10:01 +01:00
lg_commentary_set_stats_tokens(const LiveGameStats *stats, GPtrArray **token_arrays)
2005-07-01 22:51:46 +02:00
{
#ifdef DEBUG
printf("lg_commentary_set_stats_tokens\n");
#endif
2008-11-28 19:10:01 +01:00
misc_token_add(token_arrays,
2005-10-24 22:50:48 +02:00
option_int("string_token_stat_shots0", &tokens),
misc_int_to_char(stats->values[0][LIVE_GAME_STAT_VALUE_SHOTS]));
2008-11-28 19:10:01 +01:00
misc_token_add(token_arrays,
2005-10-24 22:50:48 +02:00
option_int("string_token_stat_shot_per0", &tokens),
misc_int_to_char(stats->values[0][LIVE_GAME_STAT_VALUE_SHOT_PERCENTAGE]));
2008-11-28 19:10:01 +01:00
misc_token_add(token_arrays,
2005-10-24 22:50:48 +02:00
option_int("string_token_stat_poss0", &tokens),
misc_int_to_char((gint)rint(100 *
((gfloat)stats->values[0][LIVE_GAME_STAT_VALUE_POSSESSION] /
((gfloat)stats->values[0][LIVE_GAME_STAT_VALUE_POSSESSION] +
(gfloat)stats->values[1][LIVE_GAME_STAT_VALUE_POSSESSION])))));
2008-11-28 19:10:01 +01:00
misc_token_add(token_arrays,
2005-10-24 22:50:48 +02:00
option_int("string_token_stat_pen0", &tokens),
misc_int_to_char(stats->values[0][LIVE_GAME_STAT_VALUE_PENALTIES]));
2008-11-28 19:10:01 +01:00
misc_token_add(token_arrays,
2005-10-24 22:50:48 +02:00
option_int("string_token_stat_fouls0", &tokens),
misc_int_to_char(stats->values[0][LIVE_GAME_STAT_VALUE_FOULS]));
2008-11-28 19:10:01 +01:00
misc_token_add(token_arrays,
2005-10-24 22:50:48 +02:00
option_int("string_token_stat_yellows0", &tokens),
misc_int_to_char(stats->values[0][LIVE_GAME_STAT_VALUE_CARDS]));
2008-11-28 19:10:01 +01:00
misc_token_add(token_arrays,
2005-10-24 22:50:48 +02:00
option_int("string_token_stat_reds0", &tokens),
misc_int_to_char(stats->values[0][LIVE_GAME_STAT_VALUE_REDS]));
2008-11-28 19:10:01 +01:00
misc_token_add(token_arrays,
2005-10-24 22:50:48 +02:00
option_int("string_token_stat_injs0", &tokens),
misc_int_to_char(stats->values[0][LIVE_GAME_STAT_VALUE_INJURIES]));
2008-11-28 19:10:01 +01:00
misc_token_add(token_arrays,
2005-10-24 22:50:48 +02:00
option_int("string_token_stat_shots1", &tokens),
misc_int_to_char(stats->values[1][LIVE_GAME_STAT_VALUE_SHOTS]));
2008-11-28 19:10:01 +01:00
misc_token_add(token_arrays,
2005-10-24 22:50:48 +02:00
option_int("string_token_stat_shot_per1", &tokens),
misc_int_to_char(stats->values[1][LIVE_GAME_STAT_VALUE_SHOT_PERCENTAGE]));
2008-11-28 19:10:01 +01:00
misc_token_add(token_arrays,
2005-10-24 22:50:48 +02:00
option_int("string_token_stat_poss1", &tokens),
misc_int_to_char((gint)rint(100 * ((gfloat)stats->values[1][LIVE_GAME_STAT_VALUE_POSSESSION] /
((gfloat)stats->values[0][LIVE_GAME_STAT_VALUE_POSSESSION] +
(gfloat)stats->values[1][LIVE_GAME_STAT_VALUE_POSSESSION])))));
2008-11-28 19:10:01 +01:00
misc_token_add(token_arrays,
2005-10-24 22:50:48 +02:00
option_int("string_token_stat_pen1", &tokens),
misc_int_to_char(stats->values[1][LIVE_GAME_STAT_VALUE_PENALTIES]));
2008-11-28 19:10:01 +01:00
misc_token_add(token_arrays,
2005-10-24 22:50:48 +02:00
option_int("string_token_stat_fouls1", &tokens),
misc_int_to_char(stats->values[1][LIVE_GAME_STAT_VALUE_FOULS]));
2008-11-28 19:10:01 +01:00
misc_token_add(token_arrays,
2005-10-24 22:50:48 +02:00
option_int("string_token_stat_yellows1", &tokens),
misc_int_to_char(stats->values[1][LIVE_GAME_STAT_VALUE_CARDS]));
2008-11-28 19:10:01 +01:00
misc_token_add(token_arrays,
2005-10-24 22:50:48 +02:00
option_int("string_token_stat_reds1", &tokens),
misc_int_to_char(stats->values[1][LIVE_GAME_STAT_VALUE_REDS]));
2008-11-28 19:10:01 +01:00
misc_token_add(token_arrays,
2005-10-24 22:50:48 +02:00
option_int("string_token_stat_injs1", &tokens),
misc_int_to_char(stats->values[1][LIVE_GAME_STAT_VALUE_INJURIES]));
2005-05-18 18:02:14 +02:00
}
2005-07-01 22:51:46 +02:00
/** Fill the tokens that contain general information. */
2005-05-18 18:02:14 +02:00
void
2005-07-01 22:51:46 +02:00
lg_commentary_set_basic_tokens(const LiveGameUnit *unit, const Fixture *fix)
2005-05-18 18:02:14 +02:00
{
#ifdef DEBUG
printf("lg_commentary_set_basic_tokens\n");
#endif
2005-05-18 18:02:14 +02:00
gchar buf[SMALL];
2005-07-01 22:51:46 +02:00
gint tmp_int = 1, current_min = live_game_unit_get_minute(unit);
gint avskill0 = (gint)rint(team_get_average_skill(fix->teams[0], TRUE)),
avskill1 = (gint)rint(team_get_average_skill(fix->teams[1], TRUE));
sprintf(buf, "%d : %d", unit->result[0], unit->result[1]);
2005-10-24 22:50:48 +02:00
misc_token_add(token_rep,
option_int("string_token_result", &tokens),
g_strdup(buf));
2005-05-18 18:02:14 +02:00
2005-10-24 22:50:48 +02:00
misc_token_add(token_rep,
option_int("string_token_time", &tokens),
misc_int_to_char(unit->time));
misc_token_add(token_rep,
option_int("string_token_minute", &tokens),
misc_int_to_char(current_min));
2005-07-01 22:51:46 +02:00
2005-10-20 17:45:00 +02:00
tmp_int = live_game_get_minutes_remaining(unit);
2005-07-01 22:51:46 +02:00
2005-10-20 17:45:00 +02:00
if(tmp_int > 0)
2005-10-24 22:50:48 +02:00
misc_token_add(token_rep,
option_int("string_token_minute_remaining", &tokens),
misc_int_to_char(tmp_int));
2005-07-01 22:51:46 +02:00
if(query_fixture_is_draw(fix))
tmp_int = 120 - current_min;
else
tmp_int = 90 - current_min;
if(tmp_int > 0)
2005-10-24 22:50:48 +02:00
misc_token_add(token_rep,
option_int("string_token_minute_total", &tokens),
misc_int_to_char(tmp_int));
misc_token_add(token_rep,
option_int("string_token_goals0", &tokens),
misc_int_to_char(unit->result[0]));
misc_token_add(token_rep,
option_int("string_token_goals1", &tokens),
misc_int_to_char(unit->result[1]));
misc_token_add(token_rep,
option_int("string_token_goal_diff", &tokens),
misc_int_to_char(ABS(unit->result[0] - unit->result[1])));
misc_token_add(token_rep,
option_int("string_token_extra", &tokens),
lg_commentary_get_extra_data(unit, fix));
misc_token_add(token_rep,
option_int("string_token_possession", &tokens),
misc_int_to_char(unit->possession));
misc_token_add(token_rep,
option_int("string_token_no_possession", &tokens),
misc_int_to_char(!unit->possession));
misc_token_add(token_rep,
option_int("string_token_team_avskill0", &tokens),
misc_int_to_char(avskill0));
misc_token_add(token_rep,
option_int("string_token_team_avskill1", &tokens),
misc_int_to_char(avskill1));
misc_token_add(token_rep,
option_int("string_token_team_avskilldiff", &tokens),
misc_int_to_char(ABS(avskill0 - avskill1)));
2005-07-01 22:51:46 +02:00
}
/** Set the tokens containing team data. */
void
lg_commentary_set_team_tokens(const LiveGameUnit *unit, const Fixture *fix)
{
#ifdef DEBUG
printf("lg_commentary_set_team_tokens\n");
#endif
2005-07-01 22:51:46 +02:00
if(unit->result[0] != unit->result[1])
2005-06-28 04:08:56 +02:00
{
2005-10-24 22:50:48 +02:00
misc_token_add(token_rep,
option_int("string_token_team_losing", &tokens),
g_strdup(fix->teams[(unit->result[0] > unit->result[1])]->name));
misc_token_add(token_rep,
option_int("string_token_team_winning", &tokens),
g_strdup(fix->teams[(unit->result[0] < unit->result[1])]->name));
misc_token_add(token_rep,
option_int("string_token_team_losingn", &tokens),
misc_int_to_char((unit->result[0] > unit->result[1])));
misc_token_add(token_rep,
option_int("string_token_team_winningn", &tokens),
misc_int_to_char((unit->result[0] < unit->result[1])));
2005-06-28 04:08:56 +02:00
}
2005-07-01 22:51:46 +02:00
if(unit->event.team != -1)
2005-10-24 22:50:48 +02:00
misc_token_add(token_rep,
option_int("string_token_team", &tokens),
misc_int_to_char(unit->event.team));
2005-05-18 18:02:14 +02:00
}
2005-07-01 22:51:46 +02:00
/** Set the player tokens. */
void
lg_commentary_set_player_tokens(const LiveGameUnit *unit, const Fixture *fix)
2005-05-18 18:02:14 +02:00
{
#ifdef DEBUG
printf("lg_commentary_set_player_tokens\n");
#endif
2005-07-01 22:51:46 +02:00
Player *pl1 = NULL,
*pl2 = NULL;
if(unit->event.type == LIVE_GAME_EVENT_GENERAL ||
unit->event.type == LIVE_GAME_EVENT_SCORING_CHANCE ||
unit->event.type == LIVE_GAME_EVENT_HEADER ||
unit->event.type == LIVE_GAME_EVENT_PENALTY ||
unit->event.type == LIVE_GAME_EVENT_FREE_KICK)
2005-05-18 18:02:14 +02:00
{
2005-07-01 22:51:46 +02:00
pl1 = player_of_id_team(fix->teams[unit->possession],
unit->event.player);
if(unit->event.player2 != -1)
pl2 = player_of_id_team(fix->teams[unit->possession],
unit->event.player2);
2005-05-18 18:02:14 +02:00
}
2005-07-01 22:51:46 +02:00
else if(unit->event.type == LIVE_GAME_EVENT_SEND_OFF ||
unit->event.type == LIVE_GAME_EVENT_INJURY ||
unit->event.type == LIVE_GAME_EVENT_TEMP_INJURY ||
unit->event.type == LIVE_GAME_EVENT_SUBSTITUTION)
{
pl1 = player_of_id_team(fix->teams[unit->event.team],
unit->event.player);
if(unit->event.player2 != -1)
pl2 = player_of_id_team(fix->teams[unit->event.team],
unit->event.player2);
2005-05-18 18:02:14 +02:00
}
2005-07-01 22:51:46 +02:00
else if(unit->event.type == LIVE_GAME_EVENT_LOST_POSSESSION ||
unit->event.type == LIVE_GAME_EVENT_GOAL ||
2005-06-26 13:42:01 +02:00
unit->event.type == LIVE_GAME_EVENT_MISS ||
2005-05-18 18:02:14 +02:00
unit->event.type == LIVE_GAME_EVENT_SAVE ||
unit->event.type == LIVE_GAME_EVENT_POST ||
unit->event.type == LIVE_GAME_EVENT_PLAYER_PUSHED_IN_CORNER ||
unit->event.type == LIVE_GAME_EVENT_KEEPER_PUSHED_IN_CORNER ||
2005-05-18 18:02:14 +02:00
unit->event.type == LIVE_GAME_EVENT_CROSS_BAR)
{
2005-07-01 22:51:46 +02:00
pl1 = player_of_id_team(fix->teams[unit->possession],
unit->event.player);
pl2 = player_of_id_team(fix->teams[!unit->possession],
unit->event.player2);
2005-05-18 18:02:14 +02:00
}
else if(unit->event.type == LIVE_GAME_EVENT_OWN_GOAL)
2005-07-01 22:51:46 +02:00
pl1 = player_of_id_team(fix->teams[!unit->possession],
unit->event.player);
else if(unit->event.type == LIVE_GAME_EVENT_CORNER_KICK)
pl1 = player_of_id_team(fix->teams[unit->event.team],
unit->event.player);
2005-05-18 18:02:14 +02:00
else if(unit->event.type == LIVE_GAME_EVENT_FOUL ||
unit->event.type == LIVE_GAME_EVENT_FOUL_RED ||
unit->event.type == LIVE_GAME_EVENT_FOUL_RED_INJURY ||
unit->event.type == LIVE_GAME_EVENT_FOUL_YELLOW)
{
2005-07-01 22:51:46 +02:00
pl1 = player_of_id_team(fix->teams[!unit->event.team],
unit->event.player);
pl2 = player_of_id_team(fix->teams[unit->event.team],
unit->event.player2);
2005-05-18 18:02:14 +02:00
}
2005-07-01 22:51:46 +02:00
if(pl1 != NULL)
2005-06-17 14:57:05 +02:00
{
2005-10-24 22:50:48 +02:00
misc_token_add(token_rep,
option_int("string_token_player0", &tokens),
player_get_last_name(pl1->name));
misc_token_add(token_rep,
option_int("string_token_player_goals0", &tokens),
2005-12-09 16:52:57 +01:00
misc_int_to_char(player_games_goals_get(
pl1, fix->clid, PLAYER_VALUE_GOALS)));
2005-10-24 22:50:48 +02:00
misc_token_add(token_rep,
option_int("string_token_player_goals_all0", &tokens),
misc_int_to_char(player_all_games_goals(pl1, PLAYER_VALUE_GOALS)));
2005-06-17 14:57:05 +02:00
}
2005-05-18 18:02:14 +02:00
2005-07-01 22:51:46 +02:00
if(pl2 != NULL)
{
2005-10-24 22:50:48 +02:00
misc_token_add(token_rep,
option_int("string_token_player1", &tokens),
player_get_last_name(pl2->name));
misc_token_add(token_rep,
option_int("string_token_player_goals1", &tokens),
misc_int_to_char(player_games_goals_get(pl2, fix->clid, PLAYER_VALUE_GOALS)));
misc_token_add(token_rep,
option_int("string_token_player_goals_all1", &tokens),
misc_int_to_char(player_all_games_goals(pl2, PLAYER_VALUE_GOALS)));
misc_token_add(token_rep,
option_int("string_token_player_yellows", &tokens),
misc_int_to_char(player_card_get(pl2, fix->clid, PLAYER_VALUE_CARD_YELLOW)));
2005-07-01 22:51:46 +02:00
}
2005-05-18 18:02:14 +02:00
}
/** Return some extra data depending on the unit type. */
gchar*
lg_commentary_get_extra_data(const LiveGameUnit *unit, const Fixture *fix)
{
#ifdef DEBUG
printf("lg_commentary_get_extra_data\n");
#endif
#ifdef DEBUG
printf("lg_commentary_get_extra_data\n");
#endif
2005-05-18 18:02:14 +02:00
gchar buf[SMALL];
switch(unit->event.type)
{
default:
return NULL;
break;
case LIVE_GAME_EVENT_STRUCTURE_CHANGE:
sprintf(buf, "%d",
2005-05-19 20:13:14 +02:00
fix->teams[unit->event.team]->structure);
2005-05-18 18:02:14 +02:00
break;
case LIVE_GAME_EVENT_STYLE_CHANGE_ALL_OUT_DEFEND:
2005-06-18 16:45:29 +02:00
strcpy(buf, _("ALL OUT DEFEND"));
2005-05-18 18:02:14 +02:00
break;
case LIVE_GAME_EVENT_STYLE_CHANGE_DEFEND:
2005-06-18 16:45:29 +02:00
strcpy(buf, _("DEFEND"));
2005-05-18 18:02:14 +02:00
break;
case LIVE_GAME_EVENT_STYLE_CHANGE_BALANCED:
2005-06-18 16:45:29 +02:00
strcpy(buf, _("BALANCED"));
2005-05-18 18:02:14 +02:00
break;
case LIVE_GAME_EVENT_STYLE_CHANGE_ATTACK:
2005-06-18 16:45:29 +02:00
strcpy(buf, _("ATTACK"));
2005-05-18 18:02:14 +02:00
break;
case LIVE_GAME_EVENT_STYLE_CHANGE_ALL_OUT_ATTACK:
2005-06-18 16:45:29 +02:00
strcpy(buf, _("ALL OUT ATTACK"));
2005-05-18 18:02:14 +02:00
break;
case LIVE_GAME_EVENT_BOOST_CHANGE_ANTI:
2005-06-18 16:45:29 +02:00
strcpy(buf, _("ANTI"));
2005-05-18 18:02:14 +02:00
break;
case LIVE_GAME_EVENT_BOOST_CHANGE_OFF:
2005-06-18 16:45:29 +02:00
strcpy(buf, _("OFF"));
2005-05-18 18:02:14 +02:00
break;
case LIVE_GAME_EVENT_BOOST_CHANGE_ON:
2005-06-18 16:45:29 +02:00
strcpy(buf, _("ON"));
2005-05-18 18:02:14 +02:00
break;
}
return g_strdup(buf);
}
2005-06-26 13:42:01 +02:00
2005-07-05 20:37:26 +02:00
/** Allocate memory for the token array and fill some tokens
at the beginning of the live game that don't change later. */
void
lg_commentary_initialize(const Fixture *fix)
{
#ifdef DEBUG
printf("lg_commentary_initialize\n");
#endif
2005-07-05 20:37:26 +02:00
gchar buf[SMALL];
2005-10-24 22:50:48 +02:00
token_rep[0] = g_ptr_array_new();
token_rep[1] = g_ptr_array_new();
2005-07-05 20:37:26 +02:00
2005-10-24 22:50:48 +02:00
misc_token_add(token_rep,
option_int("string_token_team_home", &tokens),
g_strdup(fix->teams[0]->name));
misc_token_add(token_rep,
option_int("string_token_team_away", &tokens),
g_strdup(fix->teams[1]->name));
2005-07-05 20:37:26 +02:00
if(fix->teams[0]->clid < ID_CUP_START)
2005-10-24 22:50:48 +02:00
misc_token_add(token_rep,
option_int("string_token_team_layer0", &tokens),
misc_int_to_char(league_from_clid(fix->teams[0]->clid)->layer));
2005-07-05 20:37:26 +02:00
if(fix->teams[1]->clid < ID_CUP_START)
2005-10-24 22:50:48 +02:00
misc_token_add(token_rep,
option_int("string_token_team_layer1", &tokens),
misc_int_to_char(league_from_clid(fix->teams[1]->clid)->layer));
2005-07-05 20:37:26 +02:00
2005-10-24 22:50:48 +02:00
if(fix->teams[0]->clid < ID_CUP_START &&
fix->teams[1]->clid < ID_CUP_START)
misc_token_add(token_rep,
option_int("string_token_team_layerdiff", &tokens),
misc_int_to_char(league_from_clid(fix->teams[0]->clid)->layer -
league_from_clid(fix->teams[1]->clid)->layer));
misc_token_add(token_rep,
option_int("string_token_league_cup_name", &tokens),
g_strdup(league_cup_get_name_string(fix->clid)));
2005-07-05 20:37:26 +02:00
if(fix->clid >= ID_CUP_START)
{
cup_get_round_name(cup_from_clid(fix->clid), fix->round, buf);
2005-10-24 22:50:48 +02:00
misc_token_add(token_rep,
option_int("string_token_cup_round_name", &tokens),
g_strdup(buf));
2005-07-05 20:37:26 +02:00
}
misc_print_grouped_int(fix->attendance, buf);
2005-10-24 22:50:48 +02:00
misc_token_add(token_rep,
option_int("string_token_attendance", &tokens),
g_strdup(buf));
2005-07-05 20:37:26 +02:00
2005-10-24 22:50:48 +02:00
misc_token_add(token_rep,
option_int("string_token_yellow_limit", &tokens),
misc_int_to_char(league_cup_get_yellow_red(fix->clid)));
2005-07-05 20:37:26 +02:00
}
/** Free the memory occupied by the tokens array and the permanent tokens. */
void
2005-10-25 17:26:53 +02:00
lg_commentary_free_tokens(void)
2005-07-05 20:37:26 +02:00
{
#ifdef DEBUG
printf("lg_commentary_free_tokens\n");
#endif
2005-10-24 22:50:48 +02:00
gint i;
for(i=token_rep[0]->len - 1;i >= 0; i--)
{
g_free(g_ptr_array_index(token_rep[0], i));
g_free(g_ptr_array_index(token_rep[1], i));
}
g_ptr_array_free(token_rep[0], TRUE);
g_ptr_array_free(token_rep[1], TRUE);
2005-07-05 20:37:26 +02:00
}
2005-06-26 13:42:01 +02:00
/** Load the appropriate commentary file by evaluating
the language option. */
void
lg_commentary_load_commentary_file_from_option(void)
{
#ifdef DEBUG
printf("lg_commentary_load_commentary_file_from_option\n");
#endif
2005-06-26 13:42:01 +02:00
gchar buf[SMALL], commentary_file[SMALL];
2006-02-21 11:20:20 +01:00
language_get_code(buf);
2005-06-26 13:42:01 +02:00
sprintf(commentary_file, "lg_commentary_%s.xml", buf);
lg_commentary_load_commentary_file(commentary_file, FALSE);
}
/** Load the commentary file given. Abort if not found and abort=TRUE,
load default english file otherwise. */
void
lg_commentary_load_commentary_file(const gchar *commentary_file, gboolean abort)
{
#ifdef DEBUG
printf("lg_commentary_load_commentary_file\n");
#endif
2005-10-25 17:52:44 +02:00
gchar *file_name = NULL;
if(g_file_test(commentary_file, G_FILE_TEST_EXISTS))
{
xml_lg_commentary_read(commentary_file);
return;
}
file_name = file_find_support_file(commentary_file, FALSE);
2005-06-26 13:42:01 +02:00
if(file_name != NULL)
{
xml_lg_commentary_read(file_name);
2005-10-25 17:52:44 +02:00
g_free(file_name);
2005-06-26 13:42:01 +02:00
}
else
{
if(abort)
2005-10-24 22:50:48 +02:00
main_exit_program(EXIT_FILE_NOT_FOUND,
"lg_commentary_load_commentary_file: file %s not found \n",
file_name);
2005-06-26 13:42:01 +02:00
else
lg_commentary_load_commentary_file("lg_commentary_en.xml", TRUE);
}
}
2005-10-25 17:26:53 +02:00
/** Load a file with tokens for commentary testing purposes. */
void
lg_commentary_test_load_token_file(const gchar *token_file)
{
#ifdef DEBUG
printf("lg_commentary_test_load_token_file\n");
#endif
2005-10-25 17:26:53 +02:00
gchar token_name[SMALL], token_value[SMALL];
FILE *fil = NULL;
file_my_fopen(token_file, "r", &fil, TRUE);
token_rep[0] = g_ptr_array_new();
token_rep[1] = g_ptr_array_new();
while(file_get_next_opt_line(fil, token_name, token_value))
{
g_ptr_array_add(token_rep[0], (gpointer)g_strdup(token_name));
g_ptr_array_add(token_rep[1], (gpointer)g_strdup(token_value));
}
}
/** Test a live game commentary file.
@param commentary_file the commentary file to test.
@param token_file the file containing the token values to use.
@param event_name the event to test (or 'all' to test all events).
@param number_of_passes how many commentaries to generate. */
void
lg_commentary_test(const gchar *commentary_file, const gchar* token_file,
const gchar *event_name, gint number_of_passes)
{
#ifdef DEBUG
printf("lg_commentary_test\n");
#endif
2005-10-25 17:26:53 +02:00
gint i, j;
LiveGame dummy;
lg_commentary_load_commentary_file(commentary_file, TRUE);
lg_commentary_test_load_token_file(token_file);
dummy.units = g_array_new(FALSE, FALSE, sizeof(gint));
statp = (gpointer)&dummy;
if(event_name == NULL)
for(i=0;i<LIVE_GAME_EVENT_END;i++)
for(j=0;j<number_of_passes;j++)
lg_commentary_generate(NULL, NULL, NULL, i);
else
for(i=0;i<number_of_passes;i++)
lg_commentary_generate(NULL, NULL, event_name, -1);
g_array_free(dummy.units, TRUE);
}