The International Simutrans Forum

Development => Scripting Scenarios and AI => Topic started by: THLeaderH on August 18, 2021, 03:18:46 PM

Title: is_ctrl_pressed() for scripting tool
Post by: THLeaderH on August 18, 2021, 03:18:46 PM
Some construction tools (for example, way construction tool) change their behavior by is_ctrl_pressed() status. Ctrl key is a very easy way to alter the tool behavior, so it should be useful too for scripting tools. Shift key pressing status should be also available for scripting tools.

I think world class should have is_ctrl_pressed() and is_shift_pressed() functions, but implementing a dedicated util class for these functions also sounds to be reasonable. How do you think?
Title: Re: is_ctrl_pressed() for scripting tool
Post by: Yona-TYT on August 18, 2021, 03:59:08 PM
I understand that "tool.set_flags (nr)" is used to change the behavior of a tool, however there is no implementation to know which key is pressed.  :-[
Edit.
Quote from: THLeaderH on August 18, 2021, 03:18:46 PMI think world class should have is_ctrl_pressed() and is_shift_pressed() functions, but implementing a dedicated util class for these functions also sounds to be reasonable. How do you think?

I think the correct thing would be a special class to process keyboard events, so it would not be limited to ctrl and shift.  8) 
Title: Re: is_ctrl_pressed() for scripting tool
Post by: prissi on August 19, 2021, 02:41:40 AM
Tool do not process keypresses. The status of of teh cntrol/shift is just recorded at the time of the keypress and sent to the command queue when calling the tool.
Title: Re: is_ctrl_pressed() for scripting tool
Post by: Dwachs on August 23, 2021, 05:47:01 PM
There are set_flags/get_flags methods in the command_x class. They can set the ctrl/shift state.
Title: Re: is_ctrl_pressed() for scripting tool
Post by: THLeaderH on August 24, 2021, 10:05:19 AM
Yes we can set ctrl/shift flags to the tool we call in the script, but we still need a way to get whether ctrl/shift key is pressed for the script.
Title: Re: is_ctrl_pressed() for scripting tool
Post by: Dwachs on August 24, 2021, 03:17:08 PM
Where would this be useful? In is_work_allowed_here ??
Title: Re: is_ctrl_pressed() for scripting tool
Post by: Yona-TYT on August 24, 2021, 04:56:54 PM
It would be useful to me in "is_work_allowed_here", for example when building a tunnel entrance, if the player does not press Ctrl show a message "You must press the [Ctrl] key to build the tunnel entrance".
Title: Re: is_ctrl_pressed() for scripting tool
Post by: THLeaderH on August 29, 2021, 01:50:12 AM
What I want to do with ctrl key is changing the behavior of the scripted tool. For example, in the two_click scripted tool which removes all objects in the selected area, I can make this tool remove the objects which are in different heights when ctrl key is pressed. If ctrl key is not pressed, the scripted tool is supposed to remove the objects only in the selected height.
Title: Re: is_ctrl_pressed() for scripting tool
Post by: Yona-TYT on August 29, 2021, 02:48:06 PM
I'll experiment to see how far I can go (remember that I barely understand c ++ :P).
Title: Re: is_ctrl_pressed() for scripting tool
Post by: Dwachs on September 03, 2021, 01:08:43 PM
One way to implement this for scripted tools is: add another parameter to tab file that indicates whether work & friends take an additional flags parameter:

// old
needs_flags = 0
function work(player, pos) { .. }
// with flags
needs_flags = 1
function work(player, pos, flags) { .. }

This seems to me the best solution without breaking existing scripts.
Title: Re: is_ctrl_pressed() for scripting tool
Post by: Dwachs on September 04, 2021, 09:40:13 AM
I submitted a change as r10077. The functions work, do_work, mark_tiles can have an additional parameter to receive the states of the keys. I added some basic compatibility script such that existing scripts do not break.
Title: Re: is_ctrl_pressed() for scripting tool
Post by: Yona-TYT on September 04, 2021, 02:27:07 PM
Very nice!. :D
Title: Re: is_ctrl_pressed() for scripting tool
Post by: THLeaderH on September 05, 2021, 09:44:44 AM
Fanstastic. Thank you!
Title: Re: is_ctrl_pressed() for scripting tool
Post by: Yona-TYT on September 05, 2021, 03:32:25 PM

Quote from: Dwachs on September 04, 2021, 09:40:13 AM
I submitted a change as r10077. The functions work, do_work, mark_tiles can have an additional parameter to receive the states of the keys. I added some basic compatibility script such that existing scripts do not break.
I wonder if it can be used on scenario.