From ca195b5e5176ed791c9082580758b756c8a95cf9 Mon Sep 17 00:00:00 2001 From: gunnar_g Date: Tue, 15 Sep 2009 12:02:44 +0000 Subject: [PATCH] Updated the default team implementation --- src/callback_func.c | 36 ++++++++++++++++++++++++++---------- src/game.c | 8 ++++---- src/live_game.c | 4 ++-- src/misc2_callback_func.c | 4 ++-- src/player.c | 23 +++++++++++++---------- src/player.h | 2 +- src/strategy.c | 4 ++-- src/user.c | 12 +++++++++--- 8 files changed, 59 insertions(+), 34 deletions(-) diff --git a/src/callback_func.c b/src/callback_func.c index 5b51a9a5..9059d117 100644 --- a/src/callback_func.c +++ b/src/callback_func.c @@ -55,22 +55,25 @@ callback_show_next_live_game(void) #endif gint i, j; + gint user_team_involved; for(i=0;ilen;i++) { usr(i).counters[COUNT_USER_TOOK_TURN] = 0; - // Store the player order before the live match this process is - // repeated so check first if it hasn't been done yet - if (usr(i).default_team->len==0 && option_int("int_opt_user_store_restore_default_team", - &usr(i).options)) { - store_default_team(&usr(i)); - } } counters[COUNT_NEWS_SHOWN] = counters[COUNT_NEW_NEWS] = 0; - for(i=0;ilen;i++) - for(j=0;jlen;j++) + for(i=0;ilen;i++) { + for(j=0;jlen;j++) { + // Store the player order before the live match: get the team that is involved, if it's a user team + // and that user has the option to always store the default team checked, store it + user_team_involved = fixture_user_team_involved(&g_array_index(lig(i).fixtures, Fixture, j)); + if (user_team_involved!=-1 && option_int("int_opt_user_store_restore_default_team", + &usr(user_team_involved).options)) { + store_default_team(&usr(user_team_involved)); + } + if(g_array_index(lig(i).fixtures, Fixture, j).week_number == week && g_array_index(lig(i).fixtures, Fixture, j).week_round_number == week_round && fixture_user_team_involved(&g_array_index(lig(i).fixtures, Fixture, j)) != -1 && @@ -83,9 +86,20 @@ callback_show_next_live_game(void) &usr(fixture_user_team_involved(&g_array_index(lig(i).fixtures, Fixture, j))).live_game); return; } + } + } + + for(i=0;ilen;i++) { + for(j=0;jfixtures->len;j++) { + // Store the player order before the live match: get the team that is involved, if it's a user team + // and that user has the option to always store the default team checked, store it + user_team_involved = fixture_user_team_involved(&g_array_index(acp(i)->fixtures, Fixture, j)); + + if (user_team_involved!=-1 && option_int("int_opt_user_store_restore_default_team", + &usr(user_team_involved).options)) { + store_default_team(&usr(user_team_involved)); + } - for(i=0;ilen;i++) - for(j=0;jfixtures->len;j++) if(g_array_index(acp(i)->fixtures, Fixture, j).week_number == week && g_array_index(acp(i)->fixtures, Fixture, j).week_round_number == week_round && fixture_user_team_involved(&g_array_index(acp(i)->fixtures, Fixture, j)) != -1 && @@ -98,6 +112,8 @@ callback_show_next_live_game(void) &usr(fixture_user_team_involved(&g_array_index(acp(i)->fixtures, Fixture, j))).live_game); return; } + } + } window_destroy(&window.live); diff --git a/src/game.c b/src/game.c index f9d87435..0bda5dfb 100644 --- a/src/game.c +++ b/src/game.c @@ -744,7 +744,7 @@ game_substitute_player(Team *tm, gint player_number) substitute = ((Player*)g_ptr_array_index(substitutes, 0))->id; - player_swap(tm, player_number, tm, player_id_index(tm, substitute)); + player_swap(tm, player_number, tm, player_id_index(tm, substitute,TRUE)); g_ptr_array_free(substitutes, TRUE); @@ -894,8 +894,8 @@ game_substitute_player_send_off(gint clid, Team *tm, gint player_number, GINT_TO_POINTER(position)); *substitute = ((Player*)g_ptr_array_index(substitutes, 0))->id; - player_swap(tm, player_id_index(tm, *to_substitute), - tm, player_id_index(tm, *substitute)); + player_swap(tm, player_id_index(tm, *to_substitute, TRUE), + tm, player_id_index(tm, *substitute, TRUE)); g_ptr_array_free(substitutes, TRUE); @@ -1235,7 +1235,7 @@ game_get_default_penalty_shooter(const Team *tm) &usr(team_is_user(tm)).options)) != NULL && player_id_index(tm, option_int("int_opt_user_penalty_shooter", - &usr(team_is_user(tm)).options)) < 11 && + &usr(team_is_user(tm)).options), TRUE) < 11 && player_of_id_team(tm, option_int("int_opt_user_penalty_shooter", &usr(team_is_user(tm)).options))->cskill != 0) diff --git a/src/live_game.c b/src/live_game.c index 0a1dc0cf..36e110da 100644 --- a/src/live_game.c +++ b/src/live_game.c @@ -552,7 +552,7 @@ live_game_event_injury(gint team, gint player, gboolean create_new) { sub_in = game_substitute_player(tms[last_unit.event.team], player_id_index(tms[last_unit.event.team], - last_unit.event.player)); + last_unit.event.player, TRUE)); if(sub_in != -1) { old_structure = tms[last_unit.event.team]->structure; @@ -1008,7 +1008,7 @@ live_game_event_send_off(gint team, gint player, gboolean second_yellow) else if(tms[team]->players->len > 11) { game_substitute_player_send_off(match->fix->clid, - tms[team], player_id_index(tms[team], player), + tms[team], player_id_index(tms[team], player, TRUE), &to_substitute, &substitute); if(to_substitute != -1) diff --git a/src/misc2_callback_func.c b/src/misc2_callback_func.c index 47ade11d..77eee2c2 100644 --- a/src/misc2_callback_func.c +++ b/src/misc2_callback_func.c @@ -84,7 +84,7 @@ misc2_callback_transfer_user_player(void) player_of_idx_team(new_team, new_team->players->len - 1)->wage = transoff(stat2, 0).wage; g_array_remove_index(current_user.tm->players, - player_id_index(current_user.tm, trans(stat2).id)); + player_id_index(current_user.tm, trans(stat2).id, TRUE)); transfer_remove_player(stat2); } else @@ -104,7 +104,7 @@ misc2_callback_transfer_user_player(void) { current_user.money += transoff(stat2, 0).fee; current_user.money_in[1][MON_IN_TRANSFERS] += transoff(stat2, 0).fee; - player_remove_from_team(current_user.tm, player_id_index(current_user.tm, trans(stat2).id)); + player_remove_from_team(current_user.tm, player_id_index(current_user.tm, trans(stat2).id, TRUE)); } } diff --git a/src/player.c b/src/player.c index 8bdde649..7d7198cb 100644 --- a/src/player.c +++ b/src/player.c @@ -334,9 +334,10 @@ player_assign_wage(const Player *pl) his id. @param tm The team. @param player_id The player's id. + @param stopWhenPlayerNotFound Should we stop when we don't find the player @return The array index or -1. */ gint -player_id_index(const Team *tm, gint player_id) +player_id_index(const Team *tm, gint player_id, gboolean stopWhenPlayerNotFound) { gint i; @@ -344,9 +345,11 @@ player_id_index(const Team *tm, gint player_id) if(g_array_index(tm->players, Player, i).id == player_id) return i; - main_exit_program(EXIT_INT_NOT_FOUND, + if (stopWhenPlayerNotFound) { + main_exit_program(EXIT_INT_NOT_FOUND, "player_id_index: didn't find player with id %d of team %s\n", player_id, tm->name); + } return -1; } @@ -480,13 +483,13 @@ player_compare_func(gconstpointer a, gconstpointer b, gpointer data) player_get_game_skill(pl2, FALSE, TRUE)); else if(type == PLAYER_COMPARE_ATTRIBUTE_POS) { - if(MIN(player_id_index(pl1->team, pl1->id), - player_id_index(pl2->team, pl2->id)) < 11 && - MAX(player_id_index(pl1->team, pl1->id), - player_id_index(pl2->team, pl2->id)) >= 11) + if(MIN(player_id_index(pl1->team, pl1->id, TRUE), + player_id_index(pl2->team, pl2->id, TRUE)) < 11 && + MAX(player_id_index(pl1->team, pl1->id, TRUE), + player_id_index(pl2->team, pl2->id, TRUE)) >= 11) return_value = - (player_id_index(pl1->team, pl1->id) > - player_id_index(pl2->team, pl2->id)) ? + (player_id_index(pl1->team, pl1->id, TRUE) > + player_id_index(pl2->team, pl2->id, TRUE)) ? 1 : -1; else if(pl1->cskill == 0) return_value = (pl2->cskill == 0) ? 0 : 1; @@ -1265,7 +1268,7 @@ player_remove_contract(Player *pl) if(debug < 50) user_event_add(user_from_team(pl->team), EVENT_TYPE_PLAYER_LEFT, -1, -1, NULL, pl->name, NULL); - player_remove_from_team(pl->team, player_id_index(pl->team, pl->id)); + player_remove_from_team(pl->team, player_id_index(pl->team, pl->id, TRUE)); } /** Remove a player from a team. @@ -1347,7 +1350,7 @@ player_replace_by_new(Player *pl, gboolean free_player) #endif Team *tm = pl->team; - gint idx = player_id_index(tm, pl->id); + gint idx = player_id_index(tm, pl->id, TRUE); Player new = player_new(tm, team_get_average_talent(tm), FALSE); new.name = name_get(pl->team->names_file); diff --git a/src/player.h b/src/player.h index 5bc3b80a..0c6a031f 100644 --- a/src/player.h +++ b/src/player.h @@ -101,7 +101,7 @@ gboolean player_substitution_good_structure(gint old_structure, gint old_pos, gint player_pos); gint -player_id_index(const Team *tm, gint player_id); +player_id_index(const Team *tm, gint player_id, gboolean stopWhenPlayerNotFound); gfloat player_get_game_skill(const Player *pl, gboolean skill, gboolean special); diff --git a/src/strategy.c b/src/strategy.c index 06fbeed1..7fb612ea 100644 --- a/src/strategy.c +++ b/src/strategy.c @@ -654,8 +654,8 @@ strategy_live_game_apply_action(LiveGame *match, gint team_idx, if(sub_in_id > 0 && sub_out_id > 0) { - player_swap(tm, player_id_index(tm, sub_out_id), - tm, player_id_index(tm, sub_in_id)); + player_swap(tm, player_id_index(tm, sub_out_id, TRUE), + tm, player_id_index(tm, sub_in_id, TRUE)); team_change_structure(tm, team_find_appropriate_structure(tm)); team_rearrange(tm); diff --git a/src/user.c b/src/user.c index 5d93b891..4f963694 100644 --- a/src/user.c +++ b/src/user.c @@ -634,7 +634,7 @@ user_event_show_next(void) player_replace_by_new(player_of_id_team(event->user->tm, event->value1), TRUE); } else - player_remove_from_team(event->user->tm, player_id_index(event->user->tm, event->value1)); + player_remove_from_team(event->user->tm, player_id_index(event->user->tm, event->value1, TRUE)); treeview_show_user_player_list(); game_gui_show_warning(buf,NULL); break; @@ -1340,12 +1340,18 @@ restore_default_team(User *user) printf("restore_player_order\n"); #endif - gint i, player1, player2; + gint i, player1, player2, player1_index, player2_index; for (i=0;itm->players->len; i++){ player1 = g_array_index(user->default_team, gint, i); player2 = g_array_index(user->tm->players, Player, i).id; if (player1 != player2) { - player_swap(user->tm,player_id_index(user->tm,player1), user->tm, player_id_index(user->tm,player2)); + // We don't want to stop because a player has been romoved from your team + player1_index = player_id_index(user->tm,player1, FALSE); + player2_index = player_id_index(user->tm,player2, FALSE); + if (player1_index==-1 || player2_index==-1) { + return; + } + player_swap(user->tm,player1_index, user->tm, player2_index); } } team_change_structure(user->tm, user->default_structure);