From 70657119f3bc73a1723eea649287802ceaba8ac3 Mon Sep 17 00:00:00 2001 From: "Zephaniah E. Hull" Date: Fri, 6 Oct 2000 16:31:53 +0000 Subject: [PATCH] Would help if I added locs.h and locs.c. --- include/locs.h | 39 ++++++++++++++ source/locs.c | 143 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 182 insertions(+) create mode 100644 include/locs.h create mode 100644 source/locs.c diff --git a/include/locs.h b/include/locs.h new file mode 100644 index 0000000..308bcc1 --- /dev/null +++ b/include/locs.h @@ -0,0 +1,39 @@ +/* + locs.h + + Parsing and handling of location files. + + Copyright (C) 2000 Anton Gavrilov (tonik@quake.ru) + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to: + + Free Software Foundation, Inc. + 59 Temple Place - Suite 330 + Boston, MA 02111-1307, USA + + $Id$ +*/ + +#include "qtypes.h" + +typedef struct +{ + vec3_t loc; + char *name; +} location_t; + +location_t *locs_find(vec3_t target); +void locs_load(char *mapname); +void locs_reset(); diff --git a/source/locs.c b/source/locs.c new file mode 100644 index 0000000..5471c3c --- /dev/null +++ b/source/locs.c @@ -0,0 +1,143 @@ +/* + locs.c + + Parsing and handling of location files. + + Copyright (C) 2000 Anton Gavrilov (tonik@quake.ru) + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to: + + Free Software Foundation, Inc. + 59 Temple Place - Suite 330 + Boston, MA 02111-1307, USA + + $Id$ + */ + +#include +#include + +#include "client.h" +#include "qtypes.h" +#include "sys.h" +#include "locs.h" +#include "console.h" + +#define LOCATION_BLOCK 128 // 128 locations per block. + +location_t **locations = NULL; +int locations_alloced = 0; +int locations_count = 0; +int location_blocks = 0; + +void locs_add(vec3_t location, char *name); +void locs_load(char *mapname); +void locs_free(); +void locs_more(); + +location_t *locs_find(vec3_t target) +{ + location_t *best = NULL, *cur; + float best_distance = 9999999, distance; + int i; + + for (i = 0; i < locations_count; i++) { + cur = locations[i]; + distance = VectorDistance_fast(target, cur->loc); + //distance = VectorDistance(target, cur->loc); + if ((distance < best_distance) || !best) { + best = cur; + best_distance = distance; + } + } + + return best; +} + +void locs_add(vec3_t location, char *name) +{ + int num; + + locations_count++; + if (locations_count >= locations_alloced) + locs_more(); + + num = locations_count - 1; + + locations[num] = malloc(sizeof(location_t)); + locations[num]->loc[0] = location[0]; + locations[num]->loc[1] = location[1]; + locations[num]->loc[2] = location[2]; + locations[num]->name = strdup(name); + if (!locations[num]->name) + Sys_Error("locs_add: Can't strdup name!"); +} + +void locs_load(char *mapname) +{ + QFile *file; + char *line, *t1, *t2; + vec3_t loc; + char tmp[PATH_MAX]; + + snprintf(tmp, sizeof(tmp), "maps/%s.loc", mapname); + COM_FOpenFile(tmp, &file); + if (!file) { + Con_Printf("Couldn't load %s\n", tmp); + return; + } + + while ((line = Qgetline(file))) { + loc[0] = strtol(line, &t1, 0) * (1.0/8); + loc[1] = strtol(t1, &t2, 0) * (1.0/8); + loc[2] = strtol(t2, &t1, 0) * (1.0/8); + t1++; + t2 = strrchr(t1, '\n'); + t2[0] = '\0'; + locs_add(loc, t1); + } +} + +void locs_reset() +{ + int i; + + for (i = 0; i < locations_count; i++) { + free((void *) locations[i]->name); + free((void *) locations[i]); + locations[i] = NULL; + } + + free(locations); + locations_alloced = 0; + locations_count = 0; +} + +void locs_more() +{ + size_t size; + + location_blocks++; + locations_alloced += LOCATION_BLOCK; + size = (sizeof(location_t *) * LOCATION_BLOCK * location_blocks); + + if (locations) + locations = realloc(locations, size); + else + locations = malloc(size); + + if (!locations) + Sys_Error("ERROR! Can not alloc memory for location block!"); +}