OSP Code QoL Day and Night Cycle

Users who are viewing this thread

Naber La

Sergeant at Arms
WBNWWF&SVCM&B
Hello fellow modders! I have managed to make a day and night cycle in a mission for scenes. Here is my work. It is free for use and no need to credit me as an author.

Code:
before_mission_start = (ti_before_mission_start, 0, 0, [],
[
    (assign, "$last_lighting_update_time", -1),
    (scene_set_day_time, 0),
])

after_mission_start = (ti_after_mission_start, 0, 0, [],
[
    (try_begin),
        (multiplayer_is_server),
        (try_begin),
            (eq, "$day_night_cycle_enabled", 1),
            (call_script, "script_spawn_skyboxes"),
        (try_end),
    (try_end),
])

skybox_update_loop = (1, 0, 0, [],
[
    (multiplayer_is_server),
    (eq, "$day_night_cycle_enabled", 1),
    (call_script, "script_cf_skybox_send_int_to_players"),
])

Code:
("spawn_skyboxes",
[
    (init_position, pos0),
    (set_spawn_position, pos0),

    (store_add, ":spr_end", "spr_skybox_sunset", 1),
    (try_for_range, ":prop_kind", "spr_skybox_cloudy_day", ":spr_end"),
        (spawn_scene_prop, ":prop_kind"),
    (try_end),
]),

("cf_skybox_send_int_to_players",
[
    (store_mission_timer_b, ":time"),

    (store_mul, ":hours", 24, "$an_in_game_hour"),

    (try_begin),
        (is_between, ":time", 0, ":hours"),
    (else_try),
        (reset_mission_timer_b),
    (try_end),

    (try_for_players, ":player_id"),
        (player_is_active, ":player_id"),
        (multiplayer_send_2_int_to_player, ":player_id", server_event_day_night_cycle_sync, ":time", "$an_in_game_hour"),
    (try_end),
]),

("game_receive_network_message",
[
    (store_script_param, ":player_id", 1),
    (store_script_param, ":event_type", 2),

    (try_begin),
        (neg|multiplayer_is_server),
        (try_begin),
            (eq, ":event_type", server_event_day_night_cycle_sync),
            (store_script_param, ":day_time", 3),
            (store_script_param, ":in_game_hour", 4),
            (call_script, "script_skybox_update", ":day_time", ":in_game_hour"),
        (try_end),
]),

("skybox_update",
[
    (store_script_param, ":time", 1),
    (store_script_param, ":in_game_hour", 2),

    (try_begin),
        (store_mission_timer_a, ":now"),
        (store_sub, ":time_passed", ":now", "$last_lighting_update_time"),
        (this_or_next|ge, ":time_passed", ":in_game_hour"),
        (le, "$last_lighting_update_time", 0),
        (call_script, "script_skybox_set_lighting_for_time", ":time", ":in_game_hour"),
        (assign, "$last_lighting_update_time", ":now"),
    (try_end),

    (try_begin),
        (scene_prop_get_instance, ":box_day_clear", "spr_skybox_clear_day", 0),
        (scene_prop_get_instance, ":box_sunset", "spr_skybox_sunset", 0),
        (scene_prop_get_instance, ":box_night_clear", "spr_skybox_clear_night", 0),
    (else_try),
        (display_message, "@Warning: Couldn't get one or more skybox prop instances!"),
    (try_end),

    (store_mul, ":hours", 5, ":in_game_hour"),
    (store_mul, ":hours2", 8, ":in_game_hour"),
    (store_mul, ":hours3", 18, ":in_game_hour"),
    (store_mul, ":hours4", 21, ":in_game_hour"),
    (store_mul, ":hours5", 24, ":in_game_hour"),

    (assign, reg0, ":hours"),
    (assign, reg1, ":hours2"),
    (assign, reg2, ":hours3"),
    (assign, reg3, ":hours4"),
    (assign, reg4, ":hours5"),

    (try_begin),
        (is_between, ":time", reg1, reg2 + 1), #day

        (scene_prop_set_visibility, ":box_sunset", 0),
        (scene_prop_set_visibility, ":box_night_clear", 0),
        (scene_prop_set_visibility, ":box_day_clear", 1),
    (else_try),
        (is_between, ":time", reg0, reg1 + 1), #sunrise

        (scene_prop_set_visibility, ":box_day_clear", 0),
        (scene_prop_set_visibility, ":box_night_clear", 0),
        (scene_prop_set_visibility, ":box_sunset", 1),
    (else_try),
        (is_between, ":time", reg2, reg3 + 1), #sunset

        (scene_prop_set_visibility, ":box_night_clear", 0),
        (scene_prop_set_visibility, ":box_day_clear", 0),
        (scene_prop_set_visibility, ":box_sunset", 1),
    (else_try),
        (this_or_next|is_between, ":time", 0, reg0 + 1), #night
        (is_between, ":time", reg3, reg4 + 1),

        (scene_prop_set_visibility, ":box_day_clear", 0),
        (scene_prop_set_visibility, ":box_sunset", 0),
        (scene_prop_set_visibility, ":box_night_clear", 1),
    (try_end),
]),

