ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/compat.C
(Generate patch)

Comparing deliantra/server/common/compat.C (file contents):
Revision 1.9 by root, Thu Apr 15 22:11:47 2010 UTC vs.
Revision 1.11 by root, Thu Apr 29 15:59:09 2010 UTC

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 */
43int 42int
44buf_overflow (const char *buf1, const char *buf2, int bufsize) 43buf_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
67void
68fatal (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 */
91int 76int
92random_roll (int r_min, int r_max, const object *op, int goodbad) 77random_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 */
115sint64 100sint64
116random_roll64 (sint64 r_min, sint64 r_max, const object *op, int goodbad) 101random_roll64 (sint64 r_min, sint64 r_max, const object *op, bool prefer_high)
117{ 102{
118 sint64 omin = r_min; 103 sint64 omin = r_min;
119 sint64 range = max (0, r_max - r_min + 1); 104 sint64 range = max (0, r_max - r_min + 1);
120 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 */
121 106
122 /* 107 /*
123 * 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
124 * a little bitshifting. 109 * a little bitshifting.
125 */ 110 */
126 sint64 ran = (sint64) rndm.next () ^ ((sint64) rndm.next () << 31); 111 sint64 ran = (sint64) rndm.next () ^ ((sint64) rndm.next () << 31);
127 112
128 if (op->type != PLAYER) 113 if (op->stats.luck)
129 return ((ran % range) + r_min); 114 {
130
131 int luck = op->stats.luck; 115 int luck = op->stats.luck;
132 116
133 if (rndm (base) < min (10, abs (luck))) 117 if (rndm (base) < min (10, abs (luck)))
134 { 118 {
135 /* we have a winner */ 119 /* we have a winner */
136 ((luck > 0) ? (luck = 1) : (luck = -1)); 120 luck = luck > 0 ? 1 : -1;
121
137 range -= luck; 122 range -= luck;
138 if (range < 1) 123 if (range < 1)
139 return (omin); /*check again */ 124 return omin; /*check again */
140 125
141 ((goodbad) ? (r_min += luck) : (range)); 126 if (prefer_high)
127 r_min += luck;
142 128
143 return (max (omin, min (r_max, (ran % range) + r_min))); 129 return clamp (ran % range + r_min, omin, r_max);
130 }
144 } 131 }
145 132
146 return ran % range + r_min; 133 return ran % range + r_min;
147} 134}
148 135
152 * Generally, op should be the player/caster/hitter requesting the roll, 139 * Generally, op should be the player/caster/hitter requesting the roll,
153 * not the recipient (ie, the poor slob getting hit). 140 * not the recipient (ie, the poor slob getting hit).
154 * The args are num D size (ie 4d6) [garbled 20010916] 141 * The args are num D size (ie 4d6) [garbled 20010916]
155 */ 142 */
156int 143int
157die_roll (int num, int size, const object *op, int goodbad) 144die_roll (int num, int size, const object *op, bool prefer_high)
158{ 145{
159 int min_roll, luck, total, i, gotlucky; 146 int min_roll, luck, total, i, gotlucky;
160 147
161 int diff = size; 148 int diff = size;
162 min_roll = 1; 149 min_roll = 1;
180 gotlucky++; 167 gotlucky++;
181 ((luck > 0) ? (luck = 1) : (luck = -1)); 168 ((luck > 0) ? (luck = 1) : (luck = -1));
182 diff -= luck; 169 diff -= luck;
183 if (diff < 1) 170 if (diff < 1)
184 return (num); /*check again */ 171 return (num); /*check again */
185 ((goodbad) ? (min_roll += luck) : (diff)); 172 ((prefer_high) ? (min_roll += luck) : (diff));
186 total += max (1, min (size, rndm (diff) + min_roll)); 173 total += max (1, min (size, rndm (diff) + min_roll));
187 } 174 }
188 else 175 else
189 total += rndm (size) + 1; 176 total += rndm (size) + 1;
190 } 177 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines