… | |
… | |
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 | r_max = max (r_min, r_max); |
95 | |
80 | |
96 | 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 */ |
97 | |
82 | |
… | |
… | |
111 | /* |
96 | /* |
112 | * 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 |
113 | * for exp loss calculations for players changing religions. |
98 | * for exp loss calculations for players changing religions. |
114 | */ |
99 | */ |
115 | sint64 |
100 | sint64 |
116 | 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) |
117 | { |
102 | { |
118 | sint64 omin = r_min; |
|
|
119 | sint64 range = max (0, r_max - r_min + 1); |
103 | sint64 range = max (0, r_max - r_min + 1); |
120 | int base = range > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
104 | int base = range > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
121 | |
105 | |
122 | /* |
106 | /* |
123 | * Make a call to get two 32 bit unsigned random numbers, and just do |
107 | * Make a call to get two 32 bit unsigned random numbers, and just do |
… | |
… | |
134 | /* we have a winner */ |
118 | /* we have a winner */ |
135 | luck = luck > 0 ? 1 : -1; |
119 | luck = luck > 0 ? 1 : -1; |
136 | |
120 | |
137 | range -= luck; |
121 | range -= luck; |
138 | if (range < 1) |
122 | if (range < 1) |
139 | return omin; /*check again */ |
123 | return r_min; /*check again */ |
140 | |
124 | |
141 | if (goodbad) |
125 | if (prefer_high) |
142 | r_min += luck; |
126 | r_min += luck; |
143 | |
127 | |
144 | return (max (omin, min (r_max, (ran % range) + r_min))); |
128 | return clamp (ran % range + r_min, r_min, r_max); |
145 | } |
129 | } |
146 | } |
130 | } |
147 | |
131 | |
148 | return ran % range + r_min; |
132 | return ran % range + r_min; |
149 | } |
133 | } |
… | |
… | |
154 | * Generally, op should be the player/caster/hitter requesting the roll, |
138 | * Generally, op should be the player/caster/hitter requesting the roll, |
155 | * not the recipient (ie, the poor slob getting hit). |
139 | * not the recipient (ie, the poor slob getting hit). |
156 | * The args are num D size (ie 4d6) [garbled 20010916] |
140 | * The args are num D size (ie 4d6) [garbled 20010916] |
157 | */ |
141 | */ |
158 | int |
142 | int |
159 | die_roll (int num, int size, const object *op, int goodbad) |
143 | die_roll (int num, int size, const object *op, bool prefer_high) |
160 | { |
144 | { |
161 | int min_roll, luck, total, i, gotlucky; |
145 | int min_roll, luck, total, i, gotlucky; |
162 | |
146 | |
163 | int diff = size; |
147 | int diff = size; |
164 | min_roll = 1; |
148 | min_roll = 1; |
165 | luck = total = gotlucky = 0; |
149 | luck = total = gotlucky = 0; |
166 | int base = diff > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
150 | int base = diff > 2 ? 20 : 50; /* d2 and d3 are corner cases */ |
167 | |
151 | |
168 | if (size < 2 || diff < 1) |
152 | if (size < 2 || diff < 1) |
169 | { |
153 | { |
170 | LOG (llevError, "Calling die_roll with num=%d size=%d\n", num, size); |
154 | LOG (llevError | logBacktrace, "Calling die_roll with num=%d size=%d\n", num, size); |
171 | return num; /* avoids a float exception */ |
155 | return num; /* avoids a float exception */ |
172 | } |
156 | } |
173 | |
157 | |
174 | if (op->type == PLAYER) |
158 | if (op->type == PLAYER) |
175 | luck = op->stats.luck; |
159 | luck = op->stats.luck; |
… | |
… | |
182 | gotlucky++; |
166 | gotlucky++; |
183 | ((luck > 0) ? (luck = 1) : (luck = -1)); |
167 | ((luck > 0) ? (luck = 1) : (luck = -1)); |
184 | diff -= luck; |
168 | diff -= luck; |
185 | if (diff < 1) |
169 | if (diff < 1) |
186 | return (num); /*check again */ |
170 | return (num); /*check again */ |
187 | ((goodbad) ? (min_roll += luck) : (diff)); |
171 | ((prefer_high) ? (min_roll += luck) : (diff)); |
188 | total += max (1, min (size, rndm (diff) + min_roll)); |
172 | total += max (1, min (size, rndm (diff) + min_roll)); |
189 | } |
173 | } |
190 | else |
174 | else |
191 | total += rndm (size) + 1; |
175 | total += rndm (size) + 1; |
192 | } |
176 | } |