Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync wirh Q2PRO: Locations #383

Merged
merged 4 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions doc/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -1924,19 +1924,16 @@ picked up automatically.

### Locations

#### `loc_add <name ...>`
Adds new location with the specified name at current player position.

#### `loc_delete`
Deletes location closest to player position.

#### `loc_update <name ...>`
Changes name of location closest to player position.

#### `loc_write`
Saves current location list into `locs/<mapname>.loc` file.

*NOTE*: Edit locations on a local server and don't forget to execute `loc_write`
#### `loc <add|del|set|list|save>`
Execute locations editing subcommand. Available subcommands:
* `add <name>`: Adds new location with the specified _name_ at current player position.
* `del`: Deletes location closest to player position.
* `set <name>`: Sets name of location closest to player position to _name_.
* `list`: Lists all locations.
* `save [name]`: Saves current location list into `locs/<name>.loc` file.
If _name_ is omitted, uses current map name.

*NOTE*: Edit locations on a local server and don't forget to execute `loc save`
command once you are finished. Otherwise all changes to location list will be
lost on map change or disconnect.

Expand Down
136 changes: 95 additions & 41 deletions src/client/locs.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ LOC_LoadLocations
*/
void LOC_LoadLocations(void)
{
char path[MAX_OSPATH];
char path[MAX_QPATH];
char *buffer, *s, *p;
int line, count;
location_t *loc;
Expand Down Expand Up @@ -184,6 +184,7 @@ void LOC_AddLocationsToScene(void)
memset(&ent, 0, sizeof(ent));
ent.model = R_RegisterModel("models/items/c_head/tris.md2");
ent.skin = R_RegisterSkin("models/items/c_head/skin.pcx");
ent.flags = RF_NOSHADOW;

nearest = LOC_FindClosest(cl.playerEntityOrigin);
if (!nearest) {
Expand Down Expand Up @@ -214,20 +215,13 @@ LOC_Here_m
*/
static size_t LOC_Here_m(char *buffer, size_t size)
{
location_t *loc;
size_t ret;

ret = Q_strlcpy(buffer, "unknown", size);
if (cls.state != ca_active) {
return ret;
}
location_t *loc = NULL;

loc = LOC_FindClosest(cl.playerEntityOrigin);
if (loc) {
ret = Q_strlcpy(buffer, loc->name, size);
if (cls.state == ca_active) {
loc = LOC_FindClosest(cl.playerEntityOrigin);
}

return ret;
return Q_strlcpy(buffer, loc ? loc->name : "unknown", size);
}

/*
Expand All @@ -237,34 +231,28 @@ LOC_There_m
*/
static size_t LOC_There_m(char *buffer, size_t size)
{
location_t *loc;
vec3_t pos;
trace_t trace;
int ret;
location_t *loc = NULL;

ret = Q_strlcpy(buffer, "unknown", size);
if (cls.state != ca_active) {
return ret;
}
if (cls.state == ca_active) {
vec3_t pos;
trace_t trace;

VectorMA(cl.playerEntityOrigin, 8192, cl.v_forward, pos);
CM_BoxTrace(&trace, cl.playerEntityOrigin, pos, vec3_origin, vec3_origin,
cl.bsp->nodes, MASK_SOLID);
VectorMA(cl.playerEntityOrigin, 8192, cl.v_forward, pos);
CM_BoxTrace(&trace, cl.playerEntityOrigin, pos, vec3_origin,
vec3_origin, cl.bsp->nodes, MASK_SOLID);

loc = LOC_FindClosest(trace.endpos);
if (loc) {
ret = Q_strlcpy(buffer, loc->name, size);
loc = LOC_FindClosest(trace.endpos);
}

return ret;
return Q_strlcpy(buffer, loc ? loc->name : "unknown", size);
}

static void LOC_Add_f(void)
{
location_t *loc;

if (Cmd_Argc() < 2) {
Com_Printf("Usage: %s <name>\n", Cmd_Argv(0));
if (Cmd_Argc() < 3) {
Com_Printf("Usage: %s <name>\n", Cmd_ArgsRange(0, 1));
return;
}

Expand All @@ -273,9 +261,11 @@ static void LOC_Add_f(void)
return;
}

loc = LOC_Alloc(Cmd_Args());
loc = LOC_Alloc(Cmd_ArgsFrom(2));
VectorCopy(cl.playerEntityOrigin, loc->origin);
List_Append(&cl_locations, &loc->entry);

Com_Printf("Added location %s at %s\n", loc->name, vtos(loc->origin));
}

static void LOC_Delete_f(void)
Expand All @@ -293,16 +283,17 @@ static void LOC_Delete_f(void)
return;
}

Com_Printf("Deleted location %s at %s\n", loc->name, vtos(loc->origin));
List_Remove(&loc->entry);
Z_Free(loc);
}

static void LOC_Update_f(void)
static void LOC_Set_f(void)
{
location_t *oldloc, *newloc;

if (Cmd_Argc() < 2) {
Com_Printf("Usage: %s <name>\n", Cmd_Argv(0));
if (Cmd_Argc() < 3) {
Com_Printf("Usage: %s <name>\n", Cmd_ArgsRange(0, 1));
return;
}

Expand All @@ -317,15 +308,42 @@ static void LOC_Update_f(void)
return;
}

newloc = LOC_Alloc(Cmd_Args());
newloc = LOC_Alloc(Cmd_ArgsFrom(2));
VectorCopy(oldloc->origin, newloc->origin);
List_Link(oldloc->entry.prev, oldloc->entry.next, &newloc->entry);
Z_Free(oldloc);

Com_Printf("Renamed location at %s to %s\n", vtos(newloc->origin), newloc->name);
}

static void LOC_Write_f(void)
static void LOC_List_f(void)
{
char buffer[MAX_OSPATH];
location_t *loc;
int count;

if (cls.state != ca_active) {
Com_Printf("Must be in a level.\n");
return;
}

if (LIST_EMPTY(&cl_locations)) {
Com_Printf("No locations to list.\n");
return;
}

count = 0;
LIST_FOR_EACH(location_t, loc, &cl_locations, entry) {
Com_Printf("%s at %s\n", loc->name, vtos(loc->origin));
count++;
}

Com_Printf("%d location%s listed\n", count, count == 1 ? "" : "s");
}

static void LOC_Save_f(void)
{
char buffer[MAX_QPATH];
const char *name;
location_t *loc;
qhandle_t f;
int count;
Expand All @@ -340,8 +358,13 @@ static void LOC_Write_f(void)
return;
}

if (Cmd_Argc() > 2)
name = Cmd_Argv(2);
else
name = cl.mapname;

f = FS_EasyOpenFile(buffer, sizeof(buffer), FS_MODE_WRITE | FS_FLAG_TEXT,
"locs/", cl.mapname, ".loc");
"locs/", name, ".loc");
if (!f) {
return;
}
Expand All @@ -363,6 +386,40 @@ static void LOC_Write_f(void)
count, count == 1 ? "" : "s", buffer);
}

