Merge
authorDavid Sharpe <decoydavid@gmail.com>
Thu, 3 Mar 2016 21:08:58 +0000 (23:08 +0200)
committerDavid Sharpe <decoydavid@gmail.com>
Thu, 3 Mar 2016 21:08:58 +0000 (23:08 +0200)
1  2 
koperkapel/generators/maps.py

index 51aae6500f4ed313707fbb5353d185eae3f2ea77,94269294e914d6f7b6b9430119d279e54b758b36..4f917c68169ea454c9feb34d63ee4a2284ff70e7
@@@ -6,76 -8,14 +8,87 @@@ import o
  i = random.randint(0,100)
  
  
++<<<<<<< HEAD
 +class Room:
 +    def __init__(self, region):
 +        """
 +        """
 +        self.regions = [region]
 +        self.max_connections = 1
 +        self.passages = []
 +        self.tunnels = []
 +
 +    def is_linked(self):
 +        """
 +        Check if the room is linked to another room
 +        :return: Whether the room has any links or not
 +        """
 +        return len(self.passages) + len(self.tunnels) > 0
 +
 +    def add_passage(self, room_region, new_region):
 +        """ Add a passage link between rooms
 +        :param room_region: coordinate of region from which we are linking
 +        :param new_region: coordinate of region to which we are linking
 +        :return:
 +        """
 +        pass
 +
 +    def add_region(self, region):
 +        """
 +        Add a new region into an existing room
 +        :param region: region coordinates to be added to room
 +        :return:
 +        """
 +        self.regions.append(region)
 +
 +    def connect_rooms(self, other_rooms):
 +        """ Find the nearest rooms to this room
 +        :param other_rooms: list of Rooms objects that we are searching
 +        :return:
 +        """
 +        distance = []
 +        other_tile = []
 +        this_tile = []
 +        target_rooms = []
 +        for local_region in self.regions:
 +            for room in other_rooms:
 +                if self == room:
 +                    continue
 +                for new_region in room.regions:
 +                    distance.append(
 +                        math.sqrt((local_region[0] - new_region[0]) ** 2 +
 +                                  (local_region[1] - new_region[1]) ** 2))
 +                    other_tile.append(new_region)
 +                    this_tile.append(local_region)
 +                    target_rooms.append(room)
 +
 +        sorted_indices = [i[0] for i in sorted(enumerate(distance),
 +                                               key=lambda x:x[0])]
 +        for index in sorted_indices:
 +            if len(self.passages) + len(self.tunnels) >= self.max_connections:
 +                break
 +            if not target_rooms[index].is_linked():
 +                self.link_passage(this_tile[index], other_tile[index])
 +                target_rooms[index].link_passage(
 +                    other_tile[index], this_tile[index])
 +
 +    def link_passage(self, local_tile, foriegn_tile):
 +        """ Link a passage between two rooms
 +        :param local_tile: tile in this room to which we wish to link
 +        :param foriegn_tile: tile in another room to which we wish to link
 +        :return:
 +        """
 +        self.passages.append([local_tile, foriegn_tile])
++=======
+ ATTRIBUTE_MAP = {
+     '#': {'base': 'cwall', 
+           'behaviour': [],
+          },
+     ' ': {'base': 'floor',
+           'behaviour': ['walk', 'fly'],
+          },
+ }
++>>>>>>> 0b2149fff63b346192148b7eb334e00ad3b9283d
  
  
  class LevelGenerator:
              file.write(''.join(l))
              file.write('\n')
          file.close()
++<<<<<<< HEAD
 +        for l in self.region_map:
++=======
+         self._to_json()
+         for l in self.regions:
++>>>>>>> 0b2149fff63b346192148b7eb334e00ad3b9283d
              print(l)
  
+     def _to_json(self):
+         level = {}
+         level['tiles'] = []
+         for l in self.map:
+             row = []
+             for t in l:
+                 row.append(ATTRIBUTE_MAP[t])
+             level['tiles'].append(row)
+         name = os.path.join(os.path.dirname(__file__), '..', 'levels', 'map.json')
+         # FIXME: Do a lot better here 
+         # Crude hack so the level is written into the levels folder
+         f = open(name, 'w')
+         json.dump(level, f)
+         f.close()
  
  if __name__ == '__main__':
      while True: