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,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2002 Mark Wedel & Crossfire Development Team |
5 | * Copyright (©) 2002 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 1992 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. |
12 | * |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
16 | * GNU General Public License for more details. |
17 | * |
17 | * |
18 | * You should have received a copy of the Affero GNU General Public License |
18 | * You should have received a copy of the Affero GNU General Public License |
19 | * and the GNU General Public License along with this program. If not, see |
19 | * and the GNU General Public License along with this program. If not, see |
20 | * <http://www.gnu.org/licenses/>. |
20 | * <http://www.gnu.org/licenses/>. |
21 | * |
21 | * |
22 | * The authors can be reached via e-mail to <support@deliantra.net> |
22 | * The authors can be reached via e-mail to <support@deliantra.net> |
23 | */ |
23 | */ |
24 | |
24 | |
25 | /* |
25 | /* |
26 | * compatibility functions for older (GPL) source code parts |
26 | * compatibility functions for older (GPL) source code parts |
… | |
… | |
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 | /* |
|
|
63 | * fatal() is meant to be called whenever a fatal signal is intercepted. |
|
|
64 | * It will call the emergency_save and the clean_tmp_files functions. |
|
|
65 | */ |
|
|
66 | //TODO: only one caller left |
|
|
67 | void |
|
|
68 | fatal (const char *msg) |
|
|
69 | { |
|
|
70 | LOG (llevError, "FATAL: %s\n", msg); |
|
|
71 | cleanup (msg, 1); |
|
|
72 | } |
57 | } |
73 | |
58 | |
74 | ///////////////////////////////////////////////////////////////////////////// |
59 | ///////////////////////////////////////////////////////////////////////////// |
75 | |
60 | |
76 | /* |
61 | /* |
… | |
… | |
87 | * 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. |
88 | * Generally, op should be the player/caster/hitter requesting the roll, |
73 | * Generally, op should be the player/caster/hitter requesting the roll, |
89 | * not the recipient (ie, the poor slob getting hit). [garbled 20010916] |
74 | * not the recipient (ie, the poor slob getting hit). [garbled 20010916] |
90 | */ |
75 | */ |
91 | int |
76 | int |
92 | 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) |
93 | { |
78 | { |
94 | r_max = max (r_min, r_max); |
79 | if (r_min > r_max) |
|
|
80 | swap (r_min, r_max); |
95 | |
81 | |
|
|
82 | int range = r_max - r_min + 1; |
|
|
83 | int num = rndm (r_min, r_max); |
|
|
84 | |
|
|
85 | if (op->stats.luck) |
|
|
86 | { |
96 | int base = r_max - r_min > 1 ? 20 : 50; /* d2 and d3 are corner cases */ |
87 | int base = range > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
97 | |
88 | |
98 | if (op->type == PLAYER) |
|
|
99 | { |
|
|
100 | int luck = op->stats.luck; |
|
|
101 | |
|
|
102 | if (rndm (base) < min (10, abs (luck))) |
89 | if (rndm (base) < min (10, abs (op->stats.luck))) |
103 | { |
|
|
104 | //TODO: take luck into account |
|
|
105 | } |
90 | { |
106 | } |
91 | // we have a winner, increase/decrease number by one accordingly |
|
|
92 | int adjust = sign (op->stats.luck); |
107 | |
93 | |
108 | return rndm (r_min, r_max); |
94 | if (!prefer_high) |
|
|
95 | adjust = -adjust; |
|
|
96 | |
|
|
97 | num = clamp (num + adjust, r_min, r_max); |
|
|
98 | } |
|
|
99 | } |
|
|
100 | |
|
|
101 | return num; |
109 | } |
102 | } |
110 | |
103 | |
111 | /* |
104 | /* |
112 | * This is a 64 bit version of random_roll above. This is needed |
105 | * This is a 64 bit version of random_roll above. This is needed |
113 | * for exp loss calculations for players changing religions. |
106 | * for exp loss calculations for players changing religions. |
114 | */ |
107 | */ |
115 | sint64 |
108 | sint64 |
116 | random_roll64 (sint64 r_min, sint64 r_max, const object *op, int goodbad) |
109 | random_roll64 (sint64 r_min, sint64 r_max, const object *op, bool prefer_high) |
117 | { |
110 | { |
118 | sint64 omin = r_min; |
111 | if (r_min > r_max) |
|
|
112 | swap (r_min, r_max); |
|
|
113 | |
119 | sint64 range = max (0, r_max - r_min + 1); |
114 | sint64 range = r_max - r_min + 1; |
120 | int base = range > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
|
|
121 | |
|
|
122 | /* |
115 | /* |
123 | * Make a call to get two 32 bit unsigned random numbers, and just to |
116 | * Make a call to get two 32 bit unsigned random numbers, and just do |
124 | * a little bitshifting. |
117 | * a little bitshifting. |
125 | */ |
118 | */ |
126 | sint64 ran = (sint64) rndm.next () ^ ((sint64) rndm.next () << 31); |
119 | sint64 num = rndm.next () ^ (sint64 (rndm.next ()) << 31); |
127 | |
120 | |
128 | if (op->type != PLAYER) |
121 | num = num % range + r_min; |
129 | return ((ran % range) + r_min); |
|
|
130 | |
122 | |
131 | int luck = op->stats.luck; |
123 | if (op->stats.luck) |
|
|
124 | { |
|
|
125 | int base = range > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
132 | |
126 | |
133 | if (rndm (base) < min (10, abs (luck))) |
127 | if (rndm (base) < min (10, abs (op->stats.luck))) |
|
|
128 | { |
|
|
129 | // we have a winner, increase/decrease number by one accordingly |
|
|
130 | int adjust = sign (op->stats.luck); |
|
|
131 | |
|
|
132 | if (!prefer_high) |
|
|
133 | adjust = -adjust; |
|
|
134 | |
|
|
135 | num = clamp (num + adjust, r_min, r_max); |
|
|
136 | } |
134 | { |
137 | } |
135 | /* we have a winner */ |
|
|
136 | ((luck > 0) ? (luck = 1) : (luck = -1)); |
|
|
137 | range -= luck; |
|
|
138 | if (range < 1) |
|
|
139 | return (omin); /*check again */ |
|
|
140 | |
138 | |
141 | ((goodbad) ? (r_min += luck) : (range)); |
139 | return num; |
142 | |
|
|
143 | return (max (omin, min (r_max, (ran % range) + r_min))); |
|
|
144 | } |
|
|
145 | |
|
|
146 | return ran % range + r_min; |
|
|
147 | } |
140 | } |
148 | |
141 | |
149 | /* |
142 | /* |
150 | * Roll a number of dice (2d3, 4d6). Uses op to determine luck, |
143 | * Roll a number of dice (2d3, 4d6). Uses op to determine luck, |
151 | * If goodbad is non-zero, luck increases the roll, if zero, it decreases. |
144 | * If goodbad is non-zero, luck increases the roll, if zero, it decreases. |
152 | * Generally, op should be the player/caster/hitter requesting the roll, |
145 | * Generally, op should be the player/caster/hitter requesting the roll, |
153 | * not the recipient (ie, the poor slob getting hit). |
146 | * not the recipient (ie, the poor slob getting hit). |
154 | * The args are num D size (ie 4d6) [garbled 20010916] |
147 | * The args are num D size (ie 4d6) [garbled 20010916] |
155 | */ |
148 | */ |
156 | int |
149 | int |
157 | die_roll (int num, int size, const object *op, int goodbad) |
150 | die_roll (int num, int size, const object *op, bool prefer_high) |
158 | { |
151 | { |
159 | int min_roll, luck, total, i, gotlucky; |
152 | int min_roll, luck, total, i, gotlucky; |
160 | |
153 | |
161 | int diff = size; |
154 | int diff = size; |
162 | min_roll = 1; |
155 | min_roll = 1; |
163 | luck = total = gotlucky = 0; |
156 | luck = total = gotlucky = 0; |
164 | int base = diff > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
157 | int base = diff > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
165 | |
158 | |
166 | if (size < 2 || diff < 1) |
159 | if (size < 2 || diff < 1) |
167 | { |
160 | { |
168 | LOG (llevError, "Calling die_roll with num=%d size=%d\n", num, size); |
161 | LOG (llevError | logBacktrace, "Calling die_roll with num=%d size=%d\n", num, size); |
169 | return num; /* avoids a float exception */ |
162 | return num; /* avoids a float exception */ |
170 | } |
163 | } |
171 | |
164 | |
172 | if (op->type == PLAYER) |
165 | if (op->type == PLAYER) |
173 | luck = op->stats.luck; |
166 | luck = op->stats.luck; |
… | |
… | |
180 | gotlucky++; |
173 | gotlucky++; |
181 | ((luck > 0) ? (luck = 1) : (luck = -1)); |
174 | ((luck > 0) ? (luck = 1) : (luck = -1)); |
182 | diff -= luck; |
175 | diff -= luck; |
183 | if (diff < 1) |
176 | if (diff < 1) |
184 | return (num); /*check again */ |
177 | return (num); /*check again */ |
185 | ((goodbad) ? (min_roll += luck) : (diff)); |
178 | ((prefer_high) ? (min_roll += luck) : (diff)); |
186 | total += max (1, min (size, rndm (diff) + min_roll)); |
179 | total += max (1, min (size, rndm (diff) + min_roll)); |
187 | } |
180 | } |
188 | else |
181 | else |
189 | total += rndm (size) + 1; |
182 | total += rndm (size) + 1; |
190 | } |
183 | } |
… | |
… | |
308 | path = path_combine (src, dst); |
301 | path = path_combine (src, dst); |
309 | path_normalize (path); |
302 | path_normalize (path); |
310 | return (path); |
303 | return (path); |
311 | } |
304 | } |
312 | |
305 | |
313 | /** |
|
|
314 | * open_and_uncompress() first searches for the original filename. If it exist, |
|
|
315 | * then it opens it and returns the file-pointer. |
|
|
316 | */ |
|
|
317 | FILE * |
|
|
318 | open_and_uncompress (const char *name, int flag, int *compressed) |
|
|
319 | { |
|
|
320 | *compressed = 0; |
|
|
321 | return fopen (name, "r"); |
|
|
322 | } |
|
|
323 | |
|
|
324 | /* |
|
|
325 | * See open_and_uncompress(). |
|
|
326 | */ |
|
|
327 | |
|
|
328 | void |
|
|
329 | close_and_delete (FILE * fp, int compressed) |
|
|
330 | { |
|
|
331 | fclose (fp); |
|
|
332 | } |
|
|
333 | |
|
|
334 | /* |
|
|
335 | * Strip out the media tags from a String. |
|
|
336 | * Warning the input string will contain the result string |
|
|
337 | */ |
|
|
338 | void |
|
|
339 | strip_media_tag (char *message) |
|
|
340 | { |
|
|
341 | int in_tag = 0; |
|
|
342 | char *dest; |
|
|
343 | char *src; |
|
|
344 | |
|
|
345 | src = dest = message; |
|
|
346 | while (*src != '\0') |
|
|
347 | { |
|
|
348 | if (*src == '[') |
|
|
349 | { |
|
|
350 | in_tag = 1; |
|
|
351 | } |
|
|
352 | else if (in_tag && (*src == ']')) |
|
|
353 | in_tag = 0; |
|
|
354 | else if (!in_tag) |
|
|
355 | { |
|
|
356 | *dest = *src; |
|
|
357 | dest++; |
|
|
358 | } |
|
|
359 | src++; |
|
|
360 | } |
|
|
361 | *dest = '\0'; |
|
|
362 | } |
|
|
363 | |
|
|
364 | #define EOL_SIZE (sizeof("\n")-1) |
306 | #define EOL_SIZE (sizeof("\n")-1) |
365 | void |
307 | void |
366 | strip_endline (char *buf) |
308 | strip_endline (char *buf) |
367 | { |
309 | { |
368 | if (strlen (buf) < sizeof ("\n")) |
310 | if (*buf && buf [strlen (buf) - 1] == '\n') |
369 | { |
|
|
370 | return; |
|
|
371 | } |
|
|
372 | if (!strcmp (buf + strlen (buf) - EOL_SIZE, "\n")) |
|
|
373 | buf[strlen (buf) - EOL_SIZE] = '\0'; |
311 | buf [strlen (buf) - 1] = '\0'; |
374 | } |
312 | } |
375 | |
313 | |
376 | /** |
314 | /** |
377 | * Replace in string src all occurrences of key by replacement. The resulting |
315 | * Replace in string src all occurrences of key by replacement. The resulting |
378 | * string is put into result; at most resultsize characters (including the |
316 | * string is put into result; at most resultsize characters (including the |