Logic Signals patch

Forum for technical discussions regarding development. If you have a general suggestion, problem or comment, please use one of the other forums.

Moderator: OpenTTD Developers

maddy_
Engineer
Engineer
Posts: 20
Joined: 24 Jan 2011 19:08

Logic Signals patch

Post by maddy_ » 21 Aug 2013 18:03

Hi guys, this is my first OpenTTD patch. It is similar to the Programmable Signals patch.
  • Uses the one-way PBS signal sprite for now.
  • You can link signals together, and evaluate them using boolean AND, OR, XOR and NOT operators.
  • Each logic signal can have multiple inputs, but each signal can only have one outgoing link.
  • For now, they are one-way signals, but I think they would work as two-way also.
  • For now, they do not interact with pre-, combo- or exit-signals. I think I will add that also.
  • The plan is to add visual representation for the signal links.
logic_signals.png
logic_signals.png (10.27 KiB) Viewed 10753 times
The first setting determines which color this signal is by default, e.g. when it is not triggered by the inputs. The second setting determines which color acts as logical 1 in the input signals. The third is the self-explanatory operator to use when evaluating the signal value. The NOT-operator just reverses the state of the first linked signal (so don't bother linking more than 1 to it).

The button to program a signal is on the main rail toolbar for now. It is not ideal, but worked out better for me than having it in the signals GUI directly.

Please provide feedback, bug reports, and the like.

Update 2019: new version released, see further down the thread.
Last edited by maddy_ on 09 Mar 2019 02:05, edited 9 times in total.

michael blunck
Tycoon
Tycoon
Posts: 5269
Joined: 27 Apr 2005 07:09
Contact:

Re: Logic Signals patch

Post by michael blunck » 21 Aug 2013 18:13

In case you don´t know, you might take a look into TTDPatch´s programmable and restricted signals, to get an idea what features had already been implemented there.

regards
Michael
Image

Eddi
Tycoon
Tycoon
Posts: 7371
Joined: 17 Jan 2007 00:14

Re: Logic Signals patch

Post by Eddi » 21 Aug 2013 19:39

maddy_ wrote:
  • Uses the one-way PBS signal sprite for now.
remind me again why you didn't use one of the 2 currently unused signal graphics?


anyway, perspecively, i'd like to see these features:
  1. Signal Templates:
    • remove all the built-in hardcoded signals, and replace them by customizable signal types
    • you can also add as many signal templates as you like
    • for each signal template you can choose one of the (8) builtin sprites. on the map, only the visual representation is stored (for drawing), the behaviour of the signal is stored off-map (for train movement, pathfinder, etc)
    • the behaviour of the template can be programmed like any individual signal
    • the input cannot be a specific signal, only "all signals of template <Y>" (possibly multiple of these)
    • the default templates will represent the current signal types, but you can add/remove/change their behaviour as you like
    • default template properties:
      1. block signal: acts as block signal, does not depend on any input signals, defaults to green
      2. entry signal: acts as block signal, depends on all signals of <exit signal> and <combo signal>, operator: and, inputs red, output red. defaults to greeen
      3. exit signal: acts as block signal, does not depend on any input signals, defaults to green
      4. combo signal: acts as block signal, depends on all signals of <exit signal> and <combo signal>, operator: and, inputs red, output red. defaults to greeen
      5. two-way path signal: acts as path signal, defaults to green (note: see below for behaviour details), can be passed from behind
      6. one-way path signal: acts as path signal, defaults to green, cannot be passed from behind
  2. Programmable Path Signals:
    this is typically a very neglected feature of this kind of patches, but it's the only feature that will even begin to make me try out the patch.
    • programmable signals get a switch "acts as block signal" and "acts as path signal"
    • block signals show red when a train is in the block, or the programmable signal state (red or green) when no train is in the block. (that means, you cannot program the signal to crash trains, safety always overrides the programmed output
    • path signals show red when no train is near, and green when a train reserved the path. the programmable signal state (red or green) is not shown, but used to determine whether a train can reserve a path through the signal. the reservation may still fail later on because of the track not being free.
    • input for a path signal is not a signal state, but the reservation state of a trackbit (or tile for simplicity)
  3. Restricted Signals:
    the "output" of this is not a signal state, but a pathfinder penalty (either a number or "dead end"). possible input values to be determined (signal states, train properties). the restriction is not checked globally, but only for the first <n> signals and the last signal, that are currently already checked for pathfinding (default: n=10)
  4. Timed Signals:
    signals get a "timetable", and a "start date" (for synchronisation). for every given duration, one method of operation is chosen for the signal output
You might not exactly be interested in Ferion, but if you are, have fun :)

maddy_
Engineer
Engineer
Posts: 20
Joined: 24 Jan 2011 19:08

Re: Logic Signals patch

Post by maddy_ » 22 Aug 2013 14:38

I did not know there was any unused signal graphics, or I might have used them.

As for the other comments, my goal is not to include all possible features and choices here. I was rather trying to do a sort of minimalistic approach, with somewhat clean and simple code, which handles the most common cases such as priority signals. If you want something else, go ahead and create a fork of my patch and add features, but that is not my interest right now.

The idea here is that a single logic signal is somewhat simple to understand and use, but then you can create fairly complex stuff by linking them together if you wish to.

User avatar
adf88
Chief Executive
Chief Executive
Posts: 644
Joined: 14 Jan 2008 15:51
Location: PL

Re: Logic Signals patch

Post by adf88 » 22 Aug 2013 17:59

maddy_ wrote:but each signal can only have one outgoing link
This could be improved easily. Just forbid creating cycles.

Don't modify MSVC project files manually. List all new files in ./source.list and then use ./projects/generate.vbs to generate project files.
saveload/logic_signals_sl.cpp wrote:struct TempStorage ...
Try harder when naming things.
command.cpp wrote:DEF_CMD(CmdProgramLogicSignal, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_PROGRAM_LOGIC_SIGNAL
Perhaps this command should be of type CMDT_OTHER_MANAGEMENT to allow reprogramming signals while paused.
logic_signals_program.cpp wrote:for (SignalReference *sref = this->linked_signals.Begin(); sref != this->linked_signals.End(); sref++)
{
...
Coding style - remove newline before opening brace.
logic_signals.cpp wrote:try { return _signal_program_list.at(GetSignalReference(tile, track)); } catch (...) { return NULL; }
...
try { return _signal_link_list.at(GetSignalReference(tile, track)); } catch (...) { return NULL; }
Don't use exceptions to implement logic. It's not an exceptional situation when an item is not in the container. Use std::map::find instead.
logic_signals.h wrote:#define OPPOSITE_SIGNAL_STATE(state) ((state) == SIGNAL_STATE_RED ? SIGNAL_STATE_GREEN : SIGNAL_STATE_RED)
...
#define MAX_LOGIC_SIGNAL_RECURSIONS 5
Avoid macros. Use a static inline function and a static const variable instead.
logic_signals_cmd.cpp wrote:if (CheckTileOwnership(tile).Failed()) return_cmd_error(STR_ERROR_OWNED_BY);
It should be

Code: Select all

CommandCost ret = CheckTileOwnership(tile);
if (ret.Failed()) return ret; 
logic_signals_gui.cpp wrote:if (IsWidgetLowered(WID_PROGSIG_ADD_LINK))
SetObjectToPlaceWnd(SPR_CURSOR_TRANSMITTER, PAL_NONE, HT_RECT, this);
else
ResetObjectToPlace();
Coding style - surround if/else sections with braces.
logic_signal_program.cpp
logic_signals_cmd.cpp
logic_signals.cpp
logic_signals_gui.cpp
logic_signals.h
Perhaps you should use existing files to put the code in: signal_type.h, signal_func.h, signal.cpp, signal_gui.cpp, rail_cmd.cpp - they seem to be a good place for your code.
:] don't worry, be happy and checkout my patches

maddy_
Engineer
Engineer
Posts: 20
Joined: 24 Jan 2011 19:08

Re: Logic Signals patch

Post by maddy_ » 23 Aug 2013 14:09

Thank you adf88 for reviewing my code and providing feedback. I have released a new version which fixes all your points with regard to coding style.

krinn
Transport Coordinator
Transport Coordinator
Posts: 336
Joined: 29 Dec 2010 19:36

Re: Logic Signals patch

Post by krinn » 23 Aug 2013 14:51

I think adf88 spoke more about the general coding style rules to add braces after an if, not only for the example he gave.

Code: Select all

+			if (trigger_states > 0) { return OppositeSignalState(this->own_default_state); }
+			break;

Code: Select all

+	if (old_type == SIGTYPE_LOGIC) { DeleteSignalProgram(tile, track); }
+	if (new_type == SIGTYPE_LOGIC) { CreateSignalProgram(tile, track); }
...

User avatar
adf88
Chief Executive
Chief Executive
Posts: 644
Joined: 14 Jan 2008 15:51
Location: PL

Re: Logic Signals patch

Post by adf88 » 23 Aug 2013 16:35

I was talking about if-else statement. Single line if statement can be left without braces. See http://wiki.openttd.org/Coding_style#Control_flow for more details.
:] don't worry, be happy and checkout my patches

maddy_
Engineer
Engineer
Posts: 20
Joined: 24 Jan 2011 19:08

Re: Logic Signals patch

Post by maddy_ » 25 Aug 2013 05:46

So has any of you actually tried the patch in a game yet? Did the signals work as expected?

2457
Engineer
Engineer
Posts: 126
Joined: 06 Dec 2009 21:57

Re: Logic Signals patch

Post by 2457 » 25 Aug 2013 23:25

is there any chanse of not only linking the diff file, but rather a compiled version ?
I'd bet there are more than many people whom would be intrested, but lack skills to compile .diff from surce.

BTW, i absolutely love this idea. prehaps my prayers where not for nothing?
programmable signals to create prioritys and sutch, is what i'm personaly missing from the game,
so i'm absolutely excited to try Your patch!
The Prophet -thx Pikka-

User avatar
Vaulter
Traffic Manager
Traffic Manager
Posts: 185
Joined: 21 Dec 2004 05:35
Skype: andrey-zaharov
Location: St. Petersburg, Russia
Contact:

Re: Logic Signals patch

Post by Vaulter » 26 Aug 2013 00:52

2457 wrote:is there any chanse of not only linking the diff file, but rather a compiled version ?
I'd bet there are more than many people whom would be intrested, but lack skills to compile .diff from surce.

BTW, i absolutely love this idea. prehaps my prayers where not for nothing?
programmable signals to create prioritys and sutch, is what i'm personaly missing from the game,
so i'm absolutely excited to try Your patch!
there is programmable waypoints built in HardPack patchpack. See links for win32 x64 bundles in my signature

maddy_
Engineer
Engineer
Posts: 20
Joined: 24 Jan 2011 19:08

Re: Logic Signals patch

Post by maddy_ » 26 Aug 2013 06:20

2457 wrote:is there any chanse of not only linking the diff file, but rather a compiled version ?
I'd bet there are more than many people whom would be intrested, but lack skills to compile .diff from surce.
Good point. I will provide a compiled version today.

2457
Engineer
Engineer
Posts: 126
Joined: 06 Dec 2009 21:57

Re: Logic Signals patch

Post by 2457 » 26 Aug 2013 10:57

YEEY, thank You!

Can't wait to FINALY get rid of the utterly silly and needless logic trains FOR EVER :)
and prehaps others can enjoy then complex signalling without building -otherwise- not needed and useless tracks all around the map.
The Prophet -thx Pikka-

maddy_
Engineer
Engineer
Posts: 20
Joined: 24 Jan 2011 19:08

Re: Logic Signals patch

Post by maddy_ » 26 Aug 2013 17:53

Binary for windows is now available for download.

2457
Engineer
Engineer
Posts: 126
Joined: 06 Dec 2009 21:57

Re: Logic Signals patch

Post by 2457 » 26 Aug 2013 20:39

hmm...
bad news.

http://oi41.tinypic.com/npfk2t.jpg

on this image You can see what i set up allready, a simple priority.
it did work, but only 1 time. secund time the train passed the signals the programmable signal did not turn red.
If i edit the signal, and choose any other logical operator (AND or XOR, or NOT) and then switch back to OR, the thing works again for a nother loop.

other than that i found no problems yet.
if You need a savegame, just say.
The Prophet -thx Pikka-

maddy_
Engineer
Engineer
Posts: 20
Joined: 24 Jan 2011 19:08

Re: Logic Signals patch

Post by maddy_ » 27 Aug 2013 05:49

Yes, a savegame could be helpful. Did you use any newGRFs? What is the question mark icon at the top right corner of the signal programming window?

2457
Engineer
Engineer
Posts: 126
Joined: 06 Dec 2009 21:57

Re: Logic Signals patch

Post by 2457 » 27 Aug 2013 09:44

hmm i disabled all newGRF-s and it is working proeprly now.
the only grf that was loaded was Lithuanian transportation set.
The Prophet -thx Pikka-

User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: Logic Signals patch

Post by Zuu » 28 Aug 2013 13:55

maddy_ wrote:What is the question mark icon at the top right corner of the signal programming window?
That question mark is because the 2457 has an OpenGFX version without a sprite for the somewhat new window size button.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)

2457
Engineer
Engineer
Posts: 126
Joined: 06 Dec 2009 21:57

Re: Logic Signals patch

Post by 2457 » 28 Aug 2013 18:14

this is a marwell!!

I just managed to build a flip-flop type trafic splitter with no logic trains or what so ever, and without unused tracks,
that is -i must say- is sutch a relaxing thing!


oh, btw, it would be nice if the logic signal had a switch to toggle between <default behaviour> of <block>,<one way path> signal. It would allow to make very nice and "evil X" behaviour-less paralell tracks for games with brakedowns enabled.
The Prophet -thx Pikka-

2457
Engineer
Engineer
Posts: 126
Joined: 06 Dec 2009 21:57

Re: Logic Signals patch

Post by 2457 » 28 Aug 2013 20:47

Look!

I made a video too.
Probably -but not sure- i will build other things with You phenomenal patch, and upload them to Youtube.





Thank You for making this patch, again.
The Prophet -thx Pikka-

Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: No registered users and 5 guests