Commits

Devin Jeanpierre committed c248ccf

MULTIPLAYER IT WORKING

  • Participants
  • Parent commits ed765c1

Comments (0)

Files changed (2)

File test/game_protocol.py

     requiresAnswer = False
     arguments = [
         ('prng_seed', amp.Integer()),
+        ('player_names', amp.ListOf(amp.String())),
+        ('player_id', amp.Integer()),
         ##('blocking', amp.ListOf(amp.Integer())),
         ##('enemy_ids', amp.ListOf(amp.Integer()))
         # TODO: add ways to create different types of enemies

File test/tutorial_game.py

         
         self.objects = objects
         self.killed = set()
-        self.player_obj = objects[0]
         
         self.init_fov_map() # sets self.fov_map, self.fov_recompute
         
                     not tile.block_sight,
                     not tile.blocked)
     
+    def find_random_pos(self):
+        while 1:
+            # hypothetically could loop forever -- doubt it.
+            
+            x = libtcod.random_get_int(self.rng, 0, MAP_WIDTH - 2)
+            y = libtcod.random_get_int(self.rng, 0, MAP_HEIGHT - 2)
+            
+            if not self.is_blocked(x, y):
+                return x, y
+    
     def place_map_objects(self):
         #choose random number of npcs
         num_npcs = libtcod.random_get_int(self.rng, 10, MAX_MAP_NPC)
     
         for i in range(num_npcs):
             #choose random spot for this npc
-            x = libtcod.random_get_int(self.rng, 0, MAP_WIDTH - 2)
-            y = libtcod.random_get_int(self.rng, 0, MAP_HEIGHT - 2)
+            x, y = self.find_random_pos()
             
-            if not self.is_blocked(x, y):
-                if libtcod.random_get_int(self.rng, 0, 100) < 80:  #80% chance of getting an orc
-                    #create an orc
-                    npc = Mob(x, y, 'o', 'Orc', libtcod.desaturated_green, blocking=True)
-                else:
-                    #create a troll
-                    npc = Mob(x, y, 'T', 'TROLL', libtcod.darker_green, blocking=True)
-    
-                self.objects.append(npc)
+            if libtcod.random_get_int(self.rng, 0, 100) < 80:  #80% chance of getting an orc
+                #create an orc
+                npc = Mob(x, y, 'o', 'Orc', libtcod.desaturated_green, blocking=True)
+            else:
+                #create a troll
+                npc = Mob(x, y, 'T', 'TROLL', libtcod.darker_green, blocking=True)
+
+            self.objects.append(npc)
                 
     def place_object(self, obj):
         self.objects.append(obj)
         self.looping_call.start(1.0/LIMIT_FPS)
         
         self.usernames = []
-        self.player = PlayerObject(
-            MAP_WIDTH/2, MAP_HEIGHT/2, '@', 'player',
-            libtcod.white, blocking=True, speed=PLAYER_SPEED)
         ##self.npc = Mob(MAP_WIDTH/2 - 5, MAP_HEIGHT/2, '@', 'Jack', libtcod.yellow, blocking=True)
-        self.objects = [self.player]
+        self.objects = []
         ##self.objects.append(self.npc)
         
         self.level = make_map(MAP_WIDTH, MAP_HEIGHT)
         
         self.panel = libtcod.console_new(SCREEN_WIDTH, PANEL_HEIGHT)
     
+    def prepend_players(self, player_names, player_id):
+        new_items = []
+        for i, name in enumerate(player_names):
+            # FIXME: randomly assign position
+            x, y = self.world.find_random_pos()
+            pl = PlayerObject(
+                x, y, '@', name,
+                libtcod.white, blocking=True, speed=PLAYER_SPEED,
+                world=self.world)
+            
+            new_items.append(pl)
+            if i == player_id:
+                self.player = pl
+        
+        self.objects[0:0] = new_items
+    
     # lobby state stuff
     @forstate('lobby')
     def check_lobby(self):
 
     @Genesis.responder
     @forstate('lobby')
-    def create_world(self, prng_seed):
+    def create_world(self, prng_seed, player_names, player_id):
         self.world = GameWorld(MAP_WIDTH, MAP_HEIGHT,
             self.level,
             self.objects,
             prng_seed=prng_seed)
         
+        # mutates self.objects / world.objects
+        self.prepend_players(player_names, player_id)
+        self.world.player_obj = self.player
+        
+        
         self.game_state = 'play'
         
         self.looping_call.stop()
             self.start_game()
 
     def start_game(self):
-        for conn in self.connections:
-            conn.callRemote(Genesis, prng_seed=42)
+        for i, conn in enumerate(self.connections):
+            # FIXME: race condition?
+            # Looks like genesis gets processed after below looping call
+            conn.callRemote(Genesis,
+                prng_seed=42,
+                player_names=[conn.username for conn in self.connections],
+                player_id=i)
         
         self.looping_call = loop = LoopingCall(self.runframe)
         self.looping_call.start(1.0/LIMIT_FPS)
             ##reactor.stop()
     
     ##loop = LoopingCall(loop_f)
-    ##loop.start(1/60.)
+    ##loop.start(1/60.)find
     
     ##reactor.run()