浏览代码

recursive-desc refactor 3

Implemented a function to handle repeated description logic, added duplex text option to house
master
jsb5468 5 年前
父节点
当前提交
b449bf797d
共有 1 个文件被更改,包括 45 次插入30 次删除
  1. +45
    -30
      recursive-macro.js

+ 45
- 30
recursive-macro.js 查看文件

@@ -19,6 +19,7 @@ var things =
clusters: 5, clusters: 5,
cluster_chances: .8, cluster_chances: .8,
contents: [], contents: [],
descriptor: ["a person", "people"]
}, },
"Human": { "Human": {
"Human": Human, "Human": Human,
@@ -27,6 +28,7 @@ var things =
clusters: 5, clusters: 5,
cluster_chances: .8, cluster_chances: .8,
contents: [], contents: [],
descriptor: ["a person", "people"]
}, },
"Cow": { "Cow": {
"Cow": Cow, "Cow": Cow,
@@ -35,6 +37,7 @@ var things =
clusters: 15, clusters: 15,
cluster_chances: .5, cluster_chances: .5,
contents: [], contents: [],
descriptor: ["a cow", "cattle"]
}, },
"Micro": { "Micro": {
"Micro": Micro, "Micro": Micro,
@@ -43,6 +46,7 @@ var things =
clusters: 50, clusters: 50,
cluster_chances: 1, cluster_chances: 1,
contents: [[]], contents: [[]],
descriptor: ["a micro", "micros"]
}, },
"Macro": { "Macro": {
"Macro": Macro, "Macro": Macro,
@@ -51,6 +55,7 @@ var things =
clusters: 0, clusters: 0,
cluster_chances: 0, cluster_chances: 0,
contents: [[]], contents: [[]],
descriptor: ["a smaller macro", "smaller macros"]
}, },
//Vehicles //Vehicles
"Empty Car": { "Empty Car": {
@@ -60,6 +65,7 @@ var things =
clusters: 2, clusters: 2,
cluster_chances: .3, cluster_chances: .3,
contents: [[]], contents: [[]],
descriptor: ["a parked car", "parked cars"]
}, },
"Car": { "Car": {
"Car": Car, "Car": Car,
@@ -68,6 +74,7 @@ var things =
clusters: 4, clusters: 4,
cluster_chances: .5, cluster_chances: .5,
contents: [["Person",1,4]], contents: [["Person",1,4]],
descriptor: ["a car", "cars"]
}, },
"Bus": { "Bus": {
"Bus": Bus, "Bus": Bus,
@@ -76,6 +83,7 @@ var things =
clusters: 1, clusters: 1,
cluster_chances: .25, cluster_chances: .25,
contents: [["Person",2,30]], contents: [["Person",2,30]],
descriptor: ["a bus", "busses"]
}, },
"Tram": { "Tram": {
"Tram": Tram, "Tram": Tram,
@@ -84,6 +92,7 @@ var things =
clusters: 1, clusters: 1,
cluster_chances: .2, cluster_chances: .2,
contents: [["Person",10,50]], contents: [["Person",10,50]],
descriptor: ["a tram", "trams"]
}, },
"Train": { "Train": {
"Train": Train, "Train": Train,
@@ -109,6 +118,7 @@ var things =
clusters: 5, clusters: 5,
cluster_chances: .5, cluster_chances: .5,
contents: [["Person",0,8],["Empty Car",0,2]], contents: [["Person",0,8],["Empty Car",0,2]],
descriptor: ["house", "houses"]
}, },
"Business": { "Business": {
"Business": Business, "Business": Business,
@@ -117,6 +127,7 @@ var things =
clusters: 5, clusters: 5,
cluster_chances: .25, cluster_chances: .25,
contents: [["Person",0,30],["Car",0,5],["Empty Car",0,20]], contents: [["Person",0,30],["Car",0,5],["Empty Car",0,20]],
descriptor: ["a local business", "buildings"]
}, },
"Barn": { "Barn": {
"Barn": Barn, "Barn": Barn,
@@ -125,6 +136,7 @@ var things =
clusters: 1, clusters: 1,
cluster_chances: .1, cluster_chances: .1,
contents: [["Person",0,2],["Cow",30,70]], contents: [["Person",0,2],["Cow",30,70]],
descriptor: ["a barn", "barns"]
}, },
"Small Skyscraper": { "Small Skyscraper": {
"Small Skyscraper": SmallSkyscraper, "Small Skyscraper": SmallSkyscraper,
@@ -133,6 +145,7 @@ var things =
clusters: 2, clusters: 2,
cluster_chances: .25, cluster_chances: .25,
contents: [["Person",150,750],["Empty Car",10,50]], contents: [["Person",150,750],["Empty Car",10,50]],
descriptor: ["a small skyscraper", "small skyscrapers"]
}, },
"Large Skyscraper": { "Large Skyscraper": {
"Large Skyscraper": LargeSkyscraper, "Large Skyscraper": LargeSkyscraper,
@@ -141,6 +154,7 @@ var things =
clusters: 1, clusters: 1,
cluster_chances: .25, cluster_chances: .25,
contents: [["Person",500,1500],["Empty Car",20,100]], contents: [["Person",500,1500],["Empty Car",20,100]],
descriptor: ["a large skyscraper", "large skyscrapers"]
}, },
"Parking Garage": { "Parking Garage": {
"Parking Garage": ParkingGarage, "Parking Garage": ParkingGarage,
@@ -745,6 +759,31 @@ function copy_defaults(self,proto) { //loads the values defined in things into t
} }
} }


