' ==========================================================================
' Script File for Conduent Ticket Stampers Control
' ==========================================================================
' Author: Adam Mathieson
'
' Description:
' This script is designed to control Conduent Ticket Stampers, using the 
' associated configuration file (const file) to define the stampers based on
' the indexes of the path points.
'
' Note:
' - The configuration file is crucial for the correct operation of the script
'   as it contains the path points necessary for stamper definitions.
' - Sound effects are only played by the first stamper.
' ==========================================================================
'
' Fichier de script pour le contrôle des valideurs de billets Conduent
' ==========================================================================
' Auteur : Adam Mathieson
'
' Description :
' Ce script est conçu pour contrôler les valideurs de billets Conduent, en
' utilisant le fichier de configuration associé (fichier const) pour définir
' les valideurs en fonction des index des points de chemin.
'
' Remarque :
' - Le fichier de configuration est essentiel pour le bon fonctionnement du 
'   script car il contient les points de chemin nécessaires pour la définition
'   des valideurs.
' - Les effets sonores sont uniquement joués par le premier valideur.
' ==========================================================================

{macro:stamper_init}
    (C.L.EMI)
    {if}
        "Valideur_conduent\TEC\" (S.$.stamper_root)
    {else}
        "Valideur_conduent\" (S.$.stamper_root)
    {endif}
    0 (S.L.stamper_display) (S.L.stamper_timer)
    -1 (S.L.stamper_stamp_time)
    "" (S.$.stamper_text_0) (S.$.stamper_text_1) (S.$.stamper_text_2) (S.$.stamper_text_3)

    (M.L.stamper_screen)
    0 s0 (M.L.stamper_update_display)
    1 s0 (M.L.stamper_update_display)
    2 s0 (M.L.stamper_update_display)
    3 s0 (M.L.stamper_update_display)
{end}

{macro:stamper_frame}
    (C.L.stamper_path_id_0) (S.L.stamper_cur_path) -1 >
    {if}
        (L.L.stamper_display_0) (S.L.stamper_display)
        (L.L.stamper_stamp_time_0) (S.L.stamper_stamp_time)
        (L.L.stamper_timer_0) (S.L.stamper_timer)
        (M.L.stamper_screen_calc)
        (L.L.stamper_timer) (S.L.stamper_timer_0)
        (L.L.stamper_stamp_time) (S.L.stamper_stamp_time_0)
        (L.L.stamper_display) (S.L.stamper_display_0)
        (L.$.stamper_text) (S.$.stamper_text_0)
        0 s0 (M.L.stamper_update_display)
    {endif}
    
    (C.L.stamper_path_id_1) (S.L.stamper_cur_path) -1 >
    {if}
        (L.L.stamper_display_1) (S.L.stamper_display)
        (L.L.stamper_stamp_time_1) (S.L.stamper_stamp_time)
        (L.L.stamper_timer_1) (S.L.stamper_timer)
        (M.L.stamper_screen_calc)
        (L.L.stamper_timer) (S.L.stamper_timer_1)
        (L.L.stamper_stamp_time) (S.L.stamper_stamp_time_1)
        (L.L.stamper_display) (S.L.stamper_display_1)
        (L.$.stamper_text) (S.$.stamper_text_1)
        1 s0 (M.L.stamper_update_display)
    {endif}
    
    (C.L.stamper_path_id_2) (S.L.stamper_cur_path) -1 >
    {if}
        (L.L.stamper_display_2) (S.L.stamper_display)
        (L.L.stamper_stamp_time_2) (S.L.stamper_stamp_time)
        (L.L.stamper_timer_2) (S.L.stamper_timer)
        (M.L.stamper_screen_calc)
        (L.L.stamper_timer) (S.L.stamper_timer_2)
        (L.L.stamper_stamp_time) (S.L.stamper_stamp_time_2)
        (L.L.stamper_display) (S.L.stamper_display_2)
        (L.$.stamper_text) (S.$.stamper_text_2)
        2 s0 (M.L.stamper_update_display)
    {endif}

    (C.L.stamper_path_id_3) (S.L.stamper_cur_path) -1 >
    {if}
        (L.L.stamper_display_3) (S.L.stamper_display)
        (L.L.stamper_stamp_time_3) (S.L.stamper_stamp_time)
        (L.L.stamper_timer_3) (S.L.stamper_timer)
        (M.L.stamper_screen_calc)
        (L.L.stamper_timer) (S.L.stamper_timer_3)
        (L.L.stamper_stamp_time) (S.L.stamper_stamp_time_3)
        (L.L.stamper_display) (S.L.stamper_display_3)
        (L.$.stamper_text) (S.$.stamper_text_3)
        3 s0 (M.L.stamper_update_display)
    {endif}
{end}

