This repository has been archived by the owner on Sep 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
124 lines (106 loc) · 2.29 KB
/
utils.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
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
const directions = {
0: "north",
2: "east",
4: "south",
6: "west",
};
const relativeDirections = {
0: "up",
2: "right",
4: "down",
6: "left",
};
const combinatorToNormal = {
undefined: "empty",
"+": "plus",
"-": "minus",
"*": "multiply",
"/": "divide",
"%": "modulo",
"^": "power",
"<<": "left_shift",
">>": "right_shift",
"&": "and",
"and": "and",
"AND": "and",
"|": "or",
"or": "or",
"OR": "or",
"xor": "xor",
"XOR": "xor",
">": "gt",
"<": "lt",
"=": "eq",
"!=": "neq",
"≠": "neq",
">=": "gte",
"≥": "gte",
"<=": "lte",
"≤": "lte"
};
function entitiesToGrid (entities) {
const grid = {};
for (let i in entities) {
const entity = entities[i];
if (grid[entity.position.x] === undefined) {
grid[entity.position.x] = {}
}
grid[entity.position.x][entity.position.y] = entity;
}
return grid;
}
class EntityGridView {
constructor (grid, centerX, centerY) {
this.grid = grid;
this.centerX = centerX;
this.centerY = centerY;
}
getRelative (relativeX, relativeY) {
const x = this.centerX + relativeX;
const y = this.centerY + relativeY;
if (this.grid[x] === undefined) {
return undefined;
}
return this.grid[x][y];
}
setCenter(centerX, centerY){
this.centerX = centerX;
this.centerY = centerY;
}
}
function isEntity (entity, target) {
if (entity === undefined) {
return 0;
}
if (entity.name === target) {
return 1;
}
return 0;
}
function isEntityInDirection (entity, target, direction) {
if (isEntity(entity, target)) {
if ((entity.direction || 0) === direction) {
return 1;
}
}
return 0;
}
function recipeHasFluids (recipe) {
const ingredients = recipe.ingredients || recipe.normal.ingredients;
for (let i = 0; i < ingredients.length; i++) {
if (ingredients[i].type === "fluid") {
return true;
}
}
return false;
}
module.exports = {
directions,
relativeDirections,
combinatorToNormal,
entitiesToGrid,
EntityGridView,
isEntity,
isEntityInDirection,
recipeHasFluids
};