("skybox_set_lighting_for_time",
[
    (store_script_param, ":time", 1),
    (store_script_param, ":in_game_hour", 2),

    (try_for_range, ":count", 0, 24),

        (store_mul, ":hours", ":count", ":in_game_hour"),

        (store_add, ":count2", ":count", 1),
        (store_mul, ":hours2", ":count2", ":in_game_hour"),
        (val_add, ":hours2", 1),

        (try_begin),
            (eq, ":count", 0),
            (is_between, ":time", ":hours", ":hours2"),
            (set_startup_sun_light, 3, 3, 7),
            (set_startup_ambient_light, 1, 1, 5),
            (set_startup_ground_ambient_light, 0, 0, 0),
        (else_try),
            (eq, ":count", 1),
            (is_between, ":time", ":hours", ":hours2"),
            (set_startup_sun_light, 5, 5, 10),
            (set_startup_ambient_light, 2, 2, 5),
            (set_startup_ground_ambient_light, 0, 0, 0),
        (else_try),
            (eq, ":count", 2),
            (is_between, ":time", ":hours", ":hours2"),
            (set_startup_sun_light, 5, 5, 10),
            (set_startup_ambient_light, 3, 3, 5),
            (set_startup_ground_ambient_light, 0, 0, 0),
        (else_try),
            (eq, ":count", 3),
            (is_between, ":time", ":hours", ":hours2"),
            (set_startup_sun_light, 10, 5, 12),
            (set_startup_ambient_light, 8, 5, 10),
            (set_startup_ground_ambient_light, 0, 0, 0),
        (else_try),
            (eq, ":count", 4),
            (is_between, ":time", ":hours", ":hours2"),
            (set_startup_sun_light, 20, 12, 20),
            (set_startup_ambient_light, 20, 15, 20),
            (set_startup_ground_ambient_light, 0, 0, 0),
        (else_try),
            (eq, ":count", 5),
            (is_between, ":time", ":hours", ":hours2"),
            (set_startup_sun_light, 40, 40, 30),
            (set_startup_ambient_light, 25, 20, 25),
            (set_startup_ground_ambient_light, 0, 0, 0),
        (else_try),
            (eq, ":count", 6),
            (is_between, ":time", ":hours", ":hours2"),
            (set_startup_sun_light, 50, 50, 45),
            (set_startup_ambient_light, 30, 25, 30),
            (set_startup_ground_ambient_light, 0, 0, 0),
        (else_try),
            (eq, ":count", 7),
            (is_between, ":time", ":hours", ":hours2"),
            (set_startup_sun_light, 60, 60, 55),
            (set_startup_ambient_light, 30, 30, 30),
            (set_startup_ground_ambient_light, 0, 0, 0),
        (else_try),
            (eq, ":count", 8),
            (is_between, ":time", ":hours", ":hours2"),
            (set_startup_sun_light, 70, 70, 70),
            (set_startup_ambient_light, 40, 40, 40),
            (set_startup_ground_ambient_light, 0, 0, 0),
        (else_try),
            (eq, ":count", 9),
            (is_between, ":time", ":hours", ":hours2"),
            (set_startup_sun_light, 80, 80, 80),
            (set_startup_ambient_light, 50, 50, 50),
            (set_startup_ground_ambient_light, 0, 0, 0),
        (else_try),
            (eq, ":count", 10),
            (is_between, ":time", ":hours",":hours2"),
            (set_startup_sun_light, 100, 90, 90),
            (set_startup_ambient_light, 60, 60, 60),
            (set_startup_ground_ambient_light, 0, 0, 0),
        (else_try),
            (eq, ":count", 11),
            (is_between, ":time", ":hours", ":hours2"),
            (set_startup_sun_light, 100, 100, 100),
            (set_startup_ambient_light, 70, 70, 70),
            (set_startup_ground_ambient_light, 0, 0, 0),
        (else_try),
            (eq, ":count", 12),
            (is_between, ":time", ":hours", ":hours2"),
            (set_startup_sun_light, 120, 120, 120),
            (set_startup_ambient_light, 85, 85, 85),
            (set_startup_ground_ambient_light, 0, 0, 0),
        (else_try),
            (eq, ":count", 13),
            (is_between, ":time", ":hours", ":hours2"),
            (set_startup_sun_light, 110, 110, 110),
            (set_startup_ambient_light, 70, 70, 70),
            (set_startup_ground_ambient_light, 0, 0, 0),
        (else_try),
            (eq, ":count", 14),
            (is_between, ":time", ":hours", ":hours2"),
            (set_startup_sun_light, 100, 90, 90),
            (set_startup_ambient_light, 60, 60, 60),
            (set_startup_ground_ambient_light, 0, 0, 0),
        (else_try),
            (eq, ":count", 15),
            (is_between, ":time", ":hours", ":hours2"),
            (set_startup_sun_light, 80, 80, 80),
            (set_startup_ambient_light, 50, 50, 55),
            (set_startup_ground_ambient_light, 0, 0, 0),
        (else_try),
            (eq, ":count", 16),
            (is_between, ":time", ":hours", ":hours2"),
            (set_startup_sun_light, 70, 70, 70),
            (set_startup_ambient_light, 40, 40, 60),
            (set_startup_ground_ambient_light, 0, 0, 0),
        (else_try),
            (eq, ":count", 17),
            (is_between, ":time", ":hours", ":hours2"),
            (set_startup_sun_light, 60, 60, 55),
            (set_startup_ambient_light, 30, 30, 65),
            (set_startup_ground_ambient_light, 0, 0, 0),
        (else_try),
            (eq, ":count", 18),
            (is_between, ":time", ":hours", ":hours2"),
            (set_startup_sun_light, 50, 50, 45),
            (set_startup_ambient_light, 30, 30, 70),
            (set_startup_ground_ambient_light, 0, 0, 0),
        (else_try),
            (eq, ":count", 19),
            (is_between, ":time", ":hours", ":hours2"),
            (set_startup_sun_light, 40, 40, 40),
            (set_startup_ambient_light, 25, 25, 75),
            (set_startup_ground_ambient_light, 0, 0, 0),
        (else_try),
            (eq, ":count", 20),
            (is_between, ":time", ":hours", ":hours2"),
            (set_startup_sun_light, 40, 30, 30),
            (set_startup_ambient_light, 25, 20, 65),
            (set_startup_ground_ambient_light, 0, 0, 0),
        (else_try),
            (eq, ":count", 21),
            (is_between, ":time", ":hours", ":hours2"),
            (set_startup_sun_light, 30, 20, 20),
            (set_startup_ambient_light, 35, 15, 40),
            (set_startup_ground_ambient_light, 0, 0, 0),
        (else_try),
            (eq, ":count", 22),
            (is_between, ":time", ":hours", ":hours2"),
            (set_startup_sun_light, 8, 6, 8),
            (set_startup_ambient_light, 15, 10, 30),
            (set_startup_ground_ambient_light, 0, 0, 0),
        (else_try),
            (eq, ":count", 23),
            (is_between, ":time", ":hours", ":hours2"),
            (set_startup_sun_light, 4, 5, 5),
            (set_startup_ambient_light, 7, 7, 18),
            (set_startup_ground_ambient_light, 0, 0, 0),
        (try_end),
    (try_end),
]),

