View Single Post
  #18  
Old 12-03-2013, 12:32 AM
maya maya is offline
Member
 
Join Date: Nov 2013
Location: United Arab Emirates
Posts: 21
basically, what I have is a multi user environment that consists of 1 server and 2 clients. what I mean by a client is the avatar. and I am using 1 pc as the server and the client.
client 1 will do a specific task (search for radiation source) where as client2 will identify the radiation source.
both clients can exchange messages between them and with the server.

I, as client1, while performing my task and navigating through the environment, I want to c client 2 (avatar2).

this is the code:
just run the server and then the 2 clients. try to control the movement of avatar1 and c if u can find avatar2 while navigating.

######server code#############
Code:
import viz
import viznet
import viztask
import vizmat
import vizact
import vizshape
import vizinfo
#add the required imports, start the Vizard engine, set the background, and position the display so that the full map will be visible 
viz.go()
#viz.MainView.setPosition([50,25,-40])
#viz.MainView.lookAt([0,0,0])
#Enable full screen anti-aliasing (FSAA) to smooth edges
viz.setMultiSample(4)
#Increase the Field of View
viz.MainWindow.fov(60)
piazza = viz.add('piazza.osgb')

#create a list of objects which must be identical to the list of objects on the client.These are the allowable character displays for each client. We then add the map, and finally an empty dictionary to store client information in.
objectList = ['duck.wrl','logo.wrl','vcc_male.cfg','vcc_female.cfg'] 
#maze = viz.add('tankmaze.wrl') 
objects = {}

#events which will be used in the interaction between the client and the server.must be defined in the same way on each client.
UPDATE = viznet.id('update') 
CLIENT_DISCONNECTED = viznet.id('client_disconnected') 
CHAT = viznet.id('chat') 

#start the server and handle client joining and client leaving events. The onStopClient function preforms some cleanup actions as described in the comments
viznet.server.start(port_in=14950,port_out=14951) 

def onStartClient(e): 
    print '** Client joined:',e.sender 
viz.callback(viznet.CLIENT_CONNECT_EVENT,onStartClient) 

def onStopClient(e): 
    print '** Client left:',e.sender 
    viznet.server.send(CLIENT_DISCONNECTED,client=e.sender) # Alert other clients 
    objects[e.sender].remove() # Remove object from our map 
    del objects[e.sender] # Delete user from array 
viz.callback(viznet.CLIENT_DISCONNECT_EVENT,onStopClient) 

#The update function essentially updates a client's position, 
#however if this is the first time the client's position is updated, it also adds the client to the map. 
#Adding the user this way makes it so we don't have to have another event to handle when the client has picked a character. 
#Further more this way we don't have to send a list of already connected clients to all new clients.
def update(e): 
    try: 
        objects[e.sender].setPosition(e.pos) 
        objects[e.sender].setEuler(e.ori) 
    except KeyError: # If key doesn't exist add the user's object 
        objects[e.sender] = viz.add(objectList[e.object],scale=[2,2,2],pos=e.pos,euler=e.ori) 
        objects[e.sender].tooltip = e.sender # Sets the tool tip for this object 
viz.callback(UPDATE,update) 

# Tool Tip 
import viztip 
tth = viztip.ToolTip() 
tth.start() 
tth.setDelay(0)

# Chat Stuff 
def updateChat(msg): # Function to update the chat display 
    text.message('\n'.join(text.getMessage().split('\n')[1:])+'\n'+msg) 
text = viz.addText('\n\n\n\n',viz.SCREEN) # Add to screen 
text.alignment(viz.TEXT_LEFT_BOTTOM) 
text.fontSize(25) 
text.setPosition(.05,.08) 
input = viz.addText('> _',viz.SCREEN) # Add to screen 
input.fontSize(25); 
input.setPosition(.032,.05) 
input.visible(0) # We only want visible when in type mode 
def onChat(e): # Update the message display 
    updateChat(e.client+': '+e.chat) 
viz.callback(CHAT,onChat) 

#
talking = False 
def onKeyDown(key): 
    global talking 
    if key == 'y' and not talking: # Start talking mode 
        talking = True 
        input.message('> _') 
        input.visible(1) 
    elif key == viz.KEY_ESCAPE and talking: # Exit talking mode 
        talking = False 
        input.visible(0) 
        return True 
    elif talking and key == viz.KEY_RETURN: # Send message 
        talking = False 
        viznet.server.send(CHAT,client='SERVER',chat=input.getMessage()[2:-1]) 
        input.visible(0) 
        # Update display with our message 
        updateChat('SERVER'+': '+input.getMessage()[2:-1]) 
    elif talking and key == viz.KEY_BACKSPACE: # Delete a character 
        if len(input.getMessage()) > 3: 
            input.message(input.getMessage()[:-2]+'_') 
    elif len(key) > 1: # Handle special keys 
        return False 
    elif talking and 32 <= ord(key) <= 126: # Input message 
        input.message(input.getMessage()[:-1]+key+'_') 
viz.callback(viz.KEYDOWN_EVENT,onKeyDown,priority=-6)
######client1############

Code:
import viz
import viznet
import vizinfo
import vizinput
import viztask
import vizact
import vizshape
import vizproximity

