tidied up locs code and changed loc commands to loc <args>

This commit is contained in:
Chris Ison 2001-01-22 23:23:28 +00:00
parent a348e15954
commit 6bea8babbe
3 changed files with 202 additions and 256 deletions

View file

@ -41,6 +41,7 @@
#include "client.h"
#include "console.h"
#include "locs.h"
#include "quakefs.h"
#include "qtypes.h"
#include "sys.h"
@ -53,28 +54,36 @@ int location_blocks = 0;
int locisgz = 0;
void locs_add (vec3_t location, char *name);
void locs_load (char *mapname);
void locs_load (char *filename);
void locs_free (void);
void locs_more (void);
int
locs_nearest (vec3_t loc)
{
location_t *cur;
float best_distance = 9999999, distance;
int i, j = -1;
for (i = 0; i < locations_count; i++) {
cur = locations[i];
distance = VectorDistance_fast (loc, cur->loc);
if ((distance < best_distance)) {
best_distance = distance;
j = i;
}
}
return (j);
}
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;
int i;
i = locs_nearest(target);
if (i == -1)
return NULL;
return locations[i];
}
void
@ -99,16 +108,16 @@ locs_add (vec3_t location, char *name)
}
void
locs_load (char *mapname)
locs_load (char *filename)
{
QFile *file;
char *line, *t1, *t2;
vec3_t loc;
char tmp[PATH_MAX];
char tmp[PATH_MAX];
char foundname[MAX_OSPATH];
int templength = 0;
snprintf (tmp, sizeof (tmp), "maps/%s.loc", mapname);
snprintf(tmp,sizeof(tmp), "maps/%s",filename);
templength = _COM_FOpenFile (tmp, &file, foundname, 1);
if (!file) {
Con_Printf ("Couldn't load %s\n", tmp);
@ -123,10 +132,16 @@ locs_load (char *mapname)
while ((line = Qgetline (file))) {
if (line[0] == '#')
continue;
loc[0] = strtol (line, &t1, 0) * (1.0 / 8);
if (line == t1)
continue;
loc[1] = strtol (t1, &t2, 0) * (1.0 / 8);
if (t2 == t1)
continue;
loc[2] = strtol (t2, &t1, 0) * (1.0 / 8);
if ((t1 == t2) || (strlen(t1) < 2))
continue;
t1++;
t2 = strrchr (t1, '\n');
if (t2) {
@ -175,3 +190,98 @@ locs_more (void)
if (!locations)
Sys_Error ("ERROR! Can not alloc memory for location block!");
}
void
locs_save (char *filename)
{
QFile *locfd;
int i;
char locfile[MAX_OSPATH];
if (locisgz) {
if (strncmp(filename + strlen(filename) - 3,".gz",3) != 0)
snprintf (locfile, sizeof (locfile), "%s.gz",filename);
else
strcpy(locfile,filename);
locfd = Qopen (locfile,"z9w+");
} else
locfd = Qopen (filename,"w+");
if (locfd == 0) {
Con_Printf("ERROR: Unable to open %s\n",filename);
return;
}
for (i=0; i < locations_count; i++)
Qprintf(locfd,"%.0f %.0f %.0f %s\n",
locations[i]->loc[0] * 8,
locations[i]->loc[1] * 8,
locations[i]->loc[2] * 8,
locations[i]->name);
Qclose (locfd);
}
void
locs_mark (char *filename, vec3_t loc, char *desc)
{
locs_add (loc,desc);
locs_save (filename);
Con_Printf ("Marked current location: %s\n",desc);
}
/*
locs_edit
call with description to modify location description
call with NULL description to modify location vectors
*/
void
locs_edit (char *filename, vec3_t loc, char *desc)
{
int i;
if (locations_count) {
i = locs_nearest (loc);
if (!desc) {
VectorCopy (loc,locations[i]->loc);
Con_Printf ("Moving location marker for %s\n",
locations[i]->name);
} else {
free ((void *) locations[i]->name);
locations[i]->name = strdup (desc);
Con_Printf ("Changing location description to %s\n",
locations[i]->name);
}
locs_save (filename);
} else
Con_Printf ("Error: No location markers to modify!\n");
}
void
locs_del (char *filename, vec3_t loc)
{
int i;
if (locations_count) {
i = locs_nearest (loc);
Con_Printf ("Removing location marker for %s\n",
locations[i]->name);
free ((void *) locations[i]->name);
free ((void *) locations[i]);
locations_count--;
for (; i < locations_count; i++)
locations[i] = locations[i+1];
locations[locations_count] = NULL;
locs_save(filename);
} else
Con_Printf ("Error: No location markers to remove\n");
}
void
map_to_loc (char *mapname, char *filename)
{
char *t1;
strcpy(filename, mapname);
t1 = strrchr(filename,'.');
if (!t1)
Sys_Error ("Can't find .!");
t1++;
strcpy(t1,"loc");
}