Skip to content

Commit

Permalink
procDung.js is working, as are GenLists and Sets.
Browse files Browse the repository at this point in the history
  • Loading branch information
Havvy committed Dec 8, 2011
1 parent 7056258 commit 6b5da01
Show file tree
Hide file tree
Showing 15 changed files with 247 additions and 136 deletions.
120 changes: 76 additions & 44 deletions code/obj/Generator.js
Expand Up @@ -16,17 +16,19 @@ Exec('../code/obj/Graph.js');


var Generator = {
create : function create(base, generator) {
create : function create(base, generator, identifier) {
return {
base : function() {
return base;
},
generate : function (seed) {
Math.seedrandom(seed);
if (seed) {
Math.seedrandom(seed);
}
return generator.apply(this.base())
},
toString : function () {
return "[Generator generator]";
return "[Generator " + (identifier || "generator") + "]";
},
constructor : Generator
};
Expand All @@ -38,15 +40,16 @@ var Generator = {
* Assuming any string is character data
* Assuming any number means "Go this this line number where start gets line 0
*/
var addGeneratingGraph = function (graphtable, genmap) {
var addGeneratingGraph = function (graphtable, genmap, identifier) {
//printline();
//printval("TABLE", graphtable);
//printval("MAP", genmap, true);

let start = new Node({name:"Start"}), end = new Node({name:"End"});
let start = new Node({name:"Start", data : "S"}),
end = new Node({name:"End", data : "E"});

function createNodes(graphtable, genmap) {
//println("3a. Creating nodes from graphtable");
//println("Creating nodes from graphtable");
return graphtable.map(function (line) {
return line.map(function map2(node) {

Expand Down Expand Up @@ -84,24 +87,20 @@ var addGeneratingGraph = function (graphtable, genmap) {
//println("\t\tNode is not a string.");
}

//printval("\tNODE", node);
//println("\t\tNode is another generator of some type.");
//println("\t\tTherefore, node is another generator of some type.");
// Otherwise, is a generator of its own.
if (!genmap.hasOwnProperty(node)) {
//printval("\t\tgenmap", genmap, true);
//printval("\t\tnode", node);
//printval("genmap", genmap, true);
//printval("node", node);
genmap[node] = createGeneratorFromFile(node, genmap);
//printval("\t\tGENERATOR END", genmap[node]);
} else {
//printval("\t\tgenmap", genmap, true);
//printval("\t\tnode", node);
}
return genmap[node];
})
});
}

function createGraph(nodes) {
function createGraph(nodes, identifier) {
//printval("Made it here with", identifier);
var graph = [start];

for (let lx = 1; lx < nodes.length; lx++) {
Expand All @@ -112,29 +111,28 @@ var addGeneratingGraph = function (graphtable, genmap) {
}

graph.push(end);
//printval("4a. Graph", graph);
//printval("Graph", graph);

for (let ix = 0; ix < nodes.length; ix++) {
let node = graph[ix];
//printval("4b. line", nodes[ix]);
//printval("line", nodes[ix]);
//printval("\tnode", node);
for (let jx = 1; jx < nodes[ix].length; jx++) {
let nextVal = nodes[ix][jx]

//printval("\tnextVal", nextVal);
if (typeof nextVal=== "number") {
//println("\tValue is a number.");
node.addAcyclicEdge(graph[nextVal]);
//println("\tAdding edge between " + node + " & " + graph[nextVal]);
graph[ix].addAcyclicEdge(graph[nextVal]);
} else if (nextVal === end) {
//println("\tValue is the end.");
//printval("\tnode", node);
node.addAcyclicEdge(end);
//println("\tAdding edge between " + node + " & " + end);
} else {
let newNode = new Node({data : nextVal})
//printval("\tnewNode", newNode);
graph.push(newNode);
node.addAcyclicEdge(newNode);
//println("\tAdding edge between " + node + " & " + newNode);
node = newNode;
}
}
Expand All @@ -146,53 +144,69 @@ var addGeneratingGraph = function (graphtable, genmap) {

let nodes = createNodes(graphtable, genmap);
//printline();
//printval("3. nodes", nodes);
//printval("NODES TABLE", nodes);
let graph = createGraph(nodes);
//printval("4. final graph", graph);
//printval("GRAPH FOR", identifier.toUpperCase());
//println(graph);
let generator = Generator.create(graph, function () {
var theGeneration = "";
let currentNode = start;
while (currentNode != end){
//printval("Current", currentNode);
//printval("\tValue", currentNode.data);
let adjacents = currentNode.adjacents();
// Adjacents := the list of nodes currentNode has an edge to.
//printval("\tadjacents", adjacents);
if (currentNode != start){
if (currentNode.data.constructor === Generator) {
theGeneration += "" + currentNode.data.generate();
let g = currentNode.data.generate();
//printval("\tg", g, true);
if (typeof g === "object") {
throw new TypeError("Return type of generator should be a String. Got Object instead.");
}
theGeneration += "" + g;
} else {
theGeneration += "" + currentNode.data;
}
}
currentNode = adjacents[Math.floor(Math.random() * adjacents.length)];


}
//printval("generation", theGeneration);
//printline();
return theGeneration;
});
},
identifier);
//printline();
return generator;
};

var addGeneratingList = function (graphtable, genmap) {
printval("graphtable", graphtable);
};

var addGeneratingSet = function (graphtable, genmap) {
//We convert the set to a graphtable and then return the
//graphtable generator. This is a quick hack.
/*
* These two methods are quick hacks, and are inefficient.
* They exist only to get the job done as quickly as possible.
*/
var addGeneratingList = function (graphtable, genlist, identifier) {
graphtable[0][0] = "GENGRAPH";

//printval("graphtable", graphtable);
for (let ix = 1; ix < graphtable.length; ix++) {
graphtable[0][ix] = ix.toString();
graphtable[ix].append("E");
};

return addGeneratingGraph(graphtable, genlist, identifier);
};

var addGeneratingSet = function (graphtable, genlist, identifier) {
graphtable[0][0] = "GENGRAPH";

for (let ix = 1; ix < graphtable.length; ix++) {
graphtable[0][ix] = ix.toString();
graphtable[ix][0] = '"' + graphtable[ix][0] + '"';
graphtable[ix][1] = 'E';
graphtable[0][ix] = ix.toString();
}

//printval("graphtable modified", graphtable);

return addGeneratingGraph(graphtable, genmap);
return addGeneratingGraph(graphtable, genlist, identifier);
};

/**
Expand All @@ -207,13 +221,31 @@ var createGeneratorFromFile = function (filename, genmap) {
"GENLIST" : addGeneratingList,
"SET" : addGeneratingSet
};
printval("filename", filename);
let file = get("gen/" + filename + ".gen"); //show Bob this fun bug. :P
//printval("filename", filename);
let file = get("gen/" + filename + ".gen");

if (file.length === 0) {
throw new ReferenceError("File not found.");
}

//printline();
//printval("file", file);
//printline();
let graphtable = file.splitMultiple('\n', '|');
//printval("1. graphtable", graphtable);
//printval("graphtable", graphtable);
//printval("genmap", genmap, true);
//printval("2. graphtable[0][0]", graphtable[0][0]);
let generator = fns[graphtable[0][0]](graphtable, genmap);
genmap[filename] = generator;
return generator;
//printval("graphtable[0][0]", graphtable);
try {
let generator = fns[graphtable[0][0]](graphtable, genmap, filename);
genmap[filename] = generator;
return generator;
} catch (e) {
if (e.name === "TypeError") {
println("Tried to call fns[" + graphtable[0][0] + "]() and failed.");
printval("Filename", filename);
throw new Error();
} else {
return null;
}
}
};
4 changes: 2 additions & 2 deletions code/obj/Graph.js
Expand Up @@ -126,11 +126,11 @@ var Graph = {

toString : function () {
var ret = "";
this.forEach(function (node, ix, graph) {
this.forEach(function (node, ix, graph) {
ret += node.name() + ": " + node.data + "\n";

node.edgelist.forEach(function (edge, jx, adjacents) {
ret += '\t' + node.name() + "-" + (edge.weight || "") + "->" + edge.to.name() + "\n";
ret += "\t" + node.name() + "-" + (edge.weight || "") + "->" + edge.to.name() + "\n";
if (edge.weight) {
for (let ix = 0; ix < edge.weight.length; ix++) {
ret += (typeof edge.weight[ix]) + " ";
Expand Down
5 changes: 2 additions & 3 deletions code/obj/Node.js
Expand Up @@ -18,8 +18,7 @@ extend(Edge.prototype, {
});

var Node = function (parameters) {

var counter = counter || 0;
Node.counter = Node.counter || 0;

this.edgelist = [];
if (parameters.adjacent && parameters.adjacent.length) {
Expand All @@ -29,7 +28,7 @@ var Node = function (parameters) {
}

this.data = parameters.data || {};
this.identifier = this.data.name || parameters.name || ("anon" + counter++);
this.identifier = this.data.name || parameters.name || ("anon" + Node.counter++);
};

extend(Node.prototype, {
Expand Down
18 changes: 10 additions & 8 deletions code/test/GeneratorTest.js
Expand Up @@ -44,6 +44,7 @@ let(gen = new TestSet(['obj/Generator.js'])) {
gen.addTest("Generate complex GenGraph. 3", function () {
let g = {};
createGeneratorFromFile('complexTest', g);

let generated = g['complexTest'].generate("Bob");

return (generated === "chickenaStrbStr");
Expand All @@ -52,33 +53,34 @@ let(gen = new TestSet(['obj/Generator.js'])) {
gen.addTest("Generate complex GenGraph 2.", function () {
let g = {};
createGeneratorFromFile('complexTest', g);

let generated = g['complexTest'].generate(NOESCAPE);

return (generated === "aStrbStr");
});

gen.addTest("Generate chicken GenGraph 3.", function () {

let g = {};
createGeneratorFromFile('chickenTest', g);

let generated = g['chickenTest'].generate("Bob");

return (generated === "chickenaStrbStr");
});

gen.addTest("Generate the set setTest.gen", function () {
gen.addTest("setTest.gen", function () {
let g = {};
createGeneratorFromFile('setTest', g);
let item = g['setTest'].generate("Bob");

return (item === "c");
let c = g['setTest'].generate("Bob");
return c = "c";
});

gen.addTest("Generate the list listTest.gen", function () {
gen.addTest("listTest.gen", function () {
let g = {};
createGeneratorFromFile('listTest', g);
let item = g['listTest'].generate("Bob");

return (item === "c");
let generated = g['listTest'].generate("_");
return generated === "aStrbStrcStr";
});

//gen.run();
Expand Down
41 changes: 37 additions & 4 deletions code/wip/procDung.js
Expand Up @@ -5,7 +5,40 @@ Exec('../code/obj/Node.js');
Exec('../code/obj/Graph.js');
Exec('../code/obj/Generator.js');

Math.seedrandom('test');
dungeon = createGeneratorFromFile("nonUnique", {});

println(dungeon.generate());
let (gens = {}) {
let seed = Math.random().toString();
let flag = true;
let roomsTraveled = 1;
Math.seedrandom(seed);
createGeneratorFromFile("creature", gens);
createGeneratorFromFile("nonUnique", gens);

println("You wake up in a dark cave. In front of you is a door.");

while (flag !== false) {
println();
println("Dare ye venture into yonder room?");
print(">");
let input = readln();

switch (input.toLowerCase()) {
case "seed":
printval("seed", seed);
break;
case "yes":
case "y":
println();
println(gens["nonUnique"].generate());
roomsTraveled++;
break;
case "n":
case "no":
println();
println("You have given up.");
println("You made it through " + roomsTraveled + " rooms.");
println("You are a " + gens["creature"].generate() + ".");
flag = false;
break;
}
}
}

0 comments on commit 6b5da01

Please sign in to comment.