| 1 |
// |
| 2 |
// Copyright(C) 2022 by Ryan Krafnick |
| 3 |
// |
| 4 |
// This program is free software; you can redistribute it and/or |
| 5 |
// modify it under the terms of the GNU General Public License |
| 6 |
// as published by the Free Software Foundation; either version 2 |
| 7 |
// of the License, or (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// DESCRIPTION: |
| 15 |
// DSDA Utility |
| 16 |
// |
| 17 |
|
| 18 |
#include <math.h> |
| 19 |
#include <stdarg.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <string.h> |
| 22 |
#include <stdio.h> |
| 23 |
|
| 24 |
#include "p_maputl.h" |
| 25 |
#include "r_main.h" |
| 26 |
#include "z_zone.h" |
| 27 |
|
| 28 |
#include "utility.h" |
| 29 |
|
| 30 |
void dsda_InitString(dsda_string_t* dest, const char* value) { |
| 31 |
dest->size = 1; // \0 |
| 32 |
dest->string = NULL; |
| 33 |
|
| 34 |
if (value) |
| 35 |
dsda_StringCat(dest, value); |
| 36 |
} |
| 37 |
|
| 38 |
void dsda_FreeString(dsda_string_t* dest) { |
| 39 |
Z_Free(dest->string); |
| 40 |
dsda_InitString(dest, NULL); |
| 41 |
} |
| 42 |
|
| 43 |
static void dsda_ExpandString(dsda_string_t* dest, size_t size) { |
| 44 |
dest->size += size; |
| 45 |
if (dest->string) |
| 46 |
dest->string = Z_Realloc(dest->string, dest->size); |
| 47 |
else |
| 48 |
dest->string = Z_Calloc(dest->size, 1); |
| 49 |
} |
| 50 |
|
| 51 |
void dsda_StringCat(dsda_string_t* dest, const char* source) { |
| 52 |
if (!source || (!source[0] && dest->string)) |
| 53 |
return; |
| 54 |
|
| 55 |
dsda_ExpandString(dest, strlen(source)); |
| 56 |
strcat(dest->string, source); |
| 57 |
} |
| 58 |
|
| 59 |
void dsda_StringCatF(dsda_string_t* dest, const char* format, ...) { |
| 60 |
size_t length; |
| 61 |
va_list v; |
| 62 |
|
| 63 |
va_start(v, format); |
| 64 |
length = vsnprintf(NULL, 0, format, v); |
| 65 |
va_end(v); |
| 66 |
|
| 67 |
dsda_ExpandString(dest, length); |
| 68 |
|
| 69 |
va_start(v, format); |
| 70 |
vsnprintf(dest->string + dest->size - 1 - length, length + 1, format, v); |
| 71 |
va_end(v); |
| 72 |
} |
| 73 |
|
| 74 |
void dsda_StringPrintF(dsda_string_t* dest, const char* format, ...) { |
| 75 |
size_t length; |
| 76 |
va_list v; |
| 77 |
|
| 78 |
dsda_InitString(dest, NULL); |
| 79 |
|
| 80 |
va_start(v, format); |
| 81 |
length = vsnprintf(NULL, 0, format, v); |
| 82 |
va_end(v); |
| 83 |
|
| 84 |
dsda_ExpandString(dest, length); |
| 85 |
|
| 86 |
va_start(v, format); |
| 87 |
vsnprintf(dest->string + dest->size - 1 - length, length + 1, format, v); |
| 88 |
va_end(v); |
| 89 |
} |
| 90 |
|
| 91 |
void dsda_UppercaseString(char* str) { |
| 92 |
for (; *str; str++) *str = toupper(*str); |
| 93 |
} |
| 94 |
|
| 95 |
void dsda_TranslateCheckSum(dsda_cksum_t* cksum) { |
| 96 |
unsigned int i; |
| 97 |
|
| 98 |
for (i = 0; i < 16; i++) |
| 99 |
snprintf(&cksum->string[i * 2], 3, "%02x", cksum->bytes[i]); |
| 100 |
cksum->string[32] = '\0'; |
| 101 |
} |
| 102 |
|
| 103 |
dboolean dsda_HasFileExt(const char* file, const char* ext) { |
| 104 |
return strlen(file) > strlen(ext) && |
| 105 |
!strcasecmp(file + strlen(file) - strlen(ext), ext); |
| 106 |
} |
| 107 |
|
| 108 |
char** dsda_SplitString(char* str, const char* delimiter) { |
| 109 |
char** result; |
| 110 |
int substring_count = 2; |
| 111 |
char* p = str; |
| 112 |
|
| 113 |
while (*p) |
| 114 |
if (*p++ == *delimiter) |
| 115 |
++substring_count; |
| 116 |
|
| 117 |
result = Z_Calloc(substring_count, sizeof(*result)); |
| 118 |
|
| 119 |
if (result) { |
| 120 |
char* token; |
| 121 |
int i = 0; |
| 122 |
|
| 123 |
token = strtok(str, delimiter); |
| 124 |
while (token) { |
| 125 |
result[i++] = token; |
| 126 |
token = strtok(NULL, delimiter); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
return result; |
| 131 |
} |
| 132 |
|
| 133 |
dsda_fixed_t dsda_SplitFixed(fixed_t x) { |
| 134 |
dsda_fixed_t result; |
| 135 |
|
| 136 |
result.negative = x < 0; |
| 137 |
result.base = x >> FRACBITS; |
| 138 |
result.frac = x & 0xffff; |
| 139 |
|
| 140 |
if (result.negative) |
| 141 |
if (result.frac) { |
| 142 |
++result.base; |
| 143 |
result.frac = 0xffff - result.frac + 1; |
| 144 |
} |
| 145 |
|
| 146 |
return result; |
| 147 |
} |
| 148 |
|
| 149 |
void dsda_FixedToString(char* str, fixed_t x) { |
| 150 |
dsda_fixed_t value; |
| 151 |
|
| 152 |
value = dsda_SplitFixed(x); |
| 153 |
|
| 154 |
if (value.frac) { |
| 155 |
if (value.negative && !value.base) |
| 156 |
snprintf(str, FIXED_STRING_LENGTH, "-%i.%05i", value.base, value.frac); |
| 157 |
else |
| 158 |
snprintf(str, FIXED_STRING_LENGTH, "%i.%05i", value.base, value.frac); |
| 159 |
} |
| 160 |
else |
| 161 |
snprintf(str, FIXED_STRING_LENGTH, "%i", value.base); |
| 162 |
} |
| 163 |
|
| 164 |
dsda_angle_t dsda_SplitAngle(angle_t x) { |
| 165 |
dsda_angle_t result; |
| 166 |
|
| 167 |
result.base = x >> 24; |
| 168 |
result.frac = (x >> 16) & 0xff; |
| 169 |
|
| 170 |
return result; |
| 171 |
} |
| 172 |
|
| 173 |
void dsda_PrintCommandMovement(char* str, ticcmd_t* cmd) { |
| 174 |
str[0] = '\0'; |
| 175 |
|
| 176 |
if (cmd->forwardmove > 0) |
| 177 |
str += snprintf(str, 6, "MF%2d ", cmd->forwardmove); |
| 178 |
else if (cmd->forwardmove < 0) |
| 179 |
str += snprintf(str, 6, "MB%2d ", -cmd->forwardmove); |
| 180 |
else |
| 181 |
str += snprintf(str, 6, " "); |
| 182 |
|
| 183 |
if (cmd->sidemove > 0) |
| 184 |
str += snprintf(str, 6, "SR%2d ", cmd->sidemove); |
| 185 |
else if (cmd->sidemove < 0) |
| 186 |
str += snprintf(str, 6, "SL%2d ", -cmd->sidemove); |
| 187 |
else |
| 188 |
str += snprintf(str, 6, " "); |
| 189 |
|
| 190 |
if (cmd->angleturn > 0) |
| 191 |
str += snprintf(str, 6, "TL%2d", cmd->angleturn >> 8); |
| 192 |
else if (cmd->angleturn < 0) |
| 193 |
str += snprintf(str, 6, "TR%2d", -(cmd->angleturn >> 8)); |
| 194 |
} |
| 195 |
|
| 196 |
char* dsda_ConcatDir(const char* a, const char* b) |
| 197 |
{ |
| 198 |
char* buffer; |
| 199 |
size_t len; |
| 200 |
|
| 201 |
buffer = Z_Malloc(strlen(a) + strlen(b) + 2); |
| 202 |
|
| 203 |
strcpy(buffer, a); |
| 204 |
|
| 205 |
len = strlen(buffer); |
| 206 |
buffer[len] = '/'; |
| 207 |
strcpy(buffer + len + 1, b); |
| 208 |
|
| 209 |
return buffer; |
| 210 |
} |
| 211 |
|
| 212 |
void dsda_CutExtension(char* str) { |
| 213 |
char* p; |
| 214 |
|
| 215 |
p = str + strlen(str); |
| 216 |
while (--p > str && *p != '/' && *p != '\\') |
| 217 |
if (*p == '.') { |
| 218 |
*p = '\0'; |
| 219 |
break; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
const char* dsda_BaseName(const char* str) |
| 224 |
{ |
| 225 |
const char* p; |
| 226 |
|
| 227 |
p = str + strlen(str); |
| 228 |
while (p > str && *(p - 1) != '/' && *(p - 1) != '\\') |
| 229 |
--p; |
| 230 |
|
| 231 |
return p; |
| 232 |
} |
| 233 |
|
| 234 |
const char* dsda_FileExtension(const char* str) |
| 235 |
{ |
| 236 |
const char* p; |
| 237 |
|
| 238 |
p = str + strlen(str); |
| 239 |
while (p > str && *(p - 1) != '.') |
| 240 |
--p; |
| 241 |
|
| 242 |
return p == str ? NULL : p; |
| 243 |
} |
| 244 |
|
| 245 |
static double dsda_DistanceLF(double x1, double y1, double x2, double y2) { |
| 246 |
return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); |
| 247 |
} |
| 248 |
|
| 249 |
double dsda_DistancePointToLine(fixed_t line_x1, fixed_t line_y1, |
| 250 |
fixed_t line_x2, fixed_t line_y2, |
| 251 |
fixed_t point_x, fixed_t point_y) { |
| 252 |
double dx, dy; |
| 253 |
double x1, x2, y1, y2, px, py; |
| 254 |
double intersect, intersect_x, intersect_y; |
| 255 |
|
| 256 |
x1 = (double) line_x1 / FRACUNIT; |
| 257 |
x2 = (double) line_x2 / FRACUNIT; |
| 258 |
y1 = (double) line_y1 / FRACUNIT; |
| 259 |
y2 = (double) line_y2 / FRACUNIT; |
| 260 |
px = (double) point_x / FRACUNIT; |
| 261 |
py = (double) point_y / FRACUNIT; |
| 262 |
|
| 263 |
if (x1 == x2 && y1 == y2) |
| 264 |
return dsda_DistanceLF(x1, y1, px, py); |
| 265 |
|
| 266 |
dx = x2 - x1; |
| 267 |
dy = y2 - y1; |
| 268 |
|
| 269 |
intersect = ((px - x1) * dx + (py - y1) * dy) / (dx * dx + dy * dy); |
| 270 |
intersect = BETWEEN(0, 1, intersect); |
| 271 |
intersect_x = x1 + intersect * dx; |
| 272 |
intersect_y = y1 + intersect * dy; |
| 273 |
|
| 274 |
return dsda_DistanceLF(intersect_x, intersect_y, px, py); |
| 275 |
} |
| 276 |
|
| 277 |
angle_t dsda_IntersectionAngle(fixed_t x, fixed_t y, |
| 278 |
fixed_t x1, fixed_t y1, |
| 279 |
fixed_t x2, fixed_t y2) { |
| 280 |
angle_t angle; |
| 281 |
|
| 282 |
angle = R_PointToAngleEx2(x, y, x2, y2) - R_PointToAngleEx2(x, y, x1, y1); |
| 283 |
|
| 284 |
return (angle > ANG180) ? (ANGLE_MAX - angle + 1) : angle; |
| 285 |
} |
| 286 |
|
| 287 |
fixed_t dsda_FixedDistancePointToLine(fixed_t line_x1, fixed_t line_y1, |
| 288 |
fixed_t line_x2, fixed_t line_y2, |
| 289 |
fixed_t point_x, fixed_t point_y, |
| 290 |
fixed_t *closest_x, fixed_t *closest_y) { |
| 291 |
angle_t angle; |
| 292 |
fixed_t line_length; |
| 293 |
fixed_t distance_to_v1; |
| 294 |
fixed_t distance_to_v2; |
| 295 |
fixed_t distance_along_line; |
| 296 |
fixed_t distance_ratio; |
| 297 |
|
| 298 |
line_length = P_AproxDistance(line_x2 - line_x1, line_y2 - line_y1); |
| 299 |
distance_to_v1 = P_AproxDistance(point_x - line_x1, point_y - line_y1); |
| 300 |
|
| 301 |
angle = dsda_IntersectionAngle(line_x1, line_y1, line_x2, line_y2, point_x, point_y); |
| 302 |
|
| 303 |
if (angle > ANG90) { |
| 304 |
*closest_x = line_x1; |
| 305 |
*closest_y = line_y1; |
| 306 |
|
| 307 |
return distance_to_v1; |
| 308 |
} |
| 309 |
|
| 310 |
distance_along_line = FixedMul(distance_to_v1, finecosine[angle >> ANGLETOFINESHIFT]); |
| 311 |
|
| 312 |
if (distance_along_line > line_length) |
| 313 |
{ |
| 314 |
distance_to_v2 = P_AproxDistance(point_x - line_x2, point_y - line_y2); |
| 315 |
|
| 316 |
*closest_x = line_x2; |
| 317 |
*closest_y = line_y2; |
| 318 |
|
| 319 |
return distance_to_v2; |
| 320 |
} |
| 321 |
|
| 322 |
distance_ratio = FixedDiv(distance_along_line, line_length); |
| 323 |
|
| 324 |
*closest_x = FixedMul(line_x2 - line_x1, distance_ratio) + line_x1; |
| 325 |
*closest_y = FixedMul(line_y2 - line_y1, distance_ratio) + line_y1; |
| 326 |
|
| 327 |
return P_AproxDistance(point_x - *closest_x, point_y - *closest_y); |
| 328 |
} |
| 329 |
|
| 330 |
fixed_t dsda_FloatToFixed(float x) |
| 331 |
{ |
| 332 |
return (fixed_t) (x * FRACUNIT); |
| 333 |
} |
| 334 |
|
| 335 |
static int pow10[8] = { |
| 336 |
10, |
| 337 |
100, |
| 338 |
1000, |
| 339 |
10000, |
| 340 |
100000, |
| 341 |
1000000, |
| 342 |
10000000, |
| 343 |
100000000, |
| 344 |
}; |
| 345 |
|
| 346 |
// Scanning a float is a lossy process, so we must go directly from string to fixed |
| 347 |
fixed_t dsda_StringToFixed(const char* x) |
| 348 |
{ |
| 349 |
dboolean negative; |
| 350 |
fixed_t result; |
| 351 |
char frac[9] = { 0 }; |
| 352 |
|
| 353 |
if (!x) |
| 354 |
return 0; |
| 355 |
|
| 356 |
result = 0; |
| 357 |
|
| 358 |
sscanf(x, "%d.%8s", &result, frac); |
| 359 |
negative = (x && x[0] == '-'); |
| 360 |
result = abs(result); |
| 361 |
result <<= FRACBITS; |
| 362 |
|
| 363 |
if (frac[0]) |
| 364 |
result += (fixed_t) ((int64_t) atoi(frac) * FRACUNIT / pow10[strlen(frac) - 1]); |
| 365 |
|
| 366 |
return negative ? -result : result; |
| 367 |
} |
| 368 |
|
| 369 |
byte dsda_FloatToPercent(float x) |
| 370 |
{ |
| 371 |
float flr; |
| 372 |
|
| 373 |
if (x > 1.f) |
| 374 |
x = 1.f; |
| 375 |
|
| 376 |
if (x < 0.f) |
| 377 |
x = 0.f; |
| 378 |
|
| 379 |
flr = floorf(x * 100); |
| 380 |
|
| 381 |
return (byte) flr; |
| 382 |
} |
| 383 |
|
| 384 |
int dsda_IntToFixed(int x) |
| 385 |
{ |
| 386 |
return (fixed_t) (x << FRACBITS); |
| 387 |
} |
| 388 |
|
| 389 |
// ANG1 is off by 256 / 360 due to rounding |
| 390 |
angle_t dsda_DegreesToAngle(float x) |
| 391 |
{ |
| 392 |
return ANG1 * x + 256 * x / 360; |
| 393 |
} |