daily pastebin goal
66%
SHARE
TWEET

Untitled

a guest Jan 25th, 2015 22 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // my remote player end step
  2.  
  3. x = server_x + server_hspeed * global.server_delay;
  4. y = server_y + server_vspeed * global.server_delay;
  5. image_angle = server_angle;
  6.  
  7. switch(server_sprite)
  8. {
  9.     case 0:
  10.     sprite_index = spr_back;
  11.     break;
  12.    
  13.     case 1:
  14.         sprite_index = spr_back;
  15.     break;
  16.    
  17.     case 2:
  18.         sprite_index = spr_base0;
  19.     break;
  20.    
  21.     case 3:
  22.         sprite_index = spr_forward;
  23.     break;
  24.    
  25.     case 4:
  26.         sprite_index = spr_tanktop;
  27.     break;
  28.    
  29.     case 5:
  30.         sprite_index = spr_base;
  31.     break;
  32. }
  33.  
  34. // My remoteplayer create event
  35.  
  36. server_x = 0;
  37. server_y = 0;
  38. server_hspeed = 0;
  39. server_vspeed = 0;
  40. server_angle = 0;
  41. title = "blank";
  42. server_sprite = 0;
  43.  
  44. // my client ibj_controller step event
  45.  
  46. var s, a, xx, yy, hs, vs, angle, spritenumber;
  47.  
  48. if global.server_delaystarted {
  49.     global.server_delay += 1;
  50. }
  51.  
  52. // send position and speed
  53. hbuffer_clear(global.buffer);
  54. hbuffer_write_uint8(global.buffer, 2);
  55. with obj_localplayer {
  56.     if id != other.id {
  57.         hbuffer_write_float32(global.buffer, x);
  58.         hbuffer_write_float32(global.buffer, y);
  59.         hbuffer_write_float32(global.buffer, hspeed);
  60.         hbuffer_write_float32(global.buffer, vspeed);
  61.         hbuffer_write_float32(global.buffer, image_angle);
  62.         switch(sprite_index)
  63.         {
  64.             case spr_back:
  65.                 spritenumber = 1
  66.             break;
  67.             case spr_base0:
  68.                 spritenumber = 2
  69.             break;
  70.             case spr_forward:
  71.                 spritenumber = 3
  72.             break;
  73.             case spr_tanktop:
  74.                 spritenumber = 4
  75.             break;
  76.             case spr_base:
  77.                 spritenumber = 5
  78.             break;
  79.         }
  80.         hbuffer_write_uint8(global.buffer, spritenumber);
  81.        
  82.     }
  83. }
  84. socket_write_message(socket, global.buffer);
  85.  
  86. socket_update_read(socket);
  87.  
  88. while socket_read_message(socket, global.buffer) {
  89.     a = hbuffer_read_uint8(global.buffer);
  90.     switch(a) {
  91.        
  92.         case 1: // player id
  93.         obj_localplayer.player_id = hbuffer_read_uint32(global.buffer);
  94.         break;
  95.        
  96.         case 2: // move remote players
  97.         global.server_delaystarted = true;
  98.         global.server_delay -= 1;
  99.         while !hbuffer_at_end(global.buffer) {
  100.             a = hbuffer_read_uint32(global.buffer);
  101.             xx = hbuffer_read_float32(global.buffer);
  102.             yy = hbuffer_read_float32(global.buffer);
  103.             hs = hbuffer_read_float32(global.buffer);
  104.             vs = hbuffer_read_float32(global.buffer);
  105.             angle = hbuffer_read_float32(global.buffer);
  106.             spritenumber = hbuffer_read_uint8(global.buffer)
  107.             with obj_remoteplayer {
  108.                 if server_id = a {
  109.                     server_x = xx;
  110.                     server_y = yy;
  111.                     server_hspeed = hs;
  112.                     server_vspeed = vs;
  113.                     server_angle = angle;
  114.                     server_sprite = spritenumber;
  115.                 }
  116.             }
  117.         }
  118.         break;
  119.        
  120.         case 3: // create remote player
  121.         a = instance_create(0, 0, obj_remoteplayer and obj_remotetop);
  122.         a.server_id = hbuffer_read_uint32(global.buffer);
  123.         a.name = hbuffer_read_string(global.buffer);
  124.         break;
  125.        
  126.         case 4: // destroy remote player
  127.         a = hbuffer_read_uint32(global.buffer);
  128.         with obj_remoteplayer {
  129.             if server_id = a {
  130.                 instance_destroy();
  131.             }
  132.         }
  133.         break;
  134.        
  135.         case 5:
  136.         var pid = hbuffer_read_uint32(global.buffer);
  137.         var msg = hbuffer_read_string(global.buffer);
  138.        
  139.         with(obj_remoteplayer)
  140.         {
  141.             if(server_id == pid)
  142.             {
  143.                 title =  msg;
  144.             }
  145.         }
  146.         break;
  147.     }
  148. }
  149.  
  150. s = socket_get_state(socket);
  151. if s = 2 and !connected {
  152.     connected = true;
  153. }
  154. if s = 4 {
  155.     show_message("Connection closed.");
  156.     game_end();
  157.     exit;
  158. }
  159. if s = 5 or socket_get_write_data_length(socket) > max_write_data_length
  160. {
  161.     socket_reset(socket);
  162.     if connected {
  163.         show_message("Connection lost.");
  164.     }
  165.     else
  166.     {
  167.         show_message("Could not connect to server.");
  168.     }
  169.     room_goto(rm_titlescreen);
  170.     exit;
  171. }
  172.  
  173. global.server_delay = max(-3, min(10, global.server_delay * 0.95));
  174.  
  175. // my client controller create event
  176.  
  177. var address, name, port;
  178.  
  179. address = get_string("Address:", "69.126.187.169");
  180. name = get_string("Nickname:", "");
  181. /*if show_question("Use delay server?#(Press no if you don't know what this means)") {
  182.     port = 39073;
  183. } else {
  184.     port = 39083;
  185. }
  186. */
  187. port = 39083
  188. socket = socket_create();
  189. socket_connect(socket, address, port);
  190.  
  191. global.buffer = hbuffer_create();
  192. connected = false;
  193.  
  194. global.server_delaystarted = false;
  195. global.server_delay = 0;
  196.  
  197. player_id = 0;
  198.  
  199. var a = instance_create(0, 0, obj_localplayer);
  200. a. name = name;
  201.  
  202. // send name
  203. hbuffer_clear(global.buffer);
  204. hbuffer_write_uint8(global.buffer, 1);
  205. hbuffer_write_string(global.buffer, name);
  206. socket_write_message(socket, global.buffer);
  207.  
  208. // my server controller step event
  209.  
  210. var a, name;
  211.  
  212. global.stepcounter += 1;
  213.  
  214. while listeningsocket_can_accept(listeningsocket) {
  215.    
  216.     global.player_id_counter += 1;
  217.    
  218.     a = instance_create(0, 0, obj_player);
  219.     a.socket = socket_create();
  220.     listeningsocket_accept(listeningsocket, a.socket);
  221.     a.ip = socket_get_peer_address(a.socket);
  222.     a.player_id =  global.player_id_counter;
  223.     a.name = "";
  224.    
  225.     // send player id
  226.     hbuffer_clear(global.buffer);
  227.     hbuffer_write_uint8(global.buffer, 1);
  228.     hbuffer_write_uint32(global.buffer, a.player_id);
  229.     socket_write_message(a.socket, global.buffer);
  230.    
  231. }
  232.  
  233. if !listeningsocket_is_listening(listeningsocket) {
  234.     show_message("Can't listen for incoming connections!");
  235.    game_end();
  236.    exit;
  237. }
  238.  
  239. // my server controller start event
  240.  
  241. listeningsocket = listeningsocket_create();
  242. listeningsocket_start_listening(listeningsocket, false, 39083, false);
  243.  
  244. global.buffer = hbuffer_create();
  245.  
  246. global.stepcounter = 0;
  247. global.player_id_counter = 0;
  248.  
  249. // my server player create event
  250.  
  251. hasstarted = false;
  252.  
  253. client_delaystarted = false;
  254. client_delay = 0;
  255.  
  256. client_x = 0;
  257. client_y = 0;
  258. client_hspeed = 0;
  259. client_vspeed = 0;
  260. client_angle = 0;
  261. client_sprite = 0;
  262.  
  263. // my server player step event
  264.  
  265. var s, a, message, newname, pid, msg;
  266.  
  267. // send position and speed of other players
  268. if hasstarted {
  269.    hbuffer_clear(global.buffer);
  270.    hbuffer_write_uint8(global.buffer, 2);
  271.    // don't send the data if the write buffer is already very large
  272.     if socket_get_write_data_length(socket) < limit_write_data_length {
  273.         with obj_player {
  274.             if id != other.id {
  275.                 hbuffer_write_uint32(global.buffer, player_id);
  276.                 hbuffer_write_float32(global.buffer, x);
  277.                 hbuffer_write_float32(global.buffer, y);
  278.                 hbuffer_write_float32(global.buffer, client_hspeed);
  279.                 hbuffer_write_float32(global.buffer, client_vspeed);
  280.                 hbuffer_write_float32(global.buffer, client_angle);
  281.                 hbuffer_write_uint8(global.buffer, client_sprite);
  282.             }
  283.         }
  284.     }
  285.     socket_write_message(socket, global.buffer);
  286. }
  287.  
  288. socket_update_read(socket);
  289.  
  290. while socket_read_message(socket, global.buffer) {
  291.     a = hbuffer_read_uint8(global.buffer);
  292.     switch(a) {
  293.        
  294.         case 1:
  295.         name = hbuffer_read_string(global.buffer);
  296.         hasstarted = true;
  297.         // send other player ids
  298.         with obj_player {
  299.             if hasstarted and id != other.id {
  300.                 hbuffer_clear(global.buffer);
  301.                 hbuffer_write_uint8(global.buffer, 3);
  302.                 hbuffer_write_uint32(global.buffer, player_id);
  303.                 hbuffer_write_string(global.buffer, name);
  304.                 socket_write_message(other.socket, global.buffer);
  305.             }
  306.         }
  307.         // tell the other players that this player has joined
  308.         with obj_player {
  309.             if hasstarted and id != other.id {
  310.                 hbuffer_clear(global.buffer);
  311.                 hbuffer_write_uint8(global.buffer, 3);
  312.                 hbuffer_write_uint32(global.buffer, other.player_id);
  313.                 hbuffer_write_string(global.buffer, other.name);
  314.                 socket_write_message(socket, global.buffer);
  315.             }
  316.         }
  317.         break;
  318.        
  319.         case 2:
  320.         client_delaystarted = true;
  321.         client_delay -= 1;
  322.         client_x = hbuffer_read_float32(global.buffer);
  323.         client_y = hbuffer_read_float32(global.buffer);
  324.         client_hspeed = hbuffer_read_float32(global.buffer);
  325.         client_vspeed = hbuffer_read_float32(global.buffer);
  326.         client_angle = hbuffer_read_float32(global.buffer);
  327.         client_sprite = hbuffer_read_uint8(global.buffer);
  328.         break;
  329.        
  330.         case 5:
  331.         var pid = hbuffer_read_uint32(global.buffer);
  332.         var msg = hbuffer_read_string(global.buffer);
  333.         hbuffer_clear(global.buffer);
  334.         hbuffer_write_uint8(global.buffer, 5);
  335.         hbuffer_write_uint32(global.buffer, pid);
  336.         hbuffer_write_string(global.buffer, msg);
  337.        
  338.         with(obj_player)
  339.         {
  340.             if (player_id != other.player_id)
  341.             {
  342.                 socket_write_message(socket, global.buffer);
  343.             }
  344.         }
  345.         break;
  346.        
  347.     }
  348. }
  349.  
  350. s = socket_get_state(socket);
  351. if s = 4 or s = 5 or socket_get_write_data_length(socket) > max_write_data_length {
  352.     // tell the other players that this player has left
  353.     with obj_player {
  354.         if hasstarted and id != other.id {
  355.             hbuffer_clear(global.buffer);
  356.             hbuffer_write_uint8(global.buffer, 4);
  357.             hbuffer_write_uint32(global.buffer, other.player_id);
  358.             socket_write_message(socket, global.buffer);
  359.         }
  360.     }
  361.     instance_destroy();
  362.     exit;
  363. }
  364.  
  365. if client_delaystarted {
  366.     client_delay += 1;
  367. }
  368.  
  369. client_delay = max(-3, min(10, client_delay * 0.95));
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top