From 51e1d6f73730fe9561fe090b85f25d3974edbf84 Mon Sep 17 00:00:00 2001 From: AdamPlenty <58278560+AdamPlenty@users.noreply.github.com> Date: Wed, 27 Nov 2024 04:52:34 +0000 Subject: [PATCH 1/2] player.colour console command Resolves #3725 --- src/console_cmd.c | 18 +++++++++++ src/lvl_script_commands.c | 67 ++------------------------------------- src/packets.c | 5 +++ src/packets.h | 1 + src/player_utils.c | 54 +++++++++++++++++++++++++++++++ src/player_utils.h | 2 ++ 6 files changed, 82 insertions(+), 65 deletions(-) diff --git a/src/console_cmd.c b/src/console_cmd.c index 9fa6c99004..ca04e8f98e 100644 --- a/src/console_cmd.c +++ b/src/console_cmd.c @@ -1852,6 +1852,22 @@ TbBool cmd_speech_test(PlayerNumber plyr_idx, char * args) return true; } +TbBool cmd_player_colour(PlayerNumber plyr_idx, char * args) +{ + char * pr2str = strsep(&args, " "); + PlayerNumber id = get_player_number_for_command(pr2str); + char * pr3str = strsep(&args, " "); + char colour_idx = get_rid(cmpgn_human_player_options, pr3str); + if (colour_idx > -1) + { + struct PlayerInfo * player = get_player(plyr_idx); + set_players_packet_action(player, PckA_SetPlayerColour, id, colour_idx, 0, 0); + process_players_global_packet_action(plyr_idx); // Dirty hack + return true; + } + return false; +} + TbBool cmd_exec(PlayerNumber plyr_idx, char * args) { struct ConsoleCommand { @@ -1951,6 +1967,8 @@ TbBool cmd_exec(PlayerNumber plyr_idx, char * args) { "herogate.zoomto", cmd_zoom_to_hero_gate }, { "sound.test", cmd_sound_test }, { "speech.test", cmd_speech_test }, + { "player.color", cmd_player_colour}, + { "player.colour", cmd_player_colour}, }; SYNCDBG(2, "Command %d: %s",(int)plyr_idx, args); const char * command = strsep(&args, " "); diff --git a/src/lvl_script_commands.c b/src/lvl_script_commands.c index 33f966978d..f26f35748f 100644 --- a/src/lvl_script_commands.c +++ b/src/lvl_script_commands.c @@ -6006,7 +6006,6 @@ static void set_power_configuration_process(struct ScriptContext *context) static void set_player_color_check(const struct ScriptLine *scline) { ALLOCATE_SCRIPT_VALUE(scline->command, scline->np[0]); - long color_idx = get_rid(cmpgn_human_player_options, scline->tp[1]); if (color_idx == -1) { @@ -6020,75 +6019,13 @@ static void set_player_color_check(const struct ScriptLine *scline) return; } } - value->shorts[0] = color_idx; + value->bytes[0] = (unsigned char)color_idx; PROCESS_SCRIPT_VALUE(scline->command); } static void set_player_color_process(struct ScriptContext *context) { - long color_idx = context->value->shorts[0]; - struct Dungeon* dungeon; - - for (int plyr_idx = context->plr_start; plyr_idx < context->plr_end; plyr_idx++) - { - dungeon = get_dungeon(plyr_idx); - - if(dungeon->color_idx == color_idx) - { - continue; - } - - dungeon->color_idx = color_idx; - - update_panel_color_player_color(plyr_idx,color_idx); - - for (MapSlabCoord slb_y=0; slb_y < gameadd.map_tiles_y; slb_y++) - { - for (MapSlabCoord slb_x=0; slb_x < gameadd.map_tiles_x; slb_x++) - { - struct SlabMap* slb = get_slabmap_block(slb_x,slb_y); - if (slabmap_owner(slb) == plyr_idx) - { - redraw_slab_map_elements(slb_x,slb_y); - } - - } - } - - const struct StructureList *slist; - slist = get_list_for_thing_class(TCls_Object); - int k = 0; - unsigned long i = slist->index; - while (i > 0) - { - struct Thing *thing; - thing = thing_get(i); - TRACE_THING(thing); - if (thing_is_invalid(thing)) { - ERRORLOG("Jump to invalid thing detected"); - break; - } - i = thing->next_of_class; - // Per-thing code - - if (thing->owner == plyr_idx) - { - ThingModel base_model = get_coloured_object_base_model(thing->model); - if(base_model != 0) - { - create_coloured_object(&thing->mappos, plyr_idx, thing->parent_idx,base_model); - delete_thing_structure(thing, 0); - } - } - // Per-thing code ends - k++; - if (k > slist->count) - { - ERRORLOG("Infinite loop detected when sweeping things list"); - break; - } - } - } + set_player_colour(context->player_idx, context->value->bytes[0]); } static void set_game_rule_check(const struct ScriptLine* scline) diff --git a/src/packets.c b/src/packets.c index 4a6766ccae..057f4bee6b 100644 --- a/src/packets.c +++ b/src/packets.c @@ -1052,6 +1052,11 @@ TbBool process_players_global_packet_action(PlayerNumber plyr_idx) query_creature(player, pckt->actn_par1, pckt->actn_par2, pckt->actn_par3); return false; } + case PckA_SetPlayerColour: + { + set_player_colour(pckt->actn_par1, pckt->actn_par2); + return false; + } default: return process_players_global_cheats_packet_action(plyr_idx, pckt); } diff --git a/src/packets.h b/src/packets.h index a036859723..112ddca159 100644 --- a/src/packets.h +++ b/src/packets.h @@ -182,6 +182,7 @@ enum TbPacketAction { PckA_SetRoomspaceDragPaint, PckA_PlyrQueryCreature, PckA_CheatGiveDoorTrap, + PckA_SetPlayerColour, }; /** Packet flags for non-action player operation. */ diff --git a/src/player_utils.c b/src/player_utils.c index bda1ba4aa6..e3639b1c2a 100644 --- a/src/player_utils.c +++ b/src/player_utils.c @@ -41,6 +41,7 @@ #include "gui_soundmsgs.h" #include "gui_frontmenu.h" #include "config_settings.h" +#include "config_spritecolors.h" #include "config_terrain.h" #include "map_blocks.h" #include "map_utils.h" @@ -1221,6 +1222,7 @@ void compute_and_update_player_payday_total(PlayerNumber plyr_idx) struct Dungeon* dungeon = get_players_num_dungeon(plyr_idx); dungeon->creatures_total_pay = compute_player_payday_total(dungeon); } + void compute_and_update_player_backpay_total(PlayerNumber plyr_idx) { SYNCDBG(15, "Starting for player %d", (int)plyr_idx); @@ -1228,4 +1230,56 @@ void compute_and_update_player_backpay_total(PlayerNumber plyr_idx) dungeon->creatures_total_backpay = compute_player_payday_total(dungeon); } +void set_player_colour(PlayerNumber plyr_idx, unsigned char colour_idx) +{ + struct Dungeon* dungeon = get_dungeon(plyr_idx); + if (dungeon->color_idx != colour_idx) + { + dungeon->color_idx = colour_idx; + update_panel_color_player_color(plyr_idx,colour_idx); + for (MapSlabCoord slb_y=0; slb_y < gameadd.map_tiles_y; slb_y++) + { + for (MapSlabCoord slb_x=0; slb_x < gameadd.map_tiles_x; slb_x++) + { + struct SlabMap* slb = get_slabmap_block(slb_x,slb_y); + if (slabmap_owner(slb) == plyr_idx) + { + redraw_slab_map_elements(slb_x,slb_y); + } + + } + } + const struct StructureList *slist = get_list_for_thing_class(TCls_Object); + int k = 0; + unsigned long i = slist->index; + while (i > 0) + { + struct Thing *thing = thing_get(i); + TRACE_THING(thing); + if (thing_is_invalid(thing)) { + ERRORLOG("Jump to invalid thing detected"); + break; + } + i = thing->next_of_class; + // Per-thing code + if (thing->owner == plyr_idx) + { + ThingModel base_model = get_coloured_object_base_model(thing->model); + if(base_model != 0) + { + create_coloured_object(&thing->mappos, plyr_idx, thing->parent_idx,base_model); + delete_thing_structure(thing, 0); + } + } + // Per-thing code ends + k++; + if (k > slist->count) + { + ERRORLOG("Infinite loop detected when sweeping things list"); + break; + } + } + } +} + /******************************************************************************/ diff --git a/src/player_utils.h b/src/player_utils.h index 70df17b3e3..144f7e6194 100644 --- a/src/player_utils.h +++ b/src/player_utils.h @@ -64,6 +64,8 @@ void init_players_local_game(void); void init_keeper_map_exploration_by_terrain(struct PlayerInfo *player); void init_keeper_map_exploration_by_creatures(struct PlayerInfo *player); void process_players(void); + +void set_player_colour(PlayerNumber plyr_idx, unsigned char colour_idx); /******************************************************************************/ #ifdef __cplusplus } From 6fd50459b970371b8f5caee99df67cc8f26b9168 Mon Sep 17 00:00:00 2001 From: AdamPlenty <58278560+AdamPlenty@users.noreply.github.com> Date: Wed, 27 Nov 2024 05:17:56 +0000 Subject: [PATCH 2/2] Removed dirty hack --- src/console_cmd.c | 1 - src/packets.c | 63 +++++++++++++++++++++++++++++------------------ src/packets.h | 1 + 3 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/console_cmd.c b/src/console_cmd.c index ca04e8f98e..c3d9975d7c 100644 --- a/src/console_cmd.c +++ b/src/console_cmd.c @@ -1862,7 +1862,6 @@ TbBool cmd_player_colour(PlayerNumber plyr_idx, char * args) { struct PlayerInfo * player = get_player(plyr_idx); set_players_packet_action(player, PckA_SetPlayerColour, id, colour_idx, 0, 0); - process_players_global_packet_action(plyr_idx); // Dirty hack return true; } return false; diff --git a/src/packets.c b/src/packets.c index 057f4bee6b..7f67f03ddd 100644 --- a/src/packets.c +++ b/src/packets.c @@ -1052,16 +1052,28 @@ TbBool process_players_global_packet_action(PlayerNumber plyr_idx) query_creature(player, pckt->actn_par1, pckt->actn_par2, pckt->actn_par3); return false; } - case PckA_SetPlayerColour: - { - set_player_colour(pckt->actn_par1, pckt->actn_par2); - return false; - } default: return process_players_global_cheats_packet_action(plyr_idx, pckt); } } +TbBool process_players_general_packet_action(PlayerNumber plyr_idx) +{ + struct PlayerInfo* player = get_player(plyr_idx); + struct Packet* pckt = get_packet_direct(player->packet_num); + SYNCDBG(6,"Processing player %d action %d",(int)plyr_idx,(int)pckt->action); + switch (pckt->action) + { + case PckA_SetPlayerColour: + { + set_player_colour(pckt->actn_par1, pckt->actn_par2); + return true; + } + default: + return false; + } +} + void process_players_map_packet_control(long plyr_idx) { SYNCDBG(6,"Starting"); @@ -1107,26 +1119,29 @@ void process_players_packet(long plyr_idx) // Different changes to the game are possible for different views. // For each there can be a control change (which is view change or mouse event not translated to action), // and action perform (which does specific action set in packet). - switch (player->view_type) + if (!process_players_general_packet_action(plyr_idx)) { - case PVT_DungeonTop: - process_players_dungeon_control_packet_control(plyr_idx); - process_players_dungeon_control_packet_action(plyr_idx); - break; - case PVT_CreatureContrl: - process_players_creature_control_packet_control(plyr_idx); - process_players_creature_control_packet_action(plyr_idx); - break; - case PVT_CreaturePasngr: - //process_players_creature_passenger_packet_control(plyr_idx); -- there are no control changes in passenger mode - process_players_creature_passenger_packet_action(plyr_idx); - break; - case PVT_MapScreen: - process_players_map_packet_control(plyr_idx); - //process_players_map_packet_action(plyr_idx); -- there are no actions to perform from map screen - break; - default: - break; + switch (player->view_type) + { + case PVT_DungeonTop: + process_players_dungeon_control_packet_control(plyr_idx); + process_players_dungeon_control_packet_action(plyr_idx); + break; + case PVT_CreatureContrl: + process_players_creature_control_packet_control(plyr_idx); + process_players_creature_control_packet_action(plyr_idx); + break; + case PVT_CreaturePasngr: + //process_players_creature_passenger_packet_control(plyr_idx); -- there are no control changes in passenger mode + process_players_creature_passenger_packet_action(plyr_idx); + break; + case PVT_MapScreen: + process_players_map_packet_control(plyr_idx); + //process_players_map_packet_action(plyr_idx); -- there are no actions to perform from map screen + break; + default: + break; + } } } SYNCDBG(8,"Finished"); diff --git a/src/packets.h b/src/packets.h index 112ddca159..73e5c403d9 100644 --- a/src/packets.h +++ b/src/packets.h @@ -308,6 +308,7 @@ TbBool process_players_dungeon_control_packet_action(long idx); void process_players_creature_control_packet_control(long idx); void process_players_creature_passenger_packet_action(long idx); void process_players_creature_control_packet_action(long idx); +TbBool process_players_general_packet_action(PlayerNumber plyr_idx); void process_frontend_packets(void); void process_map_packet_clicks(long idx); void process_pause_packet(long a1, long a2);