-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgame.js
More file actions
183 lines (155 loc) · 6.27 KB
/
game.js
File metadata and controls
183 lines (155 loc) · 6.27 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
var world = require('./world')
var genWorld = require('./genWorld')
var GameUtil = require('./gameUtil')
var Entity = require('./entity')
var game = {}
module.exports = {
tick: tick,
action: action,
game : game
}
// init world
game.world = genWorld(100, 100)
world.set(game.world)
function tick (done) {
// var startTimer = (new Date).getTime()
game.world = world.clone()
game.tick()
// done doing things
world.set(game.world)
done()
// console.log((new Date).getTime() - startTimer + ' ms')
}
function action (action) {
//to-do
}
function cacheNeighbors(){
var neighborCache = GameUtil.create2dArray(20,20);
GameUtil.forEach2d(game.world, function(tile, x , y){
neighborCache[y][x] = GameUtil.getNeighbors(game.world,x,y);
if(tile.id === 0){
neighborCache[y][x].forEach(function(n){
console.log(n.id);
})
}
});
return neighborCache;
}
game.neighbors = cacheNeighbors();
function handleAi(tile){
if(!tile) return;
}
game.tick = function() {
for(var y = 0; y < game.world.length; y++) {
for(var x = 0; x < game.world[y].length; x++) {
var tile = game.world[y][x];
tile.livingNeighbors = 0
tile.habitatNeighbors = 0
game.neighbors[y][x].forEach(function(t){
if(t.entity) {
tile.livingNeighbors++
}
if(t.vegetation.density > 0) {
tile.habitatNeighbors++
}
});
//update tile
if(tile.entity) {
tile.entity.age++
tile.domesticity++
tile.entity.hunger -= tile.entity.age
} else {
tile.domesticity = Math.max(tile.domesticity - 1,0)
}
//update entity
if(tile.entity && tile.entity.hunger < 1) {
tile.entity = false
} else if(tile.entity && game.getHabitatDensity(y,x) < 1) {
tile.entity = false
} else if(tile.entity && tile.livingNeighobrs > 3) {
tile.entity = false
} else if(tile.entity) {
if(Math.pow(100 - tile.entity.hunger, 2) / 100 >= Math.pow(Math.floor(Math.random() * 100) + 1,2) / 100) {
var volume = Math.min(Math.floor((Math.random() * (100 - tile.entity.hunger))) + 1,tile.vegetation.density)
tile.entity.hunger += volume
tile.vegetation.density -= volume
} else if(tile.vegetation.density < game.getHabitatDensity(y,x)) {
var bestNeighbor = game.neighbors[y][x].reduce(function(p,c,i,arr) {
return p.vegetation.density > c.vegetation.density || c.entity ? p : c
},tile)
if(bestNeighbor != tile) {
tile.entity.hunger -= Math.floor(Math.random() * 10) + 1
bestNeighbor.entity = tile.entity
tile.entity = false
}
} else if(tile.liveNeighbors >= 1 && Math.floor(Math.random() * tile.entity.hunger) > 100 - tile.entity.hunger) {
var bestNeighbor = game.neighbors[y][x].reduce(function(p,c,i,arr) {
return p.vegetation.density > c.vegetation.density || c.entity ? p : c
},tile)
if(bestNeighbor != tile) {
bestNeighbor.entity = new Entity()
bestNeighbor.entity.hunger = Math.floor(Math.random() * tile.entity.hunger)
tile.entity.hunger -= 50
}
}
}
//update vegetation
if(tile.domesticity > 0) {
tile.vegetation.density--
// tile.vegetation.density -= Math.floor(Math.random() * tile.domesticity) + 1
} else {
tile.vegetation.density++
}
//update events
//give tile.vegetation.density some variation
tile.vegetation.density = Math.floor((Math.random() * 10)) + tile.vegetation.density - 5
tile.vegetation.density = tile.vegetation.density > 100 ? 100 : tile.vegetation.density
tile.vegetation.density = tile.vegetation.density < 0 ? 0 : tile.vegetation.density
//give tile.entity.hunger some variation
if(tile.entity) {
tile.entity.hunger = Math.floor((Math.random() * 10)) + tile.entity.hunger - 5
tile.entity.hunger = Math.min(100,tile.entity.hunger)
tile.entity.hunger = Math.max(0,tile.entity.hunger)
}
}
}
}
game.addEntity = function(x,y){
game.world[y][x].entity = new Entity()
game.world[y][x].hunger = Math.floor(Math.random() * 100) + 1
}
game.countLivingNeighbors = function(y,x) {
var livingNeighbors = 0
for(var yIter = y - 1; yIter <= y + 1; yIter++) {
for(var xIter = x - 1; xIter <= x + 1; xIter++) {
if((xIter != x || yIter != y) && xIter >= 0 && yIter >= 0 && game.world[yIter] && game.world[yIter][xIter] && game.world[yIter][xIter].entity.alive) {
livingNeighbors++
}
}
}
return livingNeighbors
}
game.countHabitatNeighbors = function(y,x) {
var habitatNeighbors = 0
for(var yIter = y - 1; yIter <= y + 1; yIter++) {
for(var xIter = x - 1; xIter <= x + 1; xIter++) {
if((xIter != x || yIter != y) && xIter >= 0 && yIter >= 0 && game.world[yIter] && game.world[yIter][xIter] && game.world[yIter][xIter].vegetation.density > 0) {
habitatNeighbors++
}
}
}
return habitatNeighbors
}
game.getHabitatDensity = function(y,x) {
var habitatDensity = 0
var neighbors = 0
for(var yIter = y - 1; yIter <= y + 1; yIter++) {
for(var xIter = x - 1; xIter <= x + 1; xIter++) {
if(xIter >= 0 && yIter >= 0 && game.world[yIter] && game.world[yIter][xIter]) {
habitatDensity += game.world[yIter][xIter].vegetation.density
neighbors++
}
}
}
return Math.round(habitatDensity / neighbors)
}