*** empty log message ***
authorMatthew Mondor <mmondor@pulsar-zone.net>
Sat, 30 Jul 2005 23:17:01 +0000 (23:17 +0000)
committerMatthew Mondor <mmondor@pulsar-zone.net>
Sat, 30 Jul 2005 23:17:01 +0000 (23:17 +0000)
tests/js-test/js/game/objects.js

index 2a22be8..03894b0 100644 (file)
@@ -1,10 +1,16 @@
-/* $Id: objects.js,v 1.3 2005/07/30 09:04:42 mmondor Exp $ */
+/* $Id: objects.js,v 1.4 2005/07/30 23:17:01 mmondor Exp $ */
 
 /*
  * Copyright (c) 2005, Matthew Mondor
  * ALL RIGHTS RESERVED.
  */
 
+/*
+ * Primarily written in JavaScript for easy and quick prototyping/testing.
+ * May eventually be written in C afterwards if need be, but the game might
+ * actually be implemented in JavaScript too depending on future decisions.
+ */
+
 
 
 /*
@@ -31,12 +37,17 @@ function Item(name, description, volume, weight, ctype)
 }
 
 Item.prototype = {
-       volume: function()
+       isContainer: function()
+       {
+               return false;
+       },
+
+       getVolume: function()
        {
                return this.volume;
        },
 
-       weight: function()
+       getWeight: function()
        {
                return this.weight;
        }
@@ -47,6 +58,7 @@ Item.prototype = {
  * And an object to handle container Items, inheriting the Item object.
  */
 
+const E_OK             = 0;
 const E_TOOLARGE       = -1;
 const E_TOOHEAVY       = -2;
 const E_BADTYPE                = -3;
@@ -73,6 +85,11 @@ function ContainerItem(name, description, volume, weight, ctype,
 }
 
 ContainerItem.prototype = {
+       isContainer: function()
+       {
+               return true;
+       },
+
        open: function()
        {
                this.con_open = true;
@@ -83,7 +100,7 @@ ContainerItem.prototype = {
                this.con_open = false;
        },
 
-       volume: function()
+       getVolume: function()
        {
                /*
                 * Objects which expand, such as bags, need to report proper
@@ -96,7 +113,7 @@ ContainerItem.prototype = {
                    this.volume;
        },
 
-       weight: function()
+       getWeight: function()
        {
                return this.weight + this.con_weight;
        },
@@ -111,22 +128,29 @@ ContainerItem.prototype = {
                /* Can only add items to open container */
                if (!this.con_open)
                        return E_CLOSED;
+
                /* We can't add ourself in :) */
-               if (item.index == this.item.index)
+               if (item.index == this.index)
                        return E_SELF;
+
                /* Nor items which are already inside */
                if (this.con_items[item.index] != undefined)
                        return E_ALREADY;
+
                /* We only accept items of proper type */
                if ((this.con_ctype & item.ctype) == 0)
                        return E_BADTYPE;
+
                /* We can only hold objects which are small enough */
-               if (this.con_neck < item.volume())
+               if (this.con_neck < item.getVolume())
                        return E_TOOLARGE;
-               if (this.con_volume_cur + item.volume() > this.con_volume_max)
+               if (this.con_volume_cur + item.getVolume() >
+                   this.con_volume_max)
                        return E_TOOLARGE;
+
                /* We can only hold objects which are light enough */
-               if (this.con_weight_cur + item.weight() > this.con_weight_max)
+               if (this.con_weight_cur + item.getWeight() >
+                   this.con_weight_max)
                        return E_TOOHEAVY;
 
                /*
@@ -142,18 +166,43 @@ ContainerItem.prototype = {
                 * expandable, whenever objects are added/removed, and would
                 * also verify their parent's volume/weight limits anytime.
                 * We need to do this for weights anyway.
+                * So if a container is inside a container, it must have a
+                * parent.  That parent must go up iteratively until the
+                * master container is found, stopping and reverting anything
+                * when reaching the limit of any parent.
                 *
                 * Also implement a recursive runner which maps all objects,
                 * for instance for inventory purposes, but only recursing
                 * through containers which are in the open state.
                 */
 
+               /*
+                * The weight has to propagate to parent containers.
+                * The volume also has, in the case where containers
+                * are expandable.
+                * We need to observe the maximum allowed volume and weight
+                * of all parent containers as well.
+                * We need to climb through the parents list to do this.
+                * Moreover, we need to actually reverse any changes made
+                * to the parents in case of any volume/weight exceeded.
+                *
+                * XXX Hmm perhaps that I should use another method
+                * alltogether, always evaluating the volume and weight of an
+                * object with all its children?  The type of game is probably
+                * not performance critical anyways.
+                */
+               for (var o = this.con_parent; o != undefined;
+                   o = o.con_parent) {
+                       /* XXX */
+       //              if (o.weight + this.getWeight() > o.con_weight_max)
+               }
+
                this.con_items[item.index] = item;
-               this.con_volume_cur += item.volume();
-               this.con_weight_cur += item.weight();
+               this.con_volume_cur += item.getVolume();
+               this.con_weight_cur += item.getWeight();
                this.con_items_cnt++;
 
-               return 0;
+               return E_OK;
        },
 
        remove: function(item)
@@ -161,13 +210,40 @@ ContainerItem.prototype = {
                /* Can only remove items from open container */
                if (!this.con_open)
                        return E_CLOSED;
+
                /* We can only remove items which are really inside */
                if (this.con_items[item.index] == undefined)
                        return E_NONE;
 
-               this.con_weight_cur -= item.weight();
-               this.con_volume_cur -= item.volume();
+               this.con_weight_cur -= item.getWeight();
+               this.con_volume_cur -= item.getVolume();
                delete this.con_items[item.index];
                this.con_items_cnt--;
+
+               return E_OK;
        }
 };
+
+
+
+/*
+ * Some testing in order
+ */
+
+/* Create an item */
+var i = new Item('a ring', 'the one ring', 5, 5, CT_SOLID);
+
+/* Create a container item */
+var c = new ContainerItem('a bag', 'a soft cloth bag', 5, 5, CT_SOLID,
+    10, 15, 20, CT_SOLID | CT_EXPAND);
+
+/*
+ * Open container, add item in it and then close it, reopening it again to
+ * remove back the item from it and to close it once and for all.
+ */
+c.open();
+print(c.add(i));
+c.close();
+c.open();
+print(c.remove(i));
+c.close();