-
Notifications
You must be signed in to change notification settings - Fork 2
/
GameState.cs
296 lines (269 loc) · 8.29 KB
/
GameState.cs
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
// GameState.cs
/*
* This class stores the entire state of the current game session.
* Everything is kept in one place to make it easy to save/restore
* the game and to implement the Undo feature.
*
*/
using System;
using System.IO;
using System.Collections;
namespace Rails
{
public class GameState
{
public const int MaxTime = 1440; // minutes per day
public const int MaxSpend = 20; // maximum spend per day
public const int DefaultFundsGoal = 250; // money needed to win
public int NumPlayers; // how many players are there
public int Turn; // what turn is it
public int CurrentPlayer; // who's turn is it
public Player[] PlayerInfo; // about the players
public int[,,] Track; // crayon markings
public int AccumCost; // how much money has been spent this turn
public int AccumTime; // how much time has been spent this turn
public int StartingAccumTime; // how much excess time was passed on from previous day
public ArrayList Winners; // who won? null = nobody yet
public bool[] CityIncentive; // which cities are incentive cities
public bool[] CityWasVisited; // for first-to-city bonuses
public int[] Availability; // how many of each type of commodity are available
public bool[] UseTrack; // which players' tracks can be ridden
public double DisasterProbability; // the probability of a disaster per player turn
public Deck DisasterDeck; // disaster events in random order
public Deck DisasterPlayer; // players affected by disasters, in random order
public Deck DisasterSeas; // seas affected by disasters, in random order
public Deck DisasterRivers; // rivers affected by disasters, in random order
public bool DisableTax; // disable excess profit tax after someone reaches 250
// read the game state from the game save file
public GameState(BinaryReader reader, Map map)
{
int version = reader.ReadInt32();
Turn = reader.ReadInt32();
NumPlayers = reader.ReadInt32();
CurrentPlayer = reader.ReadInt32();
PlayerInfo = new Player[NumPlayers];
for(int i=0; i<NumPlayers; i++)
PlayerInfo[i] = new Player(reader);
AccumCost = reader.ReadInt32();
AccumTime = reader.ReadInt32();
int nwinners = reader.ReadInt32();
if (nwinners != 0)
{
Winners = new ArrayList();
for (int i=0; i<nwinners; i++)
Winners.Add(reader.ReadInt32());
}
int width = reader.ReadInt32();
int height = reader.ReadInt32();
Track = new int[width, height, 6];
for (int x=0; x<width; x++)
for (int y=0; y<height; y++)
{
int t = reader.ReadInt32();
for (int d=5; d>=0; d--)
{
Track[x, y, d] = (t & 7) - 1;
t >>= 3;
}
}
int cityCount;
if (version < 2)
cityCount = 48;
else
cityCount = reader.ReadInt32();
CityIncentive = new Boolean[cityCount];
CityWasVisited = new Boolean[cityCount];
for (int i=0; i<cityCount; i++)
{
CityIncentive[i] = reader.ReadBoolean();
CityWasVisited[i] = reader.ReadBoolean();
}
Availability = new int[Products.Count];
for (int i=0; i<Products.Count; i++)
Availability[i] = reader.ReadInt32();
UseTrack = new bool[NumPlayers + 1];
UseTrack[NumPlayers] = true; // nationalized track
if (version >= 1)
{
for (int i=0; i<this.NumPlayers; i++)
UseTrack[i] = reader.ReadBoolean();
}
else
UseTrack[CurrentPlayer] = true;
if (version >= 3)
{
DisasterProbability = reader.ReadDouble();
DisasterDeck = new Deck(reader);
DisasterPlayer = new Deck(reader);
DisasterSeas = new Deck(reader);
DisasterRivers = new Deck(reader);
DisableTax = reader.ReadBoolean();
}
else
{
DisasterProbability = 0.0567;
DisasterDeck = new Deck(Disaster.DisasterCount);
DisasterPlayer = new Deck(NumPlayers);
DisasterSeas = new Deck(map.Seas.Length);
DisasterRivers = new Deck(map.Rivers.Count);
// DisableTax = false;
}
if (version == 4 || version == 5)
{
// Journal data
throw new ApplicationException(Resource.GetString("GameState.VersionNotSupported"));
}
if (version >= 5)
StartingAccumTime = reader.ReadInt32();
else
StartingAccumTime = 0;
}
// write the game state to the game save file
public void Save(BinaryWriter writer, Map map)
{
writer.Write((int) 6); // version
writer.Write(Turn);
writer.Write(NumPlayers);
writer.Write(CurrentPlayer);
foreach (Player player in PlayerInfo)
player.Save(writer);
writer.Write(AccumCost);
writer.Write(AccumTime);
if (Winners == null)
writer.Write((int) 0);
else
{
writer.Write(Winners.Count);
foreach (object winner in Winners)
writer.Write((int) winner);
}
writer.Write(Track.GetLength(0));
writer.Write(Track.GetLength(1));
for (int x=0; x<Track.GetLength(0); x++)
for (int y=0; y<Track.GetLength(1); y++)
{
int t = 0;
for (int d=0; d<6; d++)
t = (t << 3) | (Track[x, y, d] + 1);
writer.Write(t);
}
writer.Write(map.CityCount);
for (int i=0; i<map.CityCount; i++)
{
writer.Write(CityIncentive[i]);
writer.Write(CityWasVisited[i]);
}
for (int i=0; i<Products.Count; i++)
writer.Write(Availability[i]);
for (int i=0; i<this.NumPlayers; i++)
writer.Write(UseTrack[i]);
writer.Write(DisasterProbability);
DisasterDeck.Save(writer);
DisasterPlayer.Save(writer);
DisasterSeas.Save(writer);
DisasterRivers.Save(writer);
writer.Write(DisableTax);
writer.Write(StartingAccumTime);
}
// initialize the game state
public GameState(Player[] playerList, Map map, Options options, Random rand)
{
#if TEST
if (playerList == null)
{
playerList = new Player[3];
playerList[0] = new Player(0, false, null);
playerList[1] = new Player(1, false, null);
playerList[2] = new Player(2, false, null);
}
#endif
NumPlayers = playerList.Length;
// CurrentPlayer = 0;
PlayerInfo = playerList;
foreach (Player player in PlayerInfo)
player.Reset(map, options.FastStart);
Track = new int[map.GridSize.Width, map.GridSize.Height, 6];
for (int x=0; x<Track.GetLength(0); x++)
for (int y=0; y<Track.GetLength(1); y++)
for (int d=0; d<Track.GetLength(2); d++)
Track[x, y, d] = -1;
// AccumCost = 0;
// AccumTime = 0;
// Winners = null;
CityIncentive = new bool[map.CityCount];
CityWasVisited = new bool[map.CityCount];
if (options.CityIncentives)
{
int n = 0;
while (n < 8)
{
int i = rand.Next(map.NumCapitals, map.CityCount - 1);
if (!CityIncentive[i]) n++;
CityIncentive[i] = true;
}
}
Availability = new int[Products.Count];
int navail;
if (options.LimitedCommodities)
navail = (NumPlayers + 1) / 2;
else
navail = 1000;
for (int i=0; i<Products.Count; i++)
Availability[i] = navail;
UseTrack = new bool[NumPlayers + 1];
UseTrack[CurrentPlayer] = true;
UseTrack[NumPlayers] = true; // nationalized track
// DisasterProbability = 0.0;
DisasterDeck = new Deck(Disaster.DisasterCount);
DisasterPlayer = new Deck(NumPlayers);
DisasterSeas = new Deck(map.Seas.Length);
DisasterRivers = new Deck(map.Rivers.Count);
Turn = 1;
}
public Player ThisPlayer
{
get { return PlayerInfo[CurrentPlayer]; }
}
public void MovePlayerTo(int x, int y)
{
PlayerInfo[CurrentPlayer].X = x;
PlayerInfo[CurrentPlayer].Y = y;
}
public void NextPlayer()
{
if (!ThisPlayer.LoseTurn)
{
ThisPlayer.ExcessTime = AccumTime - MaxTime;
if (ThisPlayer.ExcessTime < 0)
ThisPlayer.ExcessTime = 0;
}
ThisPlayer.LoseTurn = false;
CurrentPlayer = (CurrentPlayer + 1) % NumPlayers;
if (CurrentPlayer == 0)
{
Turn++;
}
AccumCost = 0;
StartingAccumTime = AccumTime = ThisPlayer.ExcessTime;
for (int i=0; i<NumPlayers; i++)
UseTrack[i] = (i == CurrentPlayer);
}
public int ReserveCommodity(int i)
{
Availability[i]--;
return i;
}
public void ReleaseCommodity(ref int i)
{
Availability[i]++;
i = -1;
}
public bool NoMoreTime
{
get
{
return this.AccumTime >= MaxTime;
}
}
}
}