2001-08-21 22:34:20 +00:00
|
|
|
/*
|
|
|
|
pr_cmds.c
|
|
|
|
|
|
|
|
(description)
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2005-08-04 15:27:09 +00:00
|
|
|
static __attribute__ ((used)) const char rcsid[] =
|
2003-01-15 15:31:36 +00:00
|
|
|
"$Id$";
|
|
|
|
|
2001-09-10 12:56:23 +00:00
|
|
|
#ifdef HAVE_ERRNO_H
|
|
|
|
# include <errno.h>
|
|
|
|
#endif
|
2001-08-21 22:34:20 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
2001-08-26 01:08:09 +00:00
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
2001-08-22 03:24:25 +00:00
|
|
|
#include <stdlib.h>
|
2001-08-21 22:34:20 +00:00
|
|
|
|
|
|
|
#include "QF/cvar.h"
|
2004-01-04 07:42:43 +00:00
|
|
|
#include "QF/dstring.h"
|
2001-08-26 01:08:09 +00:00
|
|
|
#include "QF/mathlib.h"
|
2001-08-22 03:24:25 +00:00
|
|
|
#include "QF/progs.h"
|
2001-09-21 04:22:46 +00:00
|
|
|
#include "QF/sys.h"
|
2001-08-22 03:24:25 +00:00
|
|
|
#include "QF/zone.h"
|
2001-08-21 22:34:20 +00:00
|
|
|
|
2001-08-29 17:45:53 +00:00
|
|
|
#include "compat.h"
|
|
|
|
|
2004-01-16 05:46:19 +00:00
|
|
|
#include "QF/csqc.h"
|
2005-08-04 15:27:09 +00:00
|
|
|
#define U __attribute__ ((used))
|
2004-01-16 05:46:19 +00:00
|
|
|
static U void (*const gib_progs_init)(progs_t *) = GIB_Progs_Init;
|
|
|
|
#undef U
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE const char *pr_gametype = "";
|
2002-01-20 03:09:03 +00:00
|
|
|
|
2001-09-10 12:56:23 +00:00
|
|
|
/* BUILT-IN FUNCTIONS */
|
2001-08-21 22:34:20 +00:00
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE char *
|
2001-08-21 22:34:20 +00:00
|
|
|
PF_VarString (progs_t *pr, int first)
|
|
|
|
{
|
2007-06-09 09:43:21 +00:00
|
|
|
char *out, *dst;
|
|
|
|
const char *src;
|
2001-09-10 12:56:23 +00:00
|
|
|
int len, i;
|
2001-08-21 22:34:20 +00:00
|
|
|
|
|
|
|
for (len = 0, i = first; i < pr->pr_argc; i++)
|
2003-04-22 18:20:15 +00:00
|
|
|
len += strlen (P_GSTRING (pr, i));
|
2007-06-09 09:43:21 +00:00
|
|
|
dst = out = Hunk_TempAlloc (len + 1);
|
|
|
|
for (i = first; i < pr->pr_argc; i++) {
|
|
|
|
src = P_GSTRING (pr, i);
|
|
|
|
while (*src)
|
|
|
|
*dst++ = *src++;
|
|
|
|
}
|
|
|
|
*dst = 0;
|
2001-08-21 22:34:20 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-01-04 07:42:43 +00:00
|
|
|
vector (vector v) normalize
|
2001-08-21 22:34:20 +00:00
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-08-21 22:34:20 +00:00
|
|
|
PF_normalize (progs_t *pr)
|
|
|
|
{
|
2001-09-10 12:56:23 +00:00
|
|
|
float new;
|
|
|
|
float *value1;
|
|
|
|
vec3_t newvalue;
|
2001-08-21 22:34:20 +00:00
|
|
|
|
2002-07-24 21:42:33 +00:00
|
|
|
value1 = P_VECTOR (pr, 0);
|
2001-08-21 22:34:20 +00:00
|
|
|
|
2001-08-26 01:08:09 +00:00
|
|
|
new = value1[0] * value1[0] + value1[1] * value1[1] + value1[2] *
|
|
|
|
value1[2];
|
2001-08-21 22:34:20 +00:00
|
|
|
new = sqrt (new);
|
|
|
|
|
|
|
|
if (new == 0)
|
|
|
|
newvalue[0] = newvalue[1] = newvalue[2] = 0;
|
|
|
|
else {
|
|
|
|
new = 1 / new;
|
|
|
|
newvalue[0] = value1[0] * new;
|
|
|
|
newvalue[1] = value1[1] * new;
|
|
|
|
newvalue[2] = value1[2] * new;
|
|
|
|
}
|
|
|
|
|
2002-07-24 21:42:33 +00:00
|
|
|
VectorCopy (newvalue, R_VECTOR (pr));
|
2001-08-21 22:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-01-04 07:42:43 +00:00
|
|
|
float (vector v) vlen
|
2001-08-21 22:34:20 +00:00
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-08-21 22:34:20 +00:00
|
|
|
PF_vlen (progs_t *pr)
|
|
|
|
{
|
2001-09-10 12:56:23 +00:00
|
|
|
float new;
|
|
|
|
float *value1;
|
2001-08-21 22:34:20 +00:00
|
|
|
|
2002-07-24 21:42:33 +00:00
|
|
|
value1 = P_VECTOR (pr, 0);
|
2001-08-21 22:34:20 +00:00
|
|
|
|
2001-08-26 01:08:09 +00:00
|
|
|
new = value1[0] * value1[0] + value1[1] * value1[1] + value1[2] *
|
|
|
|
value1[2];
|
2001-08-21 22:34:20 +00:00
|
|
|
new = sqrt (new);
|
|
|
|
|
2002-07-24 21:42:33 +00:00
|
|
|
R_FLOAT (pr) = new;
|
2001-08-21 22:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-01-04 07:42:43 +00:00
|
|
|
float (vector v) vectoyaw
|
2001-08-21 22:34:20 +00:00
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-08-21 22:34:20 +00:00
|
|
|
PF_vectoyaw (progs_t *pr)
|
|
|
|
{
|
2001-09-10 12:56:23 +00:00
|
|
|
float yaw;
|
|
|
|
float *value1;
|
2001-08-21 22:34:20 +00:00
|
|
|
|
2002-07-24 21:42:33 +00:00
|
|
|
value1 = P_VECTOR (pr, 0);
|
2001-08-21 22:34:20 +00:00
|
|
|
|
|
|
|
if (value1[1] == 0 && value1[0] == 0)
|
|
|
|
yaw = 0;
|
|
|
|
else {
|
|
|
|
yaw = (int) (atan2 (value1[1], value1[0]) * 180 / M_PI);
|
|
|
|
if (yaw < 0)
|
|
|
|
yaw += 360;
|
|
|
|
}
|
|
|
|
|
2002-07-24 21:42:33 +00:00
|
|
|
R_FLOAT (pr) = yaw;
|
2001-08-21 22:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-01-04 07:42:43 +00:00
|
|
|
vector (vector v) vectoangles
|
2001-08-21 22:34:20 +00:00
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-08-21 22:34:20 +00:00
|
|
|
PF_vectoangles (progs_t *pr)
|
|
|
|
{
|
2001-09-10 12:56:23 +00:00
|
|
|
float forward, pitch, yaw;
|
|
|
|
float *value1;
|
2001-08-21 22:34:20 +00:00
|
|
|
|
2002-07-24 21:42:33 +00:00
|
|
|
value1 = P_VECTOR (pr, 0);
|
2001-08-21 22:34:20 +00:00
|
|
|
|
|
|
|
if (value1[1] == 0 && value1[0] == 0) {
|
|
|
|
yaw = 0;
|
|
|
|
if (value1[2] > 0)
|
|
|
|
pitch = 90;
|
|
|
|
else
|
|
|
|
pitch = 270;
|
|
|
|
} else {
|
|
|
|
yaw = (int) (atan2 (value1[1], value1[0]) * 180 / M_PI);
|
|
|
|
if (yaw < 0)
|
|
|
|
yaw += 360;
|
|
|
|
|
|
|
|
forward = sqrt (value1[0] * value1[0] + value1[1] * value1[1]);
|
|
|
|
pitch = (int) (atan2 (value1[2], forward) * 180 / M_PI);
|
|
|
|
if (pitch < 0)
|
|
|
|
pitch += 360;
|
|
|
|
}
|
|
|
|
|
2002-07-25 14:58:02 +00:00
|
|
|
R_VECTOR (pr)[0] = pitch;
|
|
|
|
R_VECTOR (pr)[1] = yaw;
|
|
|
|
R_VECTOR (pr)[2] = 0;
|
2001-08-21 22:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-01-04 07:42:43 +00:00
|
|
|
float () random
|
2001-08-21 22:34:20 +00:00
|
|
|
|
|
|
|
Returns a number from 0<= num < 1
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-08-21 22:34:20 +00:00
|
|
|
PF_random (progs_t *pr)
|
|
|
|
{
|
2001-09-10 12:56:23 +00:00
|
|
|
float num;
|
2001-08-21 22:34:20 +00:00
|
|
|
|
|
|
|
num = (rand () & 0x7fff) / ((float) 0x7fff);
|
|
|
|
|
2002-07-24 21:42:33 +00:00
|
|
|
R_FLOAT (pr) = num;
|
2001-08-21 22:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-01-04 07:42:43 +00:00
|
|
|
void () break
|
2001-08-21 22:34:20 +00:00
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-08-21 22:34:20 +00:00
|
|
|
PF_break (progs_t *pr)
|
|
|
|
{
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("break statement\n");
|
2002-09-07 06:48:15 +00:00
|
|
|
PR_DumpState (pr);
|
2001-08-21 22:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-01-04 07:42:43 +00:00
|
|
|
float (string s) cvar
|
2001-08-21 22:34:20 +00:00
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-08-21 22:34:20 +00:00
|
|
|
PF_cvar (progs_t *pr)
|
|
|
|
{
|
2001-09-10 12:56:23 +00:00
|
|
|
const char *str;
|
2001-08-21 22:34:20 +00:00
|
|
|
|
2003-04-22 18:20:15 +00:00
|
|
|
str = P_GSTRING (pr, 0);
|
2001-08-21 22:34:20 +00:00
|
|
|
|
2002-07-24 21:42:33 +00:00
|
|
|
R_FLOAT (pr) = Cvar_VariableValue (str);
|
2001-08-21 22:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-01-04 07:42:43 +00:00
|
|
|
void (string var, string val) cvar_set
|
2001-08-21 22:34:20 +00:00
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-08-21 22:34:20 +00:00
|
|
|
PF_cvar_set (progs_t *pr)
|
|
|
|
{
|
2001-09-10 12:56:23 +00:00
|
|
|
const char *var_name, *val;
|
|
|
|
cvar_t *var;
|
2001-08-21 22:34:20 +00:00
|
|
|
|
2003-04-22 18:20:15 +00:00
|
|
|
var_name = P_GSTRING (pr, 0);
|
|
|
|
val = P_GSTRING (pr, 1);
|
2001-08-21 22:34:20 +00:00
|
|
|
var = Cvar_FindVar (var_name);
|
|
|
|
if (!var)
|
|
|
|
var = Cvar_FindAlias (var_name);
|
|
|
|
if (!var) {
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("PF_cvar_set: variable %s not found\n", var_name);
|
2001-08-21 22:34:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cvar_Set (var, val);
|
|
|
|
}
|
|
|
|
|
2004-01-04 07:42:43 +00:00
|
|
|
/*
|
|
|
|
float (float f) fabs
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-08-21 22:34:20 +00:00
|
|
|
PF_fabs (progs_t *pr)
|
|
|
|
{
|
2001-09-10 12:56:23 +00:00
|
|
|
float v;
|
2001-08-21 22:34:20 +00:00
|
|
|
|
2002-07-24 21:42:33 +00:00
|
|
|
v = P_FLOAT (pr, 0);
|
|
|
|
R_FLOAT (pr) = fabs (v);
|
2001-08-21 22:34:20 +00:00
|
|
|
}
|
|
|
|
|
2004-01-04 07:42:43 +00:00
|
|
|
/*
|
|
|
|
entity (entity start, .(...) fld, ... match) find
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-08-22 03:24:25 +00:00
|
|
|
PF_Find (progs_t *pr)
|
2001-08-21 22:34:20 +00:00
|
|
|
{
|
2004-01-04 07:42:43 +00:00
|
|
|
const char *s = 0, *t; // ev_string
|
|
|
|
int i; // ev_vector
|
2001-09-21 19:28:08 +00:00
|
|
|
int e, f;
|
|
|
|
etype_t type;
|
2001-09-10 12:56:23 +00:00
|
|
|
ddef_t *field_def;
|
|
|
|
edict_t *ed;
|
2001-08-21 22:34:20 +00:00
|
|
|
|
2002-07-24 21:42:33 +00:00
|
|
|
e = P_EDICTNUM (pr, 0);
|
|
|
|
f = P_INT (pr, 1);
|
2004-11-05 11:49:00 +00:00
|
|
|
field_def = PR_FieldAtOfs (pr, f);
|
2001-08-26 06:35:03 +00:00
|
|
|
if (!field_def)
|
2003-01-20 21:28:36 +00:00
|
|
|
PR_RunError (pr, "PF_Find: bad search field: %d", f);
|
2001-08-26 06:35:03 +00:00
|
|
|
type = field_def->type & ~DEF_SAVEGLOBAL;
|
|
|
|
|
|
|
|
if (type == ev_string) {
|
2003-04-22 18:20:15 +00:00
|
|
|
s = P_GSTRING (pr, 2);
|
2001-08-26 06:35:03 +00:00
|
|
|
if (!s)
|
|
|
|
PR_RunError (pr, "PF_Find: bad search string");
|
|
|
|
}
|
2001-08-21 22:34:20 +00:00
|
|
|
|
2001-08-22 03:24:25 +00:00
|
|
|
for (e++; e < *pr->num_edicts; e++) {
|
2001-08-21 22:34:20 +00:00
|
|
|
ed = EDICT_NUM (pr, e);
|
|
|
|
if (ed->free)
|
|
|
|
continue;
|
2001-08-26 06:35:03 +00:00
|
|
|
switch (type) {
|
|
|
|
case ev_string:
|
2003-04-22 18:20:15 +00:00
|
|
|
t = E_GSTRING (pr, ed, f);
|
2001-08-26 06:35:03 +00:00
|
|
|
if (!t)
|
|
|
|
continue;
|
|
|
|
if (strcmp (t, s))
|
|
|
|
continue;
|
|
|
|
RETURN_EDICT (pr, ed);
|
|
|
|
return;
|
|
|
|
case ev_float:
|
2002-07-24 21:42:33 +00:00
|
|
|
if (P_FLOAT (pr, 2) != E_FLOAT (ed, f))
|
2001-08-26 06:35:03 +00:00
|
|
|
continue;
|
|
|
|
RETURN_EDICT (pr, ed);
|
|
|
|
return;
|
|
|
|
case ev_vector:
|
|
|
|
for (i = 0; i <= 2; i++)
|
2002-07-24 21:42:33 +00:00
|
|
|
if (P_FLOAT (pr, 2 + i) != E_FLOAT (ed, f + i))
|
2001-08-26 06:35:03 +00:00
|
|
|
continue;
|
|
|
|
RETURN_EDICT (pr, ed);
|
|
|
|
return;
|
|
|
|
case ev_integer:
|
|
|
|
case ev_entity:
|
2002-07-24 21:42:33 +00:00
|
|
|
if (P_INT (pr, 2) != E_INT (ed, f))
|
2001-08-26 06:35:03 +00:00
|
|
|
continue;
|
|
|
|
RETURN_EDICT (pr, ed);
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
PR_Error (pr, "PF_Find: unsupported search field");
|
2001-08-21 22:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-08-22 03:24:25 +00:00
|
|
|
RETURN_EDICT (pr, *pr->edicts);
|
2001-08-21 22:34:20 +00:00
|
|
|
}
|
|
|
|
|
2004-01-04 07:42:43 +00:00
|
|
|
/*
|
|
|
|
void () coredump
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-08-21 22:34:20 +00:00
|
|
|
PF_coredump (progs_t *pr)
|
|
|
|
{
|
|
|
|
ED_PrintEdicts (pr, "");
|
|
|
|
}
|
|
|
|
|
2004-01-04 07:42:43 +00:00
|
|
|
/*
|
|
|
|
void () traceon
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-08-21 22:34:20 +00:00
|
|
|
PF_traceon (progs_t *pr)
|
|
|
|
{
|
|
|
|
pr->pr_trace = true;
|
2004-11-07 03:00:00 +00:00
|
|
|
pr->pr_trace_depth = pr->pr_depth;
|
2001-08-21 22:34:20 +00:00
|
|
|
}
|
|
|
|
|
2004-01-04 07:42:43 +00:00
|
|
|
/*
|
|
|
|
void () traceoff
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-08-21 22:34:20 +00:00
|
|
|
PF_traceoff (progs_t *pr)
|
|
|
|
{
|
|
|
|
pr->pr_trace = false;
|
|
|
|
}
|
|
|
|
|
2004-01-04 07:42:43 +00:00
|
|
|
/*
|
|
|
|
void (entity e) eprint
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-08-21 22:34:20 +00:00
|
|
|
PF_eprint (progs_t *pr)
|
|
|
|
{
|
2002-07-24 21:42:33 +00:00
|
|
|
ED_PrintNum (pr, P_EDICTNUM (pr, 0));
|
2001-08-21 22:34:20 +00:00
|
|
|
}
|
|
|
|
|
2004-01-04 07:42:43 +00:00
|
|
|
/*
|
|
|
|
void (string s) dprint
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-08-22 03:24:25 +00:00
|
|
|
PF_dprint (progs_t *pr)
|
2001-08-21 22:34:20 +00:00
|
|
|
{
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("%s", PF_VarString (pr, 0));
|
2001-08-21 22:34:20 +00:00
|
|
|
}
|
|
|
|
|
2004-01-04 07:42:43 +00:00
|
|
|
/*
|
|
|
|
float (float v) rint
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-08-21 22:34:20 +00:00
|
|
|
PF_rint (progs_t *pr)
|
|
|
|
{
|
2001-09-10 12:56:23 +00:00
|
|
|
float f;
|
2001-08-21 22:34:20 +00:00
|
|
|
|
2002-07-24 21:42:33 +00:00
|
|
|
f = P_FLOAT (pr, 0);
|
2001-08-21 22:34:20 +00:00
|
|
|
if (f > 0)
|
2002-07-24 21:42:33 +00:00
|
|
|
R_FLOAT (pr) = (int) (f + 0.5);
|
2001-08-21 22:34:20 +00:00
|
|
|
else
|
2002-07-24 21:42:33 +00:00
|
|
|
R_FLOAT (pr) = (int) (f - 0.5);
|
2001-08-21 22:34:20 +00:00
|
|
|
}
|
|
|
|
|
2004-01-04 07:42:43 +00:00
|
|
|
/*
|
|
|
|
float (float v) floor
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-08-21 22:34:20 +00:00
|
|
|
PF_floor (progs_t *pr)
|
|
|
|
{
|
2002-07-24 21:42:33 +00:00
|
|
|
R_FLOAT (pr) = floor (P_FLOAT (pr, 0));
|
2001-08-21 22:34:20 +00:00
|
|
|
}
|
|
|
|
|
2004-01-04 07:42:43 +00:00
|
|
|
/*
|
|
|
|
float (float v) ceil
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-08-21 22:34:20 +00:00
|
|
|
PF_ceil (progs_t *pr)
|
|
|
|
{
|
2002-07-24 21:42:33 +00:00
|
|
|
R_FLOAT (pr) = ceil (P_FLOAT (pr, 0));
|
2001-08-21 22:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-01-04 07:42:43 +00:00
|
|
|
entity (entity e) nextent
|
2001-08-21 22:34:20 +00:00
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-08-21 22:34:20 +00:00
|
|
|
PF_nextent (progs_t *pr)
|
|
|
|
{
|
2001-09-10 12:56:23 +00:00
|
|
|
int i;
|
|
|
|
edict_t *ent;
|
2001-08-21 22:34:20 +00:00
|
|
|
|
2002-07-24 21:42:33 +00:00
|
|
|
i = P_EDICTNUM (pr, 0);
|
2001-08-21 22:34:20 +00:00
|
|
|
while (1) {
|
|
|
|
i++;
|
2001-08-22 03:24:25 +00:00
|
|
|
if (i == *pr->num_edicts) {
|
|
|
|
RETURN_EDICT (pr, *pr->edicts);
|
2001-08-21 22:34:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ent = EDICT_NUM (pr, i);
|
|
|
|
if (!ent->free) {
|
|
|
|
RETURN_EDICT (pr, ent);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-26 22:26:31 +00:00
|
|
|
// we assume that ints are smaller than floats
|
|
|
|
#ifdef FLT_MAX_10_EXP
|
|
|
|
# define STRING_BUF (FLT_MAX_10_EXP + 8)
|
|
|
|
#else
|
|
|
|
# define STRING_BUF 128
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
integer (float f) ftoi
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-09-26 22:26:31 +00:00
|
|
|
PF_ftoi (progs_t *pr)
|
|
|
|
{
|
2002-07-24 21:42:33 +00:00
|
|
|
R_INT (pr) = P_FLOAT (pr, 0);
|
2001-09-26 22:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
string (float f) ftos
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-09-26 22:26:31 +00:00
|
|
|
PF_ftos (progs_t *pr)
|
|
|
|
{
|
|
|
|
char string[STRING_BUF];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
// trimming 0s idea thanks to Maddes
|
2002-07-24 21:42:33 +00:00
|
|
|
i = snprintf (string, sizeof (string), "%1.6f", P_FLOAT (pr, 0)) - 1;
|
2001-09-26 22:26:31 +00:00
|
|
|
for (; i > 0; i--) {
|
|
|
|
if (string[i] == '0')
|
|
|
|
string[i] = '\0';
|
|
|
|
else if (string[i] == '.') {
|
|
|
|
string[i] = '\0';
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
RETURN_STRING (pr, string);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
float (integer i) itof
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-09-26 22:26:31 +00:00
|
|
|
PF_itof (progs_t *pr)
|
|
|
|
{
|
2002-07-24 21:42:33 +00:00
|
|
|
R_FLOAT (pr) = P_INT (pr, 0);
|
2001-09-26 22:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
string (integer i) itos
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-09-26 22:26:31 +00:00
|
|
|
PF_itos (progs_t *pr)
|
|
|
|
{
|
|
|
|
char string[STRING_BUF];
|
|
|
|
|
2002-07-24 21:42:33 +00:00
|
|
|
snprintf (string, sizeof (string), "%d", P_INT (pr, 0));
|
2001-09-26 22:26:31 +00:00
|
|
|
|
|
|
|
RETURN_STRING (pr, string);
|
|
|
|
}
|
|
|
|
|
2001-08-21 22:34:20 +00:00
|
|
|
/*
|
2001-09-26 22:26:31 +00:00
|
|
|
float (string s) stof
|
2001-08-21 22:34:20 +00:00
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-08-21 22:34:20 +00:00
|
|
|
PF_stof (progs_t *pr)
|
|
|
|
{
|
2003-04-22 18:20:15 +00:00
|
|
|
R_FLOAT (pr) = atof (P_GSTRING (pr, 0));
|
2001-09-26 22:26:31 +00:00
|
|
|
}
|
2001-08-21 22:34:20 +00:00
|
|
|
|
2001-09-26 22:26:31 +00:00
|
|
|
/*
|
|
|
|
integer (string s) stoi
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-09-26 22:26:31 +00:00
|
|
|
PF_stoi (progs_t *pr)
|
|
|
|
{
|
2003-04-22 18:20:15 +00:00
|
|
|
R_INT (pr) = atoi (P_GSTRING (pr, 0));
|
2001-09-26 22:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
vector (string s) stov
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-09-26 22:26:31 +00:00
|
|
|
PF_stov (progs_t *pr)
|
|
|
|
{
|
|
|
|
float v[3] = {0, 0, 0};
|
|
|
|
|
2003-04-22 18:20:15 +00:00
|
|
|
sscanf (P_GSTRING (pr, 0), "'%f %f %f'", v, v + 1, v + 2);
|
2001-09-26 22:26:31 +00:00
|
|
|
|
|
|
|
RETURN_VECTOR (pr, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
string (vector v) vtos
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-09-26 22:26:31 +00:00
|
|
|
PF_vtos (progs_t *pr)
|
|
|
|
{
|
|
|
|
char string[STRING_BUF * 3 + 5];
|
|
|
|
|
|
|
|
snprintf (string, sizeof (string), "'%5.1f %5.1f %5.1f'",
|
2002-07-24 21:42:33 +00:00
|
|
|
P_VECTOR (pr, 0)[0],
|
|
|
|
P_VECTOR (pr, 0)[1],
|
|
|
|
P_VECTOR (pr, 0)[2]);
|
2001-08-21 22:34:20 +00:00
|
|
|
|
2001-09-26 22:26:31 +00:00
|
|
|
RETURN_STRING (pr, string);
|
2001-08-21 22:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-01-04 07:42:43 +00:00
|
|
|
float (string s) strlen
|
2001-08-21 22:34:20 +00:00
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-08-21 22:34:20 +00:00
|
|
|
PF_strlen (progs_t *pr)
|
|
|
|
{
|
2001-09-10 12:56:23 +00:00
|
|
|
const char *s;
|
2001-08-21 22:34:20 +00:00
|
|
|
|
2003-04-22 18:20:15 +00:00
|
|
|
s = P_GSTRING (pr, 0);
|
2002-07-24 21:42:33 +00:00
|
|
|
R_FLOAT (pr) = strlen(s);
|
2001-08-21 22:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-01-04 07:42:43 +00:00
|
|
|
float (string char, string s) charcount
|
2001-08-21 22:34:20 +00:00
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-08-21 22:34:20 +00:00
|
|
|
PF_charcount (progs_t *pr)
|
|
|
|
{
|
2001-09-10 12:56:23 +00:00
|
|
|
char goal;
|
2001-08-21 22:34:20 +00:00
|
|
|
const char *s;
|
2001-09-10 12:56:23 +00:00
|
|
|
int count;
|
2001-08-21 22:34:20 +00:00
|
|
|
|
2003-04-22 18:20:15 +00:00
|
|
|
goal = (P_GSTRING (pr, 0))[0];
|
2001-08-26 00:05:31 +00:00
|
|
|
if (goal == '\0') {
|
2002-07-24 21:42:33 +00:00
|
|
|
R_FLOAT (pr) = 0;
|
2001-08-26 00:05:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2001-08-21 22:34:20 +00:00
|
|
|
|
|
|
|
count = 0;
|
2003-04-22 18:20:15 +00:00
|
|
|
s = P_GSTRING (pr, 1);
|
2001-08-21 22:34:20 +00:00
|
|
|
while (*s) {
|
|
|
|
if (*s == goal)
|
|
|
|
count++;
|
|
|
|
s++;
|
|
|
|
}
|
2002-07-24 21:42:33 +00:00
|
|
|
R_FLOAT (pr) = count;
|
2001-08-21 22:34:20 +00:00
|
|
|
}
|
|
|
|
|
2001-08-27 03:17:59 +00:00
|
|
|
#if (INT_MAX == 2147483647) && (INT_MIN == -2147483648)
|
|
|
|
# define INT_WIDTH 11
|
|
|
|
#else /* I hope... */
|
|
|
|
# define INT_WIDTH 20
|
|
|
|
#endif
|
|
|
|
|
2003-04-22 20:11:16 +00:00
|
|
|
#define MAX_ARG 7
|
2004-01-04 07:42:43 +00:00
|
|
|
/*
|
|
|
|
string (...) sprintf
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-08-27 03:17:59 +00:00
|
|
|
PF_sprintf (progs_t *pr)
|
|
|
|
{
|
2004-01-04 07:42:43 +00:00
|
|
|
const char *fmt = P_GSTRING (pr, 0);
|
|
|
|
int count = pr->pr_argc - 1;
|
|
|
|
pr_type_t **args = pr->pr_params + 1;
|
|
|
|
dstring_t *dstr;
|
2001-08-27 03:17:59 +00:00
|
|
|
|
2011-03-24 02:45:58 +00:00
|
|
|
dstr = dstring_newstr ();
|
2004-11-11 09:40:00 +00:00
|
|
|
PR_Sprintf (pr, dstr, "PF_sprintf", fmt, count, args);
|
2006-12-19 14:15:27 +00:00
|
|
|
RETURN_STRING (pr, dstr->str);
|
|
|
|
dstring_delete (dstr);
|
2001-08-27 03:17:59 +00:00
|
|
|
}
|
|
|
|
|
2004-01-04 07:42:43 +00:00
|
|
|
/*
|
|
|
|
string () gametype
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-01-20 03:09:03 +00:00
|
|
|
PR_gametype (progs_t *pr)
|
|
|
|
{
|
|
|
|
RETURN_STRING (pr, pr_gametype);
|
|
|
|
}
|
|
|
|
|
2004-11-12 11:15:00 +00:00
|
|
|
static void
|
|
|
|
PF_PR_SetField (progs_t *pr)
|
|
|
|
{
|
|
|
|
edict_t *ent = P_EDICT (pr, 0);
|
|
|
|
ddef_t *field = PR_FindField (pr, P_GSTRING (pr, 1));
|
|
|
|
const char *value = P_GSTRING (pr, 2);
|
|
|
|
|
|
|
|
R_INT (pr) = 0;
|
|
|
|
if (field)
|
|
|
|
R_INT (pr) = ED_ParseEpair (pr, ent->v, field, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
PF_PR_FindFunction (progs_t *pr)
|
|
|
|
{
|
|
|
|
dfunction_t *func = PR_FindFunction (pr, P_GSTRING (pr, 0));
|
|
|
|
R_FUNCTION (pr) = 0;
|
|
|
|
if (func)
|
|
|
|
R_FUNCTION (pr) = func - pr->pr_functions;
|
|
|
|
}
|
|
|
|
|
2004-01-06 07:00:39 +00:00
|
|
|
#define QF (PR_RANGE_QF << PR_RANGE_SHIFT) |
|
|
|
|
|
2004-01-04 07:42:43 +00:00
|
|
|
static builtin_t builtins[] = {
|
2004-11-12 11:15:00 +00:00
|
|
|
{"break", PF_break, 6},
|
|
|
|
{"random", PF_random, 7},
|
|
|
|
{"normalize", PF_normalize, 9},
|
|
|
|
{"vlen", PF_vlen, 12},
|
|
|
|
{"vectoyaw", PF_vectoyaw, 13},
|
|
|
|
{"find", PF_Find, 18},
|
|
|
|
{"dprint", PF_dprint, 25},
|
|
|
|
{"ftos", PF_ftos, 26},
|
|
|
|
{"vtos", PF_vtos, 27},
|
|
|
|
{"coredump", PF_coredump, 28},
|
|
|
|
{"traceon", PF_traceon, 29},
|
|
|
|
{"traceoff", PF_traceoff, 30},
|
|
|
|
{"eprint", PF_eprint, 31},
|
|
|
|
{"rint", PF_rint, 36},
|
|
|
|
{"floor", PF_floor, 37},
|
|
|
|
{"ceil", PF_ceil, 38},
|
|
|
|
{"fabs", PF_fabs, 43},
|
|
|
|
{"cvar", PF_cvar, 45},
|
|
|
|
{"nextent", PF_nextent, 47},
|
|
|
|
{"vectoangles", PF_vectoangles, 51},
|
|
|
|
{"cvar_set", PF_cvar_set, 72},
|
|
|
|
{"stof", PF_stof, 81},
|
|
|
|
|
|
|
|
|
|
|
|
{"strlen", PF_strlen, QF 100},
|
|
|
|
{"charcount", PF_charcount, QF 101},
|
|
|
|
{"sprintf", PF_sprintf, QF 109},
|
|
|
|
{"ftoi", PF_ftoi, QF 110},
|
|
|
|
{"itof", PF_itof, QF 111},
|
|
|
|
{"itos", PF_itos, QF 112},
|
|
|
|
{"stoi", PF_stoi, QF 113},
|
|
|
|
{"stov", PF_stov, QF 114},
|
|
|
|
{"gametype", PR_gametype, QF 115},
|
|
|
|
|
|
|
|
{"PR_SetField", PF_PR_SetField, -1},
|
|
|
|
{"PR_FindFunction", PF_PR_FindFunction, -1},
|
2004-01-06 05:51:09 +00:00
|
|
|
{0}
|
2004-01-04 07:42:43 +00:00
|
|
|
};
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2001-08-22 03:24:25 +00:00
|
|
|
PR_Cmds_Init (progs_t *pr)
|
|
|
|
{
|
2004-01-06 05:51:09 +00:00
|
|
|
PR_RegisterBuiltins (pr, builtins);
|
|
|
|
}
|