[CODE][M] Multiplayer Sound script

Users who are viewing this thread

DWARF3

Veteran
Multiplayer Sound script
In general, If the server played sound, client can't hear that.
This script can play sound in a multiplayer game.

FEATURE
This script does not use the send_message_server command.(Except for the initial setting)
As a substitute, This script uses animation_finished trigger by scene_prop.
And, It can play plural sounds in the same trigger at the same time.(max: 4)


(multiplayer_is_server),
(call_script, "script_cf_sound_and_particle_helper", pos1, 7),
#pos1=play sound position
#7=sound_no(optional no)

#This script must run in the server side.

CODES
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
player_joined_trigger = (ti_server_player_joined, 0, 0,[(multiplayer_is_server)],
  [
            (store_trigger_param_1, ":player_no"),
            (multiplayer_send_4_int_to_player, ":player_no", multiplayer_event_receive4_client1, "$g_sound_and_particle_helper_ins1", "$g_sound_and_particle_helper_ins2", "$g_sound_and_particle_helper_ins3", "$g_sound_and_particle_helper_ins4"),
            (multiplayer_send_4_int_to_player, ":player_no", multiplayer_event_receive4_client2, "$g_sound_and_particle_helper_ins5", "$g_sound_and_particle_helper_ins6", "$g_sound_and_particle_helper_ins7", "$g_sound_and_particle_helper_ins8"),
  ])
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
initial_trigger = (0, 0.1, ti_once,[],
  [
            (assign, "$g_init_flag", 1),
        (multiplayer_is_server),
            (call_script, "script_cf_sound_and_particle_helper_init"),
  ])

#Please register trigger with each game mode.
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------
#in "script_game_receive_network_message"
      #client
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_ban_voteable),
        (try_begin),
          (store_script_param, ":value", 3),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 0, 2),
          #condition checks are done
          (assign, "$g_multiplayer_ban_voteable", ":value"),
          (get_max_players, ":num_players"),
          (try_for_range, ":cur_player", 1, ":num_players"),
            (player_is_active, ":cur_player"),
            (multiplayer_send_int_to_player, ":cur_player", multiplayer_event_return_ban_voteable, ":value"),
          (try_end),
        (try_end),
#add_mod-----------------------------------------------------------------
      (else_try),
          (eq, ":event_type", multiplayer_event_receive4_client1),
          (store_script_param, "$g_sound_and_particle_helper_ins1", 3),
          (store_script_param, "$g_sound_and_particle_helper_ins2", 4),
          (store_script_param, "$g_sound_and_particle_helper_ins3", 5),
          (store_script_param, "$g_sound_and_particle_helper_ins4", 6),
      (else_try),
          (eq, ":event_type", multiplayer_event_receive4_client2),
          (store_script_param, "$g_sound_and_particle_helper_ins5", 3),
          (store_script_param, "$g_sound_and_particle_helper_ins6", 4),
          (store_script_param, "$g_sound_and_particle_helper_ins7", 5),
          (store_script_param, "$g_sound_and_particle_helper_ins8", 6),
#end_mod-----------------------------------------------------------------


#---------------------------------------------------------------------------------------------------------------
#add_script
  # script_cf_sound_and_particle_helper_init
  ("cf_sound_and_particle_helper_init",
  [
      (multiplayer_is_server),
        (init_position, pos0),
        (position_set_x, pos0, 0),
        (position_set_y, pos0, 0),
        (position_set_z, pos0, 0),
        (set_spawn_position, pos0),

        (spawn_scene_prop, "spr_empty"),
        (assign, "$g_sound_and_particle_helper_ins1", reg0),
        (spawn_scene_prop, "spr_empty"),
        (assign, "$g_sound_and_particle_helper_ins2", reg0),
        (spawn_scene_prop, "spr_empty"),
        (assign, "$g_sound_and_particle_helper_ins3", reg0),
        (spawn_scene_prop, "spr_empty"),
        (assign, "$g_sound_and_particle_helper_ins4", reg0),
        (spawn_scene_prop, "spr_empty"),
        (assign, "$g_sound_and_particle_helper_ins5", reg0),
        (spawn_scene_prop, "spr_empty"),
        (assign, "$g_sound_and_particle_helper_ins6", reg0),
        (spawn_scene_prop, "spr_empty"),
        (assign, "$g_sound_and_particle_helper_ins7", reg0),
        (spawn_scene_prop, "spr_empty"),
        (assign, "$g_sound_and_particle_helper_ins8", reg0),
  ]),
