The International Simutrans Forum

Development => Patches & Projects => Incorporated Patches and Solved Bug Reports => Topic started by: Yona-TYT on October 17, 2024, 01:49:24 AM

Title: [Script API] Support for start_pos, is_drag_tool, is_ctrl and is_shift pressed
Post by: Yona-TYT on October 17, 2024, 01:49:24 AM
I did an experiment to implement all those methods that I have needed for the tutorial for years.
In my tests it worked very well.

        koord3d start_pos = koord3d::invalid;
        bool is_drag_tool = false;
        bool is_ctrl = false;
        bool is_shift = false;

        uint8 player_nr = player ? player->get_player_nr() : PLAYER_UNOWNED;
        if(player_nr != PLAYER_UNOWNED){
            if (two_click_tool_t *two_tool = dynamic_cast<two_click_tool_t*>(welt->get_tool(player_nr))) {
                start_pos = two_tool->get_start_pos();
                is_drag_tool = !two_tool->is_first_click();
                is_ctrl = two_tool->is_ctrl_pressed();
                is_shift = two_tool->is_shift_pressed();
            }
            else if (tool_t *tool = dynamic_cast<tool_t*>(welt->get_tool(player_nr))) {
                is_ctrl = tool->is_ctrl_pressed();
                is_shift = tool->is_shift_pressed();
            }
        }
        static plainstring msg;
        const char *err = script->call_function(script_vm_t::FORCE, "is_work_allowed_here", msg, (uint8)(player ? player->get_player_nr() : PLAYER_UNOWNED), tool_id, pos, script_api::mytool_data_t(start_pos, is_drag_tool, is_ctrl, is_shift));

    struct mytool_data_t
    {
        koord3d start_pos;
        bool is_drag_tool;
        bool is_ctrl;
        bool is_shift;

        mytool_data_t(koord3d pos, bool drag, bool ctrl, bool shift) :
            start_pos(pos), is_drag_tool(drag), is_ctrl(ctrl), is_shift(shift)
        {}
    };

It would be very useful to be able to implement this.  8)

ADD-Scenario-start_pos-is_drag_tool-is_ctrl-and-is_s.patch  
Title: Re: [Script API] Support for start_pos, is_drag_tool, is_ctrl and is_shift pressed
Post by: prissi on October 17, 2024, 07:53:36 AM
As it just adds to the API, it should have no impact on the game state. I think we can include this.
Title: Re: [Script API] Support for start_pos, is_drag_tool, is_ctrl and is_shift pressed
Post by: Yona-TYT on October 17, 2024, 11:49:51 AM
Quote from: prissi on October 17, 2024, 07:53:36 AMAs it just adds to the API, it should have no impact on the game state. I think we can include this.
I also wanted to get the system type, but the "tool _t / two_click_tool_t" class doesn't have them, although I don't think it's difficult to add them.
Title: Re: [Script API] Support for start_pos, is_drag_tool, is_ctrl and is_shift pressed
Post by: Yona-TYT on October 30, 2024, 10:06:18 PM
Is there any chance this will be implemented soon?, there are many things in the tutorial that I can improve with this.
Title: Re: [Script API] Support for start_pos, is_drag_tool, is_ctrl and is_shift pressed
Post by: prissi on November 02, 2024, 08:13:20 AM
Added in r11429
Title: Re: [Script API] Support for start_pos, is_drag_tool, is_ctrl and is_shift pressed
Post by: Yona-TYT on November 02, 2024, 09:00:13 AM
Quote from: prissi on November 02, 2024, 08:13:20 AMAdded in r11429
Wow, thank you so much! This is great. ;D ;D ;D ;D

Edit:

@Prissi,

I think you forgot this part:

        koord3d start_pos = pos;
        bool is_drag_tool = false;
        bool is_ctrl = false;
        bool is_shift = false;

        uint8 player_nr = player ? player->get_player_nr() : PLAYER_UNOWNED;
        if(player_nr != PLAYER_UNOWNED){
            if (two_click_tool_t *two_tool = dynamic_cast<two_click_tool_t*>(welt->get_tool(player_nr))) {
                start_pos = two_tool->get_start_pos();
                is_drag_tool = !two_tool->is_first_click();
                is_ctrl = two_tool->is_ctrl_pressed();
                is_shift = two_tool->is_shift_pressed();
            }
            else if (tool_t *tool = dynamic_cast<tool_t*>(welt->get_tool(player_nr))) {
                is_ctrl = tool->is_ctrl_pressed();
                is_shift = tool->is_shift_pressed();
            }
        }
        static plainstring msg;
        const char *err = script->call_function(script_vm_t::FORCE, "is_work_allowed_here", msg, (uint8)(player ? player->get_player_nr() : PLAYER_UNOWNED), tool_id, pos, script_api::mytool_data_t(start_pos, is_drag_tool, is_ctrl, is_shift));


By the way, I also think it's better to do "koord3d start_pos = pos;" so that there are no invalid coordinates.



Title: Re: [Script API] Support for start_pos, is_drag_tool, is_ctrl and is_shift pressed
Post by: ceeac on November 02, 2024, 09:30:42 AM
The command_x script class already has a get_flags method which can be deprecated now, I think.
Title: Re: [Script API] Support for start_pos, is_drag_tool, is_ctrl and is_shift pressed
Post by: prissi on November 02, 2024, 01:29:53 PM
Sorry, you had a patch file and I patched it. That code fragment has not indication where you want to paste it. Please provide a proper patch, that I can look at. Same for your cryptic remark. I am bad at reading minds ...
Title: Re: [Script API] Support for start_pos, is_drag_tool, is_ctrl and is_shift pressed
Post by: Yona-TYT on November 02, 2024, 03:03:10 PM
Quote from: prissi on November 02, 2024, 01:29:53 PMSorry, you had a patch file and I patched it. That code fragment has not indication where you want to paste it. Please provide a proper patch, that I can look at. Same for your cryptic remark. I am bad at reading minds ...

0001-Fix-Pacth.patch
Title: Re: [Script API] Support for start_pos, is_drag_tool, is_ctrl and is_shift pressed
Post by: prissi on November 03, 2024, 11:21:31 AM
submit in r11430
Title: Re: [Script API] Support for start_pos, is_drag_tool, is_ctrl and is_shift pressed
Post by: Yona-TYT on November 03, 2024, 11:25:59 AM
Quote from: prissi on November 03, 2024, 11:21:31 AMsubmit in r11430
Great, I'll get to work on it in a moment. :D