+#include "misc.h"
+#include "setup.h"
+#include "random.h"
+#include "text.h"
+#include "image.h"
+
+
+// ============================================================================
+// some generic helper functions
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// logging functions
+// ----------------------------------------------------------------------------
+
+#define DUPLICATE_LOG_OUT_TO_STDOUT TRUE
+#define DUPLICATE_LOG_ERR_TO_STDERR TRUE
+
+
+#if defined(PLATFORM_ANDROID)
+static int android_log_prio = ANDROID_LOG_INFO;
+static char *android_log_buffer = NULL;
+
+static void append_to_android_log_buffer(char *format, va_list ap)
+{
+ char text_new[MAX_OUTPUT_LINESIZE];
+
+ // print text to temporary string
+ vsnprintf(text_new, MAX_OUTPUT_LINESIZE, format, ap);
+
+ if (android_log_buffer == NULL)
+ {
+ android_log_buffer = getStringCopy(text_new);
+ }
+ else
+ {
+ char *android_log_buffer_old = android_log_buffer;
+
+ // append new text to existing text
+ android_log_buffer = getStringCat2(android_log_buffer, text_new);
+
+ checked_free(android_log_buffer_old);
+ }
+}
+
+static void vprintf_log_nonewline(char *format, va_list ap)
+{
+ // add log output to buffer until text with newline is printed
+ append_to_android_log_buffer(format, ap);
+}
+
+static void vprintf_log(char *format, va_list ap)
+{
+ // add log output to buffer
+ append_to_android_log_buffer(format, ap);
+
+ // __android_log_vprint(android_log_prio, program.program_title, format, ap);
+ __android_log_write(android_log_prio, program.program_title,
+ android_log_buffer);
+
+ checked_free(android_log_buffer);
+ android_log_buffer = NULL;
+}
+
+#else
+
+static void vprintf_log_nonewline(char *format, va_list ap)
+{
+ FILE *file = program.log_file[LOG_ERR_ID];
+
+#if DUPLICATE_LOG_ERR_TO_STDERR
+ if (file != program.log_file_default[LOG_ERR_ID])
+ {
+ va_list ap2;
+ va_copy(ap2, ap);
+
+ vfprintf(program.log_file_default[LOG_ERR_ID], format, ap2);
+
+ va_end(ap2);
+ }
+#endif
+
+ vfprintf(file, format, ap);
+}
+
+static void vprintf_log(char *format, va_list ap)
+{
+ FILE *file = program.log_file[LOG_ERR_ID];
+ char *newline = STRING_NEWLINE;
+
+#if DUPLICATE_LOG_ERR_TO_STDERR
+ if (file != program.log_file_default[LOG_ERR_ID])
+ {
+ va_list ap2;
+ va_copy(ap2, ap);
+
+ vfprintf(program.log_file_default[LOG_ERR_ID], format, ap2);
+ fprintf(program.log_file_default[LOG_ERR_ID], "%s", newline);
+
+ va_end(ap2);
+ }
+#endif
+
+ vfprintf(file, format, ap);
+ fprintf(file, "%s", newline);
+}
+#endif
+
+static void printf_log_nonewline(char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ vprintf_log_nonewline(format, ap);
+ va_end(ap);
+}
+
+static void printf_log(char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ vprintf_log(format, ap);
+ va_end(ap);
+}
+
+static void printf_log_line(char *line_chars, int line_length)
+{
+ int i;
+
+ for (i = 0; i < line_length; i++)
+ printf_log_nonewline("%s", line_chars);
+
+ printf_log("");
+}
+
+
+// ----------------------------------------------------------------------------
+// platform independent wrappers for printf() et al.
+// ----------------------------------------------------------------------------
+
+void fprintf_line(FILE *file, char *line_chars, int line_length)
+{
+ int i;
+
+ for (i = 0; i < line_length; i++)
+ fprintf(file, "%s", line_chars);
+
+ fprintf(file, "\n");
+}
+
+void fprintf_line_with_prefix(FILE *file, char *prefix, char *line_chars,
+ int line_length)
+{
+ fprintf(file, "%s", prefix);
+ fprintf_line(file, line_chars, line_length);
+}
+
+void printf_line(char *line_chars, int line_length)
+{
+ fprintf_line(stdout, line_chars, line_length);
+}
+
+void printf_line_with_prefix(char *prefix, char *line_chars, int line_length)
+{
+ fprintf_line_with_prefix(stdout, prefix, line_chars, line_length);
+}
+
+static void vPrint(char *format, va_list ap)
+{
+ FILE *file = program.log_file[LOG_OUT_ID];
+
+#if DUPLICATE_LOG_OUT_TO_STDOUT
+ if (file != program.log_file_default[LOG_OUT_ID])
+ {
+ va_list ap2;
+ va_copy(ap2, ap);
+
+ vfprintf(program.log_file_default[LOG_OUT_ID], format, ap2);
+
+ va_end(ap2);
+ }
+#endif
+
+ vfprintf(file, format, ap);
+}
+
+void Print(char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ vPrint(format, ap);
+ va_end(ap);
+}
+
+void PrintNoLog(char *format, ...)
+{
+ FILE *file = program.log_file_default[LOG_OUT_ID];
+ va_list ap;
+
+ va_start(ap, format);
+ vfprintf(file, format, ap);
+ va_end(ap);
+
+ fflush(file);
+}
+
+void PrintLine(char *line_chars, int line_length)
+{
+ int i;
+
+ for (i = 0; i < line_length; i++)
+ Print(line_chars);
+
+ Print("\n");
+}
+
+void PrintLineWithPrefix(char *prefix, char *line_chars, int line_length)
+{
+ Print(prefix);
+ PrintLine(line_chars, line_length);
+}
+
+
+// ----------------------------------------------------------------------------
+// generic logging and error handling functions
+// ----------------------------------------------------------------------------
+
+enum log_levels
+{
+ LOG_UNKNOWN = 0,
+ LOG_DEBUG,
+ LOG_INFO,
+ LOG_WARN,
+ LOG_ERROR,
+ LOG_FATAL
+};
+
+static char *log_tokens[] =
+{
+ "UNKNOWN",
+ "DEBUG",
+ "INFO",
+ "WARN",
+ "ERROR",
+ "FATAL"
+};
+
+static void printf_log_prefix(int log_level, char *mode)
+{
+ if (log_level < 0 || log_level > LOG_FATAL)
+ return;
+
+ char *log_token = log_tokens[log_level];
+
+ if (log_level == LOG_DEBUG)
+ printf_log_nonewline("[%s] [%s] ", log_token, mode);
+ else
+ printf_log_nonewline("[%s] ", log_token);
+}
+
+static void vLog(int log_level, char *mode, char *format, va_list ap)
+{
+ if (log_level < 0 || log_level > LOG_FATAL)
+ return;
+
+ if (log_level == LOG_DEBUG)
+ {
+ // only show debug messages when in debug mode
+ if (!options.debug)
+ return;
+
+ // if optional debug mode specified, limit debug output accordingly
+ if (options.debug_mode != NULL &&
+ strstr(mode, options.debug_mode) == NULL)
+ return;
+ }
+ else if (log_level == LOG_WARN)
+ {
+ // only show warning messages when in verbose mode
+ if (!options.verbose)
+ return;
+ }
+
+#if defined(PLATFORM_ANDROID)
+ android_log_prio = (log_level == LOG_DEBUG ? ANDROID_LOG_DEBUG :
+ log_level == LOG_INFO ? ANDROID_LOG_INFO :
+ log_level == LOG_WARN ? ANDROID_LOG_WARN :
+ log_level == LOG_ERROR ? ANDROID_LOG_ERROR :
+ log_level == LOG_FATAL ? ANDROID_LOG_FATAL :
+ ANDROID_LOG_UNKNOWN);
+#endif
+
+ static boolean last_line_was_separator = FALSE;
+ char *log_token = log_tokens[log_level];
+
+ if (strEqual(format, "===") ||
+ strEqual(format, "---"))
+ {
+ static char *mode_last = NULL;
+ char line_char[2] = { format[0], '\0' };
+ int line_length = 80 - strlen(log_token) - 3;
+
+ if (log_level == LOG_DEBUG)
+ line_length -= strlen(mode) + 3;
+
+ if (last_line_was_separator && strEqual(mode, mode_last))
+ return;
+
+ printf_log_prefix(log_level, mode);
+ printf_log_line(line_char, line_length);
+
+ if (!strEqual(mode, mode_last))
+ setString(&mode_last, mode);
+
+ last_line_was_separator = TRUE;
+
+ return;
+ }
+
+ last_line_was_separator = FALSE;
+
+ printf_log_prefix(log_level, mode);
+
+ vprintf_log(format, ap);
+}
+
+static void Log(int log_level, char *mode, char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ vLog(log_level, mode, format, ap);
+ va_end(ap);
+}
+
+void DebugContinued(char *mode, char *format, ...)
+{
+ static char message[MAX_LINE_LEN] = { 0 };
+
+ // initialize message (optional)
+ if (strEqual(format, ""))
+ {
+ message[0] = '\0';
+
+ return;
+ }
+
+ char *message_ptr = message + strlen(message);
+ int size_left = MAX_LINE_LEN - strlen(message);
+ va_list ap;
+
+ // append message
+ va_start(ap, format);
+ vsnprintf(message_ptr, size_left, format, ap);
+ va_end(ap);
+
+ // finalize message
+ if (strSuffix(format, "\n"))
+ {
+ message[strlen(message) - 1] = '\0';
+
+ Debug(mode, message);
+
+ message[0] = '\0';
+ }
+}
+
+void Debug(char *mode, char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ vLog(LOG_DEBUG, mode, format, ap);
+ va_end(ap);
+}
+
+void Info(char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ vLog(LOG_INFO, NULL, format, ap);
+ va_end(ap);
+}
+
+void Warn(char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ vLog(LOG_WARN, NULL, format, ap);
+ va_end(ap);
+}
+
+void Error(char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ vLog(LOG_ERROR, NULL, format, ap);
+ va_end(ap);
+}
+
+void Fail(char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ vLog(LOG_FATAL, NULL, format, ap);
+ va_end(ap);
+
+ if (!network.is_server_thread)
+ {
+ va_start(ap, format);
+ program.exit_message_function(format, ap);
+ va_end(ap);
+ }
+
+ Log(LOG_FATAL, NULL, "aborting");
+
+ // network server thread: normal exit
+ if (network.is_server_thread)
+ exit(1);
+
+ // main process: clean up stuff
+ program.exit_function(1);
+}
+
+void FailWithHelp(char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ vLog(LOG_FATAL, NULL, format, ap);
+ va_end(ap);
+
+ Log(LOG_FATAL, NULL, "try option '--help' for more information");
+ Log(LOG_FATAL, NULL, "aborting");
+
+ // main process: clean up stuff
+ program.exit_function(1);
+}
+
+
+// ----------------------------------------------------------------------------
+// string functions
+// ----------------------------------------------------------------------------
+
+/* int2str() returns a number converted to a string;
+ the used memory is static, but will be overwritten by later calls,
+ so if you want to save the result, copy it to a private string buffer;
+ there can be 10 local calls of int2str() without buffering the result --
+ the 11th call will then destroy the result from the first call and so on. */
+
+char *int2str(int number, int size)
+{
+ static char shift_array[10][40];
+ static int shift_counter = 0;
+ char *s = shift_array[shift_counter];
+
+ shift_counter = (shift_counter + 1) % 10;
+
+ if (size > 20)
+ size = 20;
+
+ if (size > 0)
+ {
+ sprintf(s, " %09d", number);
+ return &s[strlen(s) - size];
+ }
+ else
+ {
+ sprintf(s, "%d", number);
+ return s;
+ }
+}
+
+
+// something similar to "int2str()" above, but allocates its own memory
+// and has a different interface; we cannot use "itoa()", because this
+// seems to be already defined when cross-compiling to the win32 target
+
+char *i_to_a(unsigned int i)
+{
+ static char *a = NULL;
+
+ checked_free(a);
+
+ if (i > 2147483647) // yes, this is a kludge
+ i = 2147483647;
+
+ a = checked_malloc(10 + 1);
+
+ sprintf(a, "%d", i);
+
+ return a;
+}
+
+
+// calculate base-2 logarithm of argument (rounded down to integer;
+// this function returns the number of the highest bit set in argument)
+
+int log_2(unsigned int x)
+{
+ int e = 0;
+
+ while ((1 << e) < x)
+ {
+ x -= (1 << e); // for rounding down (rounding up: remove this line)
+ e++;
+ }
+
+ return e;
+}
+
+boolean getTokenValueFromString(char *string, char **token, char **value)
+{
+ return getTokenValueFromSetupLine(string, token, value);
+}
+
+
+// ----------------------------------------------------------------------------
+// UUID functions
+// ----------------------------------------------------------------------------
+
+#define UUID_BYTES 16
+#define UUID_CHARS (UUID_BYTES * 2)
+#define UUID_LENGTH (UUID_CHARS + 4)
+
+static unsigned int uuid_random_function(int max)
+{
+ return GetBetterRandom(max);
+}
+
+char *getUUIDExt(unsigned int (*random_function)(int max))
+{
+ static char uuid[UUID_LENGTH + 1];
+ int data[UUID_BYTES];
+ int count = 0;
+ int i;
+
+ for (i = 0; i < UUID_BYTES; i++)
+ data[i] = random_function(256);
+
+ data[6] = 0x40 | (data[6] & 0x0f);
+ data[8] = 0x80 | (data[8] & 0x3f);
+
+ for (i = 0; i < UUID_BYTES; i++)
+ {
+ sprintf(&uuid[count], "%02x", data[i]);
+ count += 2;
+
+ if (i == 3 || i == 5 || i == 7 || i == 9)
+ strcat(&uuid[count++], "-");
+ }
+
+ return uuid;
+}
+
+char *getUUID(void)
+{
+ return getUUIDExt(uuid_random_function);
+}
+
+
+// ----------------------------------------------------------------------------
+// counter functions
+// ----------------------------------------------------------------------------
+
+// maximal allowed length of a command line option
+#define MAX_OPTION_LEN 256
+
+static unsigned int getCurrentMS(void)
+{
+ return SDL_GetTicks();
+}
+
+static unsigned int mainCounter(int mode)
+{
+ static unsigned int base_ms = 0;
+ unsigned int current_ms;
+
+ // get current system milliseconds
+ current_ms = getCurrentMS();
+
+ // reset base timestamp in case of counter reset or wrap-around
+ if (mode == INIT_COUNTER || current_ms < base_ms)
+ base_ms = current_ms;
+
+ // return milliseconds since last counter reset
+ return current_ms - base_ms;
+}
+
+void InitCounter() // set counter back to zero
+{
+ mainCounter(INIT_COUNTER);
+}
+
+unsigned int Counter() // get milliseconds since last call of InitCounter()
+{
+ return mainCounter(READ_COUNTER);
+}
+
+static void sleep_milliseconds(unsigned int milliseconds_delay)
+{
+ SDL_Delay(milliseconds_delay);
+}
+
+void Delay(unsigned int delay) // Sleep specified number of milliseconds
+{
+ sleep_milliseconds(delay);
+}
+
+boolean DelayReachedExt2(unsigned int *counter_var, unsigned int delay,
+ unsigned int actual_counter)
+{
+ if (actual_counter >= *counter_var &&
+ actual_counter < *counter_var + delay)
+ return FALSE;
+
+ *counter_var = actual_counter;
+
+ return TRUE;
+}
+
+boolean DelayReachedExt(DelayCounter *counter, unsigned int actual_counter)
+{
+ return DelayReachedExt2(&counter->count, counter->value, actual_counter);
+}
+
+boolean FrameReached(DelayCounter *counter)
+{
+ return DelayReachedExt(counter, FrameCounter);
+}
+
+boolean DelayReached(DelayCounter *counter)
+{
+ return DelayReachedExt(counter, Counter());
+}
+
+void ResetDelayCounterExt(DelayCounter *counter, unsigned int actual_counter)
+{
+ DelayReachedExt2(&counter->count, 0, actual_counter);
+}
+
+void ResetFrameCounter(DelayCounter *counter)
+{
+ ResetDelayCounterExt(counter, FrameCounter);
+}
+
+void ResetDelayCounter(DelayCounter *counter)
+{
+ ResetDelayCounterExt(counter, Counter());
+}
+
+int WaitUntilDelayReached(DelayCounter *counter)
+{
+ unsigned int *counter_var = &counter->count;
+ unsigned int delay = counter->value;
+ unsigned int actual_counter;
+ int skip_frames = 0;
+
+ while (1)
+ {
+ actual_counter = Counter();
+
+ if (actual_counter >= *counter_var &&
+ actual_counter < *counter_var + delay)
+ sleep_milliseconds((*counter_var + delay - actual_counter) / 2);
+ else
+ break;
+ }
+
+ if (*counter_var != 0 &&
+ delay != 0 &&
+ actual_counter >= *counter_var + delay)
+ {
+ int lag = actual_counter - (*counter_var + delay);
+ int delay2 = (delay + 1) / 2;
+
+ if (lag >= delay2)
+ skip_frames = (lag + delay2) / delay;
+ }
+
+ *counter_var = actual_counter;
+
+ return skip_frames;
+}
+
+void SkipUntilDelayReached(DelayCounter *counter,
+ int *loop_var, int last_loop_value)
+{
+ int skip_frames = WaitUntilDelayReached(counter);
+
+#if 0
+#if DEBUG
+ if (skip_frames)
+ Debug("internal:SkipUntilDelayReached",
+ "%d: %d ms -> SKIP %d FRAME(S) [%d ms]",
+ *loop_var, counter->value,
+ skip_frames, skip_frames * counter->value);
+ else
+ Debug("internal:SkipUntilDelayReached",
+ "%d: %d ms",
+ *loop_var, counter->value);
+#endif
+#endif
+
+ if (skip_frames == 0)
+ return;
+
+ // when skipping frames, make sure to never skip the last frame, as
+ // this may be needed for animations to reach a defined end state;
+ // furthermore, we assume that this function is called at the end
+ // of a "for" loop, which continues by incrementing the loop variable
+ // by one before checking the loop condition again; therefore we have
+ // to check against the last loop value minus one here
+
+ last_loop_value--;
+
+ if (*loop_var < last_loop_value) // never skip the last frame
+ {
+ *loop_var += skip_frames;
+
+ if (*loop_var > last_loop_value) // never skip the last frame
+ *loop_var = last_loop_value;
+ }
+}
+
+
+// ----------------------------------------------------------------------------
+// random generator functions
+// ----------------------------------------------------------------------------
+
+static unsigned int init_random_number_ext(int nr, int seed)
+{
+ if (seed == NEW_RANDOMIZE)
+ {
+ // default random seed
+ seed = (int)time(NULL); // seconds since the epoch
+
+#if !defined(PLATFORM_WINDOWS)
+ // add some more randomness
+ struct timeval current_time;
+
+ gettimeofday(¤t_time, NULL);
+
+ seed += (int)current_time.tv_usec; // microseconds since the epoch
+#endif
+
+ // add some more randomness
+ seed += (int)SDL_GetTicks(); // milliseconds since SDL init