Skip to content

Instantly share code, notes, and snippets.

@kenwebb
Last active June 17, 2018 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenwebb/4afa0d063848c2c040cf7d109a180a88 to your computer and use it in GitHub Desktop.
Save kenwebb/4afa0d063848c2c040cf7d109a180a88 to your computer and use it in GitHub Desktop.
Island 2
<?xml version="1.0" encoding="UTF-8"?>
<!--Xholon Workbook http://www.primordion.com/Xholon/gwt/ MIT License, Copyright (C) Ken Webb, Sun Jun 17 2018 12:53:20 GMT-0400 (Eastern Daylight Time)-->
<XholonWorkbook>
<Notes><![CDATA[
Xholon
------
Title: Island 2
Description:
Url: http://www.primordion.com/Xholon/gwt/
InternalName: 4afa0d063848c2c040cf7d109a180a88 based on c8e4d796b9d76021d677a1d712ecadae
Keywords:
My Notes
--------
May 23, 2018
Jen and I met yesterday to kick off the Island project. See my written notes from yesterday.
She described her initial requirements, and I will implement an initial prototype in this Xholon workbook.
To Run
------
http://www.primordion.com/Xholon/gwt/Xholon.html?app=c8e4d796b9d76021d677a1d712ecadae&src=gist&gui=none
http://127.0.0.1:8888/Xholon.html?app=Island+1&src=lstr&gui=none
http://localhost:3000/?app=4afa0d063848c2c040cf7d109a180a88&src=gist&gui=none
Backstory
---------
A castaway tries to survive on a small island with renewable but limited resources.
Castaway
--------
For now, I will use the Xholon system Avatar as the single "castaway" on the island.
TODO the castaway will have a Calorie meter and a Health meter
Fish
----
A fish is an individual node containing zero or more behaviors, similar to a cat in my Bestiary model.
It moves randomly in the ocean.
It gets bigger each time step.
It dies (looses all its living behaviors):
- when it is caught by the castaway, or
- when it reaches old age, or
- randomly while swimming in the ocean (something eats it, or it's injured, etc.)
If caught by the castaway, a fish acquires a new behavior:
- it starts to decompose and the castaway must eat it before it "becomes bad"
Grid
----
I may want to stitch together 2 or more grids to create the overall IslandSystem.
- 2 grids with Island at the top and Ocean at the bottom; TB or BT or LR or RL
- 9 grids with Island inn the middle and parts of the Ocean in the 8 surrounding grids
- see [2] for one way to do this
- gridType can be
- "Gmt" NEIGHBORHOOD_MOORE (8 neighbors) + BOUNDARY_TORUS (boundaries wrap)
- "Gmg" NEIGHBORHOOD_MOORE (8 neighbors) + BOUNDARY_GRID
- OR just use one grid, where each gridCell contains an Ocean or Island building block
- similar to WallSection and other node types in Bestiary
- this approach would allow an island with any shape
custom Avatar behaviors
-----------------------
ava.append("<script>$wnd.console.log('TEST');</script>");
ava.append("<script>$wnd.console.log(this);</script>");
ava.append("<script>$wnd.console.log(this.parent());</script>");
ava.append('<script>
var ava = this.parent();
ava.action("build Lumber; take lumber;");
var node = ava.first();
if (node) {
if (node.xhc().name() == "Lumber") {
ava.action("build Hut role HomeSweetHome; eat lumber;");
}
}
</script>');
ava.append('<script>var ava=this.parent();ava.action("build Lumber;take lumber;");var node=ava.first();if(node){if(node.xhc().name()=="Lumber"){ava.action("build Hut role HomeSweetHome;eat lumber;");}}</script>');
ava.append('<script>var ava=this.parent();console.log(ava.name());ava.action("build Lumber;take lumber;");var node=ava.first();console.log(node.name());if(node){if(node.xhc().name()=="Lumber"){ava.action("build Hut role HomeSweetHome;eat lumber;");}}</script>');
// assume that Avatar has collected some lumber
var ava = xh.avatar();
ava.append('<script>var ava=this.parent();console.log(ava.name());var node=ava.first();console.log(node.name());if(node){if(node.xhc().name()=="Lumber"){ava.action("build Hut role HomeSweetHome;eat lumber;");}}</script>');
// could this somehow be in xh.avatarKeyMap, so it could be invoked with a single key?
ava.append('<script>var ava=this.parent();var node=ava.first();if(node){if(node.xhc().name()=="Lumber"){ava.action("build Hut role HomeSweetHome;eat lumber;");}}</script>');
// the following sort-of works
ava.action("if xpath(Lumber) build Hut role Shack; eat lumber;");
// this works
ava.action("if xpath(Avatar/Lumber) build Hut role Shack;");
ava.action("if xpath(Avatar/Lumber) build Hut role Shack;if xpath(Avatar/Lumber) eat lumber;");
Speech generation
-----------------
ava.action("out speech Hello");
ava.action("param speech true");
Speech recognition (only works with localhost)
------------------
xh.speechRecognition();
June 5, 2018
------------
I started Island 2, to implement the exact Minecraft (1.12.2) way of starting a game. [4]
Basically, in Minecraft:
- chop several trees (Birch or Oak or other(s)?) into 10 blocks
- move close to a tree until a block boundary appears
- hold down on the left mouse button until the block falls out of the tree
- take the block into inventory by moving close to it
- press E to engage with the inventory
- move tree blocks onto the built-in 2x2 crafting table
-
colors
------
these are the colors I used in Cube:
#b8ccea
#d0c883
#171c8f
#f1b434
#9cdbd9
#c2e189
TODO
----
- fix: Lumber has a Plantbehavior, which means it "rots" and gets removed when in the Avatar inventory
References
----------
(1) http://www.primordion.com/Xholon/gwt/wb/editwb.html?app=1f961eced0536248701adc9977faf43f&src=gist
) http://www.primordion.com/Xholon/gwt/Xholon.html?app=1f961eced0536248701adc9977faf43f&src=gist&gui=none
see ideas in this General Relativity workbook; uses GridGenerator
(2) http://www.primordion.com/Xholon/gwt/wb/editwb.html?app=8d4f8e4b85906690116a1787b90118cd&src=gist
) http://www.primordion.com/Xholon/gwt/XholonTether.html?app=8d4f8e4b85906690116a1787b90118cd&src=gist&gui=none
see ideas in this Cube workbook; multiple grids, uses GridGenerator 6 times, uses Tether JS library
implements Avatar movement between grids
(3) http://www.primordion.com/Xholon/gwt/wb/editwb.html?app=7381da240bf298a3db6d6112b4440fc2&src=gist
Xholon behavior subclasses
This workbook explores how to implement JavaScript classes and subclasses for Xholon behaviors.
(4) http://www.minecraftopia.com/how_to_play_minecraft
Upon entering a new world, the two most important things to do are crafting tools and building a shelter, all before the first night-cycle hits. Finding trees
and harvesting wood are the first steps towards making tools.
Once you have found a couple trees, use your fist to punch them and collect the wood that drops. To harvest resources, hold down the left mouse button
until the cracks in the block cause it to break. Once the block breaks, you can walk near it to automatically place the item in your inventory. Keep
punching trees until you have gathered 10 pieces of wood.
Now that you finished gathering wood, start looking for a place to build your night-one shelter. Keep in mind that your night-one shelter should be a
simple covering designed to keep monsters out. High up areas on top of mountains are usually a good pace to build, but anywhere will do.
Once you have identified a suitable location for your night-one shelter, it’s time to convert your wood into wooden planks. Press ‘E’ to open your inventory,
where you should notice the 2x2 crafting grid to the right of your character. When you place wood into the crafting squares, each piece of wood can be
turned into 4 wooden planks. Use the wood blocks you gathered to create 24 wooden planks.
Now that you have wooden planks, you can build your first crafting table. Crafting tables are the workbenches by which you will build most tools and
items in Minecraft, so be sure to place it in an accessible location.
Once your crafting table is placed, you can open it (right-click it) to reveal a 3x3 crafting grid, similar to the one in your inventory. Sticks are required to
build tools, so place two wooden planks in the crafting grid, one on top of the other. Use 6 of your wooden planks to create 12 sticks.
]]></Notes>
<_-.XholonClass>
<IslandSystem/>
<Ocean/>
<Island/>
<!-- environment -->
<Weather/>
<!-- types of food available to the castaway on the island -->
<!-- seafood from the ocean -->
<Fish/>
<!-- edible plants on the island -->
<Plant>
<Fruit/>
<Vegetables/>
</Plant>
<PlantBehaviors/>
<FishBehaviors/>
<AnimalBehaviors/> <!-- cats -->
<!-- building materials available to the castaway -->
<Lumber/> <!-- maybe driftwood or beams from a sunken ship or logs lying on the ground -->
<!-- things that can be built -->
<Hut/> <!-- a hut can be built out of lumber -->
<Raft/> <!-- TODO a raft can be built out of 2 lumber -->
<Islet/> <!-- TODO an islet can be built out of 10 of some building material (rocks?) -->
<!-- grid -->
<!-- Luke's stuff -->
<Cat/> <!-- for now, a Cat is a kind of Fish so it can use the existing FishMovebehavior -->
<Stick/>
<String/>
<Hook/>
<FishingRod/> <!-- craft out of a stick + a string + a hook -->
<!--<OceanCell>--> <!-- OceanCell is the default -->
<!--<LandCell/>--> <!-- for now LandCell has to be a subclass of OceanCell -->
<!--<CoastalCell/>
</OceanCell>-->
<IslandGridCell>
<OceanCell/> <!-- OceanCell is the default -->
<LandCell/>
<CoastalCell/>
</IslandGridCell>
<GridCellPattern superClass="Attribute_String"/>
<!-- Minecraft stuff -->
<Tree>
<AcaciaTree/>
<BirchTree/>
<DarkOakTree/>
<JungleTree/>
<OakTree/>
<SpruceTree/>
</Tree>
<!-- Oak, Spruce, Birch, Jungle, Acacia, and Dark Oak -->
<Wood>
<AcaciaWood/>
<BirchWood/>
<DarkOakWood/>
<JungleWood/>
<OakWood/>
<SpruceWood/>
</Wood>
<Leaves/>
<Stump/>
<!-- Island Control Centre -->
<City/>
<University/>
<Library/>
<CoffeeShop/>
<IslandControlCentre/>
</_-.XholonClass>
<xholonClassDetails>
<LandCell><Color>PaleGoldenRod</Color></LandCell> <!-- PaleGoldenRod -->
<CoastalCell><Color>#d0c883</Color></CoastalCell> <!-- LightSeaGreen #d6d6ad Cube: #b8ccea(lightblue) #d0c883(olive) #171c8f(darkblue) #f1b434(orange) #9cdbd9(lightblue) #c2e189(greenish) -->
<Avatar><Color>red</Color></Avatar>
<OakTree><DefaultContent><![CDATA[
<_-.oakdc>
<OakWood maxClones="6"/>
<Leaves maxClones="16"/>
<Stump/>
</_-.oakdc>
]]></DefaultContent></OakTree>
<BirchTree><DefaultContent><![CDATA[
<_-.birchdc>
<BirchWood maxClones="6"/>
<Leaves maxClones="16"/>
<Stump/>
</_-.birchdc>
]]></DefaultContent></BirchTree>
</xholonClassDetails>
<IslandSystem>
<!-- rows and cols must be same as const ROWS and const COLS in Behaviors -->
<GridGenerator rows="80" cols="120" gridType="Gmt" names="Space,FieldRow,OceanCell" columnColor="171c8f" gridViewerParams="IslandSystem/Space,8,Island Viewer,true" cellsCanSupplyOwnColor="true"> <!-- columnColor 000088 -->
<Attribute_String>TODO CellPattern ?</Attribute_String>
</GridGenerator>
<!-- L = LandCell c = CoastalCell -->
<GridCellPattern xpos="20" ypos="15"><![CDATA[
..CCCCCCCCCCCC
..CLLCLLLCCLLC
CCCCLLLLLLLLCCCC
CLLLLLLLLLLLLLLC
CLLLLLLLLLLLLLLC
CCCCLLLLLLLLLCCC
..CCCCCLLLLCCC
..CLL.LLLLLLC
.CCCLLLLLLLLC
.CLLLLLLLLLLCCC
.CCLLLLLLLLLLLCCCCCCCCC
..CCCLLLLLLLLLLLLLLLLLC
...CLLLLLLLLLLLLLLLLCCC
...CCCLLLLLLLLLLLLLCC
...CLLLLLLLLLCCCCCCC
..CCCCCLLLLCCC
..CLL.LLLLLLLC
CCCCLLLLLLLLCCCC
CLLLLLLLLLLLLLLC
CLLLLLLLLLLLLLLC
CCCCLLLLLLLLLCCC
...CCCCLLLLCCC
......CCCCCC
]]></GridCellPattern>
<GridCellPattern xpos="50" ypos="40"><![CDATA[
..........CCCCC.CCC..CCCCCCC
.........CCLCLCCCLCCCCLCCLLCC
........CCCLLLLLLLLLLLLLLLLLC
......CCCLLLLLLLLLLLLLLLLLCCC
......CLLLLLLLLLLLLLLLLLCCC
..CCCCCCLLLLLLLLLLLLLLLLLLCC
CCCLLLLLLLLLLLLLLLLLLLLLLLLCCC
CLLLLLLLLLLLLLLLLLLLLLLLLLLLLCCCCCCCC
CCLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLCCCC
.CCLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLC
..CCCLLLLLLLLLLLLLLLLLLLLLLLLLLLLLCCCCCC
...CLLLLLLLLLLLLLLLLLLLLLLLLLLLLLCCC
...CCCLLLLLLLLLLLLLLLLLLLLLLLLLLLLLCCCC
...CLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLCCCCC
...CCCCLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLCCCC
......CCCLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLC
.......CLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLCCC
.......CCCLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLCCC
.......CLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLCCCC
.......CCCCCCCLLLLLLLLLLLLLLLLLLLLLCCCC
............CLLLLLLLLLLLLLLLLLLLLLLLC
............CCCLLLCCCCCCCCCCCCLLLLCCC
..............CCCCC..........CCCCCC
]]></GridCellPattern>
<!-- each PlantBehavior and FishBehavior node moves itself into one of these containers at runtime -->
<PlantBehaviors/>
<FishBehaviors/>
<AnimalBehaviors/>
<!-- see Xml2Xholon - DefaultContent only works if I include RoomModel somewhere before I need to use DefaultContent -->
<RoomModel/>
<Ocean>
<Fish multiplicity="30"/>
<Island>
<Fruit multiplicity="10"/>
<Vegetables multiplicity="10"/>
<Lumber multiplicity="10"/>
<Cat roleName="Licorice"/>
<!--<BirchTree multiplicity="20"/>-->
<!-- TODO multiplicity causes DefaultContent content to be duplicated in each instance node -->
<BirchTree/>
<BirchTree/>
<BirchTree/>
<BirchTree/>
<BirchTree/>
<BirchTree/>
<BirchTree/>
<BirchTree/>
<BirchTree/>
<BirchTree/>
<BirchTree/>
<BirchTree/>
<BirchTree/>
<BirchTree/>
<BirchTree/>
<BirchTree/>
<BirchTree/>
<BirchTree/>
<BirchTree/>
<BirchTree/>
<!--<OakTree multiplicity="5"/>-->
<OakTree/>
<OakTree/>
<OakTree/>
<OakTree/>
<OakTree/>
<OakTree/>
<OakTree/>
<OakTree/>
<OakTree/>
<OakTree/>
<OakTree/>
<OakTree/>
<OakTree/>
<OakTree/>
<OakTree/>
<OakTree/>
<OakTree/>
<OakTree/>
<OakTree/>
<OakTree/>
<City roleName="Ottawa">
<University roleName="Carleton">
<Library>
<CoffeeShop roleName="Starbucks">
<IslandControlCentre/>
</CoffeeShop>
</Library>
</University>
</City>
</Island>
</Ocean>
<Weather state="sunny and windy"/>
<!-- Prevent children and siblings from running their act(), to prevent navigating through the large grid each timestep. -->
<ActRegulator val="1.0"/>
<!-- the Space grid is inserted here at runtime -->
</IslandSystem>
<IslandSystembehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
const ROWS = 80; // same as value in GridGenerator
const COLS = 120;
$wnd.xh.param("TimeStepInterval","100"); // "500" "100"
$wnd.xh.param("AppM","true");
// position and set commands for built-in Avatar
// "x" prevents exiting from a Cell node
$wnd.xh.avatarKeyMap('{"SHIFT":"true","UP":"go port0","DOWN":"go port2","LEFT":"prev","RIGHT":"next","a":"appear","D":"drop","d":"drop first","e":"eat","f":"follow prev","F":"unfollow","h":"if xpath(Avatar/Lumber) build Hut role Shack;if xpath(Avatar/Lumber) unbuild lumber;","i":"inventory","k":"enter *Tree;takeclone *Wood;exit;","l":"look","n":"enter","p":"pause","r":"start","s":"step"," ":"step","T":"take","t":"take prev","v":"vanish","w":"who;where;","x":"if xpath(../../FieldRow) wait else exit","z":"param transcript toggle;","<":"flip prev",">":"flip next",",":"flip prev",".":"flip next","0":"out transcript ZERO","1":"out transcript ONE","2":"out transcript TWO"}');
var ava = $wnd.xh.avatar();
ava.action('enter;enter space_;enter;enter;appear;param transcript true;');
ava.action('param meteor true;');
//ava.action('param meteormove true;');
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// specify Plant behavior
$wnd.xh.Plantbehavior = function Plantbehavior() {}
$wnd.xh.Plantbehavior.prototype.postConfigure = function() {
this.plant = this.cnode.parent();
this.plant.calories = 0;
this.plant.xpath("ancestor::IslandSystem/PlantBehaviors").append(this.cnode.remove());
};
$wnd.xh.Plantbehavior.prototype.act = function() {
//this.plant.println("I am " + this.plant.name() + " calories:" + this.plant.calories);
if (this.plant.parent() == null) {
// I've been eaten, so remove this behavior from the simulation
this.cnode.remove();
}
else if (this.plant.parent().xhc().name() == "Avatar") {
// I've been picked
this.plant.calories--;
if (this.plant.calories <= 0) {
// I've gone bad
this.plant.remove();
this.cnode.remove();
}
}
else if (this.plant.parent().xhc().name() == "Island") {
this.plant.calories++;
this.moveSelfToGrid();
}
else {
this.plant.calories++;
}
};
$wnd.xh.Plantbehavior.prototype.moveSelfToGrid = function() {
// DO NOT run this code in postConfigure(); it prevents a next Plant node from obtaining a Plantbehavior
// move this.plant to a random position within the land part of the grid
var ix = $wnd.Math.floor($wnd.Math.random() * landCellArr.length);
var fcell = landCellArr[ix];
fcell.append(this.plant.remove());
};
// specify Fruit behavior
$wnd.xh.Fruitbehavior = function Fruitbehavior() {};
$wnd.xh.Fruitbehavior.prototype = $wnd.Object.create($wnd.xh.Plantbehavior.prototype);
// specify Vegetables behavior
$wnd.xh.Vegetablesbehavior = function Vegetablesbehavior() {};
$wnd.xh.Vegetablesbehavior.prototype = $wnd.Object.create($wnd.xh.Plantbehavior.prototype);
// specify Lumber behavior
$wnd.xh.Lumberbehavior = function Lumberbehavior() {};
$wnd.xh.Lumberbehavior.prototype = $wnd.Object.create($wnd.xh.Plantbehavior.prototype);
// specify BirchTree behavior
$wnd.xh.BirchTreebehavior = function BirchTreebehavior() {};
$wnd.xh.BirchTreebehavior.prototype = $wnd.Object.create($wnd.xh.Plantbehavior.prototype);
// specify OakTree behavior
$wnd.xh.OakTreebehavior = function OakTreebehavior() {};
$wnd.xh.OakTreebehavior.prototype = $wnd.Object.create($wnd.xh.Plantbehavior.prototype);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// specify Fish Grow behavior
$wnd.xh.FishGrowbehavior = function FishGrowbehavior() {}
$wnd.xh.FishGrowbehavior.prototype.postConfigure = function() {
this.fish = this.cnode.parent();
this.fish.calories = 0;
this.fish.xpath("ancestor::IslandSystem/FishBehaviors").append(this.cnode.remove());
};
$wnd.xh.FishGrowbehavior.prototype.act = function() {
//this.fish.println("I am " + this.fish.name() + " calories:" + this.fish.calories);
if (this.fish.parent() == null) {
// I've been eaten, so remove this behavior from the simulation
this.cnode.remove();
}
else if (this.fish.parent().xhc().name() == "Avatar") {
// I've been caught
this.fish.calories--;
if (this.fish.calories <= 0) {
// I've gone bad
this.fish.remove();
this.cnode.remove();
}
}
else if (this.fish.parent().xhc().name() == "Ocean") {
this.fish.calories++;
//this.moveSelfToGrid();
}
else if (this.fish.parent().xhc().name() == "OceanCell") {
this.fish.calories++;
//this.moveSelfToGrid();
}
else {
// I've probably been caught and am now somewhere on land in a hut, or maybe in a boat
this.fish.calories--;
if (this.fish.calories <= 0) {
// I've gone bad :-(
this.fish.remove();
this.cnode.remove();
}
}
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// specify Fish Move behavior
$wnd.xh.FishMovebehavior = function FishMovebehavior() { // (destCellTypeNot) {
//$wnd.xh.FishMovebehavior.destCellTypeNot = destCellTypeNot;
}
$wnd.xh.FishMovebehavior.prototype.postConfigure = function() {
this.fish = this.cnode.parent();
this.fish.xpath("ancestor::IslandSystem/FishBehaviors").append(this.cnode.remove());
};
$wnd.xh.FishMovebehavior.prototype.act = function() {
//this.fish.println("I am " + this.fish.name() + " moving");
if (this.fish.parent() == null) {
// I've been eaten, so remove this behavior from the simulation
this.cnode.remove();
}
else if (this.fish.parent().xhc().name() == "Ocean") {
this.moveSelfToGrid();
}
else if (this.fish.parent().xhc().name().endsWith("Cell")) { // FieldCell OceanCell LandCell
// move randomly in the grid
this.move();
}
else if (this.fish.parent().xhc().name() == "Avatar") {
// I've been caught, so don't move
}
else {
$wnd.console.log("ERROR " + this.fish.parent().name());
}
};
$wnd.xh.FishMovebehavior.prototype.moveSelfToGrid = function() {
// DO NOT run this code in postConfigure(); it prevents a next Fish node from obtaining a Fishbehavior
// move this.fish to a random position within the ocean part of the grid
var ix = $wnd.Math.floor($wnd.Math.random() * oceanCellArr.length);
var fcell = oceanCellArr[ix];
fcell.append(this.fish.remove());
};
$wnd.xh.FishMovebehavior.prototype.move = function() {
var foundNewLocation = false;
var count = 0;
while ((!foundNewLocation) && (count < 1)) { // 10
var moveX = $wnd.Math.floor($wnd.Math.random() * 3) - 1;
var moveY = $wnd.Math.floor($wnd.Math.random() * 3) - 1;
if ((moveX == 0) && (moveY == 0)) {
return;
}
var portX = -1;
var portY = -1;
if (moveX > 0) {
portX = 1; //IGrid.P_EAST
}
else {
portX = 3; //IGrid.P_WEST
}
if (moveY > 0) {
portY = 0; //IGrid.P_NORTH
}
else {
portY = 2; //IGrid.P_SOUTH
}
count++;
var destination = this.fish.parent();
if (moveX != 0) {
destination = destination.port(portX);
}
if (moveY != 0) {
destination = destination.port(portY);
}
if (destination && (destination.xhc().name() != "LandCell")) { //$wnd.xh.FishMovebehavior.destCellTypeNot)) {
destination.append(this.fish.remove());
foundNewLocation = true;
}
}
};
// specify Cat behavior OLD
//$wnd.xh.CatMovebehavior = function CatMovebehavior() {};
//$wnd.xh.CatMovebehavior.prototype = $wnd.Object.create($wnd.xh.FishMovebehavior.prototype);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// specify Cat Move behavior
$wnd.xh.CatMovebehavior = function CatMovebehavior() {}
$wnd.xh.CatMovebehavior.prototype.postConfigure = function() {
this.cat = this.cnode.parent();
this.cat.xpath("ancestor::IslandSystem/AnimalBehaviors").append(this.cnode.remove());
};
$wnd.xh.CatMovebehavior.prototype.act = function() {
//this.cat.println("I am " + this.cat.name() + " moving");
if (this.cat.parent() == null) {
// I've been eaten, so remove this behavior from the simulation
this.cnode.remove();
}
else if (this.cat.parent().xhc().name() == "Island") {
this.moveSelfToGrid();
}
else if (this.cat.parent().xhc().name().endsWith("Cell")) { // FieldCell OceanCell LandCell
// move randomly in the grid
this.move();
}
else if (this.cat.parent().xhc().name() == "Avatar") {
// I've been caught, so don't move
}
else {
$wnd.console.log("ERROR " + this.cat.parent().name());
}
};
$wnd.xh.CatMovebehavior.prototype.moveSelfToGrid = function() {
// DO NOT run this code in postConfigure(); it prevents a next Cat node from obtaining a Catbehavior
// move this.cat to a random position within the ocean part of the grid
var ix = $wnd.Math.floor($wnd.Math.random() * landCellArr.length);
var fcell = landCellArr[ix];
fcell.append(this.cat.remove());
};
$wnd.xh.CatMovebehavior.prototype.move = function() {
var foundNewLocation = false;
var count = 0;
while ((!foundNewLocation) && (count < 1)) { // 10
var moveX = $wnd.Math.floor($wnd.Math.random() * 3) - 1;
var moveY = $wnd.Math.floor($wnd.Math.random() * 3) - 1;
if ((moveX == 0) && (moveY == 0)) {
return;
}
var portX = -1;
var portY = -1;
if (moveX > 0) {
portX = 1; //IGrid.P_EAST
}
else {
portX = 3; //IGrid.P_WEST
}
if (moveY > 0) {
portY = 0; //IGrid.P_NORTH
}
else {
portY = 2; //IGrid.P_SOUTH
}
count++;
var destination = this.cat.parent();
if (moveX != 0) {
destination = destination.port(portX);
}
if (moveY != 0) {
destination = destination.port(portY);
}
if (destination && (destination.xhc().name() != "OceanCell")) {
destination.append(this.cat.remove());
foundNewLocation = true;
}
}
};
var oceanCellArr = [];
var landCellArr = [];
var coastalCellArr = [];
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// specify behavior to add all Cell nodes into class-specific arrays, for use by moveSelfToGrid functions
// position system Avatar
$wnd.xh.CellArraybehavior = function CellArraybehavior() {}
$wnd.xh.CellArraybehavior.prototype.postConfigure = function() {
this.isys = this.cnode.parent();
var space = this.isys.xpath("Space");
var row = space.first();
while (row) {
var cell = row.first();
while (cell) {
switch (cell.xhc().name()) {
case "OceanCell":
oceanCellArr.push(cell);
break;
case "LandCell":
landCellArr.push(cell);
break;
case "CoastalCell":
coastalCellArr.push(cell);
break;
default: break;
}
cell = cell.next();
}
row = row.next();
}
//ava.action('go xpath()');
var ix = $wnd.Math.floor($wnd.Math.random() * coastalCellArr.length);
var fcell = coastalCellArr[ix];
fcell.append(ava.remove());
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// specify City behavior
$wnd.xh.Citybehavior = function Citybehavior() {}
$wnd.xh.Citybehavior.prototype.postConfigure = function() {
this.city = this.cnode.parent();
//this.plant.calories = 0;
//this.plant.xpath("ancestor::IslandSystem/PlantBehaviors").append(this.cnode.remove());
var ix = $wnd.Math.floor($wnd.Math.random() * coastalCellArr.length);
var ccell = coastalCellArr[ix];
ccell.append(this.city.remove());
this.cnode.remove();
};
//# sourceURL=IslandSystembehavior.js
]]></IslandSystembehavior>
<Fruitbehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
var beh = new $wnd.xh.Fruitbehavior();
//# sourceURL=Fruitbehavior.js
]]></Fruitbehavior>
<Vegetablesbehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
var beh = new $wnd.xh.Vegetablesbehavior();
//# sourceURL=Vegetablesbehavior.js
]]></Vegetablesbehavior>
<Lumberbehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
var beh = new $wnd.xh.Lumberbehavior();
//# sourceURL=Lumberbehavior.js
]]></Lumberbehavior>
<BirchTreebehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
var beh = new $wnd.xh.BirchTreebehavior();
//# sourceURL=BirchTreebehavior.js
]]></BirchTreebehavior>
<OakTreebehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
var beh = new $wnd.xh.OakTreebehavior();
//# sourceURL=OakTreebehavior.js
]]></OakTreebehavior>
<Fishbehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
var beh = new $wnd.xh.FishGrowbehavior();
//# sourceURL=FishGrowbehavior.js
]]></Fishbehavior>
<Fishbehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
var beh = new $wnd.xh.FishMovebehavior(); //("LandCell");
//# sourceURL=FishMovebehavior.js
]]></Fishbehavior>
<Catbehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
var beh = new $wnd.xh.CatMovebehavior();
//# sourceURL=CatMovebehavior.js
]]></Catbehavior>
<GridCellPatternbehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
var me, beh = {
postConfigure: function() {
me = this.cnode.parent();
this.cnode.remove();
//me.println("xpos:" + me.xpos + " ypos:" + me.ypos);
var gcpstr = me.text().trim();
var gcparr = gcpstr.split("\n");
var row = me.xpath("../Space/FieldRow[" + me.ypos + "]");
var fcol = row.xpath("OceanCell[" + me.xpos + "]");
var col = fcol;
for (var i = 0; i < gcparr.length; i++) {
var gcpline = gcparr[i].trim();
//me.println(gcpline);
for (var j = 0; j < gcpline.length; j++) {
switch (gcpline[j]) {
case "L":
col.xhc("LandCell");
break;
case "C":
col.xhc("CoastalCell");
break;
default: // "."
break;
}
col = col.next();
}
fcol = fcol.port(2);
col = fcol;
}
}
}
//# sourceURL=GridCellPatternbehavior.js
]]></GridCellPatternbehavior>
<IslandSystembehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
var beh = new $wnd.xh.CellArraybehavior();
//# sourceURL=IslandSystembehavior2.js
]]></IslandSystembehavior>
<Citybehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
var beh = new $wnd.xh.Citybehavior();
//# sourceURL=Citybehavior.js
]]></Citybehavior>
<SvgClient><Attribute_String roleName="svgUri"><![CDATA[data:image/svg+xml,
<svg width="140" height="10" xmlns="http://www.w3.org/2000/svg">
<g>
<title>Ocean</title>
<rect id="IslandSystem/Ocean" fill="#98FB98" height="10" width="50" x="0" y="0"/>
<g>
<title>Island</title>
<rect id="IslandSystem/Ocean/Island" fill="#6AB06A" height="10" width="10" x="55" y="0"/>
</g>
<g>
<title>PlantBehaviors</title>
<rect id="IslandSystem/PlantBehaviors" fill="orange" height="10" width="10" x="75" y="0"/>
</g>
<g>
<title>FishBehaviors</title>
<rect id="IslandSystem/FishBehaviors" fill="orange" height="10" width="10" x="95" y="0"/>
</g>
</g>
</svg>
]]></Attribute_String><Attribute_String roleName="setup">${MODELNAME_DEFAULT},${SVGURI_DEFAULT}</Attribute_String></SvgClient>
</XholonWorkbook>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment