~ubuntu-branches/ubuntu/hardy/bygfoot/hardy

« back to all changes in this revision

Viewing changes to src/game.c

  • Committer: Bazaar Package Importer
  • Author(s): Isaac Clerencia
  • Date: 2006-06-04 17:51:19 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060604175119-0pbfuhr617031qa9
Tags: 2.0.0-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   game.c
 
3
 
 
4
   Bygfoot Football Manager -- a small and simple GTK2-based
 
5
   football management game.
 
6
 
 
7
   http://bygfoot.sourceforge.net
 
8
 
 
9
   Copyright (C) 2005  Gyözö Both (gyboth@bygfoot.com)
 
10
 
 
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.
 
15
 
 
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.
 
20
 
 
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.
 
24
*/
 
25
 
1
26
#include "cup.h"
2
27
#include "finance.h"
3
28
#include "fixture.h"
39
64
        style_factor = (gfloat)tm[i]->style * const_float("float_game_style_factor");
40
65
 
41
66
        team_values[i][GAME_TEAM_VALUE_GOALIE] = 
42
 
            game_get_player_contribution(player_of_idx_team(tm[i], 0), FALSE) * 
43
 
            (1 + home_advantage * (i == 0)) *
44
 
            (1 + const_float("float_player_boost_skill_effect") * tm[i]->boost);
 
67
            game_get_player_contribution(player_of_idx_team(tm[i], 0), FALSE, TRUE) * 
 
68
            (1 + home_advantage * (i == 0));
45
69
 
46
70
        for(j=1;j<11;j++)
47
 
        {
48
 
            team_values[i][GAME_TEAM_VALUE_ATTACK] +=
49
 
                game_get_player_contribution(player_of_idx_team(tm[i], j), GAME_TEAM_VALUE_ATTACK);
50
 
            team_values[i][GAME_TEAM_VALUE_MIDFIELD] +=
51
 
                game_get_player_contribution(player_of_idx_team(tm[i], j), GAME_TEAM_VALUE_MIDFIELD);
52
 
            team_values[i][GAME_TEAM_VALUE_DEFEND] +=
53
 
                game_get_player_contribution(player_of_idx_team(tm[i], j), GAME_TEAM_VALUE_DEFEND);
54
 
        }
 
71
            if(player_of_idx_team(tm[i], j)->cskill > 0)
 
72
            {
 
73
                team_values[i][GAME_TEAM_VALUE_ATTACK] +=
 
74
                    game_get_player_contribution(player_of_idx_team(tm[i], j), 
 
75
                                                 GAME_TEAM_VALUE_ATTACK, TRUE);
 
76
                team_values[i][GAME_TEAM_VALUE_MIDFIELD] +=
 
77
                    game_get_player_contribution(player_of_idx_team(tm[i], j), 
 
78
                                                 GAME_TEAM_VALUE_MIDFIELD, TRUE);
 
79
                team_values[i][GAME_TEAM_VALUE_DEFEND] +=
 
80
                    game_get_player_contribution(player_of_idx_team(tm[i], j), 
 
81
                                                 GAME_TEAM_VALUE_DEFEND, TRUE);
 
82
            }
55
83
 
56
84
        for(j=GAME_TEAM_VALUE_DEFEND;j<GAME_TEAM_VALUE_DEFEND + 3;j++)
57
85
            team_values[i][j] *= 
58
 
                ((1 + home_advantage * (i == 0)) *
59
 
                 (1 + const_float("float_player_boost_skill_effect") * tm[i]->boost));
 
86
                ((1 + home_advantage * (i == 0)));
60
87
 
61
88
        team_values[i][GAME_TEAM_VALUE_DEFEND] *= (1 - style_factor);
62
89
        team_values[i][GAME_TEAM_VALUE_ATTACK] *= (1 + style_factor);
66
93
/** Return the contribution of a player to the attack, midfield or defend.
67
94
    @param pl The player.
68
95
    @param type Whether we have defend, midfield or attack value.
 
96
    @param special Whether to count special influence like boost and
 
97
    streak.
69
98
    @return The player's contribution depending on position and
70
99
    fitness. */