viz.go()
viz.MainView.setPosition([0,0.1,0])
#viz.MainView.lookAt([50,25,-40])
#Enable full screen anti-aliasing (FSAA) to smooth edges
viz.setMultiSample(4)
#Increase the Field of View
viz.MainWindow.fov(60)
piazza = viz.add('piazza.osgb')
#maze = viz.add('tankmaze.wrl')

sphere = vizshape.addSphere(radius=0.2)
sphere.setPosition(0,0,20)
sphere.color(viz.RED)

text = viz.addText('radiation source',parent=sphere)
text.setPosition([-0.3,0.3,0])
text.setScale([0.2]*3)
text.color(viz.RED)
text.visible(viz.ON)

text_2D = viz.addText('radiation source area', viz.SCREEN )
text_2D.setPosition(0,0,20)
text_2D.color(viz.RED)
text_2D.visible(viz.OFF)


objects = {}

#
UPDATE = viznet.id('update') 
CLIENT_DISCONNECTED = viznet.id('client_disconnected') 
CHAT = viznet.id('chat') 

#
SERVER_NAME=vizinput.input('Enter Server Name') 
while not viznet.client.connect(SERVER_NAME,port_in=14951,port_out=14950): 
    if vizinput.ask('Could not connect to ' + SERVER_NAME + ' would you like to try another?'): 
        SERVER_NAME=vizinput.input('Enter Server Name') 
    else: 
        viz.quit() 
        break 
#
objectList = ['duck.wrl','logo.wrl','vcc_male.cfg','vcc_female.cfg'] 
obj_choice=vizinput.choose('Connected. Select your character',objectList) 


#
#Create proximity manager
manager = vizproximity.Manager()
#manager.setDebug(viz.ON)

#Add main viewpoint as proximity target
target = vizproximity.Target(viz.MainView)
manager.addTarget(target)

#Create sensor using male avatar
#avatar = viz.addAvatar('vcc_male2.cfg',pos=[0,0,4],euler=[180,0,0])
#avatar.state(1)

#sensor = vizproximity.Sphere(0.2, centre=(0,0,0))
sensor = vizproximity.Sensor( vizproximity.CircleArea(5),source=sphere)

manager.addSensor(sensor)

#Change state of avatar to talking when the user gets near
def EnterProximity(e):
    text_2D.visible(viz.ON)
    
    #text.visible(viz.ON)

#Change state of avatar to idle when the user moves away
def ExitProximity(e):
    #text.visible(viz.OFF)
    text_2D.visible(viz.OFF)

manager.onEnter(sensor,EnterProximity)
manager.onExit(sensor,ExitProximity)
     
#vizact.onkeydown('d',manager.setDebug,viz.TOGGLE) 

#
def sendUpdate():
     ori= viz.MainView.getEuler()
     pos = viz.MainView.getPosition()
     viznet.client.sendAll(UPDATE,client=viz.getComputerName(),object=obj_choice,pos=pos,ori=ori)
vizact.ontimer(.05,sendUpdate)

#
def update(e): 
    if e.client != viz.getComputerName(): 
        try: 
            objects[e.client].setPosition(e.pos) 
            objects[e.client].setEuler(e.ori) 
        except KeyError: 
            objects[e.client] = viz.add(objectList[e.object],scale=[2,2,2],pos=e.pos,euler=e.ori) 
            objects[e.client].tooltip = e.client 
viz.callback(UPDATE,update) 

#
def client_disconnected(e): 
    objects[e.client].remove() 
    del objects[e.client] 
viz.callback(CLIENT_DISCONNECTED,client_disconnected) 

def onStopServer(e): 
    print 'Disconnected from server' 
    viz.quit() 
viz.callback(viznet.SERVER_DISCONNECT_EVENT,onStopServer) 

#
import viztip 
tth = viztip.ToolTip() 
tth.start() 
tth.setDelay(0) 

#
def updateChat(msg): 
    text.message('\n'.join(text.getMessage().split('\n')[1:])+'\n'+msg) 

text = viz.addText('\n\n\n\n',viz.SCREEN) 
text.alignment(viz.TEXT_LEFT_BOTTOM) 
text.fontSize(25) 
text.setPosition(.05,.08) 

input = viz.addText('> _',viz.SCREEN) 
input.fontSize(25) 
input.setPosition(.032,.05) 
input.visible(0) 

def onChat(e): 
    updateChat(e.client+': '+e.chat) 
viz.callback(CHAT,onChat) 

talking = False 
def onKeyDown(key): 
    global talking 
    if key == 'y' and not talking: 
        talking = True 
        input.message('> _') 
        input.visible(1) 
    elif key == viz.KEY_ESCAPE and talking: 
        talking = False 
        input.visible(0) 
        return True 
    elif talking and key == viz.KEY_RETURN: 
        talking = False 
        viznet.client.sendAll(CHAT,client=viz.getComputerName(),chat=input.getMessage()[2:-1]) 
        input.visible(0) 
    elif talking and key == viz.KEY_BACKSPACE: 
        if len(input.getMessage()) > 3: 
            input.message(input.getMessage()[:-2]+'_') 
    elif len(key) > 1: 
        return False 
    elif talking and 32 <= ord(key) <= 126: 
        input.message(input.getMessage()[:-1]+key+'_') 
viz.callback(viz.KEYDOWN_EVENT,onKeyDown,priority=-6)


hope it is clear now.

thanx
Reply With Quote