home *** CD-ROM | disk | FTP | other *** search
/ DarkBasic Professional / DarkBasicPro.iso / data1.cab / Lang_Files_(English) / Help / examples / multiplayer / multiplayer2-example.dba < prev   
Encoding:
Text File  |  2004-09-22  |  7.2 KB  |  293 lines

  1. rem Multiplayer Functionality
  2.  
  3. gosub _findconnections : gosub _waitkey
  4. gosub _findsessions : gosub _waitkey
  5. gosub _createagame : gosub _waitkey
  6. gosub _findplayers : gosub _waitkey
  7. gosub _handlenetgame : gosub _waitkey
  8. gosub _handlesimplemessages : gosub _waitkey
  9. gosub _handlecomplexmessages : gosub _waitkey
  10. gosub _destroyagame
  11.  
  12. rem End of Program
  13. end
  14.  
  15. _findconnections:
  16.  
  17. rem Set Network Connection
  18. perform checklist for net connections
  19. cls : print "CONNECTIONS (Can Occur In Any Order)"
  20. print
  21. for c=1 to checklist quantity()
  22.  print c;". ";checklist string$(c)
  23. next c
  24.  
  25. rem Have User Select a Connection
  26. SelectionIndex=0
  27. if checklist quantity()=0
  28.  print "No Connections Found"
  29. else
  30.  while SelectionIndex<1 or SelectionIndex>checklist quantity()
  31.   print  : input "Select A Number>";SelectionIndex
  32.   input "Enter Additional Data ( IP Address or PhoneNumber )>";data$
  33.  endwhile
  34. endif
  35.  
  36. rem Set Connection
  37. if data$="" then set net connection SelectionIndex
  38. if data$<>"" then set net connection SelectionIndex,data$
  39.  
  40. rem Prompt connection
  41. print : print "SELECTED ";checklist string$(SelectionIndex)
  42.  
  43. return
  44.  
  45. _findsessions:
  46.  
  47. rem Set Network Session (Game On Selected Network Connection)
  48. perform checklist for net sessions
  49. cls : print "SESSIONS (Found on the Selected Connection)"
  50. print
  51. for c=1 to checklist quantity()
  52.  print c;". ";checklist string$(c)
  53. next c
  54.  
  55. rem Have User Select a Session
  56. SessionIndex=0
  57. if checklist quantity()=0
  58.  print "No Sessions Found"
  59. else
  60.  while SessionIndex<1 or SessionIndex>checklist quantity()
  61.   print  : input "Select A Number>";SessionIndex
  62.  endwhile
  63. endif
  64.  
  65. return
  66.  
  67. _createagame:
  68.  
  69. rem Create or Join a game
  70. if SessionIndex=0
  71.  PlayerMax=4 : KindOfGame=1
  72.  create net game "gamename", "playername", PlayerMax, KindOfGame
  73. else
  74.  join net game SessionIndex, "playername"
  75. endif
  76.  
  77. rem Was game created successfully
  78. if net game exists()=1
  79.  print : print "GAME SESSION STARTED"
  80. else
  81.  print "COULD NOT CREATE A SESSION"
  82.  print "PRESS ANY KEY"
  83.  wait key : end
  84. endif
  85.  
  86. return
  87.  
  88. _findplayers:
  89.  
  90. rem Create an EXTRA Player (used rarely)
  91. cls : print : print "EXTRA PLAYER CREATION"
  92. PlayerNumber = create net player("NPC Player")
  93.  
  94. rem Find all current players in game
  95. perform checklist for net players
  96. print : print "PLAYERS (At Time Of Call)"
  97. print
  98. for c=1 to checklist quantity()
  99.  print c;". ";checklist string$(c);" ID:";checklist value a(c);" UNIQUE:";checklist value b(c);" ";
  100.  if checklist value c(c)=1 then print "(me) ";
  101.  if checklist value d(c)=1 then print "(host) ";
  102.  print
  103. next c
  104.  
  105. rem Delete the EXTRA Player (used rarely)
  106. print : print "EXTRA PLAYER DESTRUCTION"
  107. free net player PlayerNumber
  108.  
  109. return
  110.  
  111. _handlenetgame:
  112.  
  113. rem Network game can change during session
  114. while mouseclick()<>2
  115.  cls : print "HANDLE NET GAME (Right Mouse Button To Continue)" : print
  116.  if net game now hosting()=1 then Hosting=1
  117.  if Hosting=1 then PRINT "THIS PLAYER IS NOW HOSTING THE GAME"
  118.  if net game lost()=1 then LostGame=1
  119.  if LostGame=1 then PRINT "THE GAME SESSION WAS LOST"
  120.  returnvalue=net player created()
  121.  if returnvalue>0 then PlayerIn=returnvalue
  122.  if PlayerIn>0 then PRINT "A NEW PLAYER HAS JOINED THE GAME (";PlayerIn; ")"
  123.  returnvalue=net player destroyed()
  124.  if returnvalue>0 then PlayerOut=returnvalue
  125.  if PlayerOut>0 then PRINT "A PLAYER HAS LEFT THE GAME (";PlayerOut; ")"
  126.  sync
  127. endwhile
  128.  
  129. return
  130.  
  131. _handlesimplemessages:
  132.  
  133. rem Network game can communicate numbers and strings
  134. while inkey$()<>"x"
  135.  cls : print "HANDLE NET MESSAGES (Press X To Continue)"
  136.  
  137.  rem Show Latest Return Values
  138.  print
  139.  print "TO: ";ReturnTo
  140.  print "FROM: ";ReturnFrom
  141.  print "INTEGER: ";ReturnInteger
  142.  print "FLOAT: ";ReturnFloat#
  143.  print "STRING: ";ReturnString$
  144.  print
  145.  
  146.  rem Send Part
  147.  if mouseclick()=1
  148.   print "SENDING..."
  149.   send net message integer 0,mousex()
  150.   send net message float 0,mousey()*1.0
  151.   send net message string 0,"hello world"
  152.  endif
  153.  
  154.  rem Receive Part (from all other players)
  155.  get net message
  156.  if net message exists()=1 then print "RECEIVING..."
  157.  while net message exists()=1
  158.   ReturnTo=net message player to()
  159.   ReturnFrom=net message player from()
  160.   if net message type()=1 then ReturnInteger=net message integer()
  161.   if net message type()=2 then ReturnFloat#=net message float()
  162.   if net message type()=3 then ReturnString$=net message string$()
  163.   get net message
  164.  endwhile
  165.  
  166.  sync
  167.  
  168. endwhile
  169.  
  170. return
  171.  
  172. _handlecomplexmessages:
  173.  
  174. rem Network game can communicate sounds, images and 3D data
  175. while inkey$()<>"z"
  176.  
  177.  rem User Prompt
  178.  cls : print "HANDLE ADVANCED NET MESSAGES (Press Z To Continue)"
  179.  print "PRESS [1] through [5] to SEND RESOURCE"
  180.  
  181.  rem Show Latest Return Values
  182.  print
  183.  print "TO: ";ReturnTo
  184.  print "FROM: ";ReturnFrom
  185.  print "MEMBLOCK: ";ReturnMemblockIndex
  186.  print "IMAGE: ";ReturnImageIndex
  187.  print "BITMAP: ";ReturnBitmapIndex
  188.  print "SOUND: ";ReturnSoundIndex
  189.  print "MESH: ";ReturnMeshIndex
  190.  print
  191.  
  192.  rem Send Part
  193.  k$=inkey$()
  194.  if k$>="1" and k$<="5"
  195.   print "SENDING..."
  196.   GuarenteePacket=1
  197.   if k$="1"
  198.    MemblockIndex=1
  199.    make memblock MemblockIndex, 1024
  200.    send net message memblock 0, MemblockIndex, GuarenteePacket
  201.    delete memblock MemblockIndex
  202.   endif
  203.   if k$="2"
  204.    ImageIndex=1
  205.    get image ImageIndex, 0, 0, 64, 64
  206.    send net message image 0, ImageIndex, GuarenteePacket
  207.    delete image ImageIndex
  208.   endif
  209.   if k$="3"
  210.    BitmapIndex=1
  211.    load bitmap "face.bmp", BitmapIndex
  212.    send net message bitmap 0, BitmapIndex, GuarenteePacket
  213.    delete bitmap BitmapIndex
  214.   endif
  215.   if k$="4"
  216.    SoundIndex=1
  217.    load sound "gun.wav", SoundIndex
  218.    send net message sound 0, SoundIndex, GuarenteePacket
  219.    delete sound SoundIndex
  220.   endif
  221.   if k$="5"
  222.    MeshIndex=1
  223.    load mesh "mesh.x", MeshIndex
  224.    send net message mesh 0, MeshIndex, GuarenteePacket
  225.    delete mesh MeshIndex
  226.   endif
  227.  endif
  228.  
  229.  rem Receive Part (from all other players)
  230.  get net message
  231.  if net message exists()=1 then print "RECEIVING..."
  232.  while net message exists()=1
  233.   ReturnTo=net message player to()
  234.   ReturnFrom=net message player from()
  235.   if net message type()=4
  236.    ReturnMemblockIndex=1
  237.    net message memblock ReturnMemblockIndex
  238.    print "MEMBLOCK SIZE:";get memblock size(ReturnMemblockIndex)
  239.    sync
  240.    delete memblock ReturnMemblockIndex
  241.   endif
  242.   if net message type()=5
  243.    ReturnImageIndex=1
  244.    net message image ReturnImageIndex
  245.    paste image ReturnImageIndex,50,50
  246.    sync
  247.    delete image ReturnImageIndex
  248.   endif
  249.   if net message type()=6
  250.    ReturnBitmapIndex=1
  251.    net message bitmap ReturnBitmapIndex
  252.    copy bitmap ReturnBitmapIndex,0
  253.    sync
  254.    delete bitmap ReturnBitmapIndex
  255.   endif
  256.   if net message type()=7
  257.    ReturnSoundIndex=1
  258.    net message sound ReturnSoundIndex
  259.    play sound ReturnSoundIndex
  260.    sync
  261.    delete sound ReturnSoundIndex
  262.   endif
  263.   if net message type()=8
  264.    ReturnMeshIndex=1
  265.    net message mesh ReturnMeshIndex
  266.    backdrop off
  267.    make object 1,ReturnMeshIndex,0
  268.    sync
  269.    delete object 1
  270.    delete mesh ReturnMeshIndex
  271.   endif
  272.   get net message
  273.  endwhile
  274.  
  275.  rem Update screen
  276.  sync
  277.  
  278. endwhile
  279. return
  280.  
  281. _destroyagame:
  282.  
  283. rem Free the current game session
  284. free net game
  285.  
  286. return
  287.  
  288. _waitkey:
  289.  print : print "Press Any Key"
  290.  wait key
  291. return
  292.  
  293.