OSP Code Combat Multiplayer Sprinting and Walking

Users who are viewing this thread

Caba`drin said:
Because of this, I would rename the event from
multiplayer_event_set_agent_id
to
multiplayer_event_set_movement_state
In this case yes.
But I also use this server event for shield bash etc.
So I wont rename the event, because of this.

I will test the code today.
Thank you so much for your help :wink:


One more question.
Does "agent_set_speed_modifier" also works for horses ???
Because "agent_set_horse_speed_factor" cant get a value over 100%.
 
Latest Update
May 28, 2012


Hello guys.
So here is my updated code with a walking- and sprinting mode for horses.
Now the "agent speed modifier" and "sprint time limit" depends on the agents athletics or riding skill.
I also shortened the code a bit.

You can take what you want.
But give me credit

Maybe I will post this in the OSP thread.

header_common
Code:
multiplayer_event_set_agent_id                                = 48

module_constants
Code:
slot_agent_walk                   = 27
slot_agent_sprint_time            = 28
slot_agent_sprint_limit           = 29
slot_agent_sprint_mode            = 30

module_mission_templates
Code:
#walking
walk_limiter = (
  0, 0, 0.5,
  [
    (key_clicked, key_left_shift),

    (try_begin),
      (game_in_multiplayer_mode),
      (neg|multiplayer_is_server),
      (multiplayer_send_int_to_server, multiplayer_event_set_agent_id, "script_cf_agent_walk"),
    (else_try),
      (get_player_agent_no, ":player_agent"),
      (gt, ":player_agent", -1),
      (agent_is_alive,":player_agent"),
      (call_script, "script_cf_agent_walk", ":player_agent"),
    (try_end),
  ], [])

#sprinting
sprint_limiter = (
  0, 0, 0.5,
  [
    (game_key_is_down, gk_move_forward),
    (game_key_clicked, gk_crouch),

    (try_begin),
      (game_in_multiplayer_mode),
      (neg|multiplayer_is_server),
      (multiplayer_send_int_to_server, multiplayer_event_set_agent_id, "script_cf_agent_sprint_limiter"),
    (else_try),
      (get_player_agent_no, ":player_agent"),
      (gt, ":player_agent", -1),
      (agent_is_alive,":player_agent"),
      (call_script, "script_cf_agent_sprint_limiter", ":player_agent"),
    (try_end),
  ], [])

sprint_timer = (
  1, 0, 0,
  [
    (try_begin),#keys pressed
      (game_key_is_down, gk_move_forward),
      (game_key_is_down, gk_crouch),

      (try_begin),
        (game_in_multiplayer_mode),
        (neg|multiplayer_is_server),
        (multiplayer_send_int_to_server, multiplayer_event_set_agent_id, "script_cf_agent_sprint_time"),
      (else_try),
        (get_player_agent_no, ":player_agent"),
        (gt, ":player_agent", -1),
        (agent_is_alive,":player_agent"),
        (call_script, "script_cf_agent_sprint_time", ":player_agent"),
      (try_end),
    (else_try),#keys losed
      (try_begin),
        (game_in_multiplayer_mode),
        (neg|multiplayer_is_server),
        (multiplayer_send_int_to_server, multiplayer_event_set_agent_id, "script_cf_agent_sprint_stop"),
      (else_try),
        (get_player_agent_no, ":player_agent"),
        (gt, ":player_agent", -1),
        (agent_is_alive,":player_agent"),
        (call_script, "script_cf_agent_sprint_stop", ":player_agent"),
      (try_end),
    (try_end),
  ], [])

module_scripts
server event
Code:
      # multiplayer_event_set_agent_id
      (else_try),
        (eq, ":event_type", multiplayer_event_set_agent_id),
        (store_script_param, ":script_id", 3),

        (player_get_agent_id, ":player_agent_no", ":player_no"),
        (gt, ":player_agent_no", -1),
        (agent_is_alive, ":player_agent_no"),

        (ge, ":script_id", "script_cf_agent_walk"),#added for security
        (le, ":script_id", "script_cf_agent_sprint_stop"),#added for security
        (call_script, ":script_id", ":player_agent_no"),
scripts
Code:
  ("cf_agent_walk",
  [
    (store_script_param, ":agent_id", 1),
     
    (this_or_next|multiplayer_is_server),
    (neg|game_in_multiplayer_mode),
    
    (agent_slot_eq, ":agent_id", slot_agent_sprint_mode, 0),
    (try_begin),
      (agent_slot_eq, ":agent_id", slot_agent_walk, 0),
      (agent_set_slot, ":agent_id", slot_agent_walk, 1),
      (agent_set_speed_modifier, ":agent_id", 50),
      (agent_set_horse_speed_factor, ":agent_id", 30),
    (else_try),
      (agent_set_slot, ":agent_id", slot_agent_walk, 0),
      (agent_set_speed_modifier, ":agent_id", 100),
      (agent_set_horse_speed_factor, ":agent_id", 100),
    (try_end),
  ]),

  ("cf_agent_sprint_limiter",
  [
    (store_script_param, ":agent_id", 1),
     
    (this_or_next|multiplayer_is_server),
    (neg|game_in_multiplayer_mode),
    
    (agent_get_wielded_item, ":weapon_r", ":agent_id", 0),#right hand
    (agent_get_wielded_item, ":weapon_l", ":agent_id", 1),#left hand
    (le, ":weapon_r", 0),#no wielded weapon while sprinting
    (le, ":weapon_l", 0),#no wielded shield while sprinting
    (agent_slot_eq, ":agent_id", slot_agent_walk, 0),
    (agent_slot_eq, ":agent_id", slot_agent_sprint_time, 0),
    (agent_set_slot, ":agent_id", slot_agent_sprint_time, 1),
    (agent_set_slot, ":agent_id", slot_agent_sprint_mode, 1),
    
    (agent_get_troop_id, ":sprint_troop", ":agent_id"),
    (gt, ":sprint_troop", -1),

    (agent_get_horse, ":horse", ":agent_id"),
    (try_begin),# unmounted agent
      (le, ":horse", 0),
      (assign, ":speed", 200),
      (assign, ":limit", 24),#sprint time = 1/2
      (store_skill_level, ":skl", "skl_athletics", ":sprint_troop"),
      (try_begin),
        (gt, ":skl", 0),
        (store_mul, ":skl_speed", ":skl", 2),
        (val_sub, ":speed", ":skl_speed"),
        (val_add, ":limit", ":skl"),
      (try_end),
      (agent_set_speed_modifier, ":agent_id", ":speed"),
    (else_try),# mounted agent
      (assign, ":speed", 150),
      (assign, ":limit", 32),#sprint time = 1/2
      (store_skill_level, ":skl", "skl_riding", ":sprint_troop"),
      (try_begin),
        (gt, ":skl", 0),
        (val_sub, ":speed", ":skl"),
        (val_add, ":limit", ":skl"),
      (try_end),
      (agent_set_horse_speed_factor, ":agent_id", ":speed"),
    (try_end),
    (agent_set_slot, ":agent_id", slot_agent_sprint_limit, ":limit"),
  ]),

  ("cf_agent_sprint_time",
  [
    (store_script_param, ":agent_id", 1),
     
    (this_or_next|multiplayer_is_server),
    (neg|game_in_multiplayer_mode),
    
    (agent_get_slot, ":time", ":agent_id", slot_agent_sprint_time),
    (agent_get_slot, ":limit", ":agent_id", slot_agent_sprint_limit),

    (try_begin),
      (ge, ":time", 1),
      (agent_get_wielded_item, ":weapon_r", ":agent_id", 0),#right hand
      (agent_get_wielded_item, ":weapon_l", ":agent_id", 1),#left hand
      (assign, ":lower_time", 0),
      (try_begin),
        (agent_slot_eq, ":agent_id", slot_agent_sprint_mode, 1),
        (le, ":weapon_r", 0),#no wielded weapon while sprinting
        (le, ":weapon_l", 0),#no wielded shield while sprinting
        (val_add, ":time", 2),#1/2 sec
      (else_try),
        (assign, ":lower_time", 1),
      (try_end),
      (try_begin),
        (gt, ":time", ":limit"),
        (assign, ":lower_time", 1),
      (try_end),
      (try_begin),
        (eq, ":lower_time", 1),
        (agent_get_slot, ":time", ":agent_id", slot_agent_sprint_time),
        (val_sub, ":time", 1),
        (val_max, ":time", 0),
        (agent_set_slot, ":agent_id", slot_agent_sprint_mode, 0),
        (agent_slot_eq, ":agent_id", slot_agent_walk, 0),
        (agent_get_horse, ":horse", ":agent_id"),
        (try_begin),# unmounted agent
          (le, ":horse", 0),
          (agent_set_speed_modifier, ":agent_id", 100),
        (else_try),# mounted agent
          (agent_set_horse_speed_factor, ":agent_id", 100),
        (try_end),
      (try_end),
      (agent_set_slot, ":agent_id", slot_agent_sprint_time, ":time"),
    (try_end),
  ]),
  
  ("cf_agent_sprint_stop",
  [
    (store_script_param, ":agent_id", 1),
     
    (this_or_next|multiplayer_is_server),
    (neg|game_in_multiplayer_mode),
    
    (agent_get_slot, ":time", ":agent_id", slot_agent_sprint_time),
    (try_begin),
      (gt, ":time", 0),
      (val_sub, ":time", 1),
      (agent_set_slot, ":agent_id", slot_agent_sprint_time, ":time"),
      (agent_set_slot, ":agent_id", slot_agent_sprint_mode, 0),
      (agent_slot_eq, ":agent_id", slot_agent_walk, 0),
      (agent_get_horse, ":horse", ":agent_id"),
      (try_begin),# unmounted agent
        (le, ":horse", 0),
        (agent_set_speed_modifier, ":agent_id", 100),
      (else_try),# mounted agent
        (agent_set_horse_speed_factor, ":agent_id", 100),
      (try_end),
    (try_end),
  ]),
 
Sounds awesome.

Would be a great addition to here:

http://mbmodwiki.ollclan.eu/Category:Script

If no one wants to add it, and someone independent can validate that this script works, I'll add it when I have time :smile:.

 
A little update.

Now the script name is send direktly to the server event, instead of sending a number for each script.
This shortened the event alot, because no script_id check is needed.

You can see the changes in the above script.

SonKidd said:
Sounds awesome.

Would be a great addition to here:

http://mbmodwiki.ollclan.eu/Category:Script

If no one wants to add it, and someone independent can validate that this script works, I'll add it when I have time :smile:.
It works.
I tested it a couple of times on my server with 1 to 4 people.
 
_Sebastian_ said:
A little update.
Now the script name is send direktly to the server event, instead of sending a number for each script.
This shortened the event alot, because no script_id check is needed.

I would recommend as part of etiquette still check script id.  :!:

        (ge, ":script_id", "script_cf_agent_walk"),
        (le, ":script_id", "script_cf_agent_sprint_stop"),
 
Maxim Suvorov said:
I would recommend as part of etiquette still check script id.  :!:

        (ge, ":script_id", "script_cf_agent_walk"),
        (le, ":script_id", "script_cf_agent_sprint_stop"),
Why?
It is not necessary... untill the event gets an invalid script name.
But this wont happen if you send the script name correctly :wink:
 
cmpxchg8b said:
Because someone can exploit it to execute scripts that aren't supposed to be executed.
Maybe.
But then it is his own fail.
And I'm not going to implement a "child safety lock" for my code :wink:

GK_Lungy said:
Looks very nice _Sebastian_ , will try this thing soon when i get back home , looks very promising :grin:
Thank you.
Dont forget to enable can_crouch in the module.ini to get the crouch key.
Also notice, that the sprinting only works if the agent has no wielded items,
because of this;
Code:
    (agent_get_wielded_item, ":weapon_r", ":agent_id", 0),#right hand
    (agent_get_wielded_item, ":weapon_l", ":agent_id", 1),#left hand
    (le, ":weapon_r", 0),#no wielded weapon while sprinting
    (le, ":weapon_l", 0),#no wielded shield while sprinting
 
_Sebastian_ said:
Maybe.
But then it is his own fail.
And I'm not going to implement a "child safety lock" for my code :wink:
No, you misunderstand; someone can misuse it to make the server execute arbitrary scripts.
If I send that event with script_game_start as script id, the server will execute script_game_start. Whether it crashes the server or just stops it for a couple of seconds, it's effectively a Denial-of-service attack.
 
cmpxchg8b said:
No, you misunderstand; someone can misuse it to make the server execute arbitrary scripts.
If I send that event with script_game_start as script id, the server will execute script_game_start. Whether it crashes the server or just stops it for a couple of seconds, it's effectively a Denial-of-service attack.
Ohh...
I never thought that it is possible in this way.

Maybe it is possible to "hack" native servers with it, but how is this possible for someone without the module system of my mod?
I'm not very experienced in such things.
 
Your old script was better in the security sense. It would make more sense to post that version.

Since the python files (your module system files) are just compiled to text files, a person with good knowledge of the text files could easily change which script number is sent to the server. And there are also tools that allow people to reverse engineer the text files to more easily readable module system files.



 
SonKidd said:
Your old script was better in the security sense. It would make more sense to post that version.
Well.
I added Maxim's check method.
Code:
      # multiplayer_event_set_agent_id
      (else_try),
        (eq, ":event_type", multiplayer_event_set_agent_id),
        (store_script_param, ":script_id", 3),

        (player_get_agent_id, ":player_agent_no", ":player_no"),
        (gt, ":player_agent_no", -1),
        (agent_is_alive, ":player_agent_no"),

        (ge, ":script_id", "script_cf_agent_walk"),#added for security
        (le, ":script_id", "script_cf_agent_sprint_stop"),#added for security
        (call_script, ":script_id", ":player_agent_no"),
 
Back
Top Bottom