Commits

Anonymous committed 34d1011

basic futures test.

Comments (0)

Files changed (1)

 
 """
 
-X, Y = 160, 60
-rows = 30
+X, Y = 120, 30
+rows = 20
 
 from d6.char import Char
 from random import choice, randint, shuffle
 from termctl import *
+import sys
+if sys.version_info < (3, 2, 0, "", 0):
+    raise Exception("You need at least Python 3.2 for this experimental concurrent version. TODO: change to require concurrency.futures")
+from concurrent.futures import *
+et = ThreadPoolExecutor(max(4, (Y-20)*(rows)*2)) # one per fighter
+ep = ProcessPoolExecutor(2) # 3 - 1 processors (1 for freenet, one for the main code -> 2 left)
 
 #: Example sourcedata for creating a character.
 _swordmaster = """
 
 def attack(me, enemy, mod=0):
     win, damage, wounds = me.attack(enemy, "sword", me.weapon, me.armor, "sword", enemy.weapon, enemy.armor, mod)
+    for i in me, enemy:
+        try: i.new_experience += 0
+        except: i.new_experience = 0
     if win and wounds == 2:
         del battlefield[enemy.pos] # dead
         fighters.remove(enemy)
             team0.remove(enemy)
         else: team1.remove(enemy)
         me.enemy = None
-        me.improve(9)
+        me.new_experience += 9
     elif not win and wounds == 2:
-        del battlefield[me.pos]
-        fighters.remove(me)
-        if me.team == 0:
-            team0.remove(me)
-        else: team1.remove(me)
+        try:
+            del battlefield[me.pos]
+            fighters.remove(me)
+            if me.team == 0:
+                team0.remove(me)
+            else: team1.remove(me)
+        except KeyError: pass # already gone
         enemy.enemy = None
-        enemy.improve(9)
+        enemy.new_experience += 9
     elif win and wounds:
-        me.improve(3)
+        me.new_experience += 3
     elif not win and wounds:
-        enemy.improve(3)
+        enemy.new_experience += 3
     elif win:
-        me.improve(1)
+        me.new_experience += 1
     else:
-        enemy.improve(1)
+        enemy.new_experience += 1
 
 
+def preparemovement(me):
+    """Gather the data needed for movement."""
+    if me.wounds[1]:
+        return me
+    if me.enemy is None or not me.enemy.pos in battlefield:
+        findenemy(me)
+    if me.enemy is None:
+        return me
+    me.forwards = stepforward(me, me.enemy)
+    me.sidewards = stepsideward(me, me.enemy)
+    me.odd = oddstep(me)
+    return me
+
+def improvechar(me):
+    """use the new_experience to improve the char."""
+    me.improve(me.new_experience)
+    me.new_experience = 0
 
 def battleround():
     """a single round of battle"""
-    for me in fighters[:]:
+    # concurrent preparation
+    # step-by-step action
+    for me in et.map(preparemovement, fighters):
         if me.wounds[1]:
             continue
-        if me.enemy is None or not me.enemy.pos in battlefield:
-            findenemy(me)
         if me.enemy is None:
             continue
-        forwards = stepforward(me, me.enemy)
-        sidewards = stepsideward(me, me.enemy)
-        odd = oddstep(me)
-        for step in (forwards, sidewards, odd):
+        for step in (me.forwards, me.sidewards, me.odd):
             if step in battlefield:
                 isenemy = battlefield[step].team != me.team
                 if isenemy:
                     attack(me, me.enemy, mod)
                     break
             else: # move there
-                del battlefield[me.pos]
+                try:
+                    del battlefield[me.pos]
+                except KeyError: pass # already gone
                 me.pos = step
                 battlefield[me.pos] = me
                 break
         if me.wounds[0]:
             me.letter = me.let
+    ep.map(improvechar, fighters)
 
 def meancost(team):
     return int(sum((c.cost for c in team))/max(1, len(team)))