Main sections
07 Network Games
<B>outdated manual!!!</B>
Please refer to NET... command reference for now!
Network Games
This section has been designed to show the user how easy, fun and flexible GLBasic is when it comes network programming.
General
GLBasic has all the commands that you will require to create multiplayer network enabled games. A full feature rich set of commands are available for your client or server programs. Also various protocols have been implemented allowing extreme flexibility when it comes to planning your client or server programs.
The following example is that of a client/server but GLBasic is capable of much much more. An example of this flexibility, for example, is accessing a high score table through the internet (see the Scramble game source code from the GLBasic website for an example of this functionality).
Initializing
To start a network game, there's always one host and several clients that join the host. To host a game session use this command:
ok=NETHOSTGAME(prt, sn$, p1$, p2$)
Whereas 'prt' is the protocol type.
0=TCP/IP (Internet, or local Network)
1=IPX (Local Network)
sn$ is then name of the session (e.g. "my_game")
p1$ and p2$ are connection parameters. When using NETHOSTGAME both must be empty ("", "").
When this succeeded, 'ok'=TRUE, otherwise its FALSE.
To join a hosted session, use this command:
ok=NETJOINGAME(prt, sn$, p1$, p2$)
The parameters are the same as above. NETJOINGAME needs to know the location of the host. When using TCP/IP p1$ is used for the IP-address. An empty IP ("" or "0.0.0.0") will seek for any session in the local network that matches. All further IP-addresses can be like this:"149.23.242.24" or this "www.dream-d-sign.de".
If it succeeded, ok=TRUE otherwise FALSE.
Players please
Now it is time to bring some players into the game. Each player in the network game will be given a unique ID. To create a player use this:
id$=NETCREATEPLAYER$(name$)
Whereas name$ is an alphanumeric name ("Max"). id$ will now represent a global ID for this player. A player can be removed from the game by quitting their program.
The Postman's Here
Now a newly created player can send and receive messages. Messages that are sent reach every player in the session but not the sender itself. Players can only receive messages that are sent to them. The following commands are for this purpose:
msg$=NETGETMSG$(id$)
ok = NETSENDMSG(idfrom$, idto$, msg$)
idfrom$ is the id of the player, you created, who sends the message.
idto$ always is the ID of the player to send / receive the message.
msg$ is the sent / received message. It's empty if no message is waiting for the player.
'ok'=TRUE / FALSE depending if the message could be sent or not.
Anybody Here?
To determine how many players have joined, what their ID$'s are and names, use these functions:
NumPlayers= NETNUMPLAYERS()
playerID$[i]=NETGETPLAYERID$ (i)
playername$[i]=NETPLAYERNAME$(playerID$[i])
Here is a short code example which shows to use them:
   PRINT "Joined Players:", 100, 50
   NumPlayers=NETNUMPLAYERS()
   FOR i=0 TO NumPlayers-1
     playerID$[i]=NETGETPLAYERID$(i)
      playername$[i]=NETPLAYERNAME$(playerID$[i])
     PRINT playername$[i] + " (ID:" + playerID$[i]+") " + i, 100, i*20+100
   NEXT
A Sample
// Multiplayer Login - Demo 'game'
// ------------------------------------
PRINT "Name:", 100, 100
INPUT name$, 100, 120
IF LEN(name$)=0 THEN END
//             TCP/IP, SessionName,  IP:Local PC, Not needed
 IF NETJOINGAME(0,   "SimpleDemo", "",          "")
  HOSTING=FALSE
  PRINT "Joined a game", 100, 100
  SHOWSCREEN
  ELSE
  PRINT "Cannot join - try hosting", 100, 100
  SHOWSCREEN
  IF NETHOSTGAME(0, "SimpleDemo", "", "")
   HOSTING=TRUE
  ELSE
   PRINT "Cannot Host a new game", 50, 100
   SHOWSCREEN
   MOUSEWAIT
   END
  ENDIF
 ENDIF
