Skip to content

Commit

Permalink
enhancement: made item spawn points easier to define and added the ab…
Browse files Browse the repository at this point in the history
…ility to specify item spawn rates in .env
AustinJMann committed Jan 28, 2025
1 parent 7651d78 commit 600a6fd
Showing 7 changed files with 166 additions and 175 deletions.
38 changes: 18 additions & 20 deletions public/maps/alpha_1/map.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
{
"name": "deathmatch_1",
"respawnPoints": [
{
"position": {"x": 5, "y": 2, "z": 0},
"quaternion": {"x": 0, "y": 0.92, "z": 0, "w": 0.37}
}
],
"itemRespawnPoints": [

{
"position": {"x": -1, "y": 0.25, "z": -6},
"itemId": 2
},
{
"position": {"x": 10, "y": 0.25, "z": 0},
"itemId": 1
}

]
}
"name": "deathmatch_1",
"respawnPoints": [
{
"position": { "x": 5, "y": 2, "z": 0 },
"quaternion": { "x": 0, "y": 0.92, "z": 0, "w": 0.37 }
}
],
"itemRespawnPoints": [
{
"position": { "x": -1, "y": 0.25, "z": -6 },
"itemIds": [2]
},
{
"position": { "x": 10, "y": 0.25, "z": 0 },
"itemIds": [1]
}
]
}
82 changes: 19 additions & 63 deletions public/maps/crackhouse_1/map.json
Original file line number Diff line number Diff line change
@@ -45,123 +45,79 @@
"itemRespawnPoints": [
{
"position": { "x": 5.71, "y": 1.31, "z": -4.16 },
"itemId": 1
"itemIds": [1]
},
{
"position": { "x": -0.22, "y": 1.05, "z": 4.48 },
"itemId": 1
"itemIds": [1]
},
{
"position": { "x": -7.17, "y": 1.05, "z": 13.79 },
"itemId": 1
"itemIds": [1]
},
{
"position": { "x": -6.79, "y": 1.05, "z": 4.31 },
"itemId": 2
"itemIds": [2, 3]
},
{
"position": { "x": -7.03, "y": 3.24, "z": 14.89 },
"itemId": 2
"itemIds": [2]
},
{
"position": { "x": 4.71, "y": 3.24, "z": 10.77 },
"itemId": 2
},
{
"position": { "x": -6.79, "y": 1.05, "z": 4.31 },
"itemId": 3
},
{
"position": { "x": 4.71, "y": 3.24, "z": 10.77 },
"itemId": 1
"itemIds": [1, 2]
},
{
"position": { "x": 3.38, "y": 3.24, "z": 4.12 },
"itemId": 1
"itemIds": [1]
},
{
"position": { "x": 12.14, "y": 0.53, "z": 13.78 },
"itemId": 1
"itemIds": [1]
},
{
"position": { "x": 23.68, "y": 0.45, "z": 14.57 },
"itemId": 2
"itemIds": [2, 3]
},
{
"position": { "x": 24.3, "y": 1.2, "z": -2.25 },
"itemId": 2
},
{
"position": { "x": 23.68, "y": 0.45, "z": 14.57 },
"itemId": 3
},
{
"position": { "x": 24.3, "y": 1.2, "z": -2.25 },
"itemId": 3
"itemIds": [2, 3]
},
{
"position": { "x": 0.5, "y": 0.83, "z": -10.2 },
"itemId": 1
"itemIds": [1]
},
{
"position": { "x": -13.38, "y": 0.83, "z": -10.2 },
"itemId": 1
"itemIds": [1]
},
{
"position": { "x": 3.68, "y": 1.05, "z": 4.08 },
"itemId": 2
"itemIds": [2, 3]
},
{
"position": { "x": -0.62, "y": 0.55, "z": 16.68 },
"itemId": 2
},
{
"position": { "x": 3.68, "y": 1.05, "z": 4.08 },
"itemId": 3
},
{
"position": { "x": -0.62, "y": 0.55, "z": 16.68 },
"itemId": 3
},
{
"position": { "x": -0.62, "y": 0.55, "z": 16.68 },
"itemId": 1
"itemIds": [1, 2, 3]
},
{
"position": { "x": -18.04, "y": 0.55, "z": 3.17 },
"itemId": 1
},
{
"position": { "x": -13.9, "y": 0.76, "z": -3.94 },
"itemId": 1
},
{
"position": { "x": -13.9, "y": 0.76, "z": -3.94 },
"itemId": 2
},
{
"position": { "x": 6.83, "y": 0.55, "z": -13.59 },
"itemId": 2
"itemIds": [1]
},
{
"position": { "x": -13.9, "y": 0.76, "z": -3.94 },
"itemId": 3
"itemIds": [1, 2, 3]
},
{
"position": { "x": 6.83, "y": 0.55, "z": -13.59 },
"itemId": 3
"itemIds": [2, 3]
},
{
"position": { "x": -6.65, "y": 3.24, "z": 2.28 },
"itemId": 2
},
{
"position": { "x": -4.71, "y": 3.24, "z": 10.22 },
"itemId": 2
"itemIds": [2]
},
{
"position": { "x": -4.71, "y": 3.24, "z": 10.22 },
"itemId": 1
"itemIds": [1, 2]
}
]
}
173 changes: 86 additions & 87 deletions public/maps/deathmatch_1/map.json
Original file line number Diff line number Diff line change
@@ -1,88 +1,87 @@
{
"name": "deathmatch_1",
"respawnPoints": [
{
"position": {"x": 22.5, "y": 0.5, "z": -9},
"quaternion": {"x": 0, "y": 0.92, "z": 0, "w": 0.37}
},
{
"position": {"x": 2.6, "y": 0.5, "z": -17.4},
"quaternion": {"x": 0, "y": -0.95, "z": 0, "w": 0.33}
},
{
"position": {"x": -22.42, "y": 0.5, "z": -8.35},
"quaternion": {"x": 0, "y": -0.91, "z": 0, "w": 0.41}
},
{
"position": {"x": -6, "y": 0.5, "z": -10.4},
"quaternion": {"x": 0, "y": -0.92, "z": 0, "w": 0.4}
},
{
"position": {"x": -0.38, "y": 5.5, "z": 18.45},
"quaternion": {"x": 0, "y": 0, "z": 0, "w": 1}
},
{
"position": {"x": 5.75, "y": 0.5, "z": -9.75},
"quaternion": {"x": 0, "y": 0.94, "z": 0, "w": 0.32}
},
{
"position": {"x": -5.88, "y": 5.5, "z": 9.1},
"quaternion": {"x": 0, "y": -0.22, "z": 0, "w": 0.96}
},
{
"position": {"x": 33.1, "y": 0.5, "z": -0.22},
"quaternion": {"x": 0, "y": 0.73, "z": 0, "w": 0.67}
},
{
"position": {"x": 20, "y": 0.5, "z": 17},
"quaternion": {"x": 0, "y": 0.43, "z": 0, "w": 0.89}
}
],
"itemRespawnPoints": [
{
"position": {"x": 0, "y": 0.25, "z": 3},
"itemId": 1
},
{
"position": {"x": -29, "y": 0.25, "z": 0},
"itemId": 1
},
{
"position": {"x": -0.5, "y": 5.25, "z": 8.4},
"itemId": 1
},
{
"position": {"x": -10.3, "y": 2.75, "z": 17.65},
"itemId": 1
},
{
"position": {"x": 25, "y": 0.25, "z": 18},
"itemId": 1
},
{
"position": {"x": 14.67, "y": 0.82, "z": 0.7},
"itemId": 1
},
{
"position": {"x": 36.5, "y": 0.25, "z": -18.3},
"itemId": 2
},
{
"position": {"x": 19, "y": 0.25, "z": -11},
"itemId": 2
},
{
"position": {"x": -14, "y": 0.25, "z": 9.5},
"itemId": 2
},
{
"position": {"x": 18, "y": 0.25, "z": 0},
"itemId": 2
},
{
"position": {"x": -1, "y": 0.25, "z": -6},
"itemId": 2
}

]
}
"name": "deathmatch_1",
"respawnPoints": [
{
"position": { "x": 22.5, "y": 0.5, "z": -9 },
"quaternion": { "x": 0, "y": 0.92, "z": 0, "w": 0.37 }
},
{
"position": { "x": 2.6, "y": 0.5, "z": -17.4 },
"quaternion": { "x": 0, "y": -0.95, "z": 0, "w": 0.33 }
},
{
"position": { "x": -22.42, "y": 0.5, "z": -8.35 },
"quaternion": { "x": 0, "y": -0.91, "z": 0, "w": 0.41 }
},
{
"position": { "x": -6, "y": 0.5, "z": -10.4 },
"quaternion": { "x": 0, "y": -0.92, "z": 0, "w": 0.4 }
},
{
"position": { "x": -0.38, "y": 5.5, "z": 18.45 },
"quaternion": { "x": 0, "y": 0, "z": 0, "w": 1 }
},
{
"position": { "x": 5.75, "y": 0.5, "z": -9.75 },
"quaternion": { "x": 0, "y": 0.94, "z": 0, "w": 0.32 }
},
{
"position": { "x": -5.88, "y": 5.5, "z": 9.1 },
"quaternion": { "x": 0, "y": -0.22, "z": 0, "w": 0.96 }
},
{
"position": { "x": 33.1, "y": 0.5, "z": -0.22 },
"quaternion": { "x": 0, "y": 0.73, "z": 0, "w": 0.67 }
},
{
"position": { "x": 20, "y": 0.5, "z": 17 },
"quaternion": { "x": 0, "y": 0.43, "z": 0, "w": 0.89 }
}
],
"itemRespawnPoints": [
{
"position": { "x": 0, "y": 0.25, "z": 3 },
"itemIds": [1]
},
{
"position": { "x": -29, "y": 0.25, "z": 0 },
"itemIds": [1]
},
{
"position": { "x": -0.5, "y": 5.25, "z": 8.4 },
"itemIds": [1]
},
{
"position": { "x": -10.3, "y": 2.75, "z": 17.65 },
"itemIds": [1]
},
{
"position": { "x": 25, "y": 0.25, "z": 18 },
"itemIds": [1]
},
{
"position": { "x": 14.67, "y": 0.82, "z": 0.7 },
"itemIds": [1]
},
{
"position": { "x": 36.5, "y": 0.25, "z": -18.3 },
"itemIds": [2]
},
{
"position": { "x": 19, "y": 0.25, "z": -11 },
"itemIds": [2]
},
{
"position": { "x": -14, "y": 0.25, "z": 9.5 },
"itemIds": [2]
},
{
"position": { "x": 18, "y": 0.25, "z": 0 },
"itemIds": [2]
},
{
"position": { "x": -1, "y": 0.25, "z": -6 },
"itemIds": [2]
}
]
}
17 changes: 17 additions & 0 deletions src/server/config.ts
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ const defaults = {
//Item settings
MAX_ITEMS_IN_WORLD: '10',
ITEM_RESPAWN_TIME: '7',
ITEM_RESPAWN_RATE: '1:1,2:1,3:1',
};

