1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2005,2006,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
5 | * Copyright (©) 2002 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
6 | * Copyright (©) 1992 Frank Tore Johansen |
7 | * |
7 | * |
8 | * Deliantra is free software: you can redistribute it and/or modify it under |
8 | * Deliantra is free software: you can redistribute it and/or modify it under |
9 | * the terms of the Affero GNU General Public License as published by the |
9 | * the terms of the Affero GNU General Public License as published by the |
10 | * Free Software Foundation, either version 3 of the License, or (at your |
10 | * Free Software Foundation, either version 3 of the License, or (at your |
11 | * option) any later version. |
11 | * option) any later version. |
… | |
… | |
34 | |
34 | |
35 | #include <global.h> |
35 | #include <global.h> |
36 | #include "define.h" |
36 | #include "define.h" |
37 | #include "path.h" |
37 | #include "path.h" |
38 | |
38 | |
39 | |
|
|
40 | /* buf_overflow() - we don't want to exceed the buffer size of |
39 | /* buf_overflow() - we don't want to exceed the buffer size of |
41 | * buf1 by adding on buf2! Returns true if overflow will occur. |
40 | * buf1 by adding on buf2! Returns true if overflow will occur. |
42 | */ |
41 | */ |
43 | int |
42 | int |
44 | buf_overflow (const char *buf1, const char *buf2, int bufsize) |
43 | buf_overflow (const char *buf1, const char *buf2, int bufsize) |
… | |
… | |
53 | |
52 | |
54 | if ((len1 + len2) >= bufsize) |
53 | if ((len1 + len2) >= bufsize) |
55 | return 1; |
54 | return 1; |
56 | |
55 | |
57 | return 0; |
56 | return 0; |
58 | } |
|
|
59 | |
|
|
60 | ///////////////////////////////////////////////////////////////////////////// |
|
|
61 | |
|
|
62 | static const char *const fatalmsgs[80] = { |
|
|
63 | "Failed to allocate memory", |
|
|
64 | "Failed repeatedly to load maps", |
|
|
65 | "Hashtable for archetypes is too small", |
|
|
66 | "Too many errors" |
|
|
67 | }; |
|
|
68 | |
|
|
69 | /* |
|
|
70 | * fatal() is meant to be called whenever a fatal signal is intercepted. |
|
|
71 | * It will call the emergency_save and the clean_tmp_files functions. |
|
|
72 | */ |
|
|
73 | void |
|
|
74 | fatal (int err) |
|
|
75 | { |
|
|
76 | LOG (llevError, "Fatal: %s\n", fatalmsgs [err]); |
|
|
77 | cleanup (fatalmsgs[err], 1); |
|
|
78 | } |
57 | } |
79 | |
58 | |
80 | ///////////////////////////////////////////////////////////////////////////// |
59 | ///////////////////////////////////////////////////////////////////////////// |
81 | |
60 | |
82 | /* |
61 | /* |
… | |
… | |
93 | * and if goodbad is non-zero, luck increases the roll, if zero, it decreases. |
72 | * and if goodbad is non-zero, luck increases the roll, if zero, it decreases. |
94 | * Generally, op should be the player/caster/hitter requesting the roll, |
73 | * Generally, op should be the player/caster/hitter requesting the roll, |
95 | * not the recipient (ie, the poor slob getting hit). [garbled 20010916] |
74 | * not the recipient (ie, the poor slob getting hit). [garbled 20010916] |
96 | */ |
75 | */ |
97 | int |
76 | int |
98 | random_roll (int r_min, int r_max, const object *op, int goodbad) |
77 | random_roll (int r_min, int r_max, const object *op, bool prefer_high) |
99 | { |
78 | { |
100 | r_max = max (r_min, r_max); |
79 | r_max = max (r_min, r_max); |
101 | |
80 | |
102 | int base = r_max - r_min > 1 ? 20 : 50; /* d2 and d3 are corner cases */ |
81 | int base = r_max - r_min > 1 ? 20 : 50; /* d2 and d3 are corner cases */ |
103 | |
82 | |
… | |
… | |
117 | /* |
96 | /* |
118 | * This is a 64 bit version of random_roll above. This is needed |
97 | * This is a 64 bit version of random_roll above. This is needed |
119 | * for exp loss calculations for players changing religions. |
98 | * for exp loss calculations for players changing religions. |
120 | */ |
99 | */ |
121 | sint64 |
100 | sint64 |
122 | random_roll64 (sint64 r_min, sint64 r_max, const object *op, int goodbad) |
101 | random_roll64 (sint64 r_min, sint64 r_max, const object *op, bool prefer_high) |
123 | { |
102 | { |
124 | sint64 omin = r_min; |
103 | sint64 omin = r_min; |
125 | sint64 range = max (0, r_max - r_min + 1); |
104 | sint64 range = max (0, r_max - r_min + 1); |
126 | int base = range > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
105 | int base = range > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
127 | |
106 | |
128 | /* |
107 | /* |
129 | * Make a call to get two 32 bit unsigned random numbers, and just to |
108 | * Make a call to get two 32 bit unsigned random numbers, and just do |
130 | * a little bitshifting. |
109 | * a little bitshifting. |
131 | */ |
110 | */ |
132 | sint64 ran = (sint64) rndm.next () ^ ((sint64) rndm.next () << 31); |
111 | sint64 ran = (sint64) rndm.next () ^ ((sint64) rndm.next () << 31); |
133 | |
112 | |
134 | if (op->type != PLAYER) |
113 | if (op->stats.luck) |
135 | return ((ran % range) + r_min); |
114 | { |
136 | |
|
|
137 | int luck = op->stats.luck; |
115 | int luck = op->stats.luck; |
138 | |
116 | |
139 | if (rndm (base) < min (10, abs (luck))) |
117 | if (rndm (base) < min (10, abs (luck))) |
140 | { |
118 | { |
141 | /* we have a winner */ |
119 | /* we have a winner */ |
142 | ((luck > 0) ? (luck = 1) : (luck = -1)); |
120 | luck = luck > 0 ? 1 : -1; |
|
|
121 | |
143 | range -= luck; |
122 | range -= luck; |
144 | if (range < 1) |
123 | if (range < 1) |
145 | return (omin); /*check again */ |
124 | return omin; /*check again */ |
146 | |
125 | |
147 | ((goodbad) ? (r_min += luck) : (range)); |
126 | if (prefer_high) |
|
|
127 | r_min += luck; |
148 | |
128 | |
149 | return (max (omin, min (r_max, (ran % range) + r_min))); |
129 | return clamp (ran % range + r_min, omin, r_max); |
|
|
130 | } |
150 | } |
131 | } |
151 | |
132 | |
152 | return ran % range + r_min; |
133 | return ran % range + r_min; |
153 | } |
134 | } |
154 | |
135 | |
… | |
… | |
158 | * Generally, op should be the player/caster/hitter requesting the roll, |
139 | * Generally, op should be the player/caster/hitter requesting the roll, |
159 | * not the recipient (ie, the poor slob getting hit). |
140 | * not the recipient (ie, the poor slob getting hit). |
160 | * The args are num D size (ie 4d6) [garbled 20010916] |
141 | * The args are num D size (ie 4d6) [garbled 20010916] |
161 | */ |
142 | */ |
162 | int |
143 | int |
163 | die_roll (int num, int size, const object *op, int goodbad) |
144 | die_roll (int num, int size, const object *op, bool prefer_high) |
164 | { |
145 | { |
165 | int min, luck, total, i, gotlucky; |
146 | int min_roll, luck, total, i, gotlucky; |
166 | |
147 | |
167 | int diff = size; |
148 | int diff = size; |
168 | min = 1; |
149 | min_roll = 1; |
169 | luck = total = gotlucky = 0; |
150 | luck = total = gotlucky = 0; |
170 | int base = diff > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
151 | int base = diff > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
171 | |
152 | |
172 | if (size < 2 || diff < 1) |
153 | if (size < 2 || diff < 1) |
173 | { |
154 | { |
… | |
… | |
178 | if (op->type == PLAYER) |
159 | if (op->type == PLAYER) |
179 | luck = op->stats.luck; |
160 | luck = op->stats.luck; |
180 | |
161 | |
181 | for (i = 0; i < num; i++) |
162 | for (i = 0; i < num; i++) |
182 | { |
163 | { |
183 | if (rndm (base) < MIN (10, abs (luck)) && !gotlucky) |
164 | if (rndm (base) < min (10, abs (luck)) && !gotlucky) |
184 | { |
165 | { |
185 | /* we have a winner */ |
166 | /* we have a winner */ |
186 | gotlucky++; |
167 | gotlucky++; |
187 | ((luck > 0) ? (luck = 1) : (luck = -1)); |
168 | ((luck > 0) ? (luck = 1) : (luck = -1)); |
188 | diff -= luck; |
169 | diff -= luck; |
189 | if (diff < 1) |
170 | if (diff < 1) |
190 | return (num); /*check again */ |
171 | return (num); /*check again */ |
191 | ((goodbad) ? (min += luck) : (diff)); |
172 | ((prefer_high) ? (min_roll += luck) : (diff)); |
192 | total += MAX (1, MIN (size, rndm (diff) + min)); |
173 | total += max (1, min (size, rndm (diff) + min_roll)); |
193 | } |
174 | } |
194 | else |
175 | else |
195 | total += rndm (size) + 1; |
176 | total += rndm (size) + 1; |
196 | } |
177 | } |
197 | |
178 | |
… | |
… | |
314 | path = path_combine (src, dst); |
295 | path = path_combine (src, dst); |
315 | path_normalize (path); |
296 | path_normalize (path); |
316 | return (path); |
297 | return (path); |
317 | } |
298 | } |
318 | |
299 | |
319 | /** |
|
|
320 | * open_and_uncompress() first searches for the original filename. If it exist, |
|
|
321 | * then it opens it and returns the file-pointer. |
|
|
322 | */ |
|
|
323 | FILE * |
|
|
324 | open_and_uncompress (const char *name, int flag, int *compressed) |
|
|
325 | { |
|
|
326 | *compressed = 0; |
|
|
327 | return fopen (name, "r"); |
|
|
328 | } |
|
|
329 | |
|
|
330 | /* |
|
|
331 | * See open_and_uncompress(). |
|
|
332 | */ |
|
|
333 | |
|
|
334 | void |
|
|
335 | close_and_delete (FILE * fp, int compressed) |
|
|
336 | { |
|
|
337 | fclose (fp); |
|
|
338 | } |
|
|
339 | |
|
|
340 | /* |
|
|
341 | * Strip out the media tags from a String. |
|
|
342 | * Warning the input string will contain the result string |
|
|
343 | */ |
|
|
344 | void |
|
|
345 | strip_media_tag (char *message) |
|
|
346 | { |
|
|
347 | int in_tag = 0; |
|
|
348 | char *dest; |
|
|
349 | char *src; |
|
|
350 | |
|
|
351 | src = dest = message; |
|
|
352 | while (*src != '\0') |
|
|
353 | { |
|
|
354 | if (*src == '[') |
|
|
355 | { |
|
|
356 | in_tag = 1; |
|
|
357 | } |
|
|
358 | else if (in_tag && (*src == ']')) |
|
|
359 | in_tag = 0; |
|
|
360 | else if (!in_tag) |
|
|
361 | { |
|
|
362 | *dest = *src; |
|
|
363 | dest++; |
|
|
364 | } |
|
|
365 | src++; |
|
|
366 | } |
|
|
367 | *dest = '\0'; |
|
|
368 | } |
|
|
369 | |
|
|
370 | #define EOL_SIZE (sizeof("\n")-1) |
300 | #define EOL_SIZE (sizeof("\n")-1) |
371 | void |
301 | void |
372 | strip_endline (char *buf) |
302 | strip_endline (char *buf) |
373 | { |
303 | { |
374 | if (strlen (buf) < sizeof ("\n")) |
304 | if (*buf && buf [strlen (buf) - 1] == '\n') |
375 | { |
|
|
376 | return; |
|
|
377 | } |
|
|
378 | if (!strcmp (buf + strlen (buf) - EOL_SIZE, "\n")) |
|
|
379 | buf[strlen (buf) - EOL_SIZE] = '\0'; |
305 | buf [strlen (buf) - 1] = '\0'; |
380 | } |
306 | } |
381 | |
307 | |
382 | /** |
308 | /** |
383 | * Replace in string src all occurrences of key by replacement. The resulting |
309 | * Replace in string src all occurrences of key by replacement. The resulting |
384 | * string is put into result; at most resultsize characters (including the |
310 | * string is put into result; at most resultsize characters (including the |