News:

Use the "Forum Search"
It may help you to find anything in the forum ;).

[Script API] Support for start_pos, is_drag_tool, is_ctrl and is_shift pressed

Started by Yona-TYT, October 17, 2024, 01:49:24 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Yona-TYT

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  

prissi

As it just adds to the API, it should have no impact on the game state. I think we can include this.

Yona-TYT

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.

Yona-TYT

Is there any chance this will be implemented soon?, there are many things in the tutorial that I can improve with this.


Yona-TYT

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.




ceeac

The command_x script class already has a get_flags method which can be deprecated now, I think.

prissi

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 ...

Yona-TYT

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