("game_get_console_command",
[
    (store_script_param, ":command", 1),
    (store_script_param, ":value", 2),

     (try_begin),
        (eq, ":command", command_set_team_points_gained_for_flags),
        (assign, "$day_night_cycle_enabled", ":value"),
        (str_store_string, s1, "@Day and night cycle is disabled."),
        (try_begin),
            (eq, ":value", 1),
            (str_store_string, s1, "@Day and night cycle is enabled."),
        (else_try),
            (neg|is_between, ":value", 0, 2),
            (str_store_string, s1, "@Day and night cycle value have to be 0 to disable either 1 to enable!"),
        (try_end),
        (display_message, s1),
    (else_try),
        (eq, ":command", command_set_points_gained_for_capturing_flags),
        (try_begin),
            (lt, ":value", 24),
            (assign, ":value", 24),
        (try_end),
        (store_div, ":division", ":value", 24),
        (store_mod, ":mod", ":value", 24),
        (store_div, ":divmod", ":mod", 24),
        (store_sub, "$an_in_game_hour", ":division", ":divmod"),

        (store_div, ":hour", ":value", 3600),
        (store_mod, ":hour_mod", ":value", 3600),
        (store_mod, ":second", ":hour_mod", 60),
        (store_mod, ":second_mod", ":second", 24),
        (store_sub, ":minute_sub", ":hour_mod", ":second"),
        (store_div, ":minute", ":minute_sub", 60),
        (try_begin),
            (neq, ":mod", 0),
            (store_sub, ":value_sub", ":value", ":second_mod"),
            (store_mod, ":hour_mod", ":value_sub", 3600),
            (store_mod, ":second", ":hour_mod", 60),
            (store_sub, ":minute_sub", ":hour_mod", ":second"),
            (store_div, ":minute", ":minute_sub", 60),
        (try_end),
        (assign, reg0, ":hour"),
        (assign, reg1, ":minute"),
        (assign, reg2, ":second"),
        (str_store_string, s1, "@Entered amount of seconds applied as in game time! It is {reg0} hour(s), {reg1} minute(s), {reg2} second(s)."),
        (display_message, s1),
    (try_end),
]),

