[svn] r5824: nemerle/trunk/snippets/the-game: Makefile dice.n generator.n map.n person.n territory.n

malekith svnadmin at nemerle.org
Sun Oct 16 19:45:49 CEST 2005


Log:
Updates.

Author: malekith
Date: Sun Oct 16 19:45:45 2005
New Revision: 5824

Added:
   nemerle/trunk/snippets/the-game/dice.n
   nemerle/trunk/snippets/the-game/person.n
   nemerle/trunk/snippets/the-game/territory.n
Modified:
   nemerle/trunk/snippets/the-game/Makefile
   nemerle/trunk/snippets/the-game/generator.n
   nemerle/trunk/snippets/the-game/map.n

Modified: nemerle/trunk/snippets/the-game/Makefile
==============================================================================
--- nemerle/trunk/snippets/the-game/Makefile	(original)
+++ nemerle/trunk/snippets/the-game/Makefile	Sun Oct 16 19:45:45 2005
@@ -1,2 +1,2 @@
 all:
-	ncc -o TheGame.exe console.n generator.n map.n
+	ncc -o TheGame.exe console.n generator.n map.n person.n territory.n dice.n

Added: nemerle/trunk/snippets/the-game/dice.n
==============================================================================
--- (empty file)
+++ nemerle/trunk/snippets/the-game/dice.n	Sun Oct 16 19:45:45 2005
@@ -0,0 +1,18 @@
+module Dice
+{
+  rand : System.Random = System.Random ();
+
+  // 2d6 kind of stuff
+  public Roll (amount : int, max : int) : int
+  {
+    if (amount == 1)
+      rand.Next (max) + 1
+    else
+      rand.Next (max) + 1 + Roll (amount - 1, max)
+  }
+  
+  public Roll (max : int) : int
+  {
+    Roll (1, max)
+  }
+}

Modified: nemerle/trunk/snippets/the-game/generator.n
==============================================================================
--- nemerle/trunk/snippets/the-game/generator.n	(original)
+++ nemerle/trunk/snippets/the-game/generator.n	Sun Oct 16 19:45:45 2005
@@ -91,8 +91,8 @@
     }
     
     foreach ((x, y) in room_locations) {
-      def rx = 1 + rand.Next (2) + rand.Next (2) + rand.Next (2);
-      def ry = rand.Next (2) + rand.Next (2);
+      def rx = Dice.Roll (3, 3) - 2;
+      def ry = Dice.Roll (2, 3) - 2;
       for (mutable i = x - rx; i <= x + rx; ++i)
         for (mutable j = y - ry; j <= y + ry; ++j)
           dig (i, j)

Modified: nemerle/trunk/snippets/the-game/map.n
==============================================================================
--- nemerle/trunk/snippets/the-game/map.n	(original)
+++ nemerle/trunk/snippets/the-game/map.n	Sun Oct 16 19:45:45 2005
@@ -36,108 +36,28 @@
     this.y = y;
     Map [x, y] ::= this;
   }
-}
-
-class Territory : MapObject
-{
-  protected DisplayWhenVisible : ColoredChar;
-  protected DisplayWhenSeen : ColoredChar;
-  protected DisplayWhenUnseen : ColoredChar = ColoredChar (' ');
-
-  protected mutable ever_seen : bool;
-  
-  public override Draw () : void
-  {
-    if (Map.PlayerCanSee (this)) {
-      ever_seen = true;
-      ConsoleBuffer.DrawChar (DisplayWhenVisible)
-    } else if (ever_seen)
-      ConsoleBuffer.DrawChar (DisplayWhenSeen)
-    else
-      ConsoleBuffer.DrawChar (DisplayWhenUnseen)
-  }
-}
 
-class Floor : Territory
-{
-  public this ()
+  // returns distance squered
+  public virtual DistanceTo (target : MapObject) : int
   {
-    DisplayWhenVisible = ColoredChar ('.', ConsoleColor.Yellow);
-    DisplayWhenSeen = ColoredChar ('.', ConsoleColor.Gray);
+    def dx = X - target.X;
+    def dy = Y - target.Y;
+    dx * dx + dy * dy
   }
 
-  public override CanEnter : bool
+  public virtual PerformMove () : void
   {
-    get { true }
   }
-}
 