// Now setup stuff / Jetzt alles vorbereiten
DIM px[4]
DIM py[4]
DIM playerID$[4]
DIM playername$[4]
ply_id$=""
PlayerID=0
 ply_id$=NETCREATEPLAYER$(name$)
  // HOSTING A GAME                     / SPIEL HOSTEN
  // Wait and allow players to join     / Warten auf Spieler, die joinen
  // You can start the game             / Du kannst das Spiel starten
 IF HOSTING
  maxplayer=0
  playername$[0]=name$
  playerID$[0]=ply_id$
  WHILE TRUE
   msg$=NETGETMSG$(ply_id$)
   job$=MID$(msg$, 0, 5)
   data$=MID$(msg$, 5, 1024)
   SELECT job$
   // MAY_I54123 -> "Player#54123: MAY I JOIN??" -> NOWAY54123 or UARE#254123 (You are n#2, Player54123)
   // MAY_I54123 -> "Spieler#54123: Darf ich joinen??" -> NOWAY54123 oder UARE#254123 (Du bist #2, Spieler54123)
   CASE "MAY_I"
    FOR i=1 TO maxplayer
     IF playerID$[i]=data$ THEN GOTO play
    NEXT
    IF maxplayer<3
     maxplayer=maxplayer+1
     NETSENDMSG (ply_id$, "UARE#"+maxplayer+data$)
     playerID$[maxplayer]=data$
     playername$[maxplayer]=NETPLAYERNAME$ (data$)
    ELSE
     NETSENDMSG(ply_id$, "NOWAY"+data$) 
    ENDIF
   ENDSELECT
  skip:
   // Show what players have already joined and start the game
   // Anzeige der ge-jointen Spieler
   PRINT "Joined Players:", 100, 50
   FOR i=0 TO maxplayer
    PRINT playername$[i] + " (ID:" + playerID$[i]+") " + i, 100, i*20+100
   NEXT
   SHOWSCREEN 
   IF KEY(57)
    NETSENDMSG (ply_id$, "START"+maxplayer)
    GOTO play
   ENDIF
  WEND
  // JOIN A GAME                               / JOIN einem Spiel
 // First make yourself known to the session. / Erstmal der Session vorstellen.
 // If a new game starts, you can play.       / Wenn ein neues Spiel beginnt, kannst Du spielen.
 ELSE
  NETSENDMSG(ply_id$, "MAY_I"+ply_id$)
  WHILE TRUE
   msg$= NETGETMSG$(ply_id$)
   job$=MID$(msg$, 0, 5)
   data$=MID$(msg$, 5, 1024)
   SELECT job$
   CASE "NOWAY"
    PRINT "Too many players online", 100, 100
    SHOWSCREEN; MOUSEWAIT
    END
   CASE "UARE#"
    a$=MID$(data$, 1, 1024)
    b$=MID$(data$, 0, 1)
    IF a$=ply_id$ ar THEN PlayerID=b$
   CASE "START"
    maxplayer=data$
    IF PlayerID>0 THEN GOTO play// 0 is the host, and here I'm joining -> >=1
   ENDSELECT
   
   SELECT PlayerID
   CASE 0
    PRINT "Connecting" ,100, 100
   CASE 1 TO 3
    PRINT "I joined. My Player Number:" +PlayerID, 100, 100
   ENDSELECT
   SHOWSCREEN
  WEND
 ENDIF
// ------------------------------------------------- //
// Start a game session
// ------------------------------------------------- //
play:
 WHILE TRUE
  // Own Movement / Bei Bewegung
  IF KEY(200)
   msg$=PlayerID+"UP"
   py[PlayerID]=py[PlayerID]-1 // The message is not send to myself
                               // Die Nachricht wird nicht an mich selbst geschickt
  ENDIF
  IF KEY(208)
   msg$=PlayerID+"DN"
   py[PlayerID]=py[PlayerID]+1
  ENDIF 
  NETSENDMSG(ply_id$, msg$)
 
  // Now move all players until no message available anymore
  // Jetzt alle Spieler bewegen, bis keiner mehr was sagt
  msg$=""
  WHILE TRUE
   msg$=NETGETMSG$(ply_id$)
   IF msg$="" THEN GOTO shortcut
   
   PRINT msg$, 100, 120
   data$=MID$(msg$, 1, 2)
   a$=MID$(msg$, 0, 1)
   a=a$
   SELECT data$
   CASE "UP"
    py[a]=py[a]-1
   CASE "DN"
    py[a]=py[a]+1
   ENDSELECT
  WEND
shortcut:
  // Now show the current state
  // Aktuelle Lage anzeigen
  FOR i=0 TO maxplayer
   PRINT i, i*120, py[i]
  NEXT
 PRINT j,100,100; j=j+1
  SHOWSCREEN
 WEND
// END OF DEMO / ENDE DES DEMO

