-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathFunctions.cs
361 lines (296 loc) · 9.31 KB
/
Functions.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
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
/*******************************************************************************
*
* Space Trader for Windows 2.01
*
* Copyright (C) 2008 Jay French, All Rights Reserved
*
* Additional coding by David Pierron
* Original coding by Pieter Spronck, Sam Anderson, Samuel Goldstein, Matt Lee
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* If you'd like a copy of the GNU General Public License, go to
* http://www.gnu.org/copyleft/gpl.html.
*
* You can contact the author at [email protected]
*
******************************************************************************/
using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Permissions;
using Microsoft.Win32;
[assembly: RegistryPermissionAttribute(SecurityAction.RequestMinimum, ViewAndModify = "HKEY_CURRENT_USER")]
namespace Fryz.Apps.SpaceTrader
{
public class Functions
{
#region Static Variable/Constant Declarations
private static Random rand = new Random();
private const long DEFSEEDX = 521288629;
private const long DEFSEEDY = 362436069;
private const int MAX_WORD = 65535;
private static long SeedX = DEFSEEDX;
private static long SeedY = DEFSEEDY;
#endregion
#region Methods
public static int AdjustSkillForDifficulty(int skill)
{
switch (Game.CurrentGame.Difficulty)
{
case Difficulty.Beginner:
case Difficulty.Easy:
skill++;
break;
case Difficulty.Hard:
case Difficulty.Impossible:
skill--;
break;
default:
break;
}
return skill;
}
public static string[] ArrayListToStringArray(ArrayList list)
{
string[] items = new string[list.Count];
for (int i = 0; i < items.Length; i++)
items[i] = (string)list[i];
return items;
}
public static int Distance(StarSystem a, StarSystem b)
{
return (int)Math.Floor(Math.Sqrt(Math.Pow(a.X - b.X, 2) + Math.Pow(a.Y - b.Y, 2)));
}
public static int Distance(StarSystem a, int x, int y)
{
return (int)Math.Floor(Math.Sqrt(Math.Pow(a.X - x, 2) + Math.Pow(a.Y - y, 2)));
}
private static void DrawPartialImage(Graphics g, Image img, int start, int stop)
{
g.DrawImage(img, 2 + start, 2, new Rectangle(start, 0, stop - start, img.Height), GraphicsUnit.Pixel);
}
public static string FormatNumber(int num)
{
return String.Format("{0:n0}", num);
}
public static string FormatList(string[] listItems)
{
return StringVars(Strings.ListStrings[listItems.Length], listItems);
}
public static string FormatMoney(int num)
{
return String.Format("{0:n0} cr.", num);
}
public static string FormatPercent(int num)
{
return String.Format("{0:n0}%", num);
}
public static int GetColumnOfFirstNonWhitePixel(Image image, int direction)
{
Bitmap bitmap = new Bitmap(image);
int step = direction < 0 ? -1 : 1;
int col = step > 0 ? 0 : bitmap.Width - 1;
int stop = step > 0 ? bitmap.Width : -1;
for (; col != stop; col += step)
{
for (int row = 0; row < bitmap.Height; row++)
{
if (bitmap.GetPixel(col, row).ToArgb() != 0)
return col;
}
}
return -1;
}
public static HighScoreRecord[] GetHighScores(System.Windows.Forms.IWin32Window owner)
{
HighScoreRecord[] highScores = new HighScoreRecord[3];
object obj = LoadFile(Consts.HighScoreFile, true, owner);
if (obj != null)
highScores = (HighScoreRecord[])STSerializableObject.ArrayListToArray((ArrayList)obj, "HighScoreRecord");
return highScores;
}
public static int GetRandom(int max)
{
return GetRandom(0, max);
}
public static int GetRandom(int min, int max)
{
return rand.Next(min, max);
}
// *************************************************************************
// Pieter's new random functions, tweaked a bit by SjG
// *************************************************************************
public static int GetRandom2(int max)
{
return (int)(Rand() % max);
}
public static RegistryKey GetRegistryKey()
{
return Registry.CurrentUser.OpenSubKey("Software", true).CreateSubKey("FrenchFryz").CreateSubKey("SpaceTrader");
}
public static bool IsInt(string toParse)
{
bool isInt = true;
try
{
int.Parse(toParse);
}
catch (Exception)
{
isInt = false;
}
return isInt;
}
public static object LoadFile(string fileName, bool ignoreMissingFile, System.Windows.Forms.IWin32Window owner)
{
object obj = null;
FileStream inStream = null;
try
{
inStream = new FileStream(fileName, FileMode.Open);
obj = (new BinaryFormatter()).Deserialize(inStream);
inStream.Close();
inStream = null;
}
catch (FileNotFoundException ex)
{
if (!ignoreMissingFile)
FormAlert.Alert(AlertType.FileErrorOpen, owner, fileName, ex.Message);
}
catch (IOException ex)
{
FormAlert.Alert(AlertType.FileErrorOpen, owner, fileName, ex.Message);
}
catch (System.Runtime.Serialization.SerializationException)
{
FormAlert.Alert(AlertType.FileErrorOpen, owner, fileName, Strings.FileFormatBad);
}
finally
{
if (inStream != null)
inStream.Close();
}
return obj;
}
public static string Multiples(int num, string unit)
{
return FormatNumber(num) + " " + unit + (num == 1 ? "" : "s");
}
public static void PaintShipImage(Ship ship, Graphics g, Color backgroundColor)
{
int x = Consts.ShipImageOffsets[(int)ship.Type].X;
int width = Consts.ShipImageOffsets[(int)ship.Type].Width;
int startDamage = x + width - ship.Hull * width / ship.HullStrength;
int startShield = x + width + 2 - (ship.ShieldStrength > 0 ? ship.ShieldCharge * (width + 4) /
ship.ShieldStrength : 0);
g.Clear(backgroundColor);
if (startDamage > x)
{
if (startShield > x)
DrawPartialImage(g, ship.ImageDamaged, x, Math.Min(startDamage, startShield));
if (startShield < startDamage)
DrawPartialImage(g, ship.ImageDamagedWithShields, startShield, startDamage);
}
if (startShield > startDamage)
DrawPartialImage(g, ship.Image, startDamage, startShield);
if (startShield < x + width + 2)
DrawPartialImage(g, ship.ImageWithShields, startShield, x + width + 2);
}
private static long Rand()
{
const int a = 18000;
const int b = 30903;
SeedX = a*(SeedX&MAX_WORD) + (SeedX>>16);
SeedY = b*(SeedY&MAX_WORD) + (SeedY>>16);
return ((SeedX<<16) + (SeedY&MAX_WORD));
}
public static int RandomSkill()
{
return 1 + GetRandom(5) + GetRandom(6);
}
public static void RandSeed(int seed1, int seed2)
{
if (seed1 > 0)
SeedX = seed1; /* use default seeds if parameter is 0 */
else
SeedX = DEFSEEDX;
if (seed2 > 0)
SeedY = seed2;
else
SeedY = DEFSEEDY;
}
public static bool SaveFile(string fileName, object toSerialize, System.Windows.Forms.IWin32Window owner)
{
FileStream outStream = null;
bool saveOk = false;
try
{
outStream = new FileStream(fileName, FileMode.Create);
(new BinaryFormatter()).Serialize(outStream, toSerialize);
outStream.Close();
outStream = null;
saveOk = true;
}
catch (IOException ex)
{
FormAlert.Alert(AlertType.FileErrorSave, owner, fileName, ex.Message);
}
finally
{
if (outStream != null)
outStream.Close();
}
return saveOk;
}
public static string StringVars(string toParse, string[] vars)
{
string parsed = toParse;
for (int i = 0; i < vars.Length; i++)
parsed = parsed.Replace("^" + (i + 1).ToString(), vars[i]);
return parsed;
}
public static string StringVars(string toParse, string var1)
{
return StringVars(toParse, new string[] { var1 });
}
public static string StringVars(string toParse, string var1, string var2)
{
return StringVars(toParse, new string[] { var1, var2 });
}
// *************************************************************************
// Returns true if there exists a wormhole from a to b.
// If b < 0, then return true if there exists a wormhole
// at all from a.
// *************************************************************************
public static bool WormholeExists(int a, int b)
{
int[] wormholes = Game.CurrentGame.Wormholes;
int i = Array.IndexOf(wormholes, a);
return (i >= 0 && (b < 0 || wormholes[(i + 1) % wormholes.Length] == b));
}
public static bool WormholeExists(StarSystem a, StarSystem b)
{
StarSystem[] universe = Game.CurrentGame.Universe;
int[] wormholes = Game.CurrentGame.Wormholes;
int i = Array.IndexOf(wormholes, (int)a.Id);
return (i >= 0 && (universe[wormholes[(i + 1) % wormholes.Length]] == b));
}
public static StarSystem WormholeTarget(int a)
{
int[] wormholes = Game.CurrentGame.Wormholes;
int i = Array.IndexOf(wormholes, a);
return (i >= 0 ? (Game.CurrentGame.Universe[wormholes[(i + 1) % wormholes.Length]]) : null);
}
#endregion
}
}