[how-to] Enhancing OMSI cars' lighting behaviour

Das Forum befindet sich im reduzierten Betrieb. Die Addon- und Supportforen bleiben weiterhin verfügbar.
Bitte beachte, dass OMSI nicht mehr weiterentwickelt wird. Ein Teil der Entwickler widmet sich inzwischen der Entwicklung eines neuen Simulators. Weitere Informationen zum LOTUS-Simulator findest Du hier.
  • Note: This post is only retained for the initial incentives behind this mod. While the installation instructions still apply, the script replacements themselves are obsolete, and the ones given in post #2 should be used instead.


    Introduction


    One of the subtle annoyances which I have had with OMSI all along, is that all of a map's street lights go on or off simultaneously, if it gets darker or brighter than a threshold, respectively. Which is just unrealistic, since even in recent years -let alone in the 80's and 90's- the street lamps found in most EU cities are still controlled by plain timer switches (that are often not even properly synchronized) rather than modern light sensors. Similarly unrealistic is the fact that all AI-controlled cars and buses switch their headlights on or off at the same millisecond; whereas in reality -at least for the time being and until real-world AI-driven cars become the norm- human drivers exhibit quite different behaviour in that regard: some (typically younger ones) just leave their vehicle's headlights turned on all the time; some switch them on when a single cloud appears on the horizon, and some (typically older ones) only use them at night or when it actually gets really dark.


    Of those two minor issues, unfortunately, no workaround that is both generic (as in that it is applicable to any map) and easy to employ is currently available for us mortal "end-users" (as in not being map or object authors) for the first of them. The second issue, on the other hand, can be combated in a multitude of ways, one of which I am going to present in this post.


    Solution


    So, the intention is to add some "perceived randomness" into the way AI-controlled vehicles handle their lighting-related functionality, particularly with regard to (de-)activating their headlights. Fortunately, like buses, AI-driven OMSI cars' and trucks' behavior is to a great extent scriptable. All that basically needs to be done, is to override the default condition evaluated by the vehicles' scripts in order to determine whether it is time to turn the lights on or off.


    In the remainder of this post I am going to provide the proposed script and configuration file modifications, as well as installation instructions. After that I will briefly explain how the modification works. But before I get there I would first like to emphasize that what I am presenting here is a proof-of-concept modification, i.e. one that is incomplete, poorly tested and generally sub-optimal. What this means is that if you decide to use it, you will absolutely be doing so at own risk, and are certainly encouraged to take backups before proceeding with the installation. That is essentially the reason why I am posting this as a tutorial, rather than in the form of a "supported" modification.


    Installation


    The modification is applicable to most cars and trucks with reduced scripts; that is, vehicles which are solely destined for use by OMSI's AI. It does, obviously, not apply to buses (even "pure" AI versions of them, as their scripts generally employ a more complex structure), trains, or whatever other special kind of vehicle you might have lying around.


    The targeted vehicles fall into three broad categories: a) those directly relying upon the <OMSI>\Scripts\AI_Cars\main_AI_XXX.osc and <OMSI>\Scripts\AI_Cars\lights_varlist.txt (and which constitute the vast majority); those which declare their own main script and/or variable list; and emergency vehicles. Out of all of OMSI's "standard" vehicles, only <OMSI>\Vehicles\Trabant\Trabant(_DDR).ovh is of the second kind.


    In order to achieve a tangible result from this modification, you will have to modify all of the files mentioned below pertaining to all of the AI-controlled vehicles used on a particular map. Follow the instructions applicable to the vehicle you are modifying, depending on the category it belongs to (based on the distinction established above).


    1. Modification installation for vehicles with reduced / minimal scripts (category #1)


    1.1. Open <OMSI>\Scripts\AI_Cars\main_AI.osc or <OMSI>\Scripts\AI_Cars\main_AI_lkw.osc (for trucks).
    1.1.1. Insert the following into the {init} block:

    1.1.2. In the {frame} block, find:

    Insert the following below the aforementioned section:

    1.2. Open <OMSI>\Scripts\AI_Cars\lights_varlist.txt and add the following to it:

    1.3. Create and open <OMSI>\Scripts\AI_Cars\lights_constfile.txt and paste the following into it:

    1.4. Find and open the XXX.ovh or XXX.bus file of the vehicle you are modifying. Include the path to the newly-created file (step 1.3.) in the vehicle's list of constant file declarations. As an example, consider the Citroen BX (<OMSI>\Vehicles\Citr_BX\BX.ovh), whose constant file declarations section is by default as follows:

    You would in the particular case modify the above section like so:

    1.5. Find and open the XXXmodelXXX.cfg of the vehicle you are modifying. In the file, search and replace all occurrences of AI_Light with ai_ovh_lights_on. Such instances are usually (but not necessarily) found within [light_enh_2] declaration blocks.


    2. Modification installation for (semi-)self-sufficient vehicles (category #2)


    2.1. Repeat steps 1.1. and/or 1.2. from above, editing the corresponding files, respectively, whichever those might be. You will find the path(s) listed within the vehicle's XXX.ovh or XXX.bus file.
    2.2. Repeat steps 1.3. to 1.5. as above.


    3. Modification installation for emergency vehicles (category #3)


    3.1. Open <OMSI>\Scripts\AI_Cars\main_AI_police.osc if the common script is being reused, or the vehicle-specific main script otherwise. Add the following to its {init} block:

    3.2. If the vehicle declares its own variable declaration file (XXXvarlistXXX.txt), open it and insert the name of the newly-added variable into it:

    3.3. Repeat steps 1.4. and 1.5. as above.


    Discussion


    The additions to the main script serve to carry out two simple tasks.


    The first -performed by the section added to the {init} block- is to assign a so-called "lighting profile" to the vehicle, upon its creation. Based on the profile, the vehicle is configured to either unconditionally have its headlights on 24/7, or otherwise switch them on and of with a varying degree of "strictness", depending on the environment (brightness / sun elevation angle). Furthermore, if the vehicle is to conditionally use its headlights, two additional attributes are defined for it: a "darkness" and a "lightness" level of "tolerance", which represent the maximum time that must elapse before the vehicle will adjust its headlights' state (off -> on, on -> off, respectively). Some limited control over the choosing of the profile and assignment of the thresholds is provided via the constants file.


    The second task is to actually enforce the profile, and is accomplished via the code section having been introduced into the {frame} block of the script. There, if mandated by the profile, the headlights are either switched on once (and for good), or the tolerance thresholds are evaluated and, if having been exceeded, the headlights' state is negated.


    Emergency vehicles, finally, always have their headlights switched on, as they usually do in real life too.


    Further implementation-related details can be found in the comments included in the script and constants files.


    Miscellaneous / F.A.Q.


    Q: Newly-spawned vehicles always instantly seem to "adapt" to the environment
    A: They indeed do; that is a known limitation. The problem is that OMSI's scripting engine does not support storage of arbitrary "static" (inter-object) state; the script "lives" as long as the corresponding vehicle is spawned, so it simply cannot know what the weather conditions were like prior to its initialization. If you'd like to see how cars / trucks truly adapt, rather than driving around, just block a street with your bus and observe the traffic jam for a few minutes.

  • It's been a while and I thought I'd revisit this, adding a second, more "production-ready" iteration of the script.



    Demo


    Note that for demonstration purposes the ai_lighting_(darkness|brightness)_tolerance constants were set to 1, so as to achieve fast lighting transitions; the actual default values are much higher -- 30 and 300 seconds respectively.


    Installation


    The instructions in the OP still apply (categories 1 and 2 -- 3 is irrelevant since emergency vehicles can simply leave lights on ad infinitum and are supposed to always use indicators properly (the opposite would be a tad too ironic)); the updated content is attached below.


    Notes:

    • Multi-segment vehicles, such as trucks with trailers, are currently not properly supported. Proper integration requires the script-sharing function, as used by articulated buses, but my efforts at converting such vehicles to employ it have thus far been totally unsuccessful, only leading to shitloads of your typical OMSI Zugriffsverletzung and non-functional, occasionally completely absent trailer axles / wheels, without relevant debugging context being provided even in -debug (ironically the script itself works just fine though). Therefore you can currently only integrate the script with the "main" segment of such vehicles, if you're okay with the remainder not using lights and indicators... or integrate the script with all segments individually, at the cost of segments potentially contradicting each other in terms of light and indicator state. If you have an idea of what I might be doing wrong, I'm all ears.
    • To quickly evaluate the script without having to modify tons of vehicles beforehand, install it for just one vehicle first, then change a map's ailists.cfg to use just this one vehicle for all unscheduled traffic, then take a look.


    - - -


    Einmal editiert, zuletzt von Unorthodox Paradox () aus folgendem Grund: Added a demo; modified initialization to use NrSpecRandom instead of random, for (hopefully) better profile distribution; tweaked indicator profile #5 down a bit.