{macro:stamper_screen_calc}
    (L.L.elec_busbar_main)
    {if}
'       Show Conduent Logo
        0 (L.L.stamper_display) =
        {if}
            (L.S.GetTime) (S.L.stamper_timer)
            11 (S.L.stamper_display)
        {endif}

'       Loading 1
        11 (L.L.stamper_display) =
        (L.S.GetTime) (L.L.stamper_timer) - abs 3 > &&
        {if}
            (L.S.GetTime) (S.L.stamper_timer)
            10 (S.L.stamper_display)
        {endif}
'       Loading 2
        10 (L.L.stamper_display) =
        (L.S.GetTime) (L.L.stamper_timer) - abs 2 > &&
        {if}
            (L.S.GetTime) (S.L.stamper_timer)
            12 (S.L.stamper_display)
        {endif}
'       Loading 3
        12 (L.L.stamper_display) =
        (L.S.GetTime) (L.L.stamper_timer) - abs 2 > &&
        {if}
            (L.S.GetTime) (S.L.stamper_timer)
            13 (S.L.stamper_display)
        {endif}
'       Loading 4
        13 (L.L.stamper_display) =
        (L.S.GetTime) (L.L.stamper_timer) - abs 2 > &&
        {if}
            (L.S.GetTime) (S.L.stamper_timer)
            14 (S.L.stamper_display)
        {endif}
'       Initialisation
        14 (L.L.stamper_display) =
        (L.S.GetTime) (L.L.stamper_timer) - abs 2 > &&
        {if}
            (L.S.GetTime) (S.L.stamper_timer)
            15 (S.L.stamper_display)
        {endif}
'       Go Home
        15 (L.L.stamper_display) =
        (L.S.GetTime) (L.L.stamper_timer) - abs 2 > &&
        {if}
            (L.S.GetTime) (S.L.stamper_timer)
            20 (S.L.stamper_display)
        {endif}
        20 (L.L.stamper_display) =
        21 (L.L.stamper_display) = ||
        22 (L.L.stamper_display) = ||
        {if}
            (L.L.schedule_active)
            {if}
'           Welcome
                21 (S.L.stamper_display)
            {else}
'           Not In Service
                22 (S.L.stamper_display)
            {endif}
        {endif}
    {else}
'       Off
        0 (S.L.stamper_display)
    {endif}

    (M.L.stamper_stamping)

    (M.L.stamper_screen)
{end}
{macro:stamper_stamping}
    21 (L.L.stamper_display) =
    (L.L.stamper_cur_path) (M.V.GetHumanCountOnPathLink) &&
    -1 (L.L.stamper_stamp_time) = &&
    {if}
        (L.S.GetTime) (S.L.stamper_stamp_time)
    {endif}

    21 (L.L.stamper_display) =
    -1 (L.L.stamper_stamp_time) = ! &&
    (L.S.GetTime) (L.L.stamper_stamp_time) - abs 0.2 > &&
    {if}
        9 random 5 =
        {if}
'           Stamper Fail
            3 random 32 + (S.L.stamper_display)
        {else}
'           Stanper Success
            31 (S.L.stamper_display)
        {endif}
        (L.$.stamper_root) (L.L.stamper_display) $IntToStr $+ (S.$.stamper_display)
        (M.L.stamper_sound)
    {endif}

    31 (L.L.stamper_display) <
    -1 (L.L.stamper_stamp_time) = ! &&
    (L.S.GetTime) (L.L.stamper_stamp_time) - abs 3.2 > &&
    {if}
