Hi all.
I've taken a look at how to create an LAN multiplayer system using the
tempest plugin. The game supports at last 2 players (10 at the top). It's not very fun, but is a good example in simple multiplayer.
Code (old version!!!):
set window on
set window position 10, 50
set window size 512, 384
set display mode 1024, 768, 32
ink rgb(255, 0, 255), 0
type _mes_
sender as string
mess as string
endtype
local choise as byte
local chatting as boolean
InitMessages()
print "1. Host"
print "2. Join"
input "Choise: ", choise
input "Name: ", name$
if choise = 1
cls
if name$ = "" then name$ = "The Host"
res = THost("0.0.0.0", 10, name$)
if res = 0 then end
TRegisterGame "Estrella Wars!", "Deathmatch", "Warzone", 600
else
if choise = 2
input "IP: ", ip$
cls
if name$ = "" then name$ = "!!!<!!!Nameless!!!>!!!"
TGetGameList
ga = TGetGameAmount()
cls
print "There are ", str$( ga ), " games active!!! They are:"
for n = 1 to ga
print str$( n ), ". ", TGetGameData(n, 0)
next n
`input "Choise: ", choise
if choise = 0 then end
res = TJoin(ip$, name$)
if res = 0 then end
else
end
endif
endif
cls
sync on
sync rate 51
backdrop on
id = TGetThisID()
for n = 0 to 9
make object cube n + 1, 22
position object n + 1, rnd( 200 ) - 100, 0, rnd( 200 ) - 100
color object n + 1, rgb(rnd( 255 ), 0, rnd( 255 ))
next n
make matrix 1, 2000, 2000, 75, 75
position matrix 1, -1000, 0, -1000
repeat
if keystate( 21 ) = 1 and chatting = 0
chatting = 1
clear entry buffer
endif
rem Show enemys
for n = 1 to 10
if TPlayerExist( n ) and n <> id
show object n
if object in screen( n ) = 1
oxxx# = object screen x( n )
oyyy# = object screen y( n )
center text oxxx#, oyyy#, TGetPlayerName( n )
endif
else
hide object n
endif
next n
rem Recive data from the other players
repeat
q = q + 1
mess = TGetMessage()
if mess
from = TGetSender()
x# = TGetFloat()
z# = TGetFloat()
ay# = TGetFloat()
dam = TGetByte()
if object exist( from )
position object from, x#, 0, z#
yrotate object from, ay#
endif
if dam = id
position object id, rnd( 200 ) - 100, 0, rnd( 200 ) - 100
center text screen width() / 2, screen height() / 2, "You are dead. Time to re-spawn"
endif
dam = -1
mmm$ = TGetString()
if mmm$ <> " "
AddMessage(TGetPlayerName( q ), mmm$)
endif
endif
until not mess
q = 0
rem Display
text 1, 1, "FPS: " + str$( screen fps() )
text 1, 21, "Score: " + str$( score )
for n = 0 to 6
text 1, 41 + n * 20, messages( n ).sender + ": " + messages( n ).mess
next n
circle screen width() / 2, screen height() / 2, 3
rem Move
if chatting = 0
if leftkey() = 1
yrotate object id, object angle y( id ) - 5
else
if rightkey() = 1
yrotate object id, object angle y( id ) + 5
endif
endif
if upkey() = 1
move object id, 4
else
if downkey() = 1 then move object id, -2
endif
else
rem Chatta lite
if (timer() - chatTime) / 90 >= 1
if keystate( 14 ) = 0
if entry$() <> ""
message$ = message$ + entry$()
chatTime = timer()
endif
else
message$ = left$(message$, len( message$ ) - 1)
chatTime = timer()
endif
else
clear entry buffer
endif
text 1, screen height() - 20, "Say: " + message$
if returnkey() = 1
msge$ = message$
message$ = ""
chatting = 0
AddMessage("(You) " + name$, msge$)
endif
endif
position camera object position x( id ), 10, object position z( id )
rotate camera 0, object angle y( id ), 0
rem Fire
if spacekey() = 1
obj = pick object(screen width() / 2, screen height() / 2, 1, id - 1)
if obj = 0 then obj = pick object(screen width() / 2, screen height() / 2, id + 1, 10)
if obj > 0 then score = score + 1
endif
rem Send player data
TPutFloat object position x( id )
TPutFloat object position z( id )
TPutFloat object angle y( id )
TPutByte obj
TPutString msge$ + " "
TSendAll
msge$ = ""
obj = 0
TSync
sync
until keystate( 88 )
if TConnected()
TDisconnect
endif
flush video memory
end
function InitMessages()
dim messages( 6 ) as _mes_
endfunction
function AddMessage(sender as string, anmessage as string)
down1S as string
down1M as string
down2S as string
down2M as string
down1S = messages( 0 ).sender
down1M = messages( 0 ).mess
messages( 0 ).sender = sender
messages( 0 ).mess = anmessage
down2S = messages( 1 ).sender
down2M = messages( 1 ).mess
messages( 1 ).sender = down1S
messages( 1 ).mess = down1M
down1S = messages( 2 ).sender
down1M = messages( 2 ).mess
messages( 2 ).sender = down2S
messages( 2 ).mess = down2M
down2S = messages( 3 ).sender
down2M = messages( 3 ).mess
messages( 3 ).sender = down1S
messages( 3 ).mess = down1M
down1S = messages( 4 ).sender
down1M = messages( 4 ).mess
messages( 4 ).sender = down2S
messages( 4 ).mess = down2M
down2S = messages( 5 ).sender
down2M = messages( 5 ).mess
messages( 5 ).sender = down1S
messages( 5 ).mess = down1M
messages( 6 ).sender = down2S
messages( 6 ).mess = down2M
endfunction
Hope you find it usefull.
UPDATE:
I've updated the code. It works much better now. You are now in an environment built of boxes (collision system is made with sparkys collision plugin). You can run around, jump, kill the other player/players to score and even get a list of who is on the server. Ohhh, and there is one thing more. Do not touch the ground
.
Hope you enjoy the update. Here it is:
set display mode 1024, 768, 32
randomize timer()
autocam off
hide mouse
rem The UDT's
type _data_
dead as byte
hidden as boolean
endtype
type _mes_
mess as string
sender as string
endtype
rem Globals
global you as integer
global sw as word
global sh as word
sw = screen width() / 2
sh = screen height() / 2
rem Define variables
health as integer
jumpTime as dword
chatTime as dword
deadTime as dword
dead as byte
dam as byte
target as byte
res as boolean
chating as boolean
rem The arrays
dim death( 11 ) as _data_
InitMessages()
rem Loop
do
rem Clear screen
cls
rem Choose
print "Boxes!"
print "1) Host"
print "2) Join"
print "3) End program"
input "Choise: ", choise$
if choise$ = "1"
rem Host a game
input "Name: ", name$
input "Game name: ", gaName$
res = THost("0.0.0.0", 10, name$)
if res = 1
rem If you could host...
TRegisterGame gaName$, "Deathmatch", "Boxing cylinders", 3600000
exit
else
rem Else, display an error message.
print "ERROR: Could not set up an server."
wait key
endif
else
if choise$ = "2"
rem Connect to a server
input "Name: ", name$
input "IP: ", ipAdress$
res = TJoin(ipAdress$, name$)
if res = 1
rem If the connection was succesfull
exit
else
rem If not...
print "ERROR: Could not connect."
wait key
endif
else
if choise$ = "3"
end
endif
rem If you chose an invalid option
print "ERROR: Invalid option."
wait key
endif
endif
loop
sync on : sync rate 75
rem Setup the ground
ground = GetNewObjectNumber( 789 )
make object box ground, 10000, 150, 10000
position object ground, -207, -75, -207
color object ground, rgb(255, 0, 0)
SC_SetupObject ground, 1, 2
SC_UpdateObject ground
rem Fill the world
for n = 1 to 6
for m = 1 to 6
obj = GetNewObjectNumber( 790 )
height = 180 + 180 * sin((n ^ 2) * (m ^ 2))
make object box obj, 120, height, 87 * (m + n * sin( n ))
position object obj, n * 442, height, m * (389 + 17 * n)
color object obj, rgb(0, 255, 0)
SC_SetupObject obj, 1, 2
SC_UpdateObject obj
next m
next n
for n = 1 to 5
for m = 1 to 5
obj = GetNewObjectNumber( 790 )
make object box obj, 169, 10 + 8 * m * n, 350
position object obj, n * 711 + 378 * sin(n * m + 23 * n + cos(45 * m) + atan(1 / (n + m))), 60 * cos( tan((m / n) / 0.637) ) + (10 * n) / m + 54, m * 622 + n * 67
color object obj, rgb(0, 255, 0)
SC_SetupObject obj, 1, 2
SC_UpdateObject obj
next m
next n
for n = 1 to 4
for m = 1 to 4
obj = GetNewObjectNumber( 790 )
make object box obj, 220, 20, 550
position object obj, n * 771, 107 + 50 * n * m, m * 1089
color object obj, rgb(0, 255, 0)
SC_SetupObject obj, 1, 2
SC_UpdateObject obj
next m
next n
you = TGetThisID()
rem Create the player
make object cube you, 20
position object you, rnd( 2000 ) + 111, 430, rnd( 2000 ) + 111
color object you, rgb(255, 205, 55)
SC_SetupObject you, 1, 2
SC_UpdateObject you
rem Player properties
health = 100
repeat
rem The subrutines
gosub recive
gosub disconnect
gosub hud
if chating = 1 then gosub chat
gosub player
gosub gravity
gosub collision
gosub updateGrav
gosub jumping
gosub camera
gosub send
killedBy = 0
kb = 0
msge$ = ""
target = 0
prevY# = object position y( you )
sync
until keystate( 88 ) = 1
rem Disconnect if connected
if TConnected()
TDisconnect
endif
end
recive:
rem Recive data, if it exist
while TGetMessage()
id = id + 1
sender = TGetSender()
x# = TGetFloat()
y# = TGetFloat()
z# = TGetFloat()
ay# = TGetFloat()
death( sender ).dead = TGetByte()
dam = TGetByte()
kb = TGetInt()
mess$ = TGetString()
if mess$ <> " " and mess$ <> " " and mess$ <> " " then AddMessage(TGetPlayerName( sender ) + ": ", mess$)
if kb <> 0 and kb = you
score = score + 1
endif
if dam = you
health = health - 5
if health < 1 then killedBy = sender
endif
if object exist( sender )
if death( sender ).dead = 0
if death( sender ).hidden = 1
show object sender
death( sender ).hidden = 0
endif
position object sender, x#, y#, z#
yrotate object sender, ay#
if object in screen( sender )
center text object screen x( sender ), object screen y( sender ), TGetPlayerName( sender )
endif
else
if death( sender ).hidden = 0
hide object sender
death( sender ).hidden = 1
endif
endif
else
rem If the object does not exist, create it. And then angle it corrctly.
if death( sender ).dead = 0
make object cube sender, 20
position object sender, x#, y#, z#
yrotate object sender, ay#
color object sender, rgb(255, 205, 55)
SC_SetupObject sender, 1, 2
SC_UpdateObject sender
endif
endif
endwhile
id = 0
return
disconnect:
for n = 1 to 10
if not TPlayerExist( n )
if object exist( n )
SC_RemoveObject n
delete object n
endif
endif
next n
return
hud:
rem Display
text 1, 1, "FPS: " + str$( screen fps() )
text 1, 18, "Health: " + str$( health )
text 1, 35, "Score: " + str$( score )
if health > 0 then circle sw, sh, 3
rem Show the other chat-messages
for n = 0 to 6
text 1, 52 + 17 * (n + 1), messages( n ).sender + messages( n ).mess
next n
return
chat:
rem Write message
if keystate( 14 ) = 0
if entry$() <> "" and (timer() - chatTime) / 25 > 0
message$ = message$ + entry$()
chatTime = timer()
endif
else
if (timer() - chatTime) / 47 > 0
message$ = left$(message$, len( message$ ) - 1)
chatTime = timer()
endif
endif
rem Send message
if returnkey() = 1
chating = 0
msge$ = message$
message$ = ""
if msge$ <> "" and msge$ <> " " and msge$ <> " "
AddMessage("(You) " + name$ + ": ", msge$)
endif
endif
rem Display the message
text 1, 52, "Say: " + message$
rem Clear the entry buffer
clear entry buffer
return
player:
rem Dead or not?
if health < 1
if dead = 0
dead = 1
deadTime = timer()
color light 0, rgb(255, 0, 0)
else
center text sw, sh - text height( "Tp" ) / 2, "Time to respawn: " + str$(5 - (timer() - deadTime) / 1000)
endif
endif
rem Respawn
if dead = 1 and (timer() - deadTime) / 1000 > 4
dead = 0
health = 100
color light 0, rgb(255, 255, 255)
position object you, rnd( 1100 ), 430, rnd( 1100 )
endif
rem Store the position of the player
ox# = object position x( you )
oy# = object position y( you )
oz# = object position z( you )
if chating = 0 and dead = 0
rem Move object
move object you, 7 * keystate( 17 ) - 4 * keystate( 31 )
zrotate object you, 0
move object left you, 5 * keystate( 30 ) - 5 * keystate( 32 )
rem Start to chat
if keystate( 21 ) = 1
chating = 1
clear entry buffer
endif
endif
rem Using the weapon
if mouseclick() = 1 and dead = 0
obj = pick object(sw, sh, 1, you - 1)
if obj = 0 then obj = pick object(sw, sh, you + 1, 10)
if obj <> 0
if TPlayerExist( obj ) = 1
target = obj
endif
endif
endif
nx# = object position x( you )
nz# = object position z( you )
rem The angles
yrotate object you, object angle y( you ) + mousemovex() / 2
xrotate camera camera angle x() + mousemovey() / 2
if camera angle x() > 90
xrotate camera 90
else
if camera angle x() < -90 then xrotate camera -90
endif
if keystate( 15 ) = 1 then ListPlayers()
return
gravity:
if dead = 0 then position object you, object position x( you ), object position y( you ) + grav#, object position z( you )
ny# = object position y( you )
if object collision(you, ground) = 1 then health = health - 7
if ny# < -100 then health = -78
if health < 1 and dead = 0 then score = score - 1
return
collision:
rem Check collision
collide = SC_SphereSlideGroup(1, ox#, oy#, oz#, nx#, ny#, nz#, 19, 0)
if collide > 0
rem Perform sliding collision
newX# = SC_GetCollisionSlideX()
newY# = SC_GetCollisionSlideY()
newZ# = SC_GetCollisionSlideZ()
rem Position the player at the new position
position object you, newX#, newY#, newZ#
endif
rem Update the player
SC_UpdateObject you
return
updateGrav:
if prevY# - object position y( you ) <> 0 and grav# > -11 and dead = 0
grav# = grav# - 0.16
if grav# < -11 then grav# = -11
endif
return
jumping:
if abs(prevY# - object position y( you )) = 0 and chating = 0 and dead = 0
if spacekey() = 1 and (timer() - jumpTime) / 500 > 1
grav# = 11
jumpTime = timer()
endif
endif
return
camera:
rem Position and rotate the camera
youx# = object position x( you )
youy# = object position y( you )
youz# = object position z( you )
youa# = object angle y( you )
position camera youx#, youy#, youz#
yrotate camera youa#
return
send:
rem Send data
TPutFloat youx#
TPutFloat youy#
TPutFloat youz#
TPutFloat youa#
TPutByte dead
TPutByte target
TPutInt killedBy
TPutString msge$ + " "
TSendAll
TSync
return
function GetNewObjectNumber(start as word)
newObj = start
repeat
newObj = newObj + 1
until not object exist( newObj )
endfunction newObj
function InitMessages()
dim messages( 6 ) as _mes_
endfunction
function AddMessage(sender as string, anmessage as string)
down1S as string
down1M as string
down2S as string
down2M as string
down1S = messages( 0 ).sender
down1M = messages( 0 ).mess
messages( 0 ).sender = sender
messages( 0 ).mess = anmessage
down2S = messages( 1 ).sender
down2M = messages( 1 ).mess
messages( 1 ).sender = down1S
messages( 1 ).mess = down1M
down1S = messages( 2 ).sender
down1M = messages( 2 ).mess
messages( 2 ).sender = down2S
messages( 2 ).mess = down2M
down2S = messages( 3 ).sender
down2M = messages( 3 ).mess
messages( 3 ).sender = down1S
messages( 3 ).mess = down1M
down1S = messages( 4 ).sender
down1M = messages( 4 ).mess
messages( 4 ).sender = down2S
messages( 4 ).mess = down2M
down2S = messages( 5 ).sender
down2M = messages( 5 ).mess
messages( 5 ).sender = down1S
messages( 5 ).mess = down1M
messages( 6 ).sender = down2S
messages( 6 ).mess = down2M
endfunction
function ListPlayers()
rem Loop through the players and output them
for n = 0 to 10
if TPlayerExist( n )
plna$ = TGetPlayerName( n )
rem Is it you or not?
if n <> you
text 2 * sw - text width( plna$ ) + 1, 1 + 17 * pl, plna$
else
text 2 * sw - text width("(You) " + plna$) + 1, 1 + 17 * pl, "(You) " + plna$
endif
pl = pl + 1
endif
next n
endfunction
delete memblock 1
Controlls:
w - move forward
s - move backwards
a - strafe left
d - strafe right
spacekey - jump
y - chat
tab - view players
left mousebutton - fire
And move the mouse to look around.