forked from santanche/mlca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlca_Rule.js
65 lines (57 loc) · 1.47 KB
/
mlca_Rule.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Rule: state transition instructions
-layerID: holds the identification of the Layer this rule modifies
-conditions: the conditions to be checked
-targetState: the state the current cell becomes if the checks succeed
-layerRef: pointer to layer it modifies
-probability: if the conditions are meet, runs a probability from 0 to 1 for the rule to be applied
if none is given, probability is set to 1.
specs = {
layerID,
conditions,
probability,
targetState
}
+apply(coordinate): receives a coordinate and check if the conditions of the rule
are all successful, if so, writes the targetState into the layer
*/
mlca.Rule = function(specs){
'use strict';
this.layerID = specs.layerID;
this.conditions = specs.conditions;
this.targetState = specs.targetState;
this.layerRef = mlca.layerList.getLayerByID(this.layerID);
if(specs.probability !== undefined){
this.probability = specs.probability;
}
else{
this.probability = 1;
}
};
mlca.Rule.prototype = {
apply : function (coords){
'use strict';
var i = 0, ret = true;
for (i = 0; i<this.conditions.length; i+=1){
if (!(this.conditions[i].check(coords))){
ret = false;
break;
}
}
if (ret){
if (this.layerRef === undefined){
this.layerRef = mlca.layerList.getLayerByID(this.layerID);
}
if(Math.random() < this.probability){
this.layerRef.write(coords,this.targetState);
return true;
}
else{
return false;
}
}
else{
return false;
}
}
};