-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared-travel-utils.js
More file actions
167 lines (137 loc) · 5.29 KB
/
shared-travel-utils.js
File metadata and controls
167 lines (137 loc) · 5.29 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
// Shared travel utilities for space colonization game
// Pure functions for pathfinding and travel calculations
// Used by both client and server to ensure consistent travel logic
const { TRAVEL_CONFIG } = require('./shared-config.js');
const { positionsEqual, positionToString } = require('./shared-position-utils.js');
// =============================================================================
// PATHFINDING UTILITIES
// =============================================================================
/**
* Find adjacent nodes for a given position using shared hexagon connections
* Pure function - no side effects, no game state dependencies
*/
function getAdjacentNodes(position, intersectionNodes) {
if (!Array.isArray(intersectionNodes)) {
console.error('getAdjacentNodes: intersectionNodes is not an array:', typeof intersectionNodes);
return [];
}
const currentNodeData = intersectionNodes.find(node => positionsEqual(node.position, position));
if (!currentNodeData) return [];
const adjacentPositions = new Set();
// For each hex this node touches, find consecutive vertices on the same hex
currentNodeData.connectedHexes.forEach(hexConnection => {
const currentHexTileIndex = hexConnection.hexData.tileIndex;
const currentNodePosition = hexConnection.nodePosition; // 0-5 position on hex
// Find all other nodes that share this hex
intersectionNodes.forEach(otherNode => {
if (!positionsEqual(otherNode.position, position)) {
// Check if other node is on the same hex
const sharedHex = otherNode.connectedHexes.find(otherHex =>
otherHex.hexData.tileIndex === currentHexTileIndex
);
if (sharedHex) {
const otherNodePosition = sharedHex.nodePosition;
// Only adjacent if consecutive positions on hex perimeter (0-1, 1-2, ..., 5-0)
const positionDiff = Math.abs(currentNodePosition - otherNodePosition);
const isConsecutive = positionDiff === 1 || positionDiff === 5; // Handle 0↔5 wrap-around
if (isConsecutive) {
adjacentPositions.add(positionToString(otherNode.position));
}
}
}
});
});
return Array.from(adjacentPositions).map(posStr => {
const [x, y] = posStr.split('-').map(Number);
return { x, y };
});
}
/**
* Calculate shortest path between two positions using BFS
* Returns {distance, path} or null if no path exists
* Pure function - no side effects, no game state dependencies
*/
function validateTravelPath(fromPosition, toPosition, intersectionNodes) {
if (!Array.isArray(intersectionNodes)) {
console.error('validateTravelPath: intersectionNodes is not an array:', typeof intersectionNodes);
return null;
}
if (positionsEqual(fromPosition, toPosition)) {
return { distance: 0, path: [fromPosition] };
}
const queue = [{ position: fromPosition, distance: 0, path: [fromPosition] }];
const visited = new Set([positionToString(fromPosition)]);
while (queue.length > 0) {
const { position, distance, path } = queue.shift();
// Get adjacent nodes through shared hexagons
const neighbors = getAdjacentNodes(position, intersectionNodes);
for (const neighborPosition of neighbors) {
if (positionsEqual(neighborPosition, toPosition)) {
return {
distance: distance + 1,
path: [...path, neighborPosition]
};
}
const neighborKey = positionToString(neighborPosition);
if (!visited.has(neighborKey)) {
visited.add(neighborKey);
queue.push({
position: neighborPosition,
distance: distance + 1,
path: [...path, neighborPosition]
});
}
}
}
return null; // No path found
}
// =============================================================================
// TRAVEL COST CALCULATIONS
// =============================================================================
/**
* Calculate travel cost based on distance
* Pure function using shared travel configuration
*/
function calculateTravelCost(distance) {
const zapCost = distance * TRAVEL_CONFIG.BASE_COST_PER_HOP;
const snowflakeCost = distance > TRAVEL_CONFIG.LONG_DISTANCE_THRESHOLD ?
TRAVEL_CONFIG.LONG_DISTANCE_COST : 0;
return {
zap: zapCost,
snowflake: snowflakeCost
};
}
/**
* Validate if a player can afford a travel cost
* Pure function - checks resources against cost
*/
function canAffordTravel(playerResources, travelCost) {
return playerResources.zap >= travelCost.zap &&
playerResources.snowflake >= travelCost.snowflake;
}
/**
* Calculate complete travel information (path + cost)
* Combines pathfinding with cost calculation
*/
function calculateTravelInfo(fromPosition, toPosition, intersectionNodes) {
const pathResult = validateTravelPath(fromPosition, toPosition, intersectionNodes);
if (!pathResult) {
return null; // No valid path
}
const cost = calculateTravelCost(pathResult.distance);
return {
distance: pathResult.distance,
path: pathResult.path,
cost: cost
};
}
// =============================================================================
// MODULE EXPORTS
// =============================================================================
module.exports = {
getAdjacentNodes,
validateTravelPath,
calculateTravelCost,
canAffordTravel,
calculateTravelInfo
};