-
Notifications
You must be signed in to change notification settings - Fork 1
/
streamer.mjs
242 lines (195 loc) · 5.45 KB
/
streamer.mjs
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
import alt from "alt";
import {
ENTITY_STREAM_IN_EVENT,
ENTITY_STREAM_OUT_EVENT,
ENTITY_DATA_UPDATE_EVENT,
ENTITY_MOVE_EVENT
} from "./shared.mjs";
const TICK_UPDATE_DELAY = 1000;
const STREAMING_RANGE = 400;
let entities = [];
let tick = () => {
alt.Player.all.forEach(
(player) => {
entities.forEach(
(entity, entityID) => {
if(entity !== undefined) {
let shouldBeStreamed = shouldEntityBeStreamedToPlayer(
player,
entityID
);
let index = isEntityStreamedToPlayer(
player,
entityID
);
//Basically if something needs to be done.
if(shouldBeStreamed !== (index !== -1)) {
//To make it a bit faster pass index here too.
if(shouldBeStreamed) addEntityToPlayer(player, entityID, index);
else removeEntityFromPlayer(player, entityID, index);
}
}
}
);
}
)
}
setInterval(
tick,
1000
);
alt.on(
'playerDisconnect',
(player, reason) => {
entities.forEach(
(entity, entityID) => {
if(entity !== undefined) {
let index = isEntityStreamedToPlayer(
player,
entityID
);
if(index !== -1) removeEntityFromPlayer(player, entityID, index, false);
}
}
);
}
);
let dist = (pos1, pos2) => {
let pos = {
x: pos1.x - pos2.x,
y: pos1.y - pos2.y,
z: pos1.z - pos2.z
};
return Math.sqrt(
pos.x*pos.x + pos.y*pos.y + pos.z*pos.z
);
};
let addEntityToPlayer = (player, entityID, index = null) => {
if(index === null) index = isEntityStreamedToPlayer(player, entityID)
if(index === -1) {
let entity = entities[entityID];
entity.syncedTo.push(player.id);
alt.emitClient(
player,
ENTITY_STREAM_IN_EVENT,
entity
);
}
};
let removeEntityFromPlayer = (player, entityID, index = null, notify = true) => {
if(index === null) index = isEntityStreamedToPlayer(player, entityID);
if(index !== -1) {
let entity = entities[entityID];
entity.syncedTo.splice(index, 1);
if(notify)
alt.emitClient(
player,
ENTITY_STREAM_OUT_EVENT,
entity
);
}
};
//returns -1 if no; returns index if yes.
let isEntityStreamedToPlayer = (player, entityID) => {
let entity = entities[entityID];
let result = -1;
entity.syncedTo.forEach(
(playerID, index) => {
if(playerID === player.id) result = index;
}
)
return result;
}
let shouldEntityBeStreamedToPlayer = (player, entityID) => {
let entity = entities[entityID];
return dist(
player.pos,
entity.pos
) < STREAMING_RANGE;
}
let findUnusedEntityID = () => {
for(let i = 0; i < entities.length; i++)
if(entities[i] === undefined) return i;
return entities.length;
};
export function createEntity(pos, data) {
let id = findUnusedEntityID();
entities[id] = {
id: id,
pos: {...pos},
data: {...data},
syncedTo: []
};;
return id;
}
export function updateEntityData(id, data) {
if(!doesEntityExist(id)) return false;
entities[id].data = {
...data
};
entities[id].syncedTo.forEach(
(playerID) => {
let player = alt.Player.all[playerID];
alt.emitClient(
player,
ENTITY_DATA_UPDATE_EVENT,
entity
);
}
);
return true;
}
export function moveEntity(id, pos) {
if(!doesEntityExist(id)) return false;
entities[id].pos = {
...pos
};
let toRemove = [];
entities[id].syncedTo.forEach(
(playerID) => {
let player = alt.Player.all[playerID];
if(shouldEntityBeStreamedToPlayer(player, id))
alt.emitClient(
player,
ENTITY_MOVE_EVENT,
entity
);
else toRemove.push(playerID);
}
);
if(toRemove.length !== 0) toRemove.forEach(
(playerID) => {
removeEntityFromPlayer(
alt.Player.all[playerID],
id
);
}
)
//New players which/if are in streaming range will be added next tick.
//Needed to remove player, in order to update position on their clients.
return true;
}
export function destroyEntity(id) {
let entity = entities[id];
if(entity !== undefined) {
entity.syncedTo.forEach(
(playerID) => {
alt.emitClient(
alt.Player.all[playerID],
ENTITY_STREAM_OUT_EVENT,
entity
);
}
)
}
entities[id] = undefined;
}
export function doesEntityExist(id) {
return entities[id] !== undefined;
}
export function getEntityData(id) {
if(!doesEntityExist(id)) return null;
return {
...entities[id].data
}
}