@"MMORPG programs"
This code was thrown together by me roughly a few weeks after I got DBP. It's thrown together from tutorials, code snippets and some of my own experimenting. It is poorly formatted, with little or no remarks, and is very messy. It does however work for client and server on same machine. I haven't messed with directplay networking since this bit of code, mainly b/c directplay is so slow and the number of connections is limited.
But if you must have it, here is what I've got.
Again:
* WARNING * Ugly code ahead:
(dbp)
Rem Project: merged net and play
Rem Created: 3/22/2005 9:52:28 PM
Rem ***** Main Source File *****
Rem Project: mm-new-exp
Rem Created: 3/21/2005 10:40:10 PM
dim cnx$(10)
rem *********************************************************
Rem ***** Main Source File *****
rem Multiplayer Functionality
gosub _findconnections : sync
gosub _findsessions : sync
gosub _createagame : sync
gosub _findplayers : sync
print "click to send a test message"
DO
gosub _handlenetgame : sync
rem sync
if net message exists()=1
print "RECEIVING..."
getmsg()
endif
if mouseclick()=1
print "SENDING..."
rem send net message integer 0,mousex()
rem send net message float 0,mousey()*1.0
send net message string 0,"hello world"
endif
rem loop
sync
loop
if inkey$()=" " then gosub _handlesimplemessages : sync
gosub _handlecomplexmessages : sync
gosub _destroyagame
rem End of Program
end
_findconnections:
rem Set Network Connection
perform checklist for net connections
cls : print "Initializing Network Connectivity..."
print
sync
for c=1 to checklist quantity()
cnx$(c)=checklist string$(c)
print cnx$(c)
next c
rem Have User Select a Connection
SelectionIndex=0
if checklist quantity()=0
print "No Connections Found"
sync
wait key
end
else
selectionindex=0
for davt=1 to checklist quantity()
print str$(davt)+" "+cnx$(davt)
rem if left$(cnx$(davt),8)="Internet"
rem conn=davt
sync
rem input "Enter IP Address or Hostname >";data$
rem data$="127.0.0.1"
rem endif
next davt
print "Press the number for TCP/IP"
while k$=""
k$=inkey$()
endwhile
print "Wait..."
sync
k1=val(k$)
conn=k1
input "type the IP address of the target",data$
endif
rem Set Connection
if data$=""
print "No Data Entered...Exiting..."
sync
wait key
end
endif
if data$>"" then set net connection conn,data$
rem Prompt connection
return
_findsessions:
perform checklist for net sessions
print "Starting Server..."
print
sync
for c=1 to checklist quantity()
davs$ = checklist string$(c)
print davs$
sync
next c
rem Have User Select a Session
SessionIndex=0
if checklist quantity()=0
print "Could not start server."
sync
rem wait key
else
for c=1 to checklist quantity()
print str$(c)+" "+checklist string$(c)
rem if right$(checklist string$(c),3)="sir" then sessionindex=c
next c
print "Press the number for the session"
k$=""
while k$=""
k$=inkey$()
endwhile
sessionindex=val(k$)
print "Wait..."
endif
return
_createagame:
rem Create or Join a game
sleep 1000
if SessionIndex=0
PlayerMax=8 : KindOfGame=1
create net game "Sir", "SirFire", PlayerMax, KindOfGame
else
input "Username >";playername$
join net game SessionIndex, playername$
endif
rem Was game created successfully
if net game exists()=1
print : print "Game Link Established"
else
print "COULD NOT CREATE GAME LINK"
print "PRESS ANY KEY"
wait key : end
endif
return
_findplayers:
rem Create an EXTRA Player (used rarely)
cls : print : print "NPC PLAYER CREATION"
PlayerNumber = create net player("NPC Player")
rem Find all current players in game
perform checklist for net players
print : print "Players Detected:"
print
for c=1 to checklist quantity()
print c;". ";checklist string$(c);" ID:";checklist value a(c);" UNIQUE:";checklist value b(c);" ";
if checklist value c(c)=1 then print "(me) ";
if checklist value d(c)=1 then print "(host) ";
print
next c
rem Delete the EXTRA Player (used rarely)
print : print "Forcing NPC Disconnect."
free net player PlayerNumber
return
_handlenetgame:
rem Network game can change during session
if net game now hosting()=1 then Hosting=1
if Hosting=1 then PRINT "SERVER IS ONLINE"
if net game lost()=1 then LostGame=1
if LostGame=1 then PRINT "SERVER HAS DISCONNECTED"
returnvalue=net player created()
if returnvalue>1 then PlayerIn=returnvalue
if PlayerIn>tmplayerin
PRINT "A NEW PLAYER HAS JOINED THE GAME (";PlayerIn; ")"
tmplayerin=playerin
perform checklist for net players
print "Players Detected:"
for c=1 to checklist quantity()
print c;". ";checklist string$(c);" ID:";checklist value a(c);" UNIQUE:";checklist value b(c);" ";
if checklist value c(c)=1 then print "(me) ";
if checklist value d(c)=1 then print "(host) ";
print
next c
endif
returnvalue=net player destroyed()
if returnvalue>1 then PlayerOut=returnvalue
if PlayerOut>templayerdes then PRINT "A PLAYER HAS LEFT THE GAME (";PlayerOut; ")"
templayerdes=playerout
sync
return
_handlesimplemessages:
rem Network game can communicate numbers and strings
while inkey$()<"x"
cls : print "HANDLE NET MESSAGES (Press X To Continue)"
rem Show Latest Return Values
print
print "TO: ";ReturnTo
print "FROM: ";ReturnFrom
print "INTEGER: ";ReturnInteger
print "FLOAT: ";ReturnFloat#
print "STRING: ";ReturnString$
print
rem Send Part
if mouseclick()=1
print "SENDING..."
send net message integer 0,mousex()
send net message float 0,mousey()*1.0
send net message string 0,"hello world"
endif
rem Receive Part (from all other players)
get net message
if net message exists()=1 then print "RECEIVING..."
sync
endwhile
return
_handlecomplexmessages:
rem Network game can communicate sounds, images and 3D data
while inkey$()<"z"
rem User Prompt
cls : print "HANDLE ADVANCED NET MESSAGES (Press Z To Continue)"
print "PRESS [1] through [5] to SEND RESOURCE"
rem Show Latest Return Values
print
print "TO: ";ReturnTo
print "FROM: ";ReturnFrom
print "MEMBLOCK: ";ReturnMemblockIndex
print "IMAGE: ";ReturnImageIndex
print "BITMAP: ";ReturnBitmapIndex
print "SOUND: ";ReturnSoundIndex
print "MESH: ";ReturnMeshIndex
print
rem Send Part
k$=inkey$()
if k$>="1" and k$="5"
print "SENDING..."
GuarenteePacket=1
if k$="1"
MemblockIndex=1
make memblock MemblockIndex, 1024
send net message memblock 0, MemblockIndex, GuarenteePacket
delete memblock MemblockIndex
endif
if k$="2"
ImageIndex=1
get image ImageIndex, 0, 0, 64, 64
send net message image 0, ImageIndex, GuarenteePacket
delete image ImageIndex
endif
if k$="3"
BitmapIndex=1
load bitmap "face.bmp", BitmapIndex
send net message bitmap 0, BitmapIndex, GuarenteePacket
delete bitmap BitmapIndex
endif
if k$="4"
SoundIndex=1
load sound "gun.wav", SoundIndex
send net message sound 0, SoundIndex, GuarenteePacket
delete sound SoundIndex
endif
if k$="5"
MeshIndex=1
load mesh "mesh.x", MeshIndex
send net message mesh 0, MeshIndex, GuarenteePacket
delete mesh MeshIndex
endif
endif
rem Receive Part (from all other players)
get net message
if net message exists()=1 then print "RECEIVING..."
while net message exists()=1
ReturnTo=net message player to()
ReturnFrom=net message player from()
if net message type()=4
ReturnMemblockIndex=1
net message memblock ReturnMemblockIndex
print "MEMBLOCK SIZE:";get memblock size(ReturnMemblockIndex)
sync
delete memblock ReturnMemblockIndex
endif
if net message type()=5
ReturnImageIndex=1
net message image ReturnImageIndex
paste image ReturnImageIndex,50,50
sync
delete image ReturnImageIndex
endif
if net message type()=6
ReturnBitmapIndex=1
net message bitmap ReturnBitmapIndex
copy bitmap ReturnBitmapIndex,0
sync
delete bitmap ReturnBitmapIndex
endif
if net message type()=7
ReturnSoundIndex=1
net message sound ReturnSoundIndex
play sound ReturnSoundIndex
sync
delete sound ReturnSoundIndex
endif
if net message type()=8
if object exist(1)
delete object 1
delete mesh ReturnMeshIndex
endif
ReturnMeshIndex=1
net message mesh ReturnMeshIndex
backdrop off
make object 1,ReturnMeshIndex,0
sync
delete object 1
delete mesh ReturnMeshIndex
endif
get net message
endwhile
rem Update screen
sync
endwhile
return
_destroyagame:
rem Free the current game session
free net game
return
end
_waitkey:
print : print "Press Any Key"
wait key
return
break
function getmsg()
while net message exists()=1
ReturnTo=net message player to()
ReturnFrom=net message player from()
if net message type()=1 then ReturnInteger=net message integer()
if net message type()=2 then ReturnFloat#=net message float()
print net message string$()
get net message
endwhile
endfunction
This thing works, but isn't very functional.
Just run two instances of the compiled EXE.