'           Stanper Success
            31 (S.L.stamper_display)
            (L.$.stamper_root) (L.L.stamper_display) $IntToStr $+ (S.$.stamper_display)
            (M.L.stamper_sound)
    {endif}

    -1 (L.L.stamper_stamp_time) = !
    (L.S.GetTime) (L.L.stamper_stamp_time) - abs 3.2 > &&
    {if}
        20 (S.L.stamper_display)
        -1 (S.L.stamper_stamp_time)
    {endif}
{end}

{macro:stamper_sound}
    (C.L.stamper_path_id_1) (L.L.stamper_cur_path) =
    {if}
        (L.L.Ticketprinter)
        {if}
            (L.$.stamper_display) ".wav" $+ (T.F.stamper_tone)
        {else}
            (T.L.Stamper)
        {endif}
    {endif}
{end}

{macro:stamper_screen}
    (L.$.stamper_root) (L.L.stamper_display) $IntToStr $+ (S.$.stamper_display)
    (L.L.stamper_display) 20 >=
    {if}
        "[[" (L.S.Day) "02" $IntToStrEnh $+ "/" $+ (L.S.Month) "02" $IntToStrEnh $+ "/" $+ (L.S.Year) "04" $IntToStrEnh $+ (S.$.stamper_date)

        (L.S.Time) 3600 / s0 trunc s1 
        
        l0 l1 - 60 * trunc s2

        l1 10 <
        {if}
            "[[q"
        {else}
            l1 20 <
            {if}
            "[[w"
            {else}
                "[[e"
            {endif}
        {endif}
        "qwertyuiop" l1 10 % 1 + $SetLengthL 1 $SetLengthR $+ 
        ":" $+ "qwertyuiop" l2 10 / trunc 1 + $SetLengthL 1 $SetLengthR $+ "qwertyuiop" l2 10 % 1 + $SetLengthL 1 $SetLengthR $+ (S.$.stamper_time)
    {else}
        "" (S.$.stamper_date) (S.$.stamper_time)
    {endif}
    (L.L.stamper_display) 12 >=
    (L.L.stamper_display) 14 <= &&
    (C.L.stamper_animations) &&
    {if}
        (L.S.GetTime) 15 * 15 % trunc 1 + s1
        "(("  "asdfghjklzxcvbn" l1 $SetLengthL 1 $SetLengthR $+
        "@((" "ASDFGHJKLZXCVBN" l1 $SetLengthL 1 $SetLengthR $+ $+ (S.$.stamper_spinner)
    {else}
        "@" (S.$.stamper_spinner)
    {endif}
    
    (L.L.stamper_display) 11 = (C.L.stamper_animations) &&
    {if}
        (L.S.GetTime) (L.L.stamper_timer) - abs 2 / 15 * trunc s1
        "{" "#" l1 $* $+ (S.$.stamper_loader)
    {else}
        "" (S.$.stamper_loader)
    {endif}

   (L.$.stamper_date) "@" $+
   (L.$.stamper_time) "@@" $+ $+
   (L.$.stamper_spinner) "@" $+ $+
   (L.$.stamper_loader) "@@@" $+ $+ (S.$.stamper_text)
{end}

{macro:stamper_update_display}
    l0 0 =
    {if}
        (L.$.stamper_display) ".png" $+ (S.$.stamper_display_0)
    {else}    
        l0 1 =
        {if}
            (L.$.stamper_display) ".png" $+ (S.$.stamper_display_1)
        {else}        
            l0 2 =
            {if}
                (L.$.stamper_display) ".png" $+ (S.$.stamper_display_2)
            {else}            
                l0 3 =
                {if}
                    (L.$.stamper_display) ".png" $+ (S.$.stamper_display_3)
                {else}
                {endif}
            {endif}
        {endif}
    {endif}
{end}