1 /***************************************************************************
2                           tools.h  -  description
3                              -------------------
4     begin                : Fri Jan 19 2001
5     copyright            : (C) 2001 by Michael Speck
6     email                : kulkanie@gmx.net
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #ifndef __TOOLS_H
19 #define __TOOLS_H
20 
21 /* this file contains some useful tools */
22 
23 /* free with a check */
24 #define FREE( ptr ) { if ( ptr ) free( ptr ); ptr = 0; }
25 
26 /* check if a serious of flags is set in source */
27 #define CHECK_FLAGS( source, flags ) ( source & (flags) )
28 
29 /* return random value between ( and including ) upper,lower limit */
30 #define RANDOM(lower, upper) ((lower)+(int)(((double)((upper)-(lower)+1))*rand()/(RAND_MAX+1.0)))
31 
32 /* compute distance of two vectors */
33 #define VEC_DIST( vec1, vec2 ) ( sqrt( ( vec1.x - vec2.x ) * ( vec1.x - vec2.x ) + ( vec1.y - vec2.y ) * ( vec1.y - vec2.y ) ) )
34 
35 /* compares to strings and returns true if their first strlen(str1) chars are equal */
36 int strequal( char *str1, char *str2 );
37 
38 /* delete lines */
39 void delete_lines( char **lines, int line_number );
40 
41 /* delay struct */
42 typedef struct {
43     int limit;
44     int cur;
45 } Delay;
46 
47 /* set delay to ms milliseconds */
48 void delay_set( Delay *delay, int ms );
49 
50 /* reset delay ( cur = 0 )*/
51 void delay_reset( Delay *delay );
52 
53 /* check if time's out ( add ms milliseconds )and reset */
54 int delay_timed_out( Delay *delay, int ms );
55 
56 /* set timer so that we have a time out next call of delay_timed_out() */
57 void delay_force_time_out( Delay *delay );
58 
59 /* return distance betwteen to map positions */
60 int get_dist( int x1, int y1, int x2, int y2 );
61 
62 /* init random seed by using ftime */
63 void set_random_seed();
64 
65 /* get coordintaes from string */
66 void get_coord( char *str, int *x, int *y );
67 
68 // text structure //
69 typedef struct {
70     char **lines;
71     int count;
72 } Text;
73 // convert a str into text ( for listbox ) //
74 Text* create_text( char *str, int char_width );
75 // delete text //
76 void delete_text( Text *text );
77 
78 /*
79 ====================================================================
80 Get type and prefix from string:
81     type::prefix
82 Set both pointers 0 if failure.
83 ====================================================================
84 */
85 void get_type_and_prefix( char *arg, char **ext, char **prefix );
86 
87 /*
88 ====================================================================
89 Replace any existence of character old into new.
90 ====================================================================
91 */
92 void strrepl( char **str, char c_old, char c_new );
93 
94 /*
95 ====================================================================
96 Counter with a current float value and a target value. Approaches
97 the target value until reached when counter_update() is called.
98 ====================================================================
99 */
100 typedef struct {
101     double approach; /* approaching value usually used for a smooth counter display */
102     double value; /* actual value */
103 } Counter;
104 void counter_set( Counter *counter, double value );
105 void counter_add( Counter *counter, double add );
106 double counter_get_approach( Counter counter );
107 double counter_get( Counter counter );
108 void counter_update( Counter *counter, int ms );
109 
110 void fill_int_array_rand( int *array, int start, int count,
111                                       int low, int high );
112 
113 void fill_random_block_bags( int *bag, int bag_count, int modern );
114 
115 #endif
116