-
Notifications
You must be signed in to change notification settings - Fork 35
/
500ise_recorder.js
166 lines (146 loc) · 4.35 KB
/
500ise_recorder.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
var MODE_STANDBY = 0;
var MODE_PLAY = 1;
var MODE_RECORD = 2;
var currentRecordBuffer;
var playPointer = 0;
var curMode = MODE_STANDBY;
var waitingForPlaybackTarget = false;
var playerHeight = 1.62;
var playbackTracks = [];
var paused = true;
var startDelay = 0;
var queuedCommands = [];
var removeEntityAtEnd = true;
var enteringSequenceMode = false;
var pausedAtStart = true;
function getYOffset(entity) {
var entityId = Entity.getEntityTypeId(entity);
return entityId == 0? 1.62: 0;
}
function useItem(x, y, z, itemId, blockId, side) {
if (itemId == 267) {
paused = !paused;
}
}
function procCmd(cmd) {
if (enteringSequenceMode) {
procSequenceCmd(cmd);
return;
}
var data = cmd.split(" ");
var cmdName = data[0];
if (cmdName == "record") {
curMode = MODE_RECORD;
currentRecordBuffer = [];
clientMessage("Recording");
} else if (cmdName == "play") {
startPlayback(getPlayerEnt(), startDelay);
} else if (cmdName == "stop") {
curMode = MODE_STANDBY;
playbackTracks = [];
} else if (cmdName == "playsp") {
var spawnedEntity = spawnPigZombie(getPlayerX(), getPlayerY(), getPlayerZ(), 1, "mob/char.png");
startPlayback(spawnedEntity, startDelay);
} else if (cmdName == "playent") {
waitingForPlaybackTarget = true;
} else if (cmdName == "save") {
ModPE.saveData(data[1], JSON.stringify(currentRecordBuffer));
clientMessage("Saved as " + data[1]);
} else if (cmdName == "load") {
currentRecordBuffer = JSON.parse(ModPE.readData(data[1]))
} else if (cmdName == "delay") {
startDelay = parseInt(data[1]);
} else if (cmdName == "seqadd") {
queuedCommands.push(data.slice(1).join(" "));
} else if (cmdName == "seqrun") {
runQueuedCommands();
} else if (cmdName == "seqsave") {
ModPE.saveData("_sequence_", JSON.stringify(queuedCommands));
} else if (cmdName == "seqload") {
queuedCommands = JSON.parse(ModPE.readData("_sequence_"));
} else if (cmdName == "seqbegin") {
clientMessage("Beginning sequence");
enteringSequenceMode = true;
queuedCommands = [];
} else if (cmdName == "pauseatstart") {
pausedAtStart = (data[1] == "true");
}
}
function procSequenceCmd(cmd) {
var data = cmd.split(" ");
var cmdName = data[0];
if (cmdName == "seqend") {
clientMessage("End of sequence");
enteringSequenceMode = false;
} else {
queuedCommands.push(cmd);
clientMessage(cmd);
}
}
function runQueuedCommands() {
for (var i = 0; i < queuedCommands.length; i++) {
procCmd(queuedCommands[i]);
}
}
function attackHook(attacker, victim) {
if (waitingForPlaybackTarget) {
startPlayback(victim, startDelay);
preventDefault();
waitingForPlaybackTarget = false;
} else if (getCarriedItem() == 267) {
Entity.remove(victim);
preventDefault();
}
}
function startPlayback(entity, delay) {
var playbackTrack = {
"entity" : entity,
"pointer": 0,
"delay": delay,
"eventList": currentRecordBuffer
};
playbackTracks.push(playbackTrack);
paused = pausedAtStart;
}
function modTick() {
if (curMode == MODE_RECORD) {
var newRecord = {
"x": getPlayerX(),
"y": getPlayerY() - playerHeight,
"z": getPlayerZ(),
"yaw": getYaw(),
"pitch": getPitch()
}
currentRecordBuffer.push(newRecord);
}
handlePlayback();
}
function handlePlayback() {
for (var i = playbackTracks.length - 1; i >= 0; i--) {
var playbackTrack = playbackTracks[i];
var playbackEntity = playbackTrack["entity"];
var playbackEventList = playbackTrack["eventList"];
var delay = playbackTrack["delay"];
if (!paused && delay > 0) {
playbackTrack["delay"] -= 1;
}
var playPointer = playbackTrack["pointer"];
var nextPointer = playPointer + 1;
var record = playbackEventList[playPointer];
var atEnd = nextPointer >= playbackEventList.length;
var nextRecord = atEnd? null: playbackEventList[nextPointer];
setPosition(playbackEntity, record["x"], record["y"] + getYOffset(playbackEntity), record["z"]);
setRot(playbackEntity, record["yaw"], record["pitch"]);
setVelX(playbackEntity, atEnd? 0: nextRecord["x"] - record["x"]);
setVelY(playbackEntity, atEnd? 0: nextRecord["y"] - record["y"]);
setVelZ(playbackEntity, atEnd? 0: nextRecord["z"] - record["z"]);
if (nextPointer >= playbackEventList.length) {
playbackTracks.splice(i, 1);
if (removeEntityAtEnd && Entity.getEntityTypeId(playbackEntity) != 0) {
Entity.remove(playbackEntity);
}
} else {
if (!paused && delay <= 0) playbackTrack["pointer"] = nextPointer;
}
}
}