static void LOC_Cmd_g(genctx_t *ctx, int argnum)
{
if (argnum == 1) {
Prompt_AddMatch(ctx, "add");
Prompt_AddMatch(ctx, "del");
Prompt_AddMatch(ctx, "set");
Prompt_AddMatch(ctx, "list");
Prompt_AddMatch(ctx, "save");
}
}

static void LOC_Cmd_f(void)
{
const char *cmd = Cmd_Argv(1);

if (!strcmp(cmd, "add"))
LOC_Add_f();
else if (!strcmp(cmd, "del"))
LOC_Delete_f();
else if (!strcmp(cmd, "set"))
LOC_Set_f();
else if (!strcmp(cmd, "list"))
LOC_List_f();
else if (!strcmp(cmd, "save"))
LOC_Save_f();
else
Com_Printf("Usage: %s <add|del|set|list|save>\n", Cmd_Argv(0));
}

static const cmdreg_t c_loc[] = {
{ "loc", LOC_Cmd_f, LOC_Cmd_g },
{ NULL }
};

/*
==============
LOC_Init
Expand All @@ -377,9 +434,6 @@ void LOC_Init(void)
Cmd_AddMacro("loc_here", LOC_Here_m);
Cmd_AddMacro("loc_there", LOC_There_m);

Cmd_AddCommand("loc_add", LOC_Add_f);
Cmd_AddCommand("loc_delete", LOC_Delete_f);
Cmd_AddCommand("loc_update", LOC_Update_f);
Cmd_AddCommand("loc_write", LOC_Write_f);
Cmd_Register(c_loc);
}

Loading