async function updateEnvFile(defaults: Record<string, string>) {
@@ -87,10 +88,26 @@ function parseConfig(env: Record<string, string>) {
items: {
maxItemsInWorld: parseInt(env.MAX_ITEMS_IN_WORLD),
respawnTime: parseInt(env.ITEM_RESPAWN_TIME),
spawnRates: parseStringToMap(env.ITEM_RESPAWN_RATE),
},
};
}

const rawConfig = await updateEnvFile(defaults);
const config = parseConfig(rawConfig);
export default config;

function parseStringToMap(input: string): Map<number, number> {
const map = new Map<number, number>();

input.split(',').forEach((pair) => {
const [key, value] = pair.split(':').map(Number);
if (!isNaN(key) && !isNaN(value)) {
map.set(key, value);
} else {
throw new Error(`Invalid pair: ${pair}`);
}
});

return map;
}
27 changes: 24 additions & 3 deletions src/server/managers/ItemManager.ts
Original file line number Diff line number Diff line change
@@ -37,11 +37,32 @@ export class ItemManager {
createItem() {
if (!this.mapData) return;

const randomIndex = Math.floor(Math.random() * this.mapData.itemRespawnPoints.length);
const respawnPoint = this.mapData.itemRespawnPoints[randomIndex];
const weaponSpawnRates = config.items.spawnRates;
let weaponSpawnRatesTotal = 0;
let weaponToSpawn = 0;

for (const spawnRate of weaponSpawnRates.values()) {
weaponSpawnRatesTotal += spawnRate;
}

const magicNumber = Math.floor(Math.random() * weaponSpawnRatesTotal) + 1;

let index = 0;
for (const weapon of weaponSpawnRates.keys()) {
const prevIndex = index;
index += weaponSpawnRates.get(weapon)!;
if (index <= magicNumber && index > prevIndex) {
weaponToSpawn = weapon;
break;
}
}

const respawnPoints = [...this.mapData.itemRespawnPoints].filter((value) => value.itemIds.includes(weaponToSpawn));
const randomIndex = Math.floor(Math.random() * respawnPoints.length);
const respawnPoint = respawnPoints[randomIndex];
const newItem = new WorldItem(
new THREE.Vector3(respawnPoint.position.x, respawnPoint.position.y, respawnPoint.position.z),
respawnPoint.itemId,
weaponToSpawn,
);

if (this.isItemCloseToPoint(newItem.vector, 1)) return; // Another item is too close
2 changes: 1 addition & 1 deletion src/server/models/ItemRespawnPoint.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import * as THREE from 'three';
export class ItemRespawnPoint {
constructor(
public position: THREE.Vector3,
public itemId: number,
public itemIds: number[],
public spawnChancePerTick: number,
) {}
}
2 changes: 1 addition & 1 deletion src/server/models/MapData.ts
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ export class MapData {
(irp) =>
new ItemRespawnPoint(
new THREE.Vector3(irp.position.x, irp.position.y, irp.position.z),
irp.itemId,
irp.itemIds,
irp.spawnChancePerTick,
),
);

0 comments on commit 600a6fd

Please sign in to comment.