Commits

mcc  committed ec1851e

Can load inits

  • Participants
  • Parent commits d6c5b9b

Comments (0)

Files changed (1)

File server/server.py

 define("specs", default=10, help="species per tank", type=int)
 #define("giants", default=0, help="how many giants?", type=int)
 define("shelf_after", default=0, help="where is shelf?", type=int)
-define("shelf_size", default=0, help="how big is shelf?", type=int)
+define("shelf_size", default=-1, help="how big is shelf?", type=int)
 define("input", default='', help="initialize state from log", type=str)
 define("norun", default=0, help="print and exit", type=int)
 define("debug", default=0, help="debug runlevel", type=int)
 			history_file.write("\n")
 	
 class Genome:
-	def __init__(self, _id):
+	def __init__(self, _id, suppress = False):
 		self.id = _id
 		self.specs = {} # Unordered
 		self.specs_stack = [] # Ordered
 		self.issue_next = 0
 		self.generation_count = 1
 		self.id_generator = 0
-		for i in range(options.specs):
-			nid = self.nextid()
-			s = basic.clone().mutate()
-			s.sid = nid
-			self.specs[nid] = s
-			self.specs_stack.append(s)
-		self.last_valid = self.specs_stack[0]
-		self.specs_stack.reverse()
+		if not suppress:
+			for i in range(options.specs):
+				nid = self.nextid()
+				s = basic.clone().mutate()
+				s.sid = nid
+				self.specs[nid] = s
+				self.specs_stack.append(s)
+				self.last_valid = self.specs_stack[0]
+				self.specs_stack.reverse()
 
 	def nextid(self):
 		self.id_generator += 1
 	def __init__(self, input=None):
 		self.tanks = {}
 		self.alltanks = {}
-		if not input:
+		if not input: # GENERATE RANDOM
 			for _i in range(options.tanks):
 				i = _i + 1
 				if options.debug > 0:
 					print("Generating tank %d" % i)
 				self.alltanks[i] = Genome(i)
-				if _i < options.shelf_size:
+				if options.shelf_size <= 0 or _i < options.shelf_size:
 					self.tanks[i] = self.alltanks[i]
-		else:
-			node = input.firstChild
+		else: # GENERATE FROM XML SOURCE
+			node = input.firstChild # Iter commands
 			while node:
 				if node.nodeName == "init":
-					print("INIT")
+					if options.debug > 1:
+						print("Loading: INIT")
+					self.alltanks = {}
+					tank = node.firstChild # Iter tanks
+					while tank:
+						if not tank.nodeName == "tank":
+							continue
+						gid = int(tank.getAttribute("gid"))
+						if options.debug > 1:
+							print("\tLoading: Create tank %d" % gid)
+						t = Genome(gid, True)
+						spawn = tank.firstChild # Iter spawns
+						while spawn:
+							s = Spec(gid, True)
+							sid = int(spawn.getAttribute("sid"))
+							s.sid = sid
+							if options.debug > 1:
+								print("\t\tLoading: Create spec %d" % sid)
+							s.board = Board(int(spawn.getAttribute("mapx")), int(spawn.getAttribute("mapy")), True)
+							s.board.str = spawn.getAttribute("map")
+							e = spawn.getElementsByTagName("e")[0].firstChild # Iter entities
+							while e:
+								c = int(e.getAttribute("c"))
+								x = int(e.getAttribute("x"))
+								y = int(e.getAttribute("y"))
+								if c == 1:
+									s.board.px = x
+									s.board.py = y
+								elif c == 2:
+									s.board.ex = x
+									s.board.ey = y
+								e = e.nextSibling
+							t.specs[sid] = s
+							t.specs_stack.append(s)
+							spawn = spawn.nextSibling
+						t.last_valid = t.specs_stack[0]
+						t.specs_stack.reverse()
+						self.alltanks[gid] = t
+						tank = tank.nextSibling
+					
 				elif node.nodeName == "twhy":
-					print("TWHY")
+					if options.debug > 1:
+						print("Loading: TWHY")
+					self.report(node)
+					
 				elif node.nodeName == "breed":
-					print("TWHY")
+					if options.debug > 1:
+						print("Loading: BREED")
 				node = node.nextSibling
+			
+			self.tanks = self.alltanks
 	
 	def any(self, post_obj):
 		if player_hits(post_obj) > options.shelf_after: