home · contact · privacy
Add basic movement AI test, extend move command directions.
[plomrogue2-experiments] / server.py
index 215317dd6714cfdd97c59b3f980781414e5b269e..aa8b1c3aab7b1bf8b85fc3930d5de17d997ccd12 100755 (executable)
--- a/server.py
+++ b/server.py
@@ -90,7 +90,8 @@ class Task:
 
 class Thing:
 
-    def __init__(self, position):
+    def __init__(self, type_, position):
+        self.type = type_
         self.position = position
         self.task = Task('wait')
 
@@ -102,9 +103,18 @@ class Thing:
             self.position[0] -= 1
         elif direction == 'DOWN':
             self.position[0] += 1
+        elif direction == 'RIGHT':
+            self.position[1] += 1
+        elif direction == 'LEFT':
+            self.position[1] -= 1
 
     def decide_task(self):
-        self.set_task('wait')
+        if self.position[1] > 1:
+            self.set_task('move', 'LEFT')
+        elif self.position[1] < 3:
+            self.set_task('move', 'RIGHT')
+        else:
+            self.set_task('wait')
 
     def set_task(self, task, *args, **kwargs):
         self.task = Task(task, args, kwargs)
@@ -135,7 +145,7 @@ class World:
                     'x.X.x\n'+\
                     'x...x\n'+\
                     'xxxxx'
-        self.things = [Thing(position=[3, 3]), Thing([1, 1])]
+        self.things = [Thing('human', [3, 3]), Thing('monster', [1, 1])]
         self.player_i = 0
         self.player = self.things[self.player_i]
 
@@ -201,7 +211,9 @@ class CommandHandler:
         self.send_all('NEW_TURN ' + str(self.world.turn))
         self.send_all('MAP_SIZE ' + self.stringify_yx(self.world.map_size))
         self.send_all('TERRAIN\n' + self.world.map_)
-        self.send_all('POSITION ' + self.stringify_yx(self.world.player.position))
+        for thing in self.world.things:
+            self.send_all('THING TYPE:' + thing.type + ' '
+                          + self.stringify_yx(thing.position))
 
     def cmd_fib(self, tokens, connection_id):
         """Reply with n-th Fibonacci numbers, n taken from tokens[1:].
@@ -244,7 +256,9 @@ class CommandHandler:
         self.send_all('NEW_TURN ' + str(self.world.turn))
         self.send_all('MAP_SIZE ' + self.stringify_yx(self.world.map_size))
         self.send_all('TERRAIN\n' + self.world.map_)
-        self.send_all('POSITION ' + self.stringify_yx(self.world.player.position))
+        for thing in self.world.things:
+            self.send_all('THING TYPE:' + thing.type + ' '
+                          + self.stringify_yx(thing.position))
         self.pool_result = self.pool.map_async(fib, (35, 35))
 
     def cmd_get_turn(self, connection_id):
@@ -253,8 +267,9 @@ class CommandHandler:
 
     def cmd_move(self, direction, connection_id):
         """Set player task to 'move' with direction arg, finish player turn."""
-        if not direction in {'UP', 'DOWN'}:
-            raise ArgumentError('MOVE ARGUMENT MUST BE "UP" or "DOWN"')
+        if not direction in {'UP', 'DOWN', 'RIGHT', 'LEFT'}:
+            raise ArgumentError('MOVE ARGUMENT MUST BE ONE OF: '
+                                'UP, DOWN, RIGHT, LEFT')
         self.world.player.set_task('move', direction=direction)
         self.proceed_to_next_player_turn(connection_id)