function defaultDescribe(verbose=true, parent){
if (verbose) {
if (parent.count <= 3) {
var list = [];
for (var i = 0; i < parent.count; i++) {
list.push(parent.describeOne(parent.count <= 2));
}
if (parent.contents == []){
return merge_things(list);
} else {
return merge_things(list) + " with " + describe_all(parent.contents,verbose) + " inside";
}
} else {
if (parent.contents == []){
return parent.count + " " + things[parent.name].descriptor[1];
} else {
return parent.count + " homes with " + describe_all(parent.contents,verbose) + " inside";
}
}
} else {//not verbose
return (parent.count > 1 ? parent.count + " " + things[parent.name].descriptor[1] : things[parent.name].descriptor[0]);
}
}


function Container(contents = []) { function Container(contents = []) {
this.name = "Container"; this.name = "Container";


@@ -792,26 +831,14 @@ function Person(count = 1) {
}; };


this.describe = function(verbose=true) { this.describe = function(verbose=true) {
if (verbose) {
if (count <= 3) {
var list = [];
for (var i = 0; i < count; i++) {
list.push(this.describeOne(this.count <= 2));
}
return merge_things(list);
} else {
return this.count + " people";
}
} else {
return (this.count > 1 ? this.count + " people" : "a person");
}
};
return defaultDescribe(verbose, this);
}


return this; return this;
} }


function Human(count = 1) { function Human(count = 1) {
this.name = "Person";
this.name = "Human";


copy_defaults(this,new DefaultEntity()); copy_defaults(this,new DefaultEntity());


@@ -826,20 +853,8 @@ function Human(count = 1) {
}; };


this.describe = function(verbose=true) { this.describe = function(verbose=true) {
if (verbose) {
if (count <= 3) {
var list = [];
for (var i = 0; i < count; i++) {
list.push(this.describeOne(this.count <= 2));
}
return merge_things(list);
} else {
return this.count + " people";
}
} else {
return (this.count > 1 ? this.count + " people" : "a person");
}
};
return defaultDescribe(verbose, this);
}


return this; return this;
} }
@@ -1077,7 +1092,7 @@ function House(count = 1) {
this.describeOne = function(verbose=true) { this.describeOne = function(verbose=true) {
var size = random_desc(["little","two-story","large","well-built","run-down","cheap",], (verbose ? 0.5 : 0)); var size = random_desc(["little","two-story","large","well-built","run-down","cheap",], (verbose ? 0.5 : 0));
var color = random_desc(["blue","white","gray","tan","green","wooden","brick"], (verbose ? 0.5 : 0)); var color = random_desc(["blue","white","gray","tan","green","wooden","brick"], (verbose ? 0.5 : 0));
var name = random_desc(["house","home","house","house","house","trailer"], 1);
var name = random_desc(["house","home","duplex","house","house","trailer"], 1);
return merge_desc([size,color,name]); return merge_desc([size,color,name]);
}; };




正在加载...
取消
保存