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 (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2002 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 2002 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 1992 Frank Tore Johansen |
7 | * Copyright (©) 1992 Frank Tore Johansen |
7 | * |
8 | * |
8 | * Deliantra is free software: you can redistribute it and/or modify it under |
9 | * 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 |
10 | * 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 |
11 | * Free Software Foundation, either version 3 of the License, or (at your |
11 | * option) any later version. |
12 | * option) any later version. |
12 | * |
13 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
17 | * GNU General Public License for more details. |
17 | * |
18 | * |
18 | * You should have received a copy of the Affero GNU General Public License |
19 | * 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 |
20 | * and the GNU General Public License along with this program. If not, see |
20 | * <http://www.gnu.org/licenses/>. |
21 | * <http://www.gnu.org/licenses/>. |
21 | * |
22 | * |
22 | * The authors can be reached via e-mail to <support@deliantra.net> |
23 | * The authors can be reached via e-mail to <support@deliantra.net> |
23 | */ |
24 | */ |
24 | |
25 | |
25 | /* |
26 | /* |
26 | * compatibility functions for older (GPL) source code parts |
27 | * compatibility functions for older (GPL) source code parts |
… | |
… | |
34 | |
35 | |
35 | #include <global.h> |
36 | #include <global.h> |
36 | #include "define.h" |
37 | #include "define.h" |
37 | #include "path.h" |
38 | #include "path.h" |
38 | |
39 | |
39 | /* buf_overflow() - we don't want to exceed the buffer size of |
40 | /* buf_overflow() - we don't want to exceed the buffer size of |
40 | * buf1 by adding on buf2! Returns true if overflow will occur. |
41 | * buf1 by adding on buf2! Returns true if overflow will occur. |
41 | */ |
42 | */ |
42 | int |
43 | int |
43 | buf_overflow (const char *buf1, const char *buf2, int bufsize) |
44 | buf_overflow (const char *buf1, const char *buf2, int bufsize) |
44 | { |
45 | { |
… | |
… | |
74 | * not the recipient (ie, the poor slob getting hit). [garbled 20010916] |
75 | * not the recipient (ie, the poor slob getting hit). [garbled 20010916] |
75 | */ |
76 | */ |
76 | int |
77 | int |
77 | random_roll (int r_min, int r_max, const object *op, bool prefer_high) |
78 | random_roll (int r_min, int r_max, const object *op, bool prefer_high) |
78 | { |
79 | { |
79 | r_max = max (r_min, r_max); |
80 | if (r_min > r_max) |
|
|
81 | swap (r_min, r_max); |
80 | |
82 | |
|
|
83 | int range = r_max - r_min + 1; |
|
|
84 | int num = rndm (r_min, r_max); |
|
|
85 | |
|
|
86 | if (op->stats.luck) |
|
|
87 | { |
81 | int base = r_max - r_min > 1 ? 20 : 50; /* d2 and d3 are corner cases */ |
88 | int base = range > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
82 | |
89 | |
83 | if (op->type == PLAYER) |
|
|
84 | { |
|
|
85 | int luck = op->stats.luck; |
|
|
86 | |
|
|
87 | if (rndm (base) < min (10, abs (luck))) |
90 | if (rndm (base) < min (10, abs (op->stats.luck))) |
88 | { |
|
|
89 | //TODO: take luck into account |
|
|
90 | } |
91 | { |
91 | } |
92 | // we have a winner, increase/decrease number by one accordingly |
|
|
93 | int adjust = sign (op->stats.luck); |
92 | |
94 | |
93 | return rndm (r_min, r_max); |
95 | if (!prefer_high) |
|
|
96 | adjust = -adjust; |
|
|
97 | |
|
|
98 | num = clamp (num + adjust, r_min, r_max); |
|
|
99 | } |
|
|
100 | } |
|
|
101 | |
|
|
102 | return num; |
94 | } |
103 | } |
95 | |
104 | |
96 | /* |
105 | /* |
97 | * This is a 64 bit version of random_roll above. This is needed |
106 | * This is a 64 bit version of random_roll above. This is needed |
98 | * for exp loss calculations for players changing religions. |
107 | * for exp loss calculations for players changing religions. |
99 | */ |
108 | */ |
100 | sint64 |
109 | sint64 |
101 | random_roll64 (sint64 r_min, sint64 r_max, const object *op, bool prefer_high) |
110 | random_roll64 (sint64 r_min, sint64 r_max, const object *op, bool prefer_high) |
102 | { |
111 | { |
|
|
112 | if (r_min > r_max) |
|
|
113 | swap (r_min, r_max); |
|
|
114 | |
103 | sint64 range = max (0, r_max - r_min + 1); |
115 | sint64 range = r_max - r_min + 1; |
104 | int base = range > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
|
|
105 | |
|
|
106 | /* |
116 | /* |
107 | * Make a call to get two 32 bit unsigned random numbers, and just do |
117 | * Make a call to get two 32 bit unsigned random numbers, and just do |
108 | * a little bitshifting. |
118 | * a little bitshifting. |
109 | */ |
119 | */ |
110 | sint64 ran = (sint64) rndm.next () ^ ((sint64) rndm.next () << 31); |
120 | sint64 num = rndm.next () ^ (sint64 (rndm.next ()) << 31); |
|
|
121 | |
|
|
122 | num = num % range + r_min; |
111 | |
123 | |
112 | if (op->stats.luck) |
124 | if (op->stats.luck) |
113 | { |
125 | { |
114 | int luck = op->stats.luck; |
126 | int base = range > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
115 | |
127 | |
116 | if (rndm (base) < min (10, abs (luck))) |
128 | if (rndm (base) < min (10, abs (op->stats.luck))) |
117 | { |
129 | { |
118 | /* we have a winner */ |
130 | // we have a winner, increase/decrease number by one accordingly |
119 | luck = luck > 0 ? 1 : -1; |
131 | int adjust = sign (op->stats.luck); |
|
|
132 | |
|
|
133 | if (!prefer_high) |
|
|
134 | adjust = -adjust; |
120 | |
135 | |
121 | range -= luck; |
136 | num = clamp (num + adjust, r_min, r_max); |
122 | if (range < 1) |
|
|
123 | return r_min; /*check again */ |
|
|
124 | |
|
|
125 | if (prefer_high) |
|
|
126 | r_min += luck; |
|
|
127 | |
|
|
128 | return clamp (ran % range + r_min, r_min, r_max); |
|
|
129 | } |
137 | } |
130 | } |
138 | } |
131 | |
139 | |
132 | return ran % range + r_min; |
140 | return num; |
133 | } |
141 | } |
134 | |
142 | |
135 | /* |
143 | /* |
136 | * Roll a number of dice (2d3, 4d6). Uses op to determine luck, |
144 | * Roll a number of dice (2d3, 4d6). Uses op to determine luck, |
137 | * If goodbad is non-zero, luck increases the roll, if zero, it decreases. |
145 | * If goodbad is non-zero, luck increases the roll, if zero, it decreases. |