#---------------------------------------------------------------------------------------------------------------
#add_script
  # script_cf_sound_and_particle_helper
  ("cf_sound_and_particle_helper",
  [
        (store_script_param_1, ":position"),
        (store_script_param_2, ":sound_no"),

        (copy_position, pos31, ":position"),
        (assign, ":pos_z", ":sound_no",),
        (init_position, pos30),
        (position_set_x, pos30, 0),
        (position_set_y, pos30, 0),
        (position_set_z, pos30, ":pos_z"),

        (prop_instance_is_animating, ":animating2", "$g_sound_and_particle_helper_ins2"),
        (prop_instance_is_animating, ":animating4", "$g_sound_and_particle_helper_ins4"),
        (prop_instance_is_animating, ":animating6", "$g_sound_and_particle_helper_ins6"),
        (prop_instance_is_animating, ":animating8", "$g_sound_and_particle_helper_ins8"),

      (try_begin),
        (eq, ":animating2", 0),
        (prop_instance_set_position, "$g_sound_and_particle_helper_ins1", pos30),
        (prop_instance_animate_to_position, "$g_sound_and_particle_helper_ins2", pos31, 1),
      (else_try),
        (eq, ":animating4", 0),
        (prop_instance_set_position, "$g_sound_and_particle_helper_ins3", pos30),
        (prop_instance_animate_to_position, "$g_sound_and_particle_helper_ins4", pos31, 1),
      (else_try),
        (eq, ":animating6", 0),
        (prop_instance_set_position, "$g_sound_and_particle_helper_ins5", pos30),
        (prop_instance_animate_to_position, "$g_sound_and_particle_helper_ins6", pos31, 1),
      (else_try),
        (eq, ":animating8", 0),
        (prop_instance_set_position, "$g_sound_and_particle_helper_ins7", pos30),
        (prop_instance_animate_to_position, "$g_sound_and_particle_helper_ins8", pos31, 1),
      (try_end),
  ]),