-class Wall : Territory
-{
-  public this ()
-  {
-    DisplayWhenVisible = ColoredChar ('#', ConsoleColor.DarkYellow);
-    DisplayWhenSeen = DisplayWhenVisible;
-  }
+  public abstract Name : string { get; }
 
-  Visible : bool
+  public virtual Attack (from : MapObject, _damage : int) : void
   {
-    [Memoize]
-    get {
-      def check (x, y) { Map [x, y].Exists (_.CanEnter) }
-
-      check (X - 1, Y - 1) ||
-      check (X + 1, Y - 1) ||
-      check (X - 1, Y + 1) ||
-      check (X + 1, Y + 1) ||
-      check (X - 1, Y) ||
-      check (X, Y - 1) ||
-      check (X, Y + 1) ||
-      check (X + 1, Y)
+    when (from.IsPlayer)
+      Map.Message ($ "You attack the $(Name). It doesn't seem to be impressed.");
     }
-  }
-  
 
-  public override Draw () : void
-  {
-    if (Visible)
-      base.Draw ()
-    else
-      ConsoleBuffer.DrawChar (DisplayWhenUnseen);
-  }
-}
-
-class Player : MapObject
-{
-  public override Draw () : void
-  {
-    ConsoleBuffer.DrawChar (ColoredChar ('@', ConsoleColor.Cyan));
-  }
-
-  public PerformMove () : void
-  {
-    def key = Console.ReadKey (true);
-    def (x, y) =
-      match (key.Key) {
-        | LeftArrow => (X - 1, Y)
-        | RightArrow => (X + 1, Y)
-        | UpArrow => (X, Y - 1)
-        | DownArrow => (X, Y + 1)
-        | _ => (X, Y)
-      }
-    if (x != X || y != Y) 
-      if (Map [x, y].ForAll (_.CanEnter))
-        MoveTo (x, y)
-      else
-        Map.StatusMessage ("Cannot move there!")
-    else
-      match (key.KeyChar) {
-        | 'r' => Map.ResetMap ();
-        | 'q' => System.Environment.Exit (0);
-        | _ => 
-          Map.StatusMessage ("Move with arrows, [h] for help!")
-      }
-  }
+  public virtual IsPlayer : bool { get { false } }
 }
 
 class WorldMap {
@@ -146,7 +66,12 @@
 
   mutable status : string = "";
 
+  [Accessor (flags = WantSetter)]
+  mutable show_all : bool = false;
+
   map : array [2, list [MapObject]];
+
+  [Accessor]
   mutable player : Player;
 
   public Item [x : int, y : int] : list [MapObject]
@@ -166,6 +91,13 @@
     ResetMap ();
   }
 
+  public IterMap (f : MapObject -> void) : void
+  {
+    for (mutable y = 0; y < height; ++y)
+      for (mutable x = 0; x < width; ++x)
+        map [x, y].Iter (f);
+  }
+
   public ResetMap () : void
   {
     def gen = MapGenerator (width, height);
@@ -185,9 +117,27 @@
     player = Player ();
     player.Init (this, width / 2, height / 2);
     map [player.X, player.Y] ::= player;
+
+    def add_enemies (n) {
+      if (n < 0) {}
+      else {
+        def x = Dice.Roll (width) - 1;
+        def y = Dice.Roll (height) - 1;
+        match (map [x, y]) {
+          | [_ is Floor] =>
+            def e = EnemyUnknown ();
+            map [x, y] ::= e;
+            e.Init (this, x, y);
+            add_enemies (n - 1)
+          | _ => add_enemies (n)
+        }
+      }
+    }
+
+    add_enemies (5);
   }
 
-  public StatusMessage (msg : string) : void
+  public Message (msg : string) : void
   {
     status += $"$msg ";
   }
