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,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * |
5 | * |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * the terms of the Affero GNU General Public License as published by the |
7 | * the terms of the Affero GNU General Public License as published by the |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * option) any later version. |
9 | * option) any later version. |
… | |
… | |
193 | vprintf (format, ap); |
193 | vprintf (format, ap); |
194 | va_end (ap); |
194 | va_end (ap); |
195 | } |
195 | } |
196 | |
196 | |
197 | // simply return a mask with "bits" bits set |
197 | // simply return a mask with "bits" bits set |
198 | inline uint64 |
198 | static inline uint64 |
199 | m (int b) |
199 | m (int b) |
200 | { |
200 | { |
201 | return (uint64 (1) << b) - 1; |
201 | return (uint64 (1) << b) - 1; |
202 | } |
202 | } |
203 | |
203 | |
204 | // convert 9 digits to ascii, using only a single multiplication |
204 | // convert 9 digits to ascii, using only a single multiplication |
205 | // (depending on cpu and compiler). |
205 | // (depending on cpu and compiler). |
206 | // will generate a single 0 as output when v=lz=0 |
206 | // will generate a single 0 as output when v=lz=0 |
207 | inline char * |
207 | static inline char * |
208 | i2a_9 (char *ptr, uint32 v, bool lz) |
208 | i2a_9 (char *ptr, uint32 v, bool lz) |
209 | { |
209 | { |
210 | // convert to 4.56 fixed-point representation |
210 | // convert to 4.56 fixed-point representation |
211 | // this should be optimal on 64 bit cpus, and rather |
211 | // this should be optimal on 64 bit cpus, and rather |
212 | // slow on 32 bit cpus. go figure :) |
212 | // slow on 32 bit cpus. go figure :) |
… | |
… | |
255 | |
255 | |
256 | *ptr = '-'; ptr += i < 0 ? 1 : 0; |
256 | *ptr = '-'; ptr += i < 0 ? 1 : 0; |
257 | uint32 u = i < 0 ? -i : i; |
257 | uint32 u = i < 0 ? -i : i; |
258 | |
258 | |
259 | if (expect_true (u < 10)) // we have a lot of single-digit numbers, so optimise |
259 | if (expect_true (u < 10)) // we have a lot of single-digit numbers, so optimise |
260 | fadd (char (u + '0')); |
260 | *ptr++ = u + '0'; |
|
|
261 | else if (expect_true (u < 100)) // we have a lot of double-digit numbers, too :) |
|
|
262 | { |
|
|
263 | // let the compiler figure out sth. efficient here |
|
|
264 | *ptr++ = u / 10 + '0'; |
|
|
265 | *ptr++ = u % 10 + '0'; |
|
|
266 | } |
261 | else if (expect_true (u < 1000000000)) // 9 0's |
267 | else if (expect_true (u < 1000000000)) // 9 0's |
262 | ptr = i2a_9 (ptr, u, false); |
268 | ptr = i2a_9 (ptr, u, false); |
263 | else |
269 | else |
264 | { |
270 | { |
265 | sint32 div = u / 1000000000; |
271 | uint32 div = u / 1000000000; |
266 | uint32 rem = u % 1000000000; |
272 | uint32 rem = u % 1000000000; |
267 | |
273 | |
268 | ptr = i2a_9 (ptr, div, false); |
274 | ptr = i2a_9 (ptr, div, false); |
269 | ptr = i2a_9 (ptr, rem, true); |
275 | ptr = i2a_9 (ptr, rem, true); |
270 | } |
276 | } |
… | |
… | |
282 | // (#19) and two 9 digit parts (9..18 and 0..8) |
288 | // (#19) and two 9 digit parts (9..18 and 0..8) |
283 | |
289 | |
284 | // good compilers will only use multiplications here |
290 | // good compilers will only use multiplications here |
285 | |
291 | |
286 | if (u < 10) // we have a lot of single-digit numbers, so optimise |
292 | if (u < 10) // we have a lot of single-digit numbers, so optimise |
287 | fadd (char (u + '0')); |
293 | *ptr++ = u + '0'; |
288 | else if (expect_true (u < 1000000000)) // 9 0's |
294 | else if (expect_true (u < 1000000000)) // 9 0's |
289 | ptr = i2a_9 (ptr, u, false); |
295 | ptr = i2a_9 (ptr, u, false); |
290 | else if (expect_true (u < UINT64_C (1000000000000000000))) // 18 0's |
296 | else if (expect_true (u < UINT64_C (1000000000000000000))) // 18 0's |
291 | { |
297 | { |
292 | sint32 div = u / 1000000000; |
298 | uint32 div = u / 1000000000; |
293 | uint32 rem = u % 1000000000; |
299 | uint32 rem = u % 1000000000; |
294 | |
300 | |
295 | ptr = i2a_9 (ptr, div, false); |
301 | ptr = i2a_9 (ptr, div, false); |
296 | ptr = i2a_9 (ptr, rem, true); |
302 | ptr = i2a_9 (ptr, rem, true); |
297 | } |
303 | } |
298 | else |
304 | else |
299 | { |
305 | { |
300 | // a biggy |
306 | // a biggy, split off the topmost digit |
301 | sint32 div = u / UINT64_C (1000000000000000000); |
307 | uint32 div = u / UINT64_C (1000000000000000000); |
302 | uint64 rem = u % UINT64_C (1000000000000000000); |
308 | uint64 rem = u % UINT64_C (1000000000000000000); |
303 | |
309 | |
304 | fadd (char (div + '0')); |
310 | *ptr++ = div + '0'; |
|
|
311 | |
305 | u = rem; |
312 | u = rem; |
306 | |
313 | |
307 | { |
314 | { |
308 | sint32 div = u / 1000000000; |
315 | uint32 div = u / 1000000000; |
309 | uint32 rem = u % 1000000000; |
316 | uint32 rem = u % 1000000000; |
310 | |
317 | |
311 | ptr = i2a_9 (ptr, div, true); |
318 | ptr = i2a_9 (ptr, div, true); |
312 | ptr = i2a_9 (ptr, rem, true); |
319 | ptr = i2a_9 (ptr, rem, true); |
313 | } |
320 | } |
… | |
… | |
329 | return; |
336 | return; |
330 | |
337 | |
331 | *this << '(' << name; |
338 | *this << '(' << name; |
332 | |
339 | |
333 | const char *sep = ": "; |
340 | const char *sep = ": "; |
334 | for (int i = 0; i < NROFATTACKS; ++i) |
341 | for_all_bits_sparse_32 (abilities, i) |
335 | if (abilities & (1 << i)) |
|
|
336 | { |
342 | { |
337 | *this << sep; sep = ", "; |
343 | *this << sep; sep = ", "; |
338 | *this << attacks [i]; |
344 | *this << attacks [i]; |
339 | } |
345 | } |
340 | |
346 | |
341 | *this << ')'; |
347 | *this << ')'; |
342 | } |
348 | } |
343 | |
349 | |
344 | void |
350 | void |