|
From: <Blu...@us...> - 2010-09-30 10:18:48
|
Revision: 415
http://virtplayground.svn.sourceforge.net/virtplayground/?rev=415&view=rev
Author: BlueWolf_
Date: 2010-09-30 10:18:42 +0000 (Thu, 30 Sep 2010)
Log Message:
-----------
Server can now check which user can see an other user and will pipe the move-data to those clients
Modified Paths:
--------------
trunk/server/core/functions.py
trunk/server/core/parser.py
trunk/server/core/tick.py
Modified: trunk/server/core/functions.py
===================================================================
--- trunk/server/core/functions.py 2010-09-28 22:00:28 UTC (rev 414)
+++ trunk/server/core/functions.py 2010-09-30 10:18:42 UTC (rev 415)
@@ -147,45 +147,44 @@
return True
-def get_visible_clients(viewport, client, clients):
+def get_visible_users(viewport, client, clients, pos = None):
"""
- Get a list of all visible clients for this user
+ Get a list of all visible users for this user
"""
visible = []
+ if pos == None: pos = get_current_position(client)
- margin = client['speed']*2
+ margin = client['speed']
view = (
- client['pos'][0] - (viewport[0]/2) - margin,
- client['pos'][1] - (viewport[1]/2) - margin,
- viewport[0] + margin,
- viewport[1] + margin
+ pos[0] - (viewport[0]/2) - margin,
+ pos[1] - (viewport[1]/2) - margin,
+ viewport[0] + (margin*2),
+ viewport[1] + (margin*2)
)
for other in clients.values():
if other['status'] != "VP": continue
if client['pos'][2] != other['pos'][2]: continue
- print client['user'], "->", other['user']
+ pos_other = get_current_position(other)
# Create his viewport
- margin_other = other['speed']*2
+ margin_other = other['speed']
view_other = (
- other['pos'][0] - (viewport[0]/2) - margin,
- other['pos'][1] - (viewport[1]/2) - margin,
- viewport[0] + margin,
- viewport[1] + margin
+ pos_other[0] - margin,
+ pos_other[1] - margin,
+ margin*2,
+ margin*2
)
-
- print view, view_other
# Is this without our viewport?
if (view[0]+view[2] >= view_other[0] and view[0] <= view_other[0]+view_other[2]) and \
(view[1]+view[3] >= view_other[1] and view[1] <= view_other[1]+view_other[3]):
visible.append(other)
-
return visible
+
def get_current_position(client):
"""
Returns the current position for this user
@@ -228,15 +227,8 @@
"""
# Check if newpos is valid
- if client['moving'] == False:
- # Just check if it's the same pos
- if client['pos'] != proposed_pos:
- client['con'].send("move", {
- "cid": client['cid'],
- "pos": client['pos']
- })
-
- else:
+ if client['moving'] != False:
+ # Client is moving. CHeck is proposed_pos is near our calculated pos
current_pos = get_current_position(client)
if proposed_pos != None:
@@ -255,11 +247,47 @@
})
else:
current_pos = proposed_pos
+
+ # [TODO] Check for collisions between lastpos and current_pos
+
+ else:
+ # Just check if it's the same pos
+ if proposed_pos != None and client['pos'] != proposed_pos:
+ client['con'].send("move", {
+ "cid": client['cid'],
+ "pos": client['pos']
+ })
+ current_pos = client['pos']
+
+ # Check which other clients see this user
+ new_visible = get_visible_users(sh['config']['viewport'], client,
+ sh['core'].clients, current_pos)
+
+ # Find those who left his/her viewport
+ for other in [cl for cl in client['visible']['users'] \
+ if cl not in new_visible]:
+ client['con'].send("move", {
+ "cid": other['cid'],
+ "pos": None,
+ })
+
+ # Send all the new users who entered his/her viewport
+ for other in [cl for cl in new_visible \
+ if cl not in client['visible']['users']]:
+ data = {
+ "cid": other['cid'],
+ "pos": get_current_position(other),
+ "moving": False
+ }
+ if other['moving']:
+ data['moving'] = True
+ data['direction'] = other['moving'][1]
+ data['speed'] = other['moving'][2]
+
+ client['con'].send("move", data)
+
+ client['visible']['users'] = new_visible
+
+ client['lastpos'] = current_pos
- # Check for collisions between lastpos and current_pos
- # [TODO]
- print "Check", client['lastpos'], "->", current_pos
-
- client['lastpos'] = current_pos
-
Modified: trunk/server/core/parser.py
===================================================================
--- trunk/server/core/parser.py 2010-09-28 22:00:28 UTC (rev 414)
+++ trunk/server/core/parser.py 2010-09-30 10:18:42 UTC (rev 415)
@@ -132,14 +132,12 @@
client['user'] = screenname
client['app'] = tuple(msg['client'])
client['version'] = str(msg['version'])
- client['pos'] = [
- int(random.random()*500-250), # X
- int(random.random()*500-250), # Y
- 0] # Z
+ client['pos'] = [0, 0, 0] # X, Y, Z
client['lastpos'] = client['pos'][:] # Last pos that is checked
client['status'] = "VP"
client['speed'] = self.sh['config']['default_speed']
client['moving'] = False
+ client['visible'] = {'users': [], 'objects': []}
if msg['bot'] == False:
client['bot'] = False
@@ -174,8 +172,9 @@
# Send online clients
userlist = []
- visible = get_visible_clients(self.sh['config']['viewport'], client,
+ visible = get_visible_users(self.sh['config']['viewport'], client,
self.clients)
+ client['visible']['users'] = visible
for cid_o, client_o in self.clients.items():
if client_o['status'] != "VP": continue
@@ -204,12 +203,17 @@
# Is this user visible for this client?
if client_o in visible:
user_o['pos'] = client_o['pos']
+ else:
+ user_o['pos'] = None
+
+ # Is this client visible to the other user?
+ if client in get_visible_users(self.sh['config']['viewport'],
+ client_o, self.clients):
+ client_o['visible']['users'].append(client)
user['pos'] = client['pos']
else:
- user_o['pos'] = None
user['pos'] = None
-
# Send data to other user
if cid != cid_o:
client_o['con'].send("useronline", user)
@@ -264,7 +268,7 @@
def move(self, cid, msg):
"""
- An user is moving to a different position
+ A user is moving to a different position
* moving:
Boolean, if the user is walking or not
@@ -285,4 +289,44 @@
position_check(self.sh, client, msg['pos'])
client['pos'] = msg['pos']
client['moving'] = False
+
+ # Check for the user who could see this client, but can't anymore
+ for other in self.clients.values():
+ if other == client or other['status'] != "VP" or \
+ other['pos'][2] != client['pos'][2]:
+ continue
+ if client not in other['visible']['users']: continue
+
+ if client not in get_visible_users(
+ self.sh['config']['viewport'], other, self.clients):
+ other['visible']['users'].remove(client)
+ other['con'].send("move", {
+ "cid": client['cid'],
+ "pos": None
+ })
+
+ # Send to other clients
+ for other in self.clients.values():
+ if other == client or other['status'] != "VP" or \
+ other['pos'][2] != client['pos'][2]:
+ continue
+
+ if client in get_visible_users(self.sh['config']['viewport'], other,
+ self.clients):
+
+ if client not in other['visible']['users']:
+ other['visible']['users'].append(client)
+
+ data = {
+ "cid": client['cid'],
+ "pos": client['pos'],
+ "moving": False,
+ }
+ if client['moving']:
+ data['moving'] = True
+ data['direction'] = client['moving'][1]
+ data['speed'] = client['moving'][2]
+
+ other['con'].send("move", data)
+
Modified: trunk/server/core/tick.py
===================================================================
--- trunk/server/core/tick.py 2010-09-28 22:00:28 UTC (rev 414)
+++ trunk/server/core/tick.py 2010-09-30 10:18:42 UTC (rev 415)
@@ -79,5 +79,12 @@
amount += 1
position_check(self.sh, client)
- if amount == 0:
- return True
+ if amount == 0: return True
+
+ # Go through all non-moving users to check if someone entered
+ # their viewport
+ for client in self.clients.values():
+ if client['status'] != "VP" or client['moving'] != False:
+ continue
+
+ position_check(self.sh, client)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|