Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

misc.h

Go to the documentation of this file.
00001 /* Random definitions used everywhere in Xconq.
00002    Copyright (C) 1987-1989, 1991-1997, 1999 Stanley T. Shebs.
00003    Copyright (C) 2004-2005 Eric A. McDonald.
00004 
00005 Xconq is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation; either version 2, or (at your option)
00008 any later version.  See the file COPYING.  */
00009 
00016 #ifndef TRUE
00017 #define TRUE (1)
00018 #endif
00019 #ifndef FALSE
00020 #define FALSE (0)
00021 #endif
00022 
00023 #ifndef CONST
00024 
00027 #define CONST const
00028 #endif /* CONST */
00029 
00030 #ifndef ABS
00031 
00040 #define ABS(x) (((x) < 0) ? (0 - (x)) : (x))
00041 #endif
00042 
00043 #ifndef min
00044 
00052 #define min(x,y) (((x) < (y)) ? (x) : (y))
00053 #endif
00054 
00055 #ifndef max
00056 
00064 #define max(x,y) (((x) > (y)) ? (x) : (y))
00065 #endif
00066 
00077 #define between(lo,n,hi) \
00078         ((lo) <= (n) && (n) <= (hi))
00079 
00092 #define limitn(lo,n,hi) ((n) \
00093     < (lo) ? (lo) : ((n) > (hi) ? (hi) : (n)))
00094 
00099 #define flip_coin() \
00100         (xrandom(257) % 2)
00101 
00111 #define avg(a,b) \
00112         (((a) + (b)) / 2)
00113 
00115 
00116 #define dice1(N,NUMDICE,SPOTS,OFFSET)   \
00117     (((0 == ((N) >> 14)) || (3 == ((N) >> 14))) ?   \
00118      (NUMDICE = 0, SPOTS = 0, OFFSET = (N)) :   \
00119      (NUMDICE = (((N) >> 11) & 0x07) + 1,  \
00120       SPOTS = (((N) >> 7) & 0x0f) + 2,  \
00121       OFFSET = ((N) & 0x8000) ? (-((N) & 0x7f) - 1) : ((N) & 0x7f)))
00122 
00124 
00125 #define dice2(N,NUMDICE,SPOTS,OFFSET)   \
00126     (((0 == ((N) >> 14)) || (3 == ((N) >> 14))) ?   \
00127      (NUMDICE = 0, SPOTS = 0, OFFSET = (N)) :   \
00128      (NUMDICE = ((N) & 0x8000) ? -((((N) >> 11) & 0x07) + 1) \
00129                                   : (((N) >> 11) & 0x07) + 1,  \
00130       SPOTS = (((N) >> 7) & 0x0f) + 2,  \
00131       OFFSET = ((N) & 0x8000) ? -((N) & 0x7f) : ((N) & 0x7f)))
00132 
00134 
00135 #define normalize_on_pmscale(n,max,range) \
00136     ((max) ? (((n) * (range)) / (max)) : 0)
00137 
00138 #ifndef isspace
00139 
00149 #define isspace(c) \
00150         ((c) == ' ' || (c) == '\n' || (c) == '\t' || (c) == '\r')
00151 #endif
00152 
00153 #define lowercase(c) (isupper(c) ? tolower(c) : (c))
00154 
00155 #define uppercase(c) (islower(c) ? toupper(c) : (c))
00156 
00166 #define empty_string(s) \
00167         ((s) == NULL || s[0] == '\0')
00168 
00170 extern char spbuf[];
00172 extern char tmpbuf[];
00173 
00180 typedef struct a_packed_bool_table {
00181     int rdim1size;      /* Reduced size of the first table dimension. */
00182     int dim2size;       /* Normal size of the second dimension. */
00183     short sizeofint;    /* Precalculated sizeof(int) in bits. */
00184     short whichway;     /* Which packing direction is more efficient? */
00185     int *pbools;        /* Pointer to the packed booleans. */
00186 } PackedBoolTable;
00187 
00191 #ifdef get_packed_bool
00192 #undef get_packed_bool
00193 #endif
00194 #define get_packed_bool(tbl,m,n)    \
00195     (((tbl)->pbools[((tbl)->whichway ? (m) : (n)) * (tbl)->rdim1size + ((tbl)->whichway ? (n) : (m)) / (tbl)->sizeofint] & (1 << ((tbl)->whichway ? (n) : (m)) % (tbl)->sizeofint)) ? TRUE : FALSE)
00196 
00200 #ifdef set_packed_bool
00201 #undef set_packed_bool
00202 #endif
00203 #define set_packed_bool(tbl,m,n,boolval)    \
00204     ((boolval) ? ((tbl)->pbools[((tbl)->whichway ? (m) : (n)) * (tbl)->rdim1size + ((tbl)->whichway ? (n) : (m)) / (tbl)->sizeofint] |= (1 << ((tbl)->whichway ? (n) : (m)) % (tbl)->sizeofint)) : ((tbl)->pbools[((tbl)->whichway ? (m) : (n)) * (tbl)->rdim1size + ((tbl)->whichway ? (n) : (m)) / (tbl)->sizeofint] &= ~(1 << ((tbl)->whichway ? (n) : (m)) % (tbl)->sizeofint)))
00205 
00209 #ifdef valid_packed_bool_table
00210 #undef valid_packed_bool_table
00211 #endif
00212 #define valid_packed_bool_table(tbl) \
00213     (((tbl) != NULL) && ((tbl)->pbools != NULL) && ((tbl)->sizeofint > 0) && ((tbl)->rdim1size >= 0) && ((tbl)->dim2size > 0))
00214 
00218 extern PackedBoolTable* create_packed_bool_table(int m, int n);
00219 extern void init_packed_bool_table(PackedBoolTable* tbl);
00220 extern void destroy_packed_bool_table(PackedBoolTable* tbl);
00221 
00222 /* Debugging definitions. */
00223 /* If asserts are off, then an appropriate user diagnostic will be 
00224    generated and/or a flow control statement will be used. If asserts are on, 
00225    then we obviously get the debugging benefits and abortive behavior. */
00226 
00227 #define assert_warning(cond, msg) \
00228     { assert(cond); if (!(cond)) run_warning(msg); }
00229 #define assert_warning_break(cond, msg) \
00230     { assert(cond); if (!(cond)) { run_warning(msg); break; } }
00231 #define assert_warning_continue(cond, msg) \
00232     { assert(cond); if (!(cond)) { run_warning(msg); continue; } }
00233 #define assert_warning_goto(cond, msg, label) \
00234     { assert(cond); if (!(cond)) { run_warning(msg); goto label; } }
00235 #define assert_warning_return(cond, msg, retval) \
00236     { assert(cond); if (!(cond)) { run_warning(msg); return retval; } }
00237 #define assert_error(cond, msg) \
00238     { assert(cond); if (!(cond)) run_error(msg); }
00239 #define assert_break(cond) \
00240     { assert(cond); if (!(cond)) break; }
00241 #define assert_continue(cond) \
00242     { assert(cond); if (!(cond)) continue; }
00243 #define assert_goto(cond, label) \
00244     { assert(cond); if (!(cond)) goto label; }
00245 #define assert_return(cond, retval) \
00246     { assert(cond); if (!(cond)) return retval; }
00247 
00248 #ifdef DEBUGGING
00249 
00250 /* Debugging definitions. */
00251 
00252 #define Dprintf if (Debug && dfp) debug_printf
00253 #define DMprintf if (DebugM && dmfp) debugm_printf
00254 #define DGprintf if (DebugG && dgfp) debugg_printf
00255 
00256 #define Dprintlisp(X) if (Debug && dfp) fprintlisp(dfp, (X))
00257 #define DMprintlisp(X) if (DebugM && dmfp) fprintlisp(dmfp, (X))
00258 #define DGprintlisp(X) if (DebugG && dgfp) fprintlisp(dgfp, (X))
00259 
00260 /* If the debug flags are not macros, then declare them as globals. */
00261 
00262 #ifndef Debug
00263 extern int Debug;
00264 #endif
00265 #ifndef DebugM
00266 extern int DebugM;
00267 #endif
00268 #ifndef DebugG
00269 extern int DebugG;
00270 #endif
00271 
00272 extern FILE *dfp;
00273 extern FILE *dmfp;
00274 extern FILE *dgfp;
00275 
00276 #else /* DEBUGGING */
00277 
00278 /* Make defns and calls vanish if possible. */
00279 
00280 #define Dprintf if (0) debug_printf
00281 #define DMprintf if (0) debugm_printf
00282 #define DGprintf if (0) debugg_printf
00283 
00284 #define Dprintlisp(X)
00285 #define DMprintlisp(X)
00286 #define DGprintlisp(X)
00287 
00288 #define Debug (0)
00289 #define DebugM (0)
00290 #define DebugG (0)
00291 
00292 #define dfp stdout
00293 #define dmfp stdout
00294 #define dgfp stdout
00295 
00296 #endif /* DEBUGGING */
00297 
00302 typedef struct a_library_path {
00303     char *path;                     
00304     struct a_library_path *next;    
00305 } LibraryPath;
00306 
00311 extern LibraryPath *xconq_libs;
00312 
00317 extern LibraryPath *last_user_xconq_lib;
00318 
00325 #define for_all_library_paths(p)  \
00326   for (p = xconq_libs; p != NULL; p = p->next)
00327 
00328 extern void init_xrandom(int seed);
00329 extern int xrandom(int m);
00330 extern int probability(int prob);
00331 extern int prob_fraction(int n);
00332 
00333 /* Dice Information and Manipulation */
00334 
00335 namespace Xconq {
00336 
00338 typedef short   DiceRep;
00339 
00341 extern Z32 dice1_roll_min(DiceRep dice);
00343 extern Z32 dice2_roll_min(DiceRep dice);
00345 extern Z32 dice1_roll_mean(DiceRep dice);
00347 extern Z32 dice2_roll_mean(DiceRep dice);
00349 extern Z32 dice1_roll_max(DiceRep dice);
00351 extern Z32 dice2_roll_max(DiceRep dice);
00353 extern Z32 roll_dice1(DiceRep dice);
00355 extern Z32 roll_dice2(DiceRep dice);
00357 extern DiceRep multiply_dice1(DiceRep dice, int mult);
00359 extern DiceRep multiply_dice2(DiceRep dice, int mult);
00360 
00361 }
00362 
00363 extern void *xmalloc(int amt);
00364 extern void report_malloc(void);
00365 extern void tprintf(char *buf, char *str, ...);
00366 extern void tnprintf(char *buf, int n, char *str, ...);
00367 extern int select_by_weight(int *arr, int numvals);
00368 extern char *copy_string(char *str);
00369 extern char *pad_blanks(char *str, int n);
00370 extern int iindex(int ch, char *str);
00371 extern long idifftime(time_t t1, time_t t0);
00372 extern void case_panic(char *str, int var);
00373 extern int isqrt(int i);
00374 extern void init_debug_to_stdout(void);
00375 extern void update_debugging(void);
00376 extern void toggle_debugging(int *flagp);
00377 extern void debug_printf(char *str, ...);
00378 extern void debugm_printf(char *str, ...);
00379 extern void debugg_printf(char *str, ...);
00380 
00381 extern void prealloc_debug(void);
00382 
00383 extern void record_activity_start(char *type, int detail);
00384 extern void record_activity_end(char *type, int detail);
00385 extern void dump_activity_trace(void);
00386 
00387 extern void vtprintf(char *buf, char *str, va_list ap);
00388 
00389 extern void log_warning(char *typ, char *str);
00390 
00391 /* Needed for emergency game savers. */
00392 extern int write_entire_game_state(char *fname);
00393 
00394 /* Called from various places. */
00395 extern char *find_name(char *fname);
00396 
00397 /* New wrapper for fopen defined in mac.c and unix.c. */
00398 extern FILE *open_file(char *filename, char *mode);
00399 
00400 /* For coordinated saving of network games. */
00401 extern void save_game(char *fname);
00402 
00403 /* Needed by Unix and Windows. */
00404 extern void close_displays(void);

Generated on Sat Sep 17 10:28:53 2005 for XconqKernel by doxygen 1.3.6