71
100
gfloat
72
 
game_get_player_contribution(const Player *pl, gint type)
 
101
game_get_player_contribution(const Player *pl, gint type, gboolean special)
73
102
{
74
103
/** How the cskill of field players get weighted for the team values in
75
104
    a match. Rows are player position, columns value type. 
85
114
          const_float("float_player_team_weight_forward_midfield"), 
86
115
          const_float("float_player_team_weight_forward_attack")}};
87
116
 
88
 
    return player_get_game_skill(pl, FALSE) *
89
 
        player_weights[pl->cpos - 1][type - GAME_TEAM_VALUE_DEFEND] *
90
 
        (1 + (gfloat)pl->streak * const_float("float_player_streak_influence_skill"));
 
117
    return player_get_game_skill(pl, FALSE, special) *
 
118
        player_weights[pl->cpos - 1][type - GAME_TEAM_VALUE_DEFEND];
91
119
}
92
120
 
93
121
/** Return a random attacking or defending player
130
158
    else if(player_type == GAME_PLAYER_TYPE_PENALTY)
131
159
        return game_get_penalty_taker(tm, last_penalty);
132
160
    else
133
 
    {
134
 
        g_warning("game_get_player: unknown player type %d\n", player_type);
135
 
        main_exit_program(EXIT_INT_NOT_FOUND, NULL);
136
 
    }
 
161
        main_exit_program(EXIT_INT_NOT_FOUND, 
 
162
                          "game_get_player: unknown player type %d\n", player_type);
137
163
 
138
164
    game_get_player_probs(tm->players, probs, weights, skills);
139
165
 
154
180
    else
155
181
    {
156
182
        g_warning("game_get_player: All players injured or banned, apparently.\n");
157
 
        printf("%s %s player list:\n", league_cup_get_name_string(tm->clid), tm->name);
 
183
        g_print("%s %s player list:\n", league_cup_get_name_string(tm->clid), tm->name);
158
184
        for(i=0;i<tm->players->len;i++)
159
185
        {
160
186
            if(i < 10)
161
 
                printf("prob %.3f  ", probs[i]);
162
 
            printf("%d %20s health %d cskill %.2f\n", i, player_of_idx_team(tm, i)->name,
 
187
                g_print("prob %.3f  ", probs[i]);
 
188
            g_print("%d %20s health %d cskill %.2f\n", i, player_of_idx_team(tm, i)->name,
163
189
                   player_of_idx_team(tm, i)->health, player_of_idx_team(tm, i)->cskill);
164
190
        }
165
191
 
183
209
    gint i;
184
210
 
185
211
    probs[0] = (skills) ? 
186
 
        player_get_game_skill(&g_array_index(players, Player, 1), FALSE) *
 
212
        player_get_game_skill(&g_array_index(players, Player, 1), FALSE, TRUE) *
187
213
        weights[g_array_index(players, Player, 1).cpos - 1] :
188
214
        weights[g_array_index(players, Player, 1).cpos - 1] *
189
215
        (g_array_index(players, Player, 1).cskill != 0);
196
222
    {
197
223
        probs[i] = probs[i - 1] + 
198
224
            ((skills) ? 
199
 
             player_get_game_skill(&g_array_index(players, Player, i + 1), FALSE) *
 
225
             player_get_game_skill(&g_array_index(players, Player, i + 1), FALSE, TRUE) *
200
226
             weights[g_array_index(players, Player, i + 1).cpos - 1] :
201
227
             weights[g_array_index(players, Player, i + 1).cpos - 1] *
202
228
             (g_array_index(players, Player, i + 1).cskill != 0));
388
414
{
389
415
    const GPtrArray *teamsp = 
390
416
        (GPtrArray*)league_cup_get_teams(fix->clid);
391
 
    gfloat av_att = (query_cup_is_international(fix->clid) && teamsp->len > 0) ?
 
417
    gfloat av_att = (fix->clid >= ID_CUP_START && 
 
418
                     query_cup_is_international(fix->clid) && teamsp->len > 0) ?
392
419
        (gfloat)league_cup_average_capacity(fix->clid) :
393
420
        (gfloat)league_cup_average_capacity(lig(0).id);
394
421
 
643
670
        if(g_array_index(tm->players, Player, i).cskill > 0)
644
671
            g_ptr_array_add(substitutes, &g_array_index(tm->players, Player, i));
645
672
 
 
673
    if(substitutes->len == 0)
 
674
    {
 
675
        g_ptr_array_free(substitutes, TRUE);
 
676
        g_warning("game_substitute_player: no suitable substitutes found (all injured/banned?)");
 
677
        return -1;
 
678
    }
 
679
 
646
680
    g_ptr_array_sort_with_data(substitutes, 
647
681
                               (GCompareDataFunc)player_compare_substitute_func,
648
682
                               GINT_TO_POINTER(player_of_idx_team(tm, 
659
693
 
660
694
    substitute = ((Player*)g_ptr_array_index(substitutes, 0))->id;
661
695
 
662
 
    player_streak_add_to_prob(
663
 
        &g_array_index(tm->players, Player, player_number),
664
 
        const_float("float_player_streak_add_sub_out"));
665
 
 
666
 
    player_streak_add_to_prob((Player*)g_ptr_array_index(substitutes, 0),
667
 
                              const_float("float_player_streak_add_sub_in"));
668
 
 
669
696
    player_swap(tm, player_number, tm, player_id_index(tm, substitute));
670
697
 
671
698
    g_ptr_array_free(substitutes, TRUE);
787
814
        if(g_array_index(tm->players, Player, i).cskill > 0)
788
815
            g_ptr_array_add(substitutes, player_of_idx_team(tm, i));
789
816
 
 
817
    if(substitutes->len == 0)
 
818
    {
 
819
        g_ptr_array_free(substitutes, TRUE);
 
820
        g_warning("game_substitute_player_send_off: no suitable substitutes found (all injured/banned?)");
 
821
        *to_substitute = -1;
 
822
        return;
 
823
    }
 
824
        
790
825
    if(num_forw == 0 && MAX(num_def, num_mid) > 2)
791
826
        position = PLAYER_POS_FORWARD;
792
827
    else
822
857
 
823
858
    for(i=0;i<2;i++)
824
859
    {
825
 
        for(j=0;j<11;j++)
826
 
            if(player_of_idx_team(fix->teams[i], j)->cskill > 0)
827
 
                player_decrease_fitness(player_of_idx_team(fix->teams[i], j));
 
860
        if(debug < 50 || team_is_user(fix->teams[i]) == -1)
 
861
            for(j=0;j<11;j++)
 
862
                if(player_of_idx_team(fix->teams[i], j)->cskill > 0)
 
863
                    player_decrease_fitness(player_of_idx_team(fix->teams[i], j));
828
864
    }
829
865
}
830
866
 
832
868
    @param live_game_stats Pointer to the live game.
833
869
    @param live_game_unit The live game unit. */
834
870
void
835
 
game_update_stats(gpointer live_game, gconstpointer live_game_unit)
 
871
game_update_stats(LiveGame *lg, const LiveGameUnit *unit)
836
872
{
837
873
    gint i;
838
 
    LiveGameStats *stats = &((LiveGame*)live_game)->stats;
839
 
    const LiveGameUnit *unit = (const LiveGameUnit*)live_game_unit;
 
874
    LiveGameStats *stats = &lg->stats;
840
875
    
841
876
    if(unit->minute != -1)
842
877
        stats->values[unit->possession][LIVE_GAME_STAT_VALUE_POSSESSION]++;
850
885
    else if(unit->event.type == LIVE_GAME_EVENT_INJURY)
851
886
    {
852
887
        stats->values[unit->event.team][LIVE_GAME_STAT_VALUE_INJURIES]++;
853
 
        game_update_stats_player(live_game, live_game_unit);
 
888
        game_update_stats_player(lg, unit);
854
889
    }
855
890
    else if(unit->event.type == LIVE_GAME_EVENT_FOUL ||
856
891
            unit->event.type == LIVE_GAME_EVENT_FOUL_YELLOW ||
861
896
        if(unit->event.type == LIVE_GAME_EVENT_FOUL_YELLOW)
862
897
        {
863
898
            stats->values[unit->event.team][LIVE_GAME_STAT_VALUE_CARDS]++;
864
 
            game_update_stats_player(live_game, live_game_unit);
 
899
            game_update_stats_player(lg, unit);
865
900
        }
866
901
    }
867
902
    else if(unit->event.type == LIVE_GAME_EVENT_SEND_OFF)
868
903
    {
869
904
        stats->values[unit->event.team][LIVE_GAME_STAT_VALUE_REDS]++;
870
 
        game_update_stats_player(live_game, live_game_unit);
 
905
        game_update_stats_player(lg, unit);
871
906
    }
872
907
    else if(unit->event.type == LIVE_GAME_EVENT_GOAL ||
873
908
            unit->event.type == LIVE_GAME_EVENT_OWN_GOAL)
875
910
        if(live_game_unit_before(unit, -1)->event.type != LIVE_GAME_EVENT_PENALTY &&
876
911
           unit->event.type != LIVE_GAME_EVENT_OWN_GOAL)
877
912
            stats->values[unit->event.team][LIVE_GAME_STAT_VALUE_GOALS_REGULAR]++;
878
 
        game_update_stats_player(live_game, live_game_unit);
 
913
        game_update_stats_player(lg, unit);
879
914
    }
880
915
 
881
916
    for(i=0;i<2;i++)
891
926
    @param player The player id.
892
927
    @param type The type of the stat. */
893
928
void
894
 
game_update_stats_player(gpointer live_game, gconstpointer live_game_unit)
 
929
game_update_stats_player(LiveGame *lg, const LiveGameUnit *unit)
895
930
{
896
931
    gint i;
897
932
    gchar buf[SMALL], buf2[SMALL];    
898
 
    LiveGameStats *stats = &((LiveGame*)live_game)->stats;
899
 
    const LiveGameUnit *unit = (const LiveGameUnit*)live_game_unit;
 
933
    LiveGameStats *stats = &lg->stats;
900
934
    gint minute = live_game_unit_get_minute(unit), array_index = -1;
901
935
    gboolean own_goal;
902
936
    gint team = unit->event.team,
903
937
        player = unit->event.player,
904
938
        player2 = unit->event.player2;
905
 
    const Team *tm[2] = {((LiveGame*)live_game)->fix->teams[0], 
906
 
                         ((LiveGame*)live_game)->fix->teams[1]};
 
939
    const Team *tm[2] = {lg->fix->teams[0], 
 
940
                         lg->fix->teams[1]};
907
941
    GPtrArray *players = NULL;
908
942
    
909
943
    if(unit->event.type == LIVE_GAME_EVENT_GOAL ||
975
1009
    gint i;
976
1010
    GPtrArray *teams = NULL;
977
1011
    Cup *cup = NULL;
 
1012
    gchar buf[SMALL], buf2[SMALL];
978
1013
 
979
1014
    if((debug > 100 && fixture_user_team_involved(fix) != -1) ||
980
1015
       debug > 130)
981
 
        printf("game_post_match: %s - %s\n", 
 
1016
        g_print("game_post_match: %s - %s\n", 
982
1017
               fix->teams[0]->name,
983
1018
               fix->teams[1]->name);
984
1019
 
986
1021
        table_update(fix);
987
1022
    
988
1023
    for(i=0;i<2;i++)
989
 
    {
990
 
        if(team_is_user(fix->teams[i]) == -1)
991
 
            team_update_cpu_team(fix->teams[i],
992
 
                                 (fixture_user_team_involved(fix) != -1));
993
 
        else
994
 
            team_update_post_match(fix->teams[i], fix);
995
 
    }
 
1024
        team_update_post_match(fix->teams[i], fix);
996
1025
 
997
1026
    if(fix->clid < ID_CUP_START)
998
1027
        return;
1005
1034
        teams = cup_get_teams_sorted(cup);
1006
1035
        
1007
1036
        if(team_is_user((Team*)g_ptr_array_index(teams, 0)) != -1)
 
1037
        {
1008
1038
            user_history_add(&usr(team_is_user((Team*)g_ptr_array_index(teams, 0))),
1009
 
                             USER_HISTORY_WIN_FINAL, ((Team*)g_ptr_array_index(teams, 0))->id,
1010
 
                             fix->clid, fix->round,((Team*)g_ptr_array_index(teams, 1))->name);
 
1039
                             USER_HISTORY_WIN_FINAL, 
 
1040
                             ((Team*)g_ptr_array_index(teams, 0))->name,
 
1041
                             league_cup_get_name_string(fix->clid),
 
1042
                             ((Team*)g_ptr_array_index(teams, 1))->name, NULL);
 
1043
            user_add_cup_success(&usr(team_is_user((Team*)g_ptr_array_index(teams, 0))),
 
1044
                                 cup, fix->round, USER_HISTORY_WIN_FINAL);
 
1045
        }
1011
1046
        else if(team_is_user((Team*)g_ptr_array_index(teams, 1)) != -1)
 
1047
        {
1012
1048
            user_history_add(&usr(team_is_user((Team*)g_ptr_array_index(teams, 1))),
1013
 
                             USER_HISTORY_LOSE_FINAL, ((Team*)g_ptr_array_index(teams, 1))->id,
1014
 
                             fix->clid, fix->round,((Team*)g_ptr_array_index(teams, 0))->name);
 
1049
                             USER_HISTORY_LOSE_FINAL, 
 
1050
                             ((Team*)g_ptr_array_index(teams, 1))->name,
 
1051
                             league_cup_get_name_string(fix->clid),
 
1052
                             ((Team*)g_ptr_array_index(teams, 0))->name, NULL);
 
1053
            user_add_cup_success(&usr(team_is_user((Team*)g_ptr_array_index(teams, 1))),
 
1054
                                 cup, fix->round, USER_HISTORY_LOSE_FINAL);
 
1055
        }
1015
1056
        g_ptr_array_free(teams, TRUE);
1016
1057
    }
1017
1058
    else if(fixture_user_team_involved(fix) != -1)
1018
 
        user_history_add(&usr(fixture_user_team_involved(fix)), USER_HISTORY_REACH_CUP_ROUND,
1019
 
                         usr(fixture_user_team_involved(fix)).team_id,
1020
 
                         fix->clid, fix->round, "");
 
1059
    {
 
1060
        cup_get_round_name(cup_from_clid(fix->clid), fix->round, buf);
 
1061
        sprintf(buf2, "%d", fix->round + 1);
 
1062
 
 
1063
        user_history_add(&usr(fixture_user_team_involved(fix)),
 
1064
                         USER_HISTORY_REACH_CUP_ROUND,
 
1065
                         usr(fixture_user_team_involved(fix)).tm->name,
 
1066
                         league_cup_get_name_string(fix->clid),
 
1067
                         buf, buf2);
 
1068
        user_add_cup_success(&usr(fixture_user_team_involved(fix)),
 
1069
                             cup, fix->round, USER_HISTORY_REACH_CUP_ROUND);
 
1070
    }
1021
1071
}
1022
1072
 
1023
1073
/** Reduce stadium capacity and safety after a stadium event.
1064
1114
        for(j=1;j<11;j++)
1065
1115
        {
1066
1116
            pl.cpos = player_get_position_from_structure(442, j);
1067
 
            max_values[i] += game_get_player_contribution(&pl, i);
 
1117
            max_values[i] += game_get_player_contribution(&pl, i, FALSE);
1068
1118
        }
1069
1119
    }
1070
1120
}
1094
1144
 
1095
1145
    return return_value;
1096
1146
}
 
1147
 
 
1148
/** Deduce some money for boost during a match. */
 
1149
void
 
1150
game_boost_cost(void)
 
1151
{
 
1152
    gfloat wage_unit = finance_wage_unit(usr(stat2).tm);
 
1153
    gint deduce = 
 
1154
        (gint)rint(wage_unit * const_float("float_boost_cost_factor"));
 
1155
 
 
1156
    usr(stat2).money -= deduce;
 
1157
    usr(stat2).money_out[1][MON_OUT_BOOST] -= deduce;
 
1158
}