@@ -195,12 +145,17 @@
   public Draw () : void
   {
     ConsoleBuffer.Clear ();
-    for (mutable y = 0; y < height; ++y) {
-      ConsoleBuffer.Goto (1, y);
-      for (mutable x = 0; x < width; ++x)
+    for (mutable y = 0; y < height; ++y)
+      for (mutable x = 0; x < width; ++x) {
+        ConsoleBuffer.Goto (x + 1, y + 1);
         map [x, y].Head.Draw ();
     }
     ConsoleBuffer.Goto(0, height);
+    ConsoleBuffer.DrawString ($ 
+      "Vit: $(player.Vitality)($(player.MaxVitality)) "
+      "Str: $(player.Strength) "
+      "Sta: $(player.Stamina)", ConsoleColor.Green);
+    ConsoleBuffer.Goto(0, height + 1);
     ConsoleBuffer.DrawString (status, ConsoleColor.Yellow);
     status = "";
 
@@ -209,9 +164,16 @@
 
   public PlayerCanSee (target : MapObject) : bool
   {
-    def dx = player.X - target.X;
-    def dy = player.Y - target.Y;
-    (dx * dx + dy * dy) < 10
+    player.DistanceTo (target) < 15
+  }
+
+  public PerformMove () : void
+  {
+    IterMap (fun (x) {
+      unless (x.IsPlayer)
+        x.PerformMove ();
+    });
+    player.PerformMove ();
   }
   
   public MainLoop () : void
@@ -219,7 +181,7 @@
     try {
       while (true) {
         Draw ();
-        player.PerformMove ();
+        PerformMove ();
       }
     } catch {
       | e =>

Added: nemerle/trunk/snippets/the-game/person.n
==============================================================================
--- (empty file)
+++ nemerle/trunk/snippets/the-game/person.n	Sun Oct 16 19:45:45 2005
@@ -0,0 +1,159 @@
+using System;
+using Nemerle;
+using Nemerle.Utility;
+
+abstract class Person : MapObject
+{
+  [Accessor]
+  protected mutable stamina : int = Dice.Roll (5, 3);
+  [Accessor]
+  protected mutable strength : int = Dice.Roll (5, 3);
+  [Accessor]
+  protected mutable vitality : int = Dice.Roll (5, 3);
+  [Accessor]
+  protected mutable max_vitality : int = vitality;
+
+  protected mutable healing_every : int = Dice.Roll (5, 3);
+  protected mutable healing_for : int;
+
+  public virtual Health : string
+  {
+    get {
+      match (vitality * 4 / max_vitality) {
+        | 0 => "badly damaged"
+        | 1 => "bleeding"
+        | 2 => "damaged"
+        | _ => "still OK"
+      }
+    }
+  }
+
+  public virtual Die () : void
+  {
+    Map [X, Y] = Map [X, Y].Filter (_ != this : object);
+  }
+
+  public override Attack (from : MapObject, damage : int) : void
+  {
+    if (damage < stamina) {
+      when (from.IsPlayer)
+        Map.Message ($ "You didn't even scratch $Name.");
+    } else {
+      healing_for = 0;
+      vitality -= damage - stamina;
+      if (vitality <= 0) {
+        when (from.IsPlayer)
+          Map.Message ($ "You killed $Name.");
+        Die ();
+      } else {
+        when (from.IsPlayer)
+          Map.Message ($ "You hit $Name, it's $Health.");
+      }
+    }
+  }
+
+  public override PerformMove () : void
+  {
+    when (vitality < max_vitality) {
+      if (healing_for >= healing_every) {
+        healing_for = 0;
+        vitality++;
+      } else
+        healing_for++;
+    }
+  }
+
+  public MoveOrKill (x : int, y : int) : void
+  {
+    match (Map [x, y].Filter (fun (x) { ! x.CanEnter })) {
+      | [] => MoveTo (x, y)
+      | x :: _ =>
+        x.Attack (this, Strength / 2 + Dice.Roll (6))
+    } 
+  }
+
+  public RandomMove () : void
+  {
+    def try_move (x, y) {
+      MoveOrKill (x, y)
+    }
+    
+    match (Dice.Roll (4)) {
+      | 1 => try_move (X - 1, Y)
+      | 2 => try_move (X, Y - 1)
+      | 3 => try_move (X + 1, Y)
+      | _ => try_move (X, Y + 1)
+    }
+  }
+}
+
+class EnemyUnknown : Person
+{
+  mutable hostile : bool;
+
+  public override Draw () : void
+  {
+    when (Map.PlayerCanSee (this))
+      ConsoleBuffer.DrawChar (ColoredChar ('K', ConsoleColor.Red));
+  }
+
+  public override PerformMove () : void
+  {
+    if (hostile && DistanceTo (Map.Player) == 1)
+      MoveOrKill (Map.Player.X, Map.Player.Y)
+    else
+      RandomMove ();
+  }
+
+  public override Attack (from : MapObject, damage : int) : void
+  {
+    when (from.IsPlayer)
+      hostile = true;
+    base.Attack (from, damage)
+  }
+
+  public override Name : string { get { "enemy unknown" } }
+}
+
+class Player : Person
+{
+  public override Draw () : void
+  {
+    ConsoleBuffer.DrawChar (ColoredChar ('@', ConsoleColor.Cyan));
+  }
+
+  public override PerformMove () : void
+  {
+    base.PerformMove (); // heal
+
+    def key = Console.ReadKey (true);
+    def (x, y) =
+      match (key.Key) {
+        | LeftArrow => (X - 1, Y)
+        | RightArrow => (X + 1, Y)
+        | UpArrow => (X, Y - 1)
+        | DownArrow => (X, Y + 1)
+        | _ => (X, Y)
+      }
+    if (x != X || y != Y)
+      MoveOrKill (x, y)
+    else
+      match (key.KeyChar) {
+        | 'r' => Map.ResetMap ();
+        | 's' => Map.ShowAll = ! Map.ShowAll;
+        | 'q' => System.Environment.Exit (0);
+        | _ => 
+          Map.Message ("Move with arrows, [h] for help!")
+      }
+  }
+
+  public override Attack (from : MapObject, damage : int) : void
+  {
+    Map.Message ($ "The $(from.Name) attacks you.");
+    base.Attack (from, damage);
+  }
+  
+  public override Name : string { get { "player" } }
+  public override IsPlayer : bool { get { true } }
+}
+

Added: nemerle/trunk/snippets/the-game/territory.n
==============================================================================
--- (empty file)
+++ nemerle/trunk/snippets/the-game/territory.n	Sun Oct 16 19:45:45 2005
@@ -0,0 +1,82 @@
+using System;
+using Nemerle;
+using Nemerle.Utility;
+
+abstract class Territory : MapObject
+{
+  protected DisplayWhenVisible : ColoredChar;
+  protected DisplayWhenSeen : ColoredChar;
+  protected DisplayWhenUnseen : ColoredChar = ColoredChar (' ');
+
+  protected mutable ever_seen : bool;
+  
+  public override Draw () : void
+  {
+    if (Map.PlayerCanSee (this)) {
+      ever_seen = true;
+      ConsoleBuffer.DrawChar (DisplayWhenVisible)
+    } else if (ever_seen || Map.ShowAll)
+      ConsoleBuffer.DrawChar (DisplayWhenSeen)
+    else
+      ConsoleBuffer.DrawChar (DisplayWhenUnseen)
+  }
+}
+
+class Floor : Territory
+{
+  public this ()
+  {
+    DisplayWhenVisible = ColoredChar ('.', ConsoleColor.Yellow);
+    DisplayWhenSeen = ColoredChar ('.', ConsoleColor.Gray);
+  }
+
+  public override CanEnter : bool
+  {
+    get { true }
+  }
+
+  public override Name : string
+  {
+    get { "floor" }
+  }
+}
+
+class Wall : Territory
+{
+  public this ()
+  {
+    DisplayWhenVisible = ColoredChar ('#', ConsoleColor.DarkYellow);
+    DisplayWhenSeen = DisplayWhenVisible;
+  }
+
+  Visible : bool
+  {
+    [Memoize]
+    get {
+      def check (x, y) { Map [x, y].Exists (_.CanEnter) }
+
+      check (X - 1, Y - 1) ||
+      check (X + 1, Y - 1) ||
+      check (X - 1, Y + 1) ||
+      check (X + 1, Y + 1) ||
+      check (X - 1, Y) ||
+      check (X, Y - 1) ||
+      check (X, Y + 1) ||
+      check (X + 1, Y)
+    }
+  }
+  
+
+  public override Draw () : void
+  {
+    if (Visible)
+      base.Draw ()
+    else
+      ConsoleBuffer.DrawChar (DisplayWhenUnseen);
+  }
+
+  public override Name : string
+  {
+    get { "wall" }
+  }
+}



More information about the svn mailing list