-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpaceTraders.ps1
More file actions
267 lines (238 loc) · 8.72 KB
/
SpaceTraders.ps1
File metadata and controls
267 lines (238 loc) · 8.72 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
#Requires -Version 7.0
<#
.SYNOPSIS
PowerTraders - A Space Trader clone for PowerShell 7.x
.DESCRIPTION
Trade goods between procedurally generated star systems.
Buy low, sell high, upgrade your ship, and become the galaxy's
greatest space trader.
.EXAMPLE
pwsh ./SpaceTraders.ps1
#>
param(
[int]$SystemCount = 15,
[int]$Seed = -1,
[int]$StartingCredits = 3000
)
$ErrorActionPreference = 'Stop'
# --- Import modules ---
$modulePath = Join-Path $PSScriptRoot 'Modules'
Import-Module (Join-Path $modulePath 'UI.psm1') -Force -DisableNameChecking
Import-Module (Join-Path $modulePath 'Universe.psm1') -Force -DisableNameChecking
Import-Module (Join-Path $modulePath 'Ships.psm1') -Force -DisableNameChecking
Import-Module (Join-Path $modulePath 'Trading.psm1') -Force -DisableNameChecking
Import-Module (Join-Path $modulePath 'SaveLoad.psm1') -Force -DisableNameChecking
# ================================================================
# INTRO & START MENU
# ================================================================
Clear-Host
Write-Banner
$gameState = $null
$startMenuOptions = @(
@{ Key = '1'; Label = 'New Game' }
@{ Key = '2'; Label = 'Load Game' }
@{ Key = 'Q'; Label = 'Quit' }
)
$startChoice = Show-Menu -Title 'PowerTraders' -Options $startMenuOptions
if ($startChoice.ToUpper() -eq 'Q') {
Write-Host ""
Write-Success "Goodbye, Captain!"
Write-Host ""
return
}
if ($startChoice -eq '2') {
$gameState = Show-LoadMenu
if ($null -ne $gameState) {
Write-Host ""
Wait-ForKey
}
}
if ($null -eq $gameState) {
# ============================================================
# CHARACTER CREATION
# ============================================================
$playerName = ''
while ([string]::IsNullOrWhiteSpace($playerName)) {
$playerName = Read-PlayerInput "Enter your name, Captain: "
if ([string]::IsNullOrWhiteSpace($playerName)) {
Write-Caution "A captain needs a name!"
}
}
Write-Success "Welcome aboard, Captain $playerName!"
Write-Host ""
# ============================================================
# SHIP PURCHASE
# ============================================================
$credits = $StartingCredits
$ship = $null
Write-Info "You have $($credits.ToString('N0')) credits to start with."
Write-Info "You'll need a ship before you can trade among the stars."
Write-Host ""
while ($null -eq $ship) {
Show-ShipCatalog -PlayerCredits $credits
$shipChoice = Read-PlayerInput "Enter ship # to purchase (or 'info' for details): "
if ($shipChoice -eq 'info') {
$infoChoice = Read-PlayerInput "Enter ship # for details: "
$infoId = 0
if ([int]::TryParse($infoChoice, [ref]$infoId)) {
$catalog = Get-ShipCatalog
$infoShip = $catalog | Where-Object { $_.Id -eq $infoId }
if ($infoShip) {
Write-Header $infoShip.Name
Write-Info $infoShip.Description
Write-Info "Price : $($infoShip.Price.ToString('N0')) credits"
Write-Info "Cargo Capacity: $($infoShip.CargoCapacity) units"
Write-Info "Fuel Capacity : $($infoShip.FuelCapacity) units"
Write-Info "Fuel per LY : $($infoShip.FuelPerLY)"
Write-Info "Hull HP : $($infoShip.HullHP)"
Write-Info "Speed Rating : $($infoShip.Speed)"
}
}
continue
}
$shipId = 0
if ([int]::TryParse($shipChoice, [ref]$shipId)) {
$ship = Buy-Ship -ShipId $shipId -PlayerCredits $credits
if ($ship) {
$credits -= $ship.Price
}
} else {
Write-Caution "Please enter a ship number."
}
}
# ============================================================
# AUTO-REFUEL OPTION
# ============================================================
Write-Host ""
$autoRefuelInput = Read-PlayerInput "Auto-refuel when arriving at a new system? (Y/n): "
$autoRefuel = $autoRefuelInput -ne 'n'
if ($autoRefuel) {
Write-Success "Auto-refuel ENABLED. You'll top off your tank on arrival."
} else {
Write-Info "Auto-refuel disabled. Refuel manually at each station."
}
# ============================================================
# GENERATE UNIVERSE
# ============================================================
Write-Host ""
Write-Info "Generating universe..."
$systems = New-Universe -SystemCount $SystemCount -Seed $Seed
# Ensure all systems form a single connected graph (no isolated clusters)
$worstRange = (Get-ShipCatalog | ForEach-Object { $_.FuelCapacity / $_.FuelPerLY } | Measure-Object -Minimum).Minimum
Ensure-Connectivity -Systems $systems -MaxRange $worstRange
# Start at a random system
$currentSystem = $systems[(Get-Random -Maximum $systems.Count)]
Write-Success "Your ship is docked at $($currentSystem.Name)."
Write-Info "The galaxy has $($systems.Count) known star systems to explore."
Write-Host ""
# ============================================================
# INITIALISE GAME STATE
# ============================================================
$gameState = [PSCustomObject]@{
PlayerName = $playerName
Credits = $credits
Ship = $ship
Fuel = $ship.FuelCapacity # start with a full tank
Cargo = [System.Collections.ArrayList]::new()
CurrentSystem = $currentSystem
Systems = $systems
TurnNumber = 1
GameOver = $false
AutoRefuel = $autoRefuel
}
}
# ================================================================
# MAIN GAME LOOP
# ================================================================
$mainMenuOptions = @(
@{ Key = '1'; Label = 'View Star Map' }
@{ Key = '2'; Label = 'Trade at Market' }
@{ Key = '3'; Label = 'Sell Cargo' }
@{ Key = '4'; Label = 'Travel to Another System' }
@{ Key = '5'; Label = 'Refuel Ship' }
@{ Key = '6'; Label = 'Ship Dealer (buy/upgrade)' }
@{ Key = '7'; Label = 'Status' }
@{ Key = '8'; Label = 'Toggle Auto-Refuel' }
@{ Key = '9'; Label = 'Save Game' }
@{ Key = 'Q'; Label = 'Quit Game' }
)
while (-not $gameState.GameOver) {
Clear-Host
Show-PlayerStatus $gameState
$action = Show-Menu -Title "Turn $($gameState.TurnNumber) - $($gameState.CurrentSystem.Name)" -Options $mainMenuOptions
switch ($action.ToUpper()) {
'1' {
Show-Map $gameState
}
'2' {
Clear-Host
Buy-Goods $gameState
Wait-ForKey
}
'3' {
Clear-Host
Sell-Goods $gameState
Wait-ForKey
}
'4' {
Clear-Host
$traveled = Show-TravelOptions $gameState
Wait-ForKey
}
'5' {
Clear-Host
Refuel-Ship $gameState
Wait-ForKey
}
'6' {
Clear-Host
Show-ShipDealer $gameState
Wait-ForKey
}
'7' {
Clear-Host
Show-PlayerStatus $gameState
Wait-ForKey
}
'8' {
$gameState.AutoRefuel = -not $gameState.AutoRefuel
if ($gameState.AutoRefuel) {
Write-Success "Auto-refuel ENABLED."
} else {
Write-Info "Auto-refuel DISABLED."
}
Wait-ForKey
}
'9' {
Clear-Host
$savePath = Save-Game $gameState
Write-Success "Game saved!"
Wait-ForKey
}
'Q' {
$confirm = Read-PlayerInput "Are you sure you want to quit? (Y/n): "
if ($confirm -ne 'n') {
$gameState.GameOver = $true
}
}
default { Write-Caution "Unknown command. Try again." }
}
}
# ================================================================
# GAME OVER
# ================================================================
Write-Host ""
Write-Header "Game Over"
Write-Info "Captain $($gameState.PlayerName)"
Write-Info "Final Credits : $($gameState.Credits.ToString('N0'))"
Write-Info "Ship : $($gameState.Ship.Name)"
Write-Info "Turns Played : $($gameState.TurnNumber)"
$cargoValue = 0
foreach ($item in $gameState.Cargo) {
$cargoValue += $item.PaidPrice
}
$netWorth = $gameState.Credits + $cargoValue + [int]($gameState.Ship.Price * 0.5)
Write-Info "Est. Net Worth: $($netWorth.ToString('N0')) credits"
Write-Host ""
Write-Success "Thanks for playing PowerTraders!"
Write-Host ""