Advertisement
Guest User

UDP_FPS_Patch

a guest
Jan 11th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SeedRnd MilliSecs()
  2.  
  3. Include "UDPNetwork_lib.bb"
  4.  
  5. Global localID
  6. Global localName$ = "<Guy Fawkes>"
  7.  
  8. Global localPlayerMesh
  9.  
  10. Global hide_pointer
  11.  
  12. Global itemcollcnt
  13.  
  14. Global onground
  15.  
  16. Global all_scene_collisions_on         =     0
  17. Global camera_scene_collisions_on      =     0
  18. Global player_scene_collisions_on      =     1
  19. Global player_player_collisions_on     =     0
  20. Global bullet_scene_collisions_on      =     1
  21. Global bullet_player_collisions_on     =     1
  22. Global player_item_collisions_on       =     1
  23.  
  24. Type player
  25.  
  26.     Field id
  27.  
  28.     Field name$
  29.    
  30.     Field life
  31.    
  32.     Field mesh
  33.    
  34.     Field nextX#,nextY#,nextZ#
  35.     Field previousX#,previousY#,previousZ#
  36.     Field nextYaw#
  37.     Field previousYaw#
  38.     Field nextTime#
  39.     Field previousTime#
  40.    
  41.     Field currentY#
  42.     Field velocityY#
  43.    
  44.     Field r,g,b
  45.  
  46.     Field health_factor
  47.     Field max_health_factor
  48.  
  49.     Field magic_factor
  50.     Field max_magic_factor
  51.  
  52.     Field bullet_factor
  53.     Field max_bullet_factor
  54.  
  55. End Type
  56.  
  57. Type item
  58.  
  59.     Field id
  60.  
  61.     Field xpos#
  62.     Field ypos#
  63.     Field zpos#
  64.    
  65.     Field itemcount
  66.  
  67.     Field r,g,b
  68.  
  69. End Type
  70.  
  71. Type bullet
  72.  
  73.     Field mesh
  74.     Field angle#
  75.     Field time
  76.     Field id ; id of player who fired
  77.  
  78. End Type
  79.  
  80. Global maxhealthpacks = 4
  81.  
  82. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  83. ; Camera code from jfk EO-11110, thanks
  84. Global bounce_cam=1 ;  smooth camera motion? (0/1)
  85. Global bounce_div#=30.0 ; the higher, the more sluggish the camera will follow. Recc: 3.0 to 20.0
  86. ; some globals used by ChaseCam
  87. Global old_x#,old_y#,old_z#
  88. ; desired distance between camera and player mesh
  89. Global x_dis#=7.0,y_dis#=1,z_dis#=7.0,y_dis_or#=y_dis#
  90. ;Camera Collision Type
  91. Global type_camera = 1
  92. ;Player Collision Type
  93. Global type_player = 2
  94. ;Scene Collision Type
  95. Global type_scene = 3
  96. ;Bullet Collision Type
  97. Global type_bullet = 4
  98. ;Item Collision Type
  99. Global type_item = 5
  100. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  101.  
  102. api_ShowWindow ( SystemProperty$ ( "apphWnd" ), 1 )
  103.  
  104. ; network init ----------------------------------------;
  105. Global localMode = Net_StartInput() ; bring the library "console" to connect
  106.  
  107. If localMode = 1 Or localMode = 2 ; 1 means we are client, 2 means we are server
  108.  
  109.     If localMode = 1 Then AppTitle("Client : " + localName + " (port:" + net_port + ")")
  110.     If localMode = 2 Then AppTitle("Server : " + localName + " (port:" + net_port + ")")
  111.    
  112.     localId = Net_CreatePlayer(localName) ; this command inits the server or local client
  113.    
  114.     If localId = 0 ; error
  115.         Net_StopNetwork() ; close the network properly
  116.         RuntimeError("Failed to create player.")
  117.     EndIf
  118.    
  119. Else ; error
  120.     Net_StopNetwork()
  121.     RuntimeError("Failed to start game.")
  122. EndIf
  123. ;------------------------------------------------------;
  124.  
  125. Graphics3D 640,480,32,2
  126. SetBuffer BackBuffer()
  127.  
  128. api_ShowWindow ( SystemProperty$ ( "apphWnd" ), 1 )
  129.  
  130. CreateScene()
  131.  
  132. Global camera = CreateCamera()
  133. CameraRange camera, 0.1, 10000.0
  134. EntityPickMode camera, 2
  135. EntityType camera, type_camera
  136. EntityRadius camera, 0.1
  137. CameraFogMode camera, 1
  138. CameraFogColor camera, 38, 107, 252
  139. CameraFogRange camera, 0.0, 250.0
  140.  
  141. Global sky = CreateCube()
  142. EntityFX sky, 1+8
  143. ScaleEntity sky, 25, 25, 25
  144. EntityColor sky, 38, 107, 252
  145. EntityOrder sky, 1000
  146. FlipMesh sky
  147.  
  148. ; create our own local player
  149. localPlayerMesh = CreatePlayer(localID,localName)
  150. old_x = EntityX(localPlayerMesh):old_y = EntityY(localPlayerMesh):old_z = EntityZ(localPlayerMesh) ; update camera position
  151.  
  152. ; we send our color to the server
  153. p.player = Object.player(GetPlayer(localID))
  154. Net_SendMessage(3,p\r + "/" + p\g + "/" + p\b)
  155.  
  156. ; variables used to send our positions at a constant delay (saves bandwidth but makes a constant ping)
  157. Global updatePosition = MilliSecs()
  158. Global updateTick = 50
  159.  
  160. ; chat
  161. Global messageDisplayNumber = 10
  162. Global messageOffset
  163. Dim message$(messageDisplayNumber)
  164.  
  165. Global chatMode
  166. Global chatMessage$ = localName + ": "
  167. Global chatMillisecs = MilliSecs()
  168. Global chatCursor
  169.  
  170. While Not KeyDown(1)
  171.  
  172.     If all_scene_collisions_on
  173.    
  174.         ;; ALL COLLISIONS
  175.  
  176.         camera_scene_collisions_on      =     1
  177.         player_scene_collisions_on      =     1
  178.         player_player_collisions_on     =     1
  179.         bullet_scene_collisions_on      =     1
  180.         bullet_player_collisions_on     =     1
  181.         player_item_collisions_on       =     1
  182.  
  183.     Else
  184.  
  185.         ;; FEW COLLISIONS
  186.  
  187.         If  camera_scene_collisions_on Then Collisions type_camera, type_scene, 2, 2 ;camera to scene
  188.         If  player_scene_collisions_on Then Collisions type_player, type_scene, 2, 3 ; player to scene
  189.         If  player_player_collisions_on Then Collisions type_player, type_player, 1, 3 ; player to player
  190.         If  bullet_scene_collisions_on Then Collisions type_bullet, type_scene, 2, 1 ; bullet to scene
  191.         If  bullet_player_collisions_on Then Collisions type_bullet, type_player, 1, 1 ; bullet to player
  192.         If  player_item_collisions_on Then Collisions type_player, type_item, 1, 1  ; player to item
  193.  
  194.     EndIf
  195.  
  196.     ;; ALL the Collisions on at the SAME TIME
  197.     If all_scene_collisions_on
  198.  
  199.         Collisions type_camera, type_scene, 2, 3 ; camera to scene
  200.         Collisions type_player, type_scene, 2, 3 ; player to scene
  201.         Collisions type_player, type_player, 1, 3 ; player to player
  202.         Collisions type_bullet, type_scene, 2, 1 ; bullet to scene
  203.         Collisions type_bullet, type_player, 1, 1 ; bullet to player
  204.         Collisions type_player, type_item, 1, 1  ; player to item
  205.        
  206.     EndIf
  207.  
  208.     ; update our player
  209.     UpdateLocalPlayer()
  210.  
  211.     ; see the function, the interpolation variables are set properly here
  212.     UpdateNetwork()
  213.    
  214.     ; the interpolation is made in this function
  215.     UpdatePlayers()
  216.    
  217.     ; update the bullets... only the server check for player kill
  218.     UpdateBullets()
  219.  
  220.     UpdateItems()
  221.  
  222.     UpdateWorld()
  223.     RenderWorld()
  224.  
  225.         DrawPlayersNames()
  226.    
  227.             If KeyHit(28) And chatMode = 0 Then chatMode = 1 : FlushKeys()
  228.    
  229.             If chatMode = 1 Then UpdateChat()
  230.    
  231.         DrawMessages()
  232.  
  233.     Flip
  234.  
  235. Wend
  236.  
  237. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  238. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  239.  
  240. Net_StopNetwork()
  241.  
  242. End
  243.  
  244. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  245. Function Jump ( )
  246.  
  247.     For p.player = Each player
  248.  
  249.         ; check if player is on the ground
  250.         groundcoll = LinePick(EntityX(p\mesh),EntityY(p\mesh),EntityZ(p\mesh),0,-1.2,0)
  251.  
  252.         If PickedEntity()
  253.  
  254.             onground = 1
  255.  
  256.             ;Detect Ceiling
  257.             ceilingpicked = LinePick ( EntityX ( p\mesh ), EntityY ( p\mesh ), EntityZ ( p\mesh ), 0.0, 300.0, 0.0 )
  258.    
  259.             ;Stop Ceiling "Floating"
  260.             If ceilingpicked
  261.    
  262.                 p\velocityY = 0.0
  263.    
  264.             Else
  265.    
  266.                 p\velocityY = 0.5 ; set "inverse" of gravity velocity
  267.  
  268.             EndIf
  269.            
  270.         Else
  271.        
  272.             onground = 0
  273.    
  274.         EndIf
  275.        
  276.     Next
  277.  
  278. End Function
  279.  
  280. Function UpdateChaseCam(camera,player) ; thanks to jfk EO-11110 on bb forums for this function !
  281.     PositionEntity camera,EntityX(player,1),EntityY(player,1),EntityZ(player,1)
  282.     ResetEntity camera
  283.     x#=EntityX(player)+Sin(-EntityYaw(player)+180)*x_dis#
  284.     y#=EntityY(player)+y_dis#
  285.     z#=EntityZ(player)+Cos(-EntityYaw(player)+180)*z_dis#
  286.     obstacle=LinePick(EntityX(player),EntityY(player),EntityZ(player),(x-EntityX(player)),y_dis#,(z-EntityZ(player)),0.1)
  287.     If obstacle=0
  288.         new_x#=x
  289.         new_y#=y
  290.         new_z#=z
  291.     Else
  292.         new_x#=PickedX()
  293.         new_y#=PickedY()
  294.         new_z#=PickedZ()
  295.     EndIf
  296.     If bounce_cam=0
  297.         PositionEntity camera,new_x,new_y,new_z
  298.     Else
  299.         x=old_x+((New_x-old_x)/bounce_div#)
  300.         y=old_y+((New_y-old_y)/bounce_div#)
  301.         z=old_z+((New_z-old_z)/bounce_div#)
  302.         PositionEntity camera,x,y,z
  303.     EndIf
  304.     PointEntity camera,player
  305.     TurnEntity camera,-20,0,0     ; if you want the player mesh more on the bottom of the screen
  306.     old_x#=EntityX(camera)
  307.     old_y#=EntityY(camera)
  308.     old_z#=EntityZ(camera)
  309. End Function
  310.  
  311. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  312. Function UpdateNetwork()
  313.  
  314.     ; update incoming packets (better check ALL incoming messages so use While Wend)
  315.     While Net_CheckMessage() ; will check for a new message and fill Net_MsgType, Net_MsgData$, Net_MsgFrom and Net_MsgTo variables so we can read it
  316.        
  317.         p.player = Object.player(GetPlayer(Net_MsgFrom)) ; get the player handle
  318.        
  319.         Select Net_MsgType ; check the type of message
  320.  
  321.             Case 1 ; chat message
  322.            
  323.                 NewMessage(Net_MsgData)
  324.                                    
  325.             Case 2 ; player position and angle, example of packing data into string
  326.                
  327.                 offset1 = Instr(Net_MsgData,"/",1)
  328.                 offset2 = Instr(Net_MsgData,"/",offset1+1)
  329.                 offset3 = Instr(Net_MsgData,"/",offset2+1)
  330.                                
  331.                 p\previousX = p\nextX
  332.                 p\previousY = p\nextY
  333.                 p\previousZ = p\nextZ
  334.                 p\previousYaw = p\nextYaw
  335.                
  336.                 p\nextX = Left(Net_MsgData,offset1-1)
  337.                 p\nextY = Mid(Net_MsgData,offset1+1,offset2-offset1-1)
  338.                 p\nextZ = Mid(Net_MsgData,offset2+1,offset3-offset2-1)
  339.                 p\nextYaw = Right(Net_MsgData,Len(Net_MsgData)-offset3)
  340.                                
  341.                 p\previousTime = p\nextTime
  342.                 p\nextTime = MilliSecs()
  343.                            
  344.             Case 3 ; update player colors
  345.                        
  346.                 offset1 = Instr(Net_MsgData,"/",1)
  347.                 offset2 = Instr(Net_MsgData,"/",offset1+1)
  348.                            
  349.                 p\r = Left(Net_MsgData,offset1-1)
  350.                 p\g = Mid(Net_MsgData,offset1+1,offset2-offset1-1)
  351.                 p\b = Right(Net_MsgData,Len(Net_MsgData)-offset2)
  352.                                
  353.                 EntityColor p\mesh,p\r,p\g,p\b
  354.                
  355.             Case 4 ; a player fired
  356.            
  357.                 CreateBullet(Net_MsgFrom)
  358.            
  359.             Case 5 ; a player lost some life
  360.            
  361.                 p\life = Net_MsgData
  362.            
  363.             Case 6 ; a player killed another one
  364.            
  365.                 k.player = Object.player(GetPlayer(Net_MsgData)) ; killed player ID in MsgData
  366.                 NewMessage(p\name + " killed " + k\name)
  367.                 ResetPlayer(k\id)
  368.  
  369.             Case 100 ; new player connected, OR the server tells us who is already connected so we can create players when joining the game
  370.                            
  371.                 If localMode = 2
  372.                     For p.player = Each player
  373.                         Net_SendMessage(2,p\nextX + "/" + p\nextY + "/" + p\nextZ + "/" + p\nextYaw,p\id,Net_MsgFrom)
  374.                         Net_SendMessage(3,p\r + "/" + p\g + "/" + p\b,p\id,Net_MsgFrom)
  375.                        
  376.                         Net_SendMessage(5,p\life,p\id,Net_MsgFrom)
  377.                     Next
  378.                 EndIf
  379.                
  380.                 NewMessage(Net_MsgData + " connected")
  381.                 CreatePlayer(Net_MsgFrom,Net_MsgData)
  382.                
  383.             Case 101 ; a player has quit
  384.                        
  385.                 For p.player = Each player
  386.                     If Net_MsgFrom = p\id
  387.                         NewMessage(p\name + " disconnected")
  388.                         FreeEntity p\mesh
  389.                         Delete p
  390.                     EndIf
  391.                 Next
  392.  
  393.             Case 102 ; host has changed
  394.  
  395.                 For p.player = Each player
  396.                     If Net_MsgFrom = p\id
  397.                         If p\id = localID
  398.                             AppTitle("Server : " + localName + " (port:" + net_port + ")")
  399.                             NewMessage("You are the new host")
  400.                             localMode = 2
  401.                         Else
  402.                             NewMessage(p\name + " is the new host")
  403.                         EndIf                  
  404.                     EndIf
  405.                 Next
  406.  
  407.         End Select
  408.  
  409.     Wend
  410.  
  411. End Function
  412.  
  413. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  414. Function UpdateLocalPlayer()
  415.  
  416.     ; update our player
  417.     p.player = Object.player(GetPlayer(localID)) ; get our player
  418.  
  419.         ; inputs
  420.         If chatmode = 0
  421.             If KeyDown(200) Or KeyDown(17) Then MoveEntity p\mesh,0,0,0.3
  422.             If KeyDown(208) Or KeyDown(31) Then MoveEntity p\mesh,0,0,-0.3
  423.             If KeyDown(203) Or KeyDown(30) Then TurnEntity p\mesh,0,3,0
  424.             If KeyDown(205) Or KeyDown(32) Then TurnEntity p\mesh,0,-3,0
  425.         EndIf
  426.        
  427.         ; fire
  428.         If KeyDown(47)
  429.             CreateBullet(localID)
  430.             Net_SendMessage(4,"")
  431.         EndIf
  432.  
  433.         If hide_pointer <> 0 Then HidePointer ( )
  434.         If hide_pointer  = 0 Then ShowPointer ( )
  435.  
  436.         If Not ( KeyDown ( 29 ) Or KeyDown ( 157 ) )
  437.  
  438.             ; jump
  439.             If KeyHit(57) And chatmode = 0 Then Jump ( )
  440.  
  441.         Else
  442.  
  443.             ; spawn items
  444.             If KeyHit(57) And chatmode = 0
  445.  
  446.                 For x = 1 To maxhealthpacks
  447.  
  448.                     i.item = New item
  449.                     i\id = CreateCube()
  450.                     i\itemcount = ( maxhealthpacks )
  451.                     PositionEntity i\id, Rnd ( -25, 25 ), 1.001, ( Rnd ( -25, 25 ) )
  452.                     EntityPickMode i\id, 2
  453.                     EntityType i\id, type_item
  454.                     EntityRadius i\id, 1
  455.                     EntityColor i\id, 0, ( Rnd ( 64, 255 ) ), ( Rnd ( 64, 255 ) )
  456.                     EntityAlpha i\id, ( Rnd ( 0.25, 0.65 ) )
  457.            
  458.                 Next
  459.  
  460.             EndIf
  461.        
  462.         EndIf
  463.        
  464.         ; we create a message type "2" for player position
  465.         If MilliSecs()-updatePosition > updateTick 
  466.             Net_SendMessage(2,EntityX#(p\mesh, 1) + "/" + EntityY#(p\mesh, 1) + "/" + EntityZ#(p\mesh, 1) + "/" + EntityYaw#(p\mesh, 1))
  467.             updatePosition = MilliSecs()
  468.         EndIf
  469.  
  470.      If MouseDown(1)
  471.    
  472.          mxs#=MouseXSpeed()/4.0
  473.          mys#=(MouseYSpeed()/100.0)
  474.    
  475.              y_dis=y_dis+mys
  476.              If y_dis<-y_dis_or*2 Then y_dis=-y_dis_or*2
  477.              If y_dis>y_dis_or*15 Then y_dis=y_dis_or*15
  478.    
  479.          MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
  480.          TurnEntity p\mesh,0,-mxs,0
  481.    
  482.      EndIf
  483.    
  484.      If ( MouseDown ( 1 ) And MouseDown ( 2 ) )
  485.    
  486.         MoveEntity p\mesh, 0, 0, 0.3
  487.    
  488.      EndIf
  489.  
  490. UpdateChaseCam(camera,p\mesh)
  491.  
  492. End Function
  493.  
  494. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  495. Function UpdatePlayers()
  496.  
  497.     For p.player = Each player
  498.                
  499.         If p\id <> localID ; update other players
  500.        
  501.             ResetEntity p\mesh ; reset entity is useful here because of collisions
  502.        
  503.             ;; LINEAR INTERPOLATION ;;;;;;;;;;
  504.             curTime# = (MilliSecs()-p\nextTime) / (p\nextTime-p\previousTime) ; curTime will be ideally 0.0-1.0, if it's > 1.0 then it performs extrapolation
  505.             PositionEntity p\mesh,p\previousX+(p\nextX-p\previousX)*curTime,p\previousY+(p\nextY-p\previousY)*curTime,p\previousZ+(p\nextZ-p\previousZ)*curTime
  506.            
  507.             ; special fix for yaw as EntityYaw returns a value beetween +180 ans -180, the interpolation can go backward
  508.             If p\previousYaw < 0 Then p\previousYaw = p\previousYaw + 360
  509.             If p\nextYaw < 0 Then p\nextYaw = p\nextYaw + 360
  510.             If Abs(p\nextYaw-p\previousYaw) > 180
  511.                 If p\nextYaw > p\previousYaw
  512.                     p\previousYaw = p\previousYaw + 360
  513.                 Else
  514.                     p\previousYaw = p\previousYaw - 360
  515.                 EndIf
  516.             EndIf
  517.            
  518.             RotateEntity p\mesh,0,p\previousYaw+(p\nextYaw-p\previousYaw)*curTime,0
  519.             ;;;;;;;;;;;;;;;;;;;;;;;;;;;
  520.  
  521.         Else ; update our player, basically it just compute gravity
  522.  
  523.             p\velocityY = p\velocityY - 0.003 ; fake gravity
  524.             If EntityY(p\mesh) - p\currentY >= 0 ; if player is not falling we limit the gravity
  525.                 If p\velocityY < -0.003 Then p\velocityY = -0.003
  526.             EndIf
  527.  
  528.             p\currentY = EntityY(p\mesh)
  529.             TranslateEntity p\mesh,0,p\velocityY,0
  530.  
  531.         EndIf
  532.        
  533.     Next
  534.  
  535. End Function
  536.  
  537. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  538. Function CreatePlayer(id,name$)
  539.  
  540.     p.player = New player
  541.     p\id = id
  542.     p\name = name
  543.    
  544.     p\life = 10
  545.    
  546.     p\mesh = CreateCube()
  547.     EntityType p\mesh,type_player
  548.     EntityRadius p\mesh,1.0,1.0
  549.  
  550.     ScaleEntity p\mesh, 1, 1, 1
  551.  
  552.     NameEntity p\mesh,Handle(p)
  553.    
  554.     nose = CreateSphere(8,p\mesh):EntityColor nose,255,0,0
  555.     PositionEntity nose,0,0,1
  556.     ScaleEntity nose,0.3, 0.3, 0.3
  557.    
  558.     p\nextX = -20
  559.     p\nextY = 1
  560.     p\nextZ = -20
  561.     p\nextYaw = -45
  562.    
  563.     p\previousX = p\nextX
  564.     p\previousY = p\nextY
  565.     p\previousZ = p\nextZ
  566.     p\previousYaw = p\nextYaw
  567.    
  568.     p\r = 0
  569.     p\g = Rand ( 64, 255 )
  570.     p\b = Rand ( 64, 255 )
  571.     EntityColor p\mesh,p\r,p\g,p\b
  572.    
  573.     PositionEntity p\mesh,p\nextX,p\nextY,p\nextZ
  574.     RotateEntity p\mesh,0,p\nextYaw,0
  575.     ResetEntity p\mesh
  576.    
  577.     p\currentY = p\nextY
  578.    
  579.     p\previousTime = MilliSecs()
  580.     p\nextTime = MilliSecs()+1
  581.  
  582.     Return p\mesh
  583.  
  584. End Function
  585.  
  586. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  587. Function ResetPlayer(id)
  588.  
  589.     p.player = Object.player(GetPlayer(id))
  590.  
  591.     p\life = 90
  592.  
  593.     p\nextX = -20
  594.     p\nextY = 1
  595.     p\nextZ = -20
  596.     p\nextYaw = -45
  597.  
  598.     p\previousX = p\nextX
  599.     p\previousY = p\nextY
  600.     p\previousZ = p\nextZ
  601.     p\previousYaw = p\nextYaw
  602.  
  603.     PositionEntity p\mesh,p\nextX,p\nextY,p\nextZ
  604.     RotateEntity p\mesh,0,p\nextYaw,0
  605.     ResetEntity p\mesh
  606.    
  607.     p\currentY = p\nextY
  608.    
  609.     p\previousTime = MilliSecs()
  610.     p\nextTime = MilliSecs()+1
  611.  
  612. End Function
  613.  
  614. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  615. Function GetPlayer(id)
  616.  
  617.     For p.player = Each player
  618.         If p\id = id
  619.             Return Handle(p)                   
  620.         EndIf
  621.     Next
  622.  
  623. End Function
  624.  
  625. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  626. Function GetItem(id)
  627.  
  628.     For i.item = Each item
  629.         If i\id = id
  630.             Return Handle(i)
  631.         EndIf
  632.     Next
  633.  
  634. End Function
  635.  
  636. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  637. Function DrawPlayersNames()
  638.  
  639.     For p.player = Each player
  640.    
  641.         Color 255,255,255
  642.         CameraProject(camera,EntityX(p\mesh),EntityY(p\mesh),EntityZ(p\mesh))          
  643.         If p\id <> localID Then Text ProjectedX(),ProjectedY()-60,p\name,1,1
  644.        
  645.         Color 255,(255.0/100.0)*p\life,(255.0/100.0)*p\life
  646.         Text ProjectedX(),ProjectedY()-40,p\life,1,1
  647.  
  648.     Next
  649.    
  650. End Function
  651.  
  652. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  653. Function CreateBullet(id)
  654.  
  655.     p.player = Object.player(GetPlayer(id))
  656.  
  657.     b.bullet = New bullet
  658.    
  659.     b\id = p\id
  660.    
  661.     b\mesh = CreateSphere(2)
  662.     ScaleEntity b\mesh,0.2,0.2,0.2
  663.     EntityColor b\mesh,255,0,0
  664.        
  665.     PositionEntity b\mesh,EntityX(p\mesh),EntityY(p\mesh),EntityZ(p\mesh)
  666.     RotateEntity b\mesh,0,EntityYaw(p\mesh),0
  667.  
  668.     EntityType b\mesh,type_bullet
  669.     EntityRadius b\mesh,0.2
  670.        
  671.     b\time = MilliSecs()
  672.  
  673. End Function
  674.  
  675. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  676. Function UpdateBullets()
  677.  
  678.     For b.bullet = Each bullet
  679.    
  680.         MoveEntity b\mesh,0,0,1
  681.        
  682.         If CountCollisions(b\mesh)
  683.        
  684.             If localMode = 2 ; only the server compute bullets collision with player
  685.                 If GetEntityType(CollisionEntity(b\mesh,1)) = type_player ; a player is hit
  686.                
  687.                     touchedPlayer.player = Object.player(EntityName(CollisionEntity(b\mesh,1)))
  688.                     touchedPlayer\life = touchedPlayer\life - 10
  689.                    
  690.                     If touchedPlayer\life > 0 ; touched
  691.                         Net_SendMessage(5,touchedPlayer\life,touchedPlayer\id)
  692.                         Net_SendMessage(5,touchedPlayer\life,touchedPlayer\id,touchedPlayer\id)
  693.                     Else ; killed
  694.                         killerPlayer.player = Object.player(GetPlayer(b\id))
  695.                         Net_SendMessage(6,touchedPlayer\id,killerPlayer\id)
  696.                         Net_SendMessage(6,touchedPlayer\id,killerPlayer\id,killerPlayer\id)
  697.                         NewMessage(killerPlayer\name + " killed " + touchedPlayer\name)
  698.                         ResetPlayer(touchedPlayer\id)
  699.                     EndIf
  700.                
  701.                 EndIf
  702.             EndIf
  703.            
  704.             FreeEntity b\mesh
  705.             Delete b
  706.            
  707.         ElseIf MilliSecs()-b\time > 2000
  708.        
  709.             FreeEntity b\mesh
  710.             Delete b
  711.            
  712.         EndIf
  713.    
  714.     Next
  715.  
  716. End Function
  717.  
  718. Function UpdateItems ( )
  719.  
  720.     ;For every Player in the Scene
  721.     For p.player = Each player
  722.  
  723.         ;For every Item in the Scene
  724.         For i.item = Each item
  725.  
  726.             TurnEntity i\id, 0, -1.0, 0
  727.  
  728.             ;Is Item Collision on ?
  729.             If player_item_collisions_on
  730.  
  731.                 ;How many times did the Player hit an Item ?
  732.                 itemcollcnt = CountCollisions ( i\id )
  733.  
  734.                 ;Count the amount of collisions between an item
  735.                 For collcnt = 1 To itemcollcnt
  736.  
  737.                     ;Get Collision between Player & Item
  738.                     healthpackcoll = CollisionEntity ( p\mesh, itemcollcnt )
  739.  
  740.                     ;If there was a collision between Player & Item
  741.                     If GetEntityType ( healthpackcoll ) = type_item
  742.  
  743.                         ;Get the Touched Item
  744.                         ;touchedItem.item = Object.item(GetItem(i\id))
  745.                            
  746.                         ; If the Player already has Max Health
  747.                         If p\life < 90
  748.                            
  749.                             ; Raise the Player's Health by a Factor
  750.                             p\life = p\life + 10
  751.                            
  752.                                 ;Send the Message to the Network
  753.                                 Net_SendMessage(5,p\life,p\id,Net_MsgFrom)
  754.                        
  755.                                 ; Delete the touched Object from Game
  756.                                 FreeEntity i\id
  757.                                                
  758.                             ; Remove each touched Item from Memory
  759.                             Delete i
  760.                                                    
  761.                         ; Otherwise
  762.                         Else
  763.                        
  764.                             ; Do not let the Player get any more Health
  765.                             p\life = 90
  766.    
  767.                                 ;Send the Message to the Network
  768.                                 Net_SendMessage(5,p\life,p\id,Net_MsgFrom)
  769.  
  770.                                 ; Delete the touched Object from Game
  771.                                 FreeEntity i\id
  772.                        
  773.                             ; Remove each touched Item from Memory
  774.                             Delete i
  775.                                
  776.                         EndIf
  777.                            
  778.                     EndIf
  779.                    
  780.                 Next
  781.                
  782.             Else
  783.  
  784.                 ; Shoot a pick Line from the player straight to Item
  785.                 itempicked = LinePick ( EntityX# ( p\mesh, 1 ), EntityY# ( p\mesh, 1 ), EntityZ# ( p\mesh, 1 ), 0.0, 0.0, 1.0 )
  786.  
  787.                 ; If the Player is within the vicinity of an Item, or the Player "sees" the Item
  788.                 If EntityDistance ( p\mesh, i\id ) <= 2.0 Or ( EntityDistance ( p\mesh, i\id ) <= 2.0 And itempicked ) Or ( EntityDistance ( p\mesh, i\id ) <= 2.0 And EntityInView ( i\id, camera ) )
  789.  
  790.                     ; If the Player already has Max Health
  791.                     If p\life < 90
  792.                            
  793.                         ; Raise the Player's Health by a Factor
  794.                         p\life = p\life + 10
  795.                                
  796.                             ;Send the Message to the Network
  797.                             Net_SendMessage(5,p\life,p\id,Net_MsgFrom)
  798.  
  799.                             ; Delete the touched Object from Game
  800.                             FreeEntity i\id
  801.                        
  802.                         ; Remove Each touched Item from Memory
  803.                         Delete i
  804.    
  805.                     ; Otherwise
  806.                     Else
  807.                                                
  808.                         ; Don't give the Player anymore Health
  809.                         p\life = 90
  810.                                
  811.                             ;Send the Message to the Network
  812.                             Net_SendMessage(5,p\life,p\id,Net_MsgFrom)
  813.  
  814.                             ; Delete the touched Object from Game
  815.                             FreeEntity i\id
  816.                        
  817.                         ; Remove Each touched Item from Memory
  818.                         Delete i
  819.                                
  820.                     EndIf
  821.                                    
  822.                 EndIf
  823.  
  824.             EndIf
  825.  
  826.         Next
  827.  
  828.         PositionEntity sky, EntityX# ( p\mesh, 1 ), EntityY# ( p\mesh, 1 ), EntityZ# ( p\mesh, 1 ), 1
  829.    
  830.     Next
  831.  
  832. End Function
  833.  
  834. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  835. Function NewMessage(msg$)
  836.  
  837.     messageOffset = messageOffset + 1
  838.    
  839.     If messageOffset > messageDisplayNumber
  840.    
  841.         messageOffset = messageOffset - 1
  842.        
  843.         For i = 2 To messageDisplayNumber
  844.             message(i-1) = message(i)      
  845.         Next
  846.        
  847.         message(messageDisplayNumber) = msg
  848.    
  849.     Else
  850.    
  851.         message(messageOffset) = msg
  852.    
  853.     EndIf
  854.  
  855. End Function
  856.  
  857. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  858. Function DrawMessages()
  859.  
  860.     Color 255,255,255
  861.  
  862.     For i1 = 1 To messageOffset
  863.    
  864.         Text 0,(i1-1)*15,message(i1)   
  865.  
  866.     Next
  867.    
  868. End Function
  869.  
  870. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  871. Function UpdateChat()
  872.  
  873.     key = GetKey()
  874.  
  875.     If key
  876.         If key = 13
  877.             If chatMessage <> localName + ": "
  878.                 For i = Len(localName + ": ") To Len(chatMessage)
  879.                     If Mid(chatMessage,i,1) <> " "
  880.                         Net_SendMessage(1,chatMessage)
  881.                         NewMessage(chatMessage)
  882.                         Exit
  883.                     EndIf
  884.                 Next
  885.             EndIf
  886.             chatMessage = localName + ": "
  887.             chatMode = 0
  888.         ElseIf key = 8 And Len(chatMessage) > Len ( localName + ": " )
  889.             chatMessage = Left(chatMessage,Len(chatMessage)-1)
  890.         ElseIf key > 31 And key < 127
  891.             chatMessage = chatMessage + Chr(key)
  892.         EndIf
  893.     EndIf
  894.    
  895.     Color 255,255,255
  896.     Rect 0,GraphicsHeight()-20,GraphicsWidth(),20,1
  897.    
  898.     Color 0,0,0
  899.     Rect 0,GraphicsHeight()-20,GraphicsWidth(),20,0
  900.    
  901.    
  902.     If MilliSecs()-chatMillisecs > 500
  903.         chatCursor = Not chatCursor
  904.         chatMillisecs = MilliSecs()
  905.     EndIf
  906.    
  907.     If chatCursor
  908.         Text 5,GraphicsHeight()-17,chatMessage + "|"
  909.     Else
  910.         Text 5,GraphicsHeight()-17,chatMessage
  911.     EndIf
  912.  
  913. End Function
  914.  
  915. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  916. Function CreateScene()
  917.  
  918.     texture = TextureCreate(128)
  919.    
  920.     ground = CreatePlane()
  921.     EntityColor ground,150,150,150
  922.     EntityTexture ground,texture
  923.    
  924.     cube = CreateCube():EntityColor cube,150,200,150
  925.     ScaleEntity cube,5,5,5:PositionEntity cube,0,5,0
  926.     EntityTexture cube,texture
  927.    
  928.     sphere = CreateSphere(16):EntityColor sphere,200,150,150
  929.     ScaleEntity sphere,3,1,3:PositionEntity sphere,0,9.7,0
  930.    
  931.     platform = CreateCube():EntityColor platform,150,150,200
  932.     ScaleEntity platform,3,1,10:PositionEntity platform,0,4.13,-13.15
  933.     TurnEntity platform,-30,0,0
  934.     EntityTexture platform,texture
  935.  
  936.     FreeTexture texture
  937.    
  938.     EntityType ground,type_scene:EntityPickMode ground,2
  939.     EntityType cube,type_scene:EntityPickMode cube,2
  940.     EntityType sphere,type_scene:EntityPickMode sphere,2
  941.     EntityType platform,type_scene:EntityPickMode platform,2
  942.  
  943.     light = CreateLight()
  944.     TurnEntity light,30,70,0
  945.  
  946.     For x = 1 To maxhealthpacks
  947.  
  948.         i.item = New item
  949.         i\id = CreateCube()
  950.         i\itemcount = ( maxhealthpacks + 1 )
  951.         PositionEntity i\id, Rnd ( -25, 25 ), 1.001, ( Rnd ( -25, 25 ) )
  952.         EntityPickMode i\id, 2
  953.         EntityType i\id, type_item
  954.         EntityRadius i\id, 1
  955.         EntityColor i\id, 0, ( Rnd ( 64, 255 ) ), ( Rnd ( 64, 255 ) )
  956.         EntityAlpha i\id, ( Rnd ( 0.25, 0.65 ) )
  957.  
  958.     Next
  959.  
  960. End Function
  961.  
  962. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  963. Function TextureCreate(size)
  964.     texture = CreateTexture(size,size,9)
  965.     SetBuffer TextureBuffer(texture)
  966.     Color 255,255,255
  967.     Rect 0,0,size,size,1
  968.     Color 0,0,0
  969.     Rect 0,0,size,size,0
  970.     Line 0,0,size,size
  971.     Line 0,size,size,0
  972.     SetBuffer BackBuffer()
  973.     Return texture
  974. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement