-
Notifications
You must be signed in to change notification settings - Fork 0
/
MWRandCom.cpp
260 lines (223 loc) · 8.9 KB
/
MWRandCom.cpp
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
/*===========================================================================
*
* File: Mwrandcom.CPP
* Author: Dave Humphrey ([email protected])
* Created On: May 22, 2003
*
* Description
*
*=========================================================================*/
/* Include Files */
#include "stdafx.h"
#include "mwrandcom.h"
/*===========================================================================
*
* Begin Local Definitions
*
*=========================================================================*/
DEFINE_FILE("MWRandCom.cpp");
/* Used to generate a random item ID string */
long l_ItemIDCount = 1;
DWORD l_ItemSerialNumber = 0x12345678;
bool l_HasSerialNumber = false;
/*===========================================================================
* End of Local Definitions
*=========================================================================*/
/*===========================================================================
*
* Function - void DefaultWeaponData (WeaponData);
*
* Initializes a weapon data structure.
*
*=========================================================================*/
void DefaultWeaponData (mwri_weapondata_t& WeaponData) {
WeaponData.Number = 10;
WeaponData.ChopMin1 = 1;
WeaponData.ChopMin2 = 10;
WeaponData.ChopMax1 = 10;
WeaponData.ChopMax2 = 50;
WeaponData.SlashMin1 = 1;
WeaponData.SlashMin2 = 15;
WeaponData.SlashMax1 = 15;
WeaponData.SlashMax2 = 75;
WeaponData.ThrustMin1 = 1;
WeaponData.ThrustMin2 = 10;
WeaponData.ThrustMax1 = 10;
WeaponData.ThrustMax2 = 40;
WeaponData.EnchantChance = 0.25f;
WeaponData.IgnoreChance = 0.90f;
WeaponData.Reach1 = 0.50f;
WeaponData.Reach2 = 2.0f;
WeaponData.Speed1 = 2.0f;
WeaponData.Speed2 = 0.5f;
WeaponData.Weight1 = 20.0f;
WeaponData.Weight2 = 0.5f;
WeaponData.Condition1 = 100;
WeaponData.Condition2 = 10000;
WeaponData.Enchant1 = 0;
WeaponData.Enchant2 = 200;
WeaponData.DamageFactor = 1.0f;
WeaponData.EnchantFactor = 1.0f;
WeaponData.WeightFactor = 1.0f;
WeaponData.ReachFactor = 1.0f;
WeaponData.SpeedFactor = 1.0f;
WeaponData.HealthFactor = 1.0f;
WeaponData.ChanceNone = 5;
WeaponData.OutputList = true;
WeaponData.UseShortNumberID = false;
WeaponData.Prefix[0] = NULL_CHAR;
}
/*===========================================================================
* End of Function DefaultWeaponData()
*=========================================================================*/
/*===========================================================================
*
* Function - void l_GetItemSerial (void);
*
* Makes the item serial number for the current session. Attempts to use
* the user's hard drive serial number if available, otherwise the current
* clock tick count.
*
*=========================================================================*/
void l_GetItemSerial (void) {
BOOL Result;
/* Attempt to get the drive c: serial number */
Result = GetVolumeInformation(_T("c:\\"), NULL, 0, &l_ItemSerialNumber, NULL, NULL, NULL, 0);
/* Generate a 'random' serial number if the previous call failed */
if (!Result) l_ItemSerialNumber = (DWORD) clock();
l_HasSerialNumber = true;
}
/*===========================================================================
* End of Function l_GetItemSerial()
*=========================================================================*/
/*===========================================================================
*
* Function - float MakeRandItemNumber (FromValue, ToValue, BellValue);
*
* Returns a random number in the given range ('From' can be smaller or larger
* than 'To'). How the number is generated depends on the input flags.
*
* Default
* Random number with equal probability along the entire range.
*
* BellValue = Midpoint of range.
*
*=========================================================================*/
float MakeRandItemNumber (const float FromValue, const float ToValue, const float BellValue) {
float Result;
int HalfResult;
float FromValue1 = FromValue;
float ToValue1 = ToValue;
/* Determine which 'half' to use */
HalfResult = (int) ((float) rand()/(float) RAND_MAX * 2);
if (HalfResult == 0) ToValue1 = BellValue;
if (HalfResult == 1) FromValue1 = BellValue;
if (FromValue1 == ToValue1)
Result = FromValue1;
else if (FromValue1 > ToValue1) {
Result = ((float) rand()/(float) RAND_MAX) * (FromValue1 - ToValue1) + ToValue1;
}
else {
Result = ((float) rand()/(float) RAND_MAX) * (ToValue1 - FromValue1) + FromValue1;
}
return (Result);
}
float MakeRandItemNumber (const float FromValue, const float ToValue) {
float Result;
if (FromValue == ToValue)
Result = FromValue;
else if (FromValue > ToValue) {
Result = ((float) rand()/(float) RAND_MAX) * (FromValue - ToValue) + ToValue;
}
else {
Result = ((float) rand()/(float) RAND_MAX) * (ToValue - FromValue) + FromValue;
}
return (Result);
}
/*===========================================================================
* End of Function MakeRandItemNumber()
*=========================================================================*/
/*===========================================================================
*
* Function - const TCHAR* MakeRandomItemId (pPrefix);
*
* Creates and returns a new random item ID string using a local static string.
* The new ID is a digit string at most 32 bytes in length and will be a
* unique ID for the current application. The input prefix string is added
* to the beginning of the ID if it is not NULL (8 bytes at most).
*
* The ID is composed of the following:
* bytes 0- 7: Current system time
* bytes 8-15: Drive c: serial number (or clock ticks)
* bytes 16-23: Incremented counter per application session
* bytes 24-31: Custom prefix string if present
*
*=========================================================================*/
const TCHAR* MakeRandomItemId (const TCHAR* pPrefix) {
static TCHAR s_Buffer[MWESM_ID_MAXSIZE+4];
time_t CurrentTime = time(NULL);
/* Attempt to get a serial number if required */
if (!l_HasSerialNumber) l_GetItemSerial();
/* Make the ID string */
if (pPrefix != NULL && *pPrefix != NULL_CHAR)
snprintf (s_Buffer, 31, _T("%.8s%08lX%08lX%08lX"), pPrefix, l_ItemIDCount, CurrentTime, l_ItemSerialNumber);
else
snprintf (s_Buffer, 31, _T("%lX%08lX%08lX"), l_ItemIDCount, CurrentTime, l_ItemSerialNumber);
l_ItemIDCount++;
//SystemLog.Printf ("Item ID = '%s'", s_Buffer);
return (s_Buffer);
}
/*===========================================================================
* End of Class Method MakeRandomItemId()
*=========================================================================*/
/*===========================================================================
*
* Function - const TCHAR* MakeNumberItemId8 (pPrefix);
*
* Makes an item ID of the format:
*
* bytes 0- 7: Incremented counter per application session
* bytes 8-31: Custom prefix string
*
* The returned string is stored in a local static buffer.
*
*=========================================================================*/
const TCHAR* MakeNumberItemId8 (const TCHAR* pPrefix) {
static TCHAR s_Buffer[MWESM_ID_MAXSIZE+4];
/* Make the ID string */
if (pPrefix != NULL && *pPrefix != NULL_CHAR)
snprintf (s_Buffer, 31, _T("%.24s%08lX"), pPrefix, l_ItemIDCount);
else
snprintf (s_Buffer, 31, _T("%08lX"), l_ItemIDCount);
l_ItemIDCount++;
//SystemLog.Printf ("Item ID = '%s'", s_Buffer);
return (s_Buffer);
}
const TCHAR* MakeNumberItemId4 (const TCHAR* pPrefix) {
static TCHAR s_Buffer[MWESM_ID_MAXSIZE+4];
/* Make the ID string */
if (pPrefix != NULL && *pPrefix != NULL_CHAR)
snprintf (s_Buffer, 31, _T("%.28s%04lX"), pPrefix, l_ItemIDCount);
else
snprintf (s_Buffer, 31, _T("%04lX"), l_ItemIDCount);
l_ItemIDCount++;
//SystemLog.Printf ("Item ID = '%s'", s_Buffer);
return (s_Buffer);
}
/*===========================================================================
* End of Function TCHAR* MakeNumberItemId()
*=========================================================================*/
/*===========================================================================
*
* Function - float SplitValue (FromValue, ToValue, Ratio);
*
* Returns a value from (FromValue to ToValue) according to the input ratio
* (0.0 to 1.0).
*
*=========================================================================*/
float SplitValue (const float FromValue, const float ToValue, const float Ratio) {
return (ToValue - FromValue) * Ratio + FromValue;
}
/*===========================================================================
* End of Function SplitValue()
*=========================================================================*/