Code:
# Custom declarations
server_event_day_night_cycle_sync               = 0

# Module system commands
command_get_bot_count                           = 1
command_set_bot_count                           = 2
command_get_round_max_seconds                   = 3
command_set_round_max_seconds                   = 4
command_get_respawn_period                      = 5
command_set_respawn_period                      = 6
command_get_num_bots_voteable                   = 7
command_set_num_bots_voteable                   = 8
command_get_maps_voteable                       = 9
command_set_maps_voteable                       = 10
command_get_factions_voteable                   = 11
command_set_factions_voteable                   = 12
command_get_player_respawn_as_bot               = 13
command_set_player_respawn_as_bot               = 14
command_get_kick_voteable                       = 15
command_set_kick_voteable                       = 16
command_get_ban_voteable                        = 17
command_set_ban_voteable                        = 18
command_get_valid_vote_ratio                    = 19
command_set_valid_vote_ratio                    = 20
command_get_auto_team_balance_limit             = 21
command_set_auto_team_balance_limit             = 22
command_get_starting_gold                       = 23
command_set_starting_gold                       = 24
command_get_combat_gold_bonus                   = 25
command_set_combat_gold_bonus                   = 26
command_get_round_gold_bonus                    = 27
command_set_round_gold_bonus                    = 28
command_get_player_banners_allowed              = 29
command_set_player_banners_allowed              = 30
command_get_force_default_armor                 = 31
command_set_force_default_armor                 = 32
command_get_team_points_gained_for_flags        = 33
command_set_team_points_gained_for_flags        = 34
command_get_points_gained_for_capturing_flags   = 35
command_set_points_gained_for_capturing_flags   = 36
command_get_map_time_limit                      = 37
command_set_map_time_limit                      = 38
command_get_team_point_limit                    = 39
command_set_team_point_limit                    = 40
command_get_defender_spawn_count                = 41
command_set_defender_spawn_count                = 42
command_get_disallow_ranged_weapons             = 43
command_set_disallow_ranged_weapons             = 44
# Napoleonic Wars commands
command_use_class_limits                        = 50
command_class_limit_player_count                = 51
command_squad_size                              = 52
command_scale_squad                             = 53
command_build_points_team1                      = 54
command_build_points_team2                      = 55
command_allow_multiple_firearms                 = 56
command_enable_bonuses                          = 57
command_bonus_strength                          = 58
command_bonus_range                             = 59
command_fall_off_horse                          = 60
command_horse_dying                             = 61
command_auto_kick                               = 62
command_max_teamkills_before_kick               = 63
command_auto_horse                              = 64
command_auto_swap                               = 65
command_limit_grenadier                         = 66
command_limit_skirmisher                        = 67
command_limit_rifle                             = 68
command_limit_cavalry                           = 69
command_limit_lancer                            = 70
command_limit_hussar                            = 71
command_limit_dragoon                           = 72
command_limit_cuirassier                        = 73
command_limit_heavycav                          = 74
command_limit_artillery                         = 75
command_limit_rocket                            = 76
command_limit_sapper                            = 77
command_limit_musician                          = 78
command_limit_sergeant                          = 79
command_limit_officer                           = 80
command_limit_general                           = 81
# Hard coded commands
command_get_max_players                         = 101
command_set_max_players                         = 102
command_get_friendly_fire                       = 103
command_set_friendly_fire                       = 104
command_get_melee_friendly_fire                 = 105
command_set_melee_friendly_fire                 = 106
command_get_friendly_fire_damage_self_ratio     = 107
command_set_friendly_fire_damage_self_ratio     = 108
command_get_friendly_fire_damage_friend_ratio   = 109
command_set_friendly_fire_damage_friend_ratio   = 110
command_get_ghost_mode                          = 111
command_set_ghost_mode                          = 112
command_get_control_block_direction             = 113
command_set_control_block_direction             = 114
command_get_combat_speed                        = 115
command_set_combat_speed                        = 116
command_get_add_to_game_servers_list            = 117
command_set_add_to_game_servers_list            = 118
command_get_anti_cheat                          = 119
command_set_anti_cheat                          = 120
command_get_renaming_server_allowed             = 121
command_set_renaming_server_allowed             = 122
command_get_changing_game_type_allowed          = 123
command_set_changing_game_type_allowed          = 124
command_start_scene                             = 130
command_open_admin_panel                        = 132
command_open_game_rules                         = 134
command_set_server_mission_timer                = 136

