-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
383 lines (336 loc) · 10.7 KB
/
server.js
File metadata and controls
383 lines (336 loc) · 10.7 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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// FPV Drone Simulator - Multiplayer Server
// Run with: node server.js
const WebSocket = require('ws');
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = 3000;
// Game state
const players = new Map();
const npcs = [];
let nextPlayerId = 1;
// Random name generator
const adjectives = ['Swift', 'Crazy', 'Flying', 'Turbo', 'Nitro', 'Phantom', 'Ghost', 'Shadow', 'Thunder', 'Lightning', 'Blazing', 'Sonic', 'Hyper', 'Mega', 'Ultra'];
const nouns = ['Pilot', 'Ace', 'Hawk', 'Eagle', 'Falcon', 'Drone', 'Racer', 'Flyer', 'Wing', 'Jet', 'Storm', 'Blade', 'Arrow', 'Comet', 'Star'];
function generateRandomName() {
const adj = adjectives[Math.floor(Math.random() * adjectives.length)];
const noun = nouns[Math.floor(Math.random() * nouns.length)];
const num = Math.floor(Math.random() * 99) + 1;
return `${adj}${noun}${num}`;
}
// NPC AI behaviors
const NPC_BEHAVIORS = {
FREESTYLE: 'freestyle',
RACING: 'racing',
EXPLORER: 'explorer',
BEGINNER: 'beginner'
};
// Create NPC drones
function createNPC(behavior) {
const npc = {
id: `npc_${npcs.length + 1}`,
name: `[BOT] ${generateRandomName()}`,
behavior: behavior,
position: {
x: (Math.random() - 0.5) * 200,
y: 10 + Math.random() * 30,
z: (Math.random() - 0.5) * 200
},
rotation: { x: 0, y: Math.random() * Math.PI * 2, z: 0 },
velocity: { x: 0, y: 0, z: 0 },
state: 'idle',
targetPoint: null,
stateTimer: 0,
crashed: false
};
npcs.push(npc);
return npc;
}
// Update NPC AI
function updateNPC(npc, dt) {
if (npc.crashed) {
npc.stateTimer -= dt;
if (npc.stateTimer <= 0) {
// Respawn
npc.position = {
x: (Math.random() - 0.5) * 200,
y: 10 + Math.random() * 30,
z: (Math.random() - 0.5) * 200
};
npc.velocity = { x: 0, y: 0, z: 0 };
npc.crashed = false;
}
return;
}
npc.stateTimer -= dt;
switch (npc.behavior) {
case NPC_BEHAVIORS.FREESTYLE:
updateFreestyleNPC(npc, dt);
break;
case NPC_BEHAVIORS.RACING:
updateRacingNPC(npc, dt);
break;
case NPC_BEHAVIORS.EXPLORER:
updateExplorerNPC(npc, dt);
break;
case NPC_BEHAVIORS.BEGINNER:
updateBeginnerNPC(npc, dt);
break;
}
// Apply gravity and movement
npc.velocity.y -= 9.81 * dt * 0.3;
npc.position.x += npc.velocity.x * dt;
npc.position.y += npc.velocity.y * dt;
npc.position.z += npc.velocity.z * dt;
// Ground collision
if (npc.position.y < 1) {
if (Math.abs(npc.velocity.y) > 5) {
npc.crashed = true;
npc.stateTimer = 3; // Respawn in 3 seconds
} else {
npc.position.y = 1;
npc.velocity.y = 0;
}
}
// Boundary check
const bound = 180;
if (Math.abs(npc.position.x) > bound || Math.abs(npc.position.z) > bound) {
npc.crashed = true;
npc.stateTimer = 3;
}
}
function updateFreestyleNPC(npc, dt) {
if (npc.stateTimer <= 0) {
// Pick new random action
const action = Math.random();
if (action < 0.3) {
// Climb
npc.velocity.y = 15 + Math.random() * 10;
} else if (action < 0.5) {
// Dive
npc.velocity.y = -5;
}
// Random horizontal movement
npc.velocity.x = (Math.random() - 0.5) * 20;
npc.velocity.z = (Math.random() - 0.5) * 20;
npc.stateTimer = 1 + Math.random() * 2;
}
}
function updateRacingNPC(npc, dt) {
// Maintain consistent forward speed
const speed = 25;
if (!npc.targetPoint || npc.stateTimer <= 0) {
npc.targetPoint = {
x: (Math.random() - 0.5) * 150,
y: 15 + Math.random() * 20,
z: (Math.random() - 0.5) * 150
};
npc.stateTimer = 5;
}
// Move toward target
const dx = npc.targetPoint.x - npc.position.x;
const dy = npc.targetPoint.y - npc.position.y;
const dz = npc.targetPoint.z - npc.position.z;
const dist = Math.sqrt(dx*dx + dy*dy + dz*dz);
if (dist > 5) {
npc.velocity.x = (dx / dist) * speed;
npc.velocity.y = (dy / dist) * speed * 0.5;
npc.velocity.z = (dz / dist) * speed;
}
}
function updateExplorerNPC(npc, dt) {
// Slow, cinematic movements
if (npc.stateTimer <= 0) {
npc.targetPoint = {
x: (Math.random() - 0.5) * 180,
y: 20 + Math.random() * 40,
z: (Math.random() - 0.5) * 180
};
npc.stateTimer = 8 + Math.random() * 5;
}
const speed = 8;
const dx = npc.targetPoint.x - npc.position.x;
const dy = npc.targetPoint.y - npc.position.y;
const dz = npc.targetPoint.z - npc.position.z;
const dist = Math.sqrt(dx*dx + dy*dy + dz*dz);
if (dist > 3) {
npc.velocity.x = (dx / dist) * speed;
npc.velocity.y = (dy / dist) * speed * 0.3;
npc.velocity.z = (dz / dist) * speed;
} else {
// Hover
npc.velocity.x *= 0.9;
npc.velocity.z *= 0.9;
npc.velocity.y = Math.sin(Date.now() * 0.001) * 2;
}
}
function updateBeginnerNPC(npc, dt) {
// Hesitant, slow movements with occasional crashes
if (npc.stateTimer <= 0) {
const action = Math.random();
if (action < 0.1) {
// Occasional crash (simulate mistake)
npc.velocity.y = -20;
} else {
npc.velocity.x = (Math.random() - 0.5) * 8;
npc.velocity.z = (Math.random() - 0.5) * 8;
npc.velocity.y = (Math.random() - 0.3) * 5;
}
npc.stateTimer = 2 + Math.random() * 3;
}
// Keep low altitude
if (npc.position.y > 15) {
npc.velocity.y -= 2 * dt;
}
}
// Initialize NPCs
function initializeNPCs() {
createNPC(NPC_BEHAVIORS.FREESTYLE);
createNPC(NPC_BEHAVIORS.FREESTYLE);
createNPC(NPC_BEHAVIORS.RACING);
createNPC(NPC_BEHAVIORS.RACING);
createNPC(NPC_BEHAVIORS.EXPLORER);
createNPC(NPC_BEHAVIORS.EXPLORER);
createNPC(NPC_BEHAVIORS.BEGINNER);
createNPC(NPC_BEHAVIORS.BEGINNER);
}
// Create HTTP server for serving static files
const server = http.createServer((req, res) => {
let filePath = req.url === '/' ? '/index.html' : req.url;
filePath = path.join(__dirname, filePath);
const extname = path.extname(filePath);
const contentTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.gif': 'image/gif'
};
const contentType = contentTypes[extname] || 'text/plain';
fs.readFile(filePath, (err, content) => {
if (err) {
if (err.code === 'ENOENT') {
res.writeHead(404);
res.end('File not found');
} else {
res.writeHead(500);
res.end('Server error');
}
} else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content);
}
});
});
// Create WebSocket server
const wss = new WebSocket.Server({ server });
wss.on('connection', (ws) => {
const playerId = nextPlayerId++;
const playerName = generateRandomName();
const player = {
id: playerId,
name: playerName,
position: { x: 0, y: 5, z: -120 },
rotation: { x: 0, y: 0, z: 0 },
velocity: { x: 0, y: 0, z: 0 },
crashed: false
};
players.set(playerId, player);
// Send welcome message with player info
ws.send(JSON.stringify({
type: 'welcome',
playerId: playerId,
playerName: playerName,
players: Array.from(players.values()),
npcs: npcs
}));
// Notify other players
broadcast({
type: 'playerJoined',
player: player
}, ws);
console.log(`Player ${playerName} (${playerId}) connected. Total players: ${players.size}`);
ws.on('message', (message) => {
try {
const data = JSON.parse(message);
switch (data.type) {
case 'update':
// Update player state
const p = players.get(playerId);
if (p) {
p.position = data.position;
p.rotation = data.rotation;
p.velocity = data.velocity;
p.crashed = data.crashed;
}
break;
case 'nameChange':
const player = players.get(playerId);
if (player) {
player.name = data.name;
broadcast({
type: 'nameChanged',
playerId: playerId,
name: data.name
});
}
break;
case 'crash':
broadcast({
type: 'playerCrashed',
playerId: playerId
});
break;
}
} catch (e) {
console.error('Error parsing message:', e);
}
});
ws.on('close', () => {
players.delete(playerId);
broadcast({
type: 'playerLeft',
playerId: playerId
});
console.log(`Player ${playerName} (${playerId}) disconnected. Total players: ${players.size}`);
});
});
function broadcast(message, exclude = null) {
const msg = JSON.stringify(message);
wss.clients.forEach((client) => {
if (client !== exclude && client.readyState === WebSocket.OPEN) {
client.send(msg);
}
});
}
// Game loop for NPCs and state broadcasting
let lastTime = Date.now();
setInterval(() => {
const now = Date.now();
const dt = (now - lastTime) / 1000;
lastTime = now;
// Update NPCs
npcs.forEach(npc => updateNPC(npc, dt));
// Broadcast game state
broadcast({
type: 'gameState',
players: Array.from(players.values()),
npcs: npcs
});
}, 50); // 20 updates per second
// Initialize and start server
initializeNPCs();
server.listen(PORT, () => {
console.log(`
========================================
FPV Drone Simulator - Multiplayer Server
========================================
Server running at http://localhost:${PORT}
Open this URL in your browser to play!
Share with friends to play together.
${npcs.length} NPC drones active
========================================
`);
});