#---------------------------------------------------------------------------------------------------------------
#add_script
  # script_cf_sound_and_particle_helper_client
  ("cf_sound_and_particle_helper_client",
  [
        (store_script_param_1, ":instance_no"),

        (assign, ":true", 0),
      (try_begin),
        (eq, ":instance_no", "$g_sound_and_particle_helper_ins2"),
        (prop_instance_get_position, pos0, "$g_sound_and_particle_helper_ins1"),
        (prop_instance_get_position, pos1, "$g_sound_and_particle_helper_ins2"),
        (assign, ":true", 1),
      (else_try),
        (eq, ":instance_no", "$g_sound_and_particle_helper_ins4"),
        (prop_instance_get_position, pos0, "$g_sound_and_particle_helper_ins3"),
        (prop_instance_get_position, pos1, "$g_sound_and_particle_helper_ins4"),
        (assign, ":true", 1),
      (else_try),
        (eq, ":instance_no", "$g_sound_and_particle_helper_ins6"),
        (prop_instance_get_position, pos0, "$g_sound_and_particle_helper_ins5"),
        (prop_instance_get_position, pos1, "$g_sound_and_particle_helper_ins6"),
        (assign, ":true", 1),
      (else_try),
        (eq, ":instance_no", "$g_sound_and_particle_helper_ins8"),
        (prop_instance_get_position, pos0, "$g_sound_and_particle_helper_ins7"),
        (prop_instance_get_position, pos1, "$g_sound_and_particle_helper_ins8"),
        (assign, ":true", 1),
      (try_end),

        (eq, ":true", 1),
      (try_begin),
        (neg|multiplayer_is_server),
        (set_fixed_point_multiplier, 100),
      (try_end),
        (position_get_z, ":effect_no", pos0),

      (try_begin),
        (eq, ":effect_no", 1),
        (play_sound_at_position, "snd_sound1", pos1),
      (else_try),
        (eq, ":effect_no", 2),
        (play_sound_at_position, "snd_sound2", pos1),
      (else_try),
        (eq, ":effect_no", 3),
        (play_sound_at_position, "snd_sound3", pos1),
      (else_try),
        (eq, ":effect_no", 4),
        (play_sound_at_position, "snd_sound4", pos1),
      (else_try),
        (eq, ":effect_no", 5),
        (play_sound_at_position, "snd_sound5", pos1),
      (else_try),
        (eq, ":effect_no", 6),
        (play_sound_at_position, "snd_sound6", pos1),
      (else_try),
        (eq, ":effect_no", 7),
        (play_sound_at_position, "snd_sound7", pos1),
      (else_try),
        (eq, ":effect_no", :cool:,
        (play_sound_at_position, "snd_sound8", pos1),
      (else_try),
        (eq, ":effect_no", 9),
        (play_sound_at_position, "snd_sound9", pos1),
      (else_try),
        (eq, ":effect_no", 10),
        (play_sound_at_position, "snd_sound10", pos1),
      (else_try),
        (display_message, "@effect_no_error"),
      (try_end),

        #for_debug
        (assign, reg0, ":effect_no"),
        (display_message, "@effect_no: {reg0}"),
  ]),
#---------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------
  #("empty", 0, "0", "0", []),
#add_mod--------------------------------------------------------------------------------
  ("empty",0,"0","0",
    [
    (ti_on_scene_prop_animation_finished,
      [
    (neg|multiplayer_is_dedicated_server),
        (eq, "$g_init_flag", 1),
        (store_trigger_param_1, ":instance_no"),
        (call_script, "script_cf_sound_and_particle_helper_client", ":instance_no"),
    ]),
  ]),

#When a client logs in, "$g_init_flag" prevents animation_finished trigger from running accidentally.
#end_mod--------------------------------------------------------------------------------
#server events
multiplayer_event_return_friendly_fire_damage_self_ratio      = 100
multiplayer_event_return_friendly_fire_damage_friend_ratio    = 101
multiplayer_event_return_allow_player_banners                = 102
multiplayer_event_return_force_default_armor                  = 103
multiplayer_event_return_anti_cheat                          = 104
multiplayer_event_return_open_game_rules                      = 105
multiplayer_event_return_max_num_bots                        = 106
multiplayer_event_return_server_mission_timer_while_player_joined = 107
multiplayer_event_show_duel_request                          = 108
multiplayer_event_start_duel                                  = 109
multiplayer_event_cancel_duel                                = 110
multiplayer_event_show_server_message                        = 111
multiplayer_event_return_disallow_ranged_weapons              = 112
#add_mod------------------------------------------------------------
multiplayer_event_receive4_client1                            = 113
multiplayer_event_receive4_client2                            = 114
#end_mod------------------------------------------------------------
 
Interesting work. I'll wait for someone else to say whether/how it works, cause I haven't touched MP modding yet.

You know you can just RENAME your thread, don't you? No need to post it twice.
 
DWARF3 said:
Oh, I was not conscious because it was the part which I handled with a game engine. Where did you obtain information that the processing was heavy?
By looking at the engine. If we exclude headers (which are shared by both methods), the payload size is as follows:
animate_to_position: 230 bits
send_4_ints: 135 bits
send_3_ints: 103 bits
send_2_ints: 71 bits
send_1_int: 39 bits
send_message: 7 bits
 
Wow...Are you a famous developer?
And your answer is too difficult and transcends my understanding.
I am honored that you gave me a message.

Thank you for advice.
If you have time, please take a look at my MOD.
It may be very poor ability for you, but might give you an new idea.
 
DWARF3 said:
Wow...Are you a famous developer?
And your answer is too difficult and transcends my understanding.
I am honored that you gave me a message.

Thank you for advice.
If you have time, please take a look at my MOD.
It may be very poor ability for you, but might give you an new idea.

What he means is that you are introducing unnecessary overhead. Adding extra traffic when other options are preferred.

Every bit counts when were talking about networking, even if the game uses UDP frames (I suppose), that have a lower latency and aren't serialized. Read, faster.

Also, don't spell module as MOD. This is not the Ministry of Defence. :smile:
 
Google translate translates "mod" as "ministry of defense", though. :smile:
I also wrote mod fully capitalised, once. Looking back now, I can't believe it... but well, I guess that people do change with time...
 
Swyter said:
DWARF3 said:
Wow...Are you a famous developer?
And your answer is too difficult and transcends my understanding.
I am honored that you gave me a message.

Thank you for advice.
If you have time, please take a look at my MOD.
It may be very poor ability for you, but might give you an new idea.

What he means is that you are introducing unnecessary overhead. Adding extra traffic when other options are preferred.

Every bit counts when were talking about networking, even if the game uses UDP frames (I suppose), that have a lower latency and aren't serialized. Read, faster.

Also, don't spell module as MOD. This is not the Ministry of Defence. :smile:

Thanks your advice.
I should review this script.
 
Lumos said:
Google translate translates "mod" as "ministry of defense", though. :smile:
I also wrote mod fully capitalised, once. Looking back now, I can't believe it... but well, I guess that people do change with time...

"ministry of defense"? Ah... :grin:
Should I write it as "Mod" or "mod"?
 
Why so complicated and not robust?

In Napolenic Wars we did:


Call this script server side when you want to play a sound, (copy the position where it should play to pos56):
Code:
 # script_multiplayer_server_play_sound_at_position
  # Input: arg1 = sound_id
  # Input: pos56 = position
  # Output: reg0 = ok?
  ("multiplayer_server_play_sound_at_position",
   [
     (store_script_param, ":sound_id", 1),
     
     (assign,reg0,0),
     (try_begin),
       (this_or_next|multiplayer_is_server),
       (neg|game_in_multiplayer_mode),
       
       (is_between,":sound_id","snd_click","snd_sounds_end"), # valid sound
       
       (try_begin),
         (neg|multiplayer_is_dedicated_server),
         (play_sound_at_position, ":sound_id", pos56),
       (try_end),
       
       (try_begin),
         (game_in_multiplayer_mode),
         
         (set_fixed_point_multiplier, 100),
         (position_get_x,":xvalue", pos56),
         (position_get_y,":yvalue", pos56),
         (position_get_z,":zvalue", pos56),
         
         (try_for_range, ":cur_player", 1, multiplayer_player_loops_end),
           (player_is_active,":cur_player"),

           (multiplayer_send_4_int_to_player, ":cur_player", multiplayer_event_return_sound_at_pos,":xvalue",":yvalue",":zvalue",":sound_id"),
         (try_end),
       (try_end),
       (assign,reg0,1),
     (try_end),
   ]),

Then client side event catcher:
Code:
 (else_try),
          (eq, ":event_type", multiplayer_event_return_sound_at_pos),
          (store_script_param, ":value", 3),
          (store_script_param, ":value_2", 4),
          (store_script_param, ":value_3", 5),
          (store_script_param, ":value_4", 6),
          
          (try_begin),
            (is_between,":value_4","snd_click","snd_sounds_end"), # valid sound
            (set_fixed_point_multiplier, 100),
            (init_position, pos25),
            (position_set_x,pos25,":value"),
            (position_set_y,pos25,":value_2"),
            (position_set_z,pos25,":value_3"),
            
            (play_sound_at_position, ":value_4", pos25),
          (try_end),


Might need some changing for native Module system (replace multiplayer_player_loops_end with 250 or make it a constant, snd_sounds_end doesent exist, make a dummy in end of file for it)

But that seems a lot better to play a sound on a position, no?
 
This looks interesting. I'll be having to start coding multi stuff, for a change.
The learning curve doesn't seems too steep. :smile:

I suppose that the second code snip works within a hardcoded
Code:
get_server_thingie
style script or sth like that.
 
His code is a general.
I think that the method is the best. (The code should rewrite the part in for attach Native)

In my case, I cared about to very long  "script_game_receive_network_message".
So, I thought about the way without it. But, it way was not a good...This is trial and error.
As a result, I was able to get much advice from a mate.
Thanks.
 
Back
Top Bottom