commands_module_system_begin                    = command_get_bot_count
commands_module_system_end                      = command_set_disallow_ranged_weapons + 1
commands_napoleonic_wars_begin                  = command_use_class_limits
commands_napoleonic_wars_end                    = command_limit_general + 1
commands_hard_coded_begin                       = command_get_max_players
commands_hard_coded_end                         = command_set_anti_cheat + 1

Code:
skybox_triggers = [
  (ti_on_scene_prop_init, [
    (store_trigger_param, ":instance_id", 1),

    # Get min and max scene coordinates
    (get_scene_boundaries, pos0, pos1),

    # Scale the skybox and hide it
    (scene_prop_set_visibility, ":instance_id", 0),

    (set_fixed_point_multiplier, 1),

    (position_get_x, reg0, pos1),
    (position_get_y, reg1, pos1),
    (position_get_z, reg2, pos1),

    (try_begin),
        (this_or_next|gt, reg0, 450),
        (assign, reg0, 450),
        (this_or_next|gt, reg1, 450),
        (assign, reg1, 450),
        (this_or_next|gt, reg2, 450),
        (eq, reg2, 0),
        (assign, reg2, 450),
    (try_end),

    (prop_instance_set_scale, ":instance_id", reg0, reg1, reg2),

    (set_fixed_point_multiplier, 1000),

    (position_get_x, ":min_x", pos0),
    (position_get_y, ":min_y", pos0),
    (position_get_x, ":max_x", pos1),
    (position_get_y, ":max_y", pos1),

    # Get the center of the scene
    (store_add, ":center_x", ":max_x", ":min_x"),
    (store_add, ":center_y", ":max_y", ":min_y"),

    (set_fixed_point_multiplier, 1),
    (val_div, ":center_x", 2),
    (val_div, ":center_y", 2),

    # Move the skybox to the center
    (init_position, pos0),
    (set_fixed_point_multiplier, 1000),
    (position_set_x, pos0, ":center_x"),
    (position_set_y, pos0, ":center_y"),
    (position_set_z_to_ground_level, pos0),

    (prop_instance_set_position, ":instance_id", pos0),
  ]),
]

scene_props = [

    # native skyboxes
    ("skybox_cloudy_day", 0, "skybox_cloud_1", "0", skybox_triggers),
    ("skybox_rainy_day", 0, "skybox_cloud_2", "0", skybox_triggers),
    ("skybox_clear_day", 0, "skybox_clearday", "0", skybox_triggers),
    ("skybox_clear_night", 0, "skybox_night_1", "0", skybox_triggers),
    ("skybox_cloudy_night", 0, "skybox_night_2", "0", skybox_triggers),
    ("skybox_sunset", 0, "skybox_sunset_1", "0", skybox_triggers),
    # native skyboxes
]

It is designed to work in multiplayer but you can change it a little bit to a single player design.

Note: If you want to reflect the ambient for like real, you have to rotate scene props accordingly in specific times. No weather system included.
 
Last edited:
Creative way of implementing skyboxes.
Didn't know you can treat skyboxes as just prop instances.
Thank you for sharing.
 
Top Bottom