-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplax.js
323 lines (287 loc) · 10.6 KB
/
plax.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/**
* @file plax.js
* @copyright Copyright (c) 2022 MapMaths
* @license MIT
* @author MapMaths
* @repo https://github.com/MapMaths/plax
* @description Edit PLA JSON files.
*/
const fs = require("fs"); // Import File System.
const base = require("./tool/base.js");
const ElementClass = require("./element.js").Element;
const consts = {
DEFAULT_PATH_WINDOWS: process.env.USERPROFILE + "\\AppData\\LocalLow\\CIVITAS\\Quantum Physics\\Circuit",
wire: {
black: "黑色导线",
blue: "蓝色导线",
red: "红色导线",
green: "绿色导线",
yellow: "黄色导线"
}
}
class Editor {
constructor(filename, syntax="utf8") {
this.filename = filename;
this.JSON_ORIGIN = JSON.parse(fs.readFileSync(filename, syntax));
this.SIMPLIFIED = this.JSON_ORIGIN.Experiment ? false : true;
this.json = this.SIMPLIFIED ? this.JSON_ORIGIN : this.JSON_ORIGIN.Experiment;
}
/**
* @method Write a PLC local file from a variable.
* @param {string} filename The full absolute directory of the file.
*/
write(filename=this.filename) {
if (this.SIMPLIFIED) this.JSON_ORIGIN = this.json;
else this.JSON_ORIGIN.Experiment = this.json;
fs.writeFileSync(filename, JSON.stringify(this.JSON_ORIGIN, null, 2));
}
/**
* @method Modify camera settings.
* @param {int} mode 0 = Electricity, 1 = Graphical electricity, 3 = Universe, 4 = Electromagnetic field.
* @param {digital} dist The distance of the camera to the sight center.
* @param {[float, float, float]} pos The position of the camera, at an index of [x, y, z].
* @param {[float, float, float]} dir The direction of the camera, at an index of [xy, xz, yz].
*/
setCamera(mode=null, dist=null, pos=null, dir=null) {
let camera = this.status;
if (mode != null) camera.Mode = mode;
if (dist != null) camera.Distance = dist;
if (pos != null) camera.VisionCenter = `${pos[0]},${pos[2]},${pos[1]}`;
if (dir != null) camera.TargetRotation = `${dir[0]},${dir[2]},${dir[1]}`;
this.json.CameraSave = JSON.stringify(camera);
return this.json;
}
/*
setAttrToAll(attr, val) {
}
*/
/**
* @method Copy all elements and move them by a specific steps.
* @warning This function might be deprecated anytime since the developer of PLC is adding this function to the software program.
* @param {[float, float, float]} steps The steps with an index of [x, y, z].
*/
copyAllElementsAndMove(steps) {
let status = this.status;
let copy = status.Elements;
for (let i = 0; i < copy.length; i++) {
let pos = copy[i].Position.split(",").map(Number);
copy[i].Identifier = base.generateNewElementID(this.json);
copy[i].Position = `${pos[0] + steps[0]},${pos[1] + steps[2]},${pos[2] + steps[1]}`;
}
copy = this.status.Elements.concat(copy); // It seems two vars `copy` & `status.Elements` are bound together, but not completely
status.Elements = copy;
this.json.StatusSave = JSON.stringify(status);
this.json.Components += copy.length;
return this.json;
}
wire(SourceElement, sourcePin, TargetElement, targetPin, color='蓝色导线') {
let status = this.status;
status.Wires.push({
Source: SourceElement.id,
SourcePin: sourcePin,
Target: TargetElement.id,
TargetPin: targetPin,
ColorName: color
});
this.json.StatusSave = JSON.stringify(status);
return status.Wires;
}
/**
* @method Copy all elements and wires and move them by a specific steps.
* @warning This function might be deprecated anytime since the developer of PLC is adding this function to the software program.
* @param {[float, float, float]} steps The steps with an index of [x, y, z].
*/
copyAllAndMove(steps) {
this.copyAllElementsAndMove(steps);
let status = this.status;
let l = status.Elements.length/2;
for (let i = 0; i < status.Wires.length; i++) {
let wire = status.Wires[i],
es = new ElementClass(this.json, wire.Source), // Find the original elements
et = new ElementClass(this.json, wire.Target), // Via ID
nes = new ElementClass(this.json, null, null, es.index + l), // Find the moved elements
net = new ElementClass(this.json, null, null, et.index + l); // Via index
this.wire(nes, wire.SourcePin, net, wire.TargetPin, wire.ColorName);
}
return this.json;
}
/*
fillWires() {
}
*/
/**
* @method Insert an element into JSON.
* @param {Element} Element The element that is to be inserted.
* @param {int} n The index.
* @param {last} last The index in reversed order. (Default: last one)
*/
insert(Element, n=null, last=1) {
let status = this.status;
status.Elements.splice(n == null ? status.Elements.length-last+1 : n-1, 0, Element.json);
this.json.StatusSave = JSON.stringify(status);
return this.json;
}
/**
* @method Replace an element in JSON with another
* @param {Element} Element The element that is to replace the one in JSON.
* @param {string} id The ID of the element which is to be replaced. (Default: The one which share the same ID with the Element)
* @param {int} n The index.
* @param {last} last The index in reversed order.
*/
replace(Element, id=Element.id, n=null, last=null) {
let status = this.status;
if (n==null && last==null) {
let x = status.Elements.findIndex(x => x.Identifier == id);
x == -1 && base.elementError();
status.Elements.splice(x, 1, Element.json);
} else {
let x = status.Elements.splice(n == null ? -last : n-1, 1, Element.json);
x[0] == undefined && base.elementError();
}
this.json.StatusSave = JSON.stringify(status);
return this.json;
}
/**
* @method Replace all the elements with an ElementSet
* @param {ElementSet} ElementSet The ElementSet that is to replace the one in JSON.
*/
change(ElementSet) {
let status = this.status;
status.Elements = [];
status.Wires = [];
for(let i = 0; i < ElementSet.card; i++) {
status.Elements.push(ElementSet.json(i));
}
for(let i = 0; i < ElementSet.wires.length; i++) {
status.Wires.push(ElementSet.wires[i]);
}
this.json.StatusSave = JSON.stringify(status);
return this.json;
}
/**
* @method Push all the elements in an ElementSet into JSON. (Run `ElementSet.refreshID(Editor)` first)
* @param {ElementSet} ElementSet The ElementSet that is to be appended.
*/
append(ElementSet) {
let status = this.status;
for(let i = 0; i < ElementSet.card; i++) {
status.Elements.push(ElementSet.json(i));
}
for(let i = 0; i < ElementSet.wires.length; i++) {
status.Wires.push(ElementSet.wires[i]);
}
this.json.StatusSave = JSON.stringify(status);
return this.json;
}
/*
refresh(ElementSet) {
}
*/
get status() {
return JSON.parse(this.json.StatusSave)
}
}
class ElementSet {
constructor(Editor=null, Element=null, type=null, from=null, to=null) {
let json = Editor.json ? Editor.json : Editor;
let elements = Editor.status.Elements;
this.elements = [];
if (Editor != null) {
if (Element != null) throw SyntaxError("Unexpected argument 'Element'.");
if (type != null) {
for (let i = 0; i < elements.length; i++) {
if (elements[i].ModelID == type) e = new ElementClass(Editor, null, null, i);
this.elements.push(e);
}
} else {
let e;
for (let i = 0; i < elements.length; i++) {
e = new ElementClass(Editor, null, null, i);
this.elements.push(e);
}
}
} else if (Element != null) {
this.elements[0] = Element;
} else if (type != null) {
throw SyntaxError("No 'Editor' is found.");
}
this.wires = []
for (const wire of Editor.status.Wires) {
for (const ele of this.elements) {
if (ele.id == wire.Source || ele.id == wire.Target) {
this.wires.push(wire)
}
}
}
}
index(n) {
return this.elements[n].index;
}
json(n) {
return this.elements[n].json;
}
id(n) {
return this.elements[n].id;
}
lockAll() {
for (let i = 0; i < this.card; i++) this.elements[i].lock();
return this.elements;
}
unlockAll() {
for (let i = 0; i < this.card; i++) this.elements[i].unlock();
return this.elements;
}
burnAll() {
for (let i = 0; i < this.card; i++) this.elements[i].burn();
return this.elements;
}
fixAll() {
for (let i = 0; i < this.card; i++) this.elements[i].fix();
return this.elements;
}
moveAll(steps) {
for (let i = 0; i < this.card; i++) this.elements[i].move(steps);
return this.elements;
}
gather(pos) {
for (let i = 0; i < this.card; i++) this.elements[i].setPos(pos);
return this.elements;
}
/*
push(Element) {
}
delete(n=null, id=null) {
}
*/
refreshID(Editor) {
for (let i = 0; i < this.card; i++) {
let eleID = this.elements[i].id;
let releventWires = [];
for (const [index, wire] of this.wires.entries()) {
if (wire.Source == eleID) {
releventWires.push([index, 1]);
} else if (wire.Target == eleID) {
releventWires.push([index, 2]);
}
}
this.elements[i].newID(Editor);
eleID = this.elements[i].id;
for (const [index, place] of releventWires) {
if (place == 1) {
this.wires[index].Source = eleID;
} else {
this.wires[index].Target = eleID;
}
}
}
return this.elements;
}
get card() {
return this.elements.length;
}
}
// Export the functions.
exports.consts = consts;
exports.Editor = Editor;
exports.ElementSet = ElementSet;
exports.Element = ElementClass;