home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / sviluppo / quake_src / pr_cmds2.c < prev    next >
C/C++ Source or Header  |  2000-06-17  |  37KB  |  1,857 lines

  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  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.  
  12.  
  13. See the GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. */
  20.  
  21. #include "quakedef.h"
  22.  
  23. #define RETURN_EDICT(e) (((int *)pr_globals)[OFS_RETURN] = EDICT_TO_PROG(e))
  24.  
  25. extern int PF_newcheckclient (int check);
  26. extern void PF_checkclient (void);
  27.  
  28.  
  29. /*
  30. ===============================================================================
  31.  
  32.             BUILT-IN FUNCTIONS
  33.  
  34. ===============================================================================
  35. */
  36.  
  37. extern byte  checkpvs[MAX_MAP_LEAFS/8];
  38.  
  39. int PF_newcheckclient (int check)
  40. {
  41.   int   i;
  42.   byte  *pvs;
  43.   edict_t *ent;
  44.   mleaf_t *leaf;
  45.   vec3_t  org;
  46.  
  47. // cycle to the next one
  48.  
  49.   if (check < 1)
  50.     check = 1;
  51.   if (check > svs.maxclients)
  52.     check = svs.maxclients;
  53.  
  54.   if (check == svs.maxclients)
  55.     i = 1;
  56.   else
  57.     i = check + 1;
  58.  
  59.   for ( ;  ; i++)
  60.   {
  61.     if (i == svs.maxclients+1)
  62.       i = 1;
  63.  
  64.     ent = EDICT_NUM(i);
  65.  
  66.     if (i == check)
  67.       break;  // didn't find anything else
  68.  
  69.     if (ent->free)
  70.       continue;
  71.     if (ent->v.health <= 0)
  72.       continue;
  73.     if ((int)ent->v.flags & FL_NOTARGET)
  74.       continue;
  75.  
  76.   // anything that is a client, or has a client as an enemy
  77.     break;
  78.   }
  79.  
  80. // get the PVS for the entity
  81.   VectorAdd (ent->v.origin, ent->v.view_ofs, org);
  82.   leaf = Mod_PointInLeaf (org, sv.worldmodel);
  83.   pvs = Mod_LeafPVS (leaf, sv.worldmodel);
  84.   memcpy (checkpvs, pvs, (sv.worldmodel->numleafs+7)>>3 );
  85.  
  86.   return i;
  87. }
  88.  
  89.  
  90.  
  91. char *PF_VarString (int first)
  92. {
  93.   int   i;
  94.   static char out[256];
  95.   
  96.   out[0] = 0;
  97.   for (i=first ; i<pr_argc ; i++)
  98.   {
  99.     strcat (out, G_STRING((OFS_PARM0+i*3)));
  100.   }
  101.   return out;
  102. }
  103.  
  104.  
  105. /*
  106. =================
  107. PF_errror
  108.  
  109. This is a TERMINAL error, which will kill off the entire server.
  110. Dumps self.
  111.  
  112. error(value)
  113. =================
  114. */
  115. void PF_error (void)
  116. {
  117.   char  *s;
  118.   edict_t *ed;
  119.   
  120.   s = PF_VarString(0);
  121.   Con_Printf ("======SERVER ERROR in %s:\n%s\n"
  122.   ,pr_strings + pr_xfunction->s_name,s);
  123.   ed = PROG_TO_EDICT(pr_global_struct->self);
  124.   ED_Print (ed);
  125.  
  126.   Host_Error ("Program error");
  127. }
  128.  
  129. /*
  130. =================
  131. PF_objerror
  132.  
  133. Dumps out self, then an error message.  The program is aborted and self is
  134. removed, but the level can continue.
  135.  
  136. objerror(value)
  137. =================
  138. */
  139. void PF_objerror (void)
  140. {
  141.   char  *s;
  142.   edict_t *ed;
  143.   
  144.   s = PF_VarString(0);
  145.   Con_Printf ("======OBJECT ERROR in %s:\n%s\n"
  146.   ,pr_strings + pr_xfunction->s_name,s);
  147.   ed = PROG_TO_EDICT(pr_global_struct->self);
  148.   ED_Print (ed);
  149.   ED_Free (ed);
  150.   
  151.   Host_Error ("Program error");
  152. }
  153.  
  154.  
  155.  
  156. /*
  157. ==============
  158. PF_makevectors
  159.  
  160. Writes new values for v_forward, v_up, and v_right based on angles
  161. makevectors(vector)
  162. ==============
  163. */
  164. void PF_makevectors (void)
  165. {
  166.   AngleVectors (G_VECTOR(OFS_PARM0), pr_global_struct->v_forward, pr_global_struct->v_right, pr_global_struct->v_up);
  167. }
  168.  
  169. /*
  170. =================
  171. PF_setorigin
  172.  
  173. This is the only valid way to move an object without using the physics of the world (setting velocity and waiting).  Directly changing origin will not set internal links correctly, so clipping would be messed up.  This should be called when an object is spawned, and then only if it is teleported.
  174.  
  175. setorigin (entity, origin)
  176. =================
  177. */
  178. void PF_setorigin (void)
  179. {
  180.   edict_t *e;
  181.   float *org;
  182.   
  183.   e = G_EDICT(OFS_PARM0);
  184.   org = G_VECTOR(OFS_PARM1);
  185.   VectorCopy (org, e->v.origin);
  186.   SV_LinkEdict (e, false);
  187. }
  188.  
  189.  
  190. void SetMinMaxSize (edict_t *e, float *min, float *max, qboolean rotate)
  191. {
  192.   float *angles;
  193.   vec3_t  rmin, rmax;
  194.   float bounds[2][3];
  195.   float xvector[2], yvector[2];
  196.   float a;
  197.   vec3_t  base, transformed;
  198.   int   i, j, k, l;
  199.   
  200.   for (i=0 ; i<3 ; i++)
  201.     if (min[i] > max[i])
  202.       PR_RunError ("backwards mins/maxs");
  203.  
  204.   rotate = false;   // FIXME: implement rotation properly again
  205.  
  206.   if (!rotate)
  207.   {
  208.     VectorCopy (min, rmin);
  209.     VectorCopy (max, rmax);
  210.   }
  211.   else
  212.   {
  213.   // find min / max for rotations
  214.     angles = e->v.angles;
  215.     
  216.     a = angles[1]/180 * M_PI;
  217.     
  218.     xvector[0] = cos(a);
  219.     xvector[1] = sin(a);
  220.     yvector[0] = -sin(a);
  221.     yvector[1] = cos(a);
  222.     
  223.     VectorCopy (min, bounds[0]);
  224.     VectorCopy (max, bounds[1]);
  225.     
  226.     rmin[0] = rmin[1] = rmin[2] = 9999;
  227.     rmax[0] = rmax[1] = rmax[2] = -9999;
  228.     
  229.     for (i=0 ; i<= 1 ; i++)
  230.     {
  231.       base[0] = bounds[i][0];
  232.       for (j=0 ; j<= 1 ; j++)
  233.       {
  234.         base[1] = bounds[j][1];
  235.         for (k=0 ; k<= 1 ; k++)
  236.         {
  237.           base[2] = bounds[k][2];
  238.           
  239.         // transform the point
  240.           transformed[0] = xvector[0]*base[0] + yvector[0]*base[1];
  241.           transformed[1] = xvector[1]*base[0] + yvector[1]*base[1];
  242.           transformed[2] = base[2];
  243.           
  244.           for (l=0 ; l<3 ; l++)
  245.           {
  246.             if (transformed[l] < rmin[l])
  247.               rmin[l] = transformed[l];
  248.             if (transformed[l] > rmax[l])
  249.               rmax[l] = transformed[l];
  250.           }
  251.         }
  252.       }
  253.     }
  254.   }
  255.   
  256. // set derived values
  257.   VectorCopy (rmin, e->v.mins);
  258.   VectorCopy (rmax, e->v.maxs);
  259.   VectorSubtract (max, min, e->v.size);
  260.   
  261.   SV_LinkEdict (e, false);
  262. }
  263.  
  264.  
  265.  
  266. /*
  267. =================
  268. PF_setsize
  269.  
  270. the size box is rotated by the current angle
  271.  
  272. setsize (entity, minvector, maxvector)
  273. =================
  274. */
  275. void PF_setsize (void)
  276. {
  277.   edict_t *e;
  278.   float *min, *max;
  279.   
  280.   e = G_EDICT(OFS_PARM0);
  281.   min = G_VECTOR(OFS_PARM1);
  282.   max = G_VECTOR(OFS_PARM2);
  283.   SetMinMaxSize (e, min, max, false);
  284. }
  285.  
  286.  
  287. /*
  288. =================
  289. PF_setmodel
  290.  
  291. setmodel(entity, model)
  292. =================
  293. */
  294. void PF_setmodel (void)
  295. {
  296.   edict_t *e;
  297.   char  *m, **check;
  298.   model_t *mod;
  299.   int   i;
  300.  
  301.   e = G_EDICT(OFS_PARM0);
  302.   m = G_STRING(OFS_PARM1);
  303.  
  304. // check to see if model was properly precached
  305.   for (i=0, check = sv.model_precache ; *check ; i++, check++)
  306.     if (!strcmp(*check, m))
  307.       break;
  308.       
  309.   if (!*check)
  310.     PR_RunError ("no precache: %s\n", m);
  311.     
  312.  
  313.   e->v.model = m - pr_strings;
  314.   e->v.modelindex = i; //SV_ModelIndex (m);
  315.  
  316.   mod = sv.models[ (int)e->v.modelindex];  // Mod_ForName (m, true);
  317.   
  318.   if (mod)
  319.     SetMinMaxSize (e, mod->mins, mod->maxs, true);
  320.   else
  321.     SetMinMaxSize (e, vec3_origin, vec3_origin, true);
  322. }
  323.  
  324. /*
  325. =================
  326. PF_bprint
  327.  
  328. broadcast print to everyone on server
  329.  
  330. bprint(value)
  331. =================
  332. */
  333. void PF_bprint (void)
  334. {
  335.   char    *s;
  336.  
  337.   s = PF_VarString(0);
  338.   SV_BroadcastPrintf ("%s", s);
  339. }
  340.  
  341. /*
  342. =================
  343. PF_sprint
  344.  
  345. single print to a specific client
  346.  
  347. sprint(clientent, value)
  348. =================
  349. */
  350. void PF_sprint (void)
  351. {
  352.   char    *s;
  353.   client_t  *client;
  354.   int     entnum;
  355.   
  356.   entnum = G_EDICTNUM(OFS_PARM0);
  357.   s = PF_VarString(1);
  358.   
  359.   if (entnum < 1 || entnum > svs.maxclients)
  360.   {
  361.     Con_Printf ("tried to sprint to a non-client\n");
  362.     return;
  363.   }
  364.     
  365.   client = &svs.clients[entnum-1];
  366.     
  367.   MSG_WriteChar (&client->message,svc_print);
  368.   MSG_WriteString (&client->message, s );
  369. }
  370.  
  371.  
  372. /*
  373. =================
  374. PF_centerprint
  375.  
  376. single print to a specific client
  377.  
  378. centerprint(clientent, value)
  379. =================
  380. */
  381. void PF_centerprint (void)
  382. {
  383.   char    *s;
  384.   client_t  *client;
  385.   int     entnum;
  386.   
  387.   entnum = G_EDICTNUM(OFS_PARM0);
  388.   s = PF_VarString(1);
  389.   
  390.   if (entnum < 1 || entnum > svs.maxclients)
  391.   {
  392.     Con_Printf ("tried to sprint to a non-client\n");
  393.     return;
  394.   }
  395.     
  396.   client = &svs.clients[entnum-1];
  397.     
  398.   MSG_WriteChar (&client->message,svc_centerprint);
  399.   MSG_WriteString (&client->message, s );
  400. }
  401.  
  402.  
  403.  
  404. /*
  405. =================
  406. PF_vlen
  407.  
  408. scalar vlen(vector)
  409. =================
  410. */
  411. void PF_vlen (void)
  412. {
  413.   float *value1;
  414.   float new;
  415.   
  416.   value1 = G_VECTOR(OFS_PARM0);
  417.  
  418.   new = value1[0] * value1[0] + value1[1] * value1[1] + value1[2]*value1[2];
  419.   new = sqrt(new);
  420.   
  421.   G_FLOAT(OFS_RETURN) = new;
  422. }
  423.  
  424. /*
  425. =================
  426. PF_vectoyaw
  427.  
  428. float vectoyaw(vector)
  429. =================
  430. */
  431. void PF_vectoyaw (void)
  432. {
  433.   float *value1;
  434.   float yaw;
  435.   
  436.   value1 = G_VECTOR(OFS_PARM0);
  437.  
  438.   if (value1[1] == 0 && value1[0] == 0)
  439.     yaw = 0;
  440.   else
  441.   {
  442.     yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
  443.     if (yaw < 0)
  444.       yaw += 360;
  445.   }
  446.  
  447.   G_FLOAT(OFS_RETURN) = yaw;
  448. }
  449.  
  450.  
  451. /*
  452. =================
  453. PF_vectoangles
  454.  
  455. vector vectoangles(vector)
  456. =================
  457. */
  458. void PF_vectoangles (void)
  459. {
  460.   float *value1;
  461.   float forward;
  462.   float yaw, pitch;
  463.   
  464.   value1 = G_VECTOR(OFS_PARM0);
  465.  
  466.   if (value1[1] == 0 && value1[0] == 0)
  467.   {
  468.     yaw = 0;
  469.     if (value1[2] > 0)
  470.       pitch = 90;
  471.     else
  472.       pitch = 270;
  473.   }
  474.   else
  475.   {
  476.     yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
  477.     if (yaw < 0)
  478.       yaw += 360;
  479.  
  480.     forward = sqrt (value1[0]*value1[0] + value1[1]*value1[1]);
  481.     pitch = (int) (atan2(value1[2], forward) * 180 / M_PI);
  482.     if (pitch < 0)
  483.       pitch += 360;
  484.   }
  485.  
  486.   G_FLOAT(OFS_RETURN+0) = pitch;
  487.   G_FLOAT(OFS_RETURN+1) = yaw;
  488.   G_FLOAT(OFS_RETURN+2) = 0;
  489. }
  490.  
  491. /*
  492. =================
  493. PF_Random
  494.  
  495. Returns a number from 0<= num < 1
  496.  
  497. random()
  498. =================
  499. */
  500. void PF_random (void)
  501. {
  502.   float   num;
  503.     
  504.   num = (rand ()&0x7fff) / ((float)0x7fff);
  505.   
  506.   G_FLOAT(OFS_RETURN) = num;
  507. }
  508.  
  509. /*
  510. =================
  511. PF_particle
  512.  
  513. particle(origin, color, count)
  514. =================
  515. */
  516. void PF_particle (void)
  517. {
  518.   float   *org, *dir;
  519.   float   color;
  520.   float   count;
  521.       
  522.   org = G_VECTOR(OFS_PARM0);
  523.   dir = G_VECTOR(OFS_PARM1);
  524.   color = G_FLOAT(OFS_PARM2);
  525.   count = G_FLOAT(OFS_PARM3);
  526.   SV_StartParticle (org, dir, color, count);
  527. }
  528.  
  529.  
  530. /*
  531. =================
  532. PF_ambientsound
  533.  
  534. =================
  535. */
  536. void PF_ambientsound (void)
  537. {
  538.   char    **check;
  539.   char    *samp;
  540.   float   *pos;
  541.   float     vol, attenuation;
  542.   int     i, soundnum;
  543.  
  544.   pos = G_VECTOR (OFS_PARM0);     
  545.   samp = G_STRING(OFS_PARM1);
  546.   vol = G_FLOAT(OFS_PARM2);
  547.   attenuation = G_FLOAT(OFS_PARM3);
  548.   
  549. // check to see if samp was properly precached
  550.   for (soundnum=0, check = sv.sound_precache ; *check ; check++, soundnum++)
  551.     if (!strcmp(*check,samp))
  552.       break;
  553.       
  554.   if (!*check)
  555.   {
  556.     Con_Printf ("no precache: %s\n", samp);
  557.     return;
  558.   }
  559.  
  560. // add an svc_spawnambient command to the level signon packet
  561.  
  562.   MSG_WriteByte (&sv.signon,svc_spawnstaticsound);
  563.   for (i=0 ; i<3 ; i++)
  564.     MSG_WriteCoord(&sv.signon, pos[i]);
  565.  
  566.   MSG_WriteByte (&sv.signon, soundnum);
  567.  
  568.   MSG_WriteByte (&sv.signon, vol*255);
  569.   MSG_WriteByte (&sv.signon, attenuation*64);
  570.  
  571. }
  572.  
  573. /*
  574. =================
  575. PF_sound
  576.  
  577. Each entity can have eight independant sound sources, like voice,
  578. weapon, feet, etc.
  579.  
  580. Channel 0 is an auto-allocate channel, the others override anything
  581. allready running on that entity/channel pair.
  582.  
  583. An attenuation of 0 will play full volume everywhere in the level.
  584. Larger attenuations will drop off.
  585.  
  586. =================
  587. */
  588. void PF_sound (void)
  589. {
  590.   char    *sample;
  591.   int     channel;
  592.   edict_t   *entity;
  593.   int     volume;
  594.   float attenuation;
  595.     
  596.   entity = G_EDICT(OFS_PARM0);
  597.   channel = G_FLOAT(OFS_PARM1);
  598.   sample = G_STRING(OFS_PARM2);
  599.   volume = G_FLOAT(OFS_PARM3) * 255;
  600.   attenuation = G_FLOAT(OFS_PARM4);
  601.   
  602.   if (volume < 0 || volume > 255)
  603.     Sys_Error ("SV_StartSound: volume = %i", volume);
  604.  
  605.   if (attenuation < 0 || attenuation > 4)
  606.     Sys_Error ("SV_StartSound: attenuation = %f", attenuation);
  607.  
  608.   if (channel < 0 || channel > 7)
  609.     Sys_Error ("SV_StartSound: channel = %i", channel);
  610.  
  611.   SV_StartSound (entity, channel, sample, volume, attenuation);
  612. }
  613.  
  614. /*
  615. =================
  616. PF_break
  617.  
  618. break()
  619. =================
  620. */
  621. void PF_break (void)
  622. {
  623. Con_Printf ("break statement\n");
  624. *(int *)-4 = 0; // dump to debugger
  625. //  PR_RunError ("break statement");
  626. }
  627.  
  628.  
  629. /*
  630. =================
  631. PF_checkpos
  632.  
  633. Returns true if the given entity can move to the given position from it's
  634. current position by walking or rolling.
  635. FIXME: make work...
  636. scalar checkpos (entity, vector)
  637. =================
  638. */
  639. void PF_checkpos (void)
  640. {
  641. }
  642.  
  643.  
  644. /*
  645. =================
  646. PF_normalize
  647.  
  648. vector normalize(vector)
  649. =================
  650. */
  651. void PF_normalize (void)
  652. {
  653.   float *value1;
  654.   vec3_t  newvalue;
  655.   float new;
  656.   
  657.   value1 = G_VECTOR(OFS_PARM0);
  658.  
  659.   new = value1[0] * value1[0] + value1[1] * value1[1] + value1[2]*value1[2];
  660.   new = sqrt(new);
  661.   
  662.   if (new == 0)
  663.     newvalue[0] = newvalue[1] = newvalue[2] = 0;
  664.   else
  665.   {
  666.     new = 1/new;
  667.     newvalue[0] = value1[0] * new;
  668.     newvalue[1] = value1[1] * new;
  669.     newvalue[2] = value1[2] * new;
  670.   }
  671.   
  672.   VectorCopy (newvalue, G_VECTOR(OFS_RETURN));  
  673. }
  674.  
  675.  
  676. /*
  677. =================
  678. PF_traceline
  679.  
  680. Used for use tracing and shot targeting
  681. Traces are blocked by bbox and exact bsp entityes, and also slide box entities
  682. if the tryents flag is set.
  683.  
  684. traceline (vector1, vector2, tryents)
  685. =================
  686. */
  687. void PF_traceline (void)
  688. {
  689.   float *v1, *v2;
  690.   trace_t trace;
  691.   int   nomonsters;
  692.   edict_t *ent;
  693.  
  694.   v1 = G_VECTOR(OFS_PARM0);
  695.   v2 = G_VECTOR(OFS_PARM1);
  696.   nomonsters = G_FLOAT(OFS_PARM2);
  697.   ent = G_EDICT(OFS_PARM3);
  698.  
  699.   trace = SV_Move (v1, vec3_origin, vec3_origin, v2, nomonsters, ent);
  700.  
  701.   pr_global_struct->trace_allsolid = trace.allsolid;
  702.   pr_global_struct->trace_startsolid = trace.startsolid;
  703.   pr_global_struct->trace_fraction = trace.fraction;
  704.   pr_global_struct->trace_inwater = trace.inwater;
  705.   pr_global_struct->trace_inopen = trace.inopen;
  706.   VectorCopy (trace.endpos, pr_global_struct->trace_endpos);
  707.   VectorCopy (trace.plane.normal, pr_global_struct->trace_plane_normal);
  708.   pr_global_struct->trace_plane_dist =  trace.plane.dist; 
  709.   if (trace.ent)
  710.     pr_global_struct->trace_ent = EDICT_TO_PROG(trace.ent);
  711.   else
  712.     pr_global_struct->trace_ent = EDICT_TO_PROG(sv.edicts);
  713. }
  714.  
  715.  
  716. /*
  717. =============
  718. PF_aim
  719.  
  720. Pick a vector for the player to shoot along
  721. vector aim(entity, missilespeed)
  722. =============
  723. */
  724. cvar_t  sv_aim = {"sv_aim", "0.93"};
  725. void PF_aim (void)
  726. {
  727.   edict_t *ent, *check, *bestent;
  728.   vec3_t  start, dir, end, bestdir;
  729.   int   i, j;
  730.   trace_t tr;
  731.   float dist, bestdist;
  732.   float speed;
  733.   
  734.   ent = G_EDICT(OFS_PARM0);
  735.   speed = G_FLOAT(OFS_PARM1);
  736.  
  737.   VectorCopy (ent->v.origin, start);
  738.   start[2] += 20;
  739.  
  740. // try sending a trace straight
  741.   VectorCopy (pr_global_struct->v_forward, dir);
  742.   VectorMA (start, 2048, dir, end);
  743.   tr = SV_Move (start, vec3_origin, vec3_origin, end, false, ent);
  744.   if (tr.ent && tr.ent->v.takedamage == DAMAGE_AIM
  745.   && (!teamplay.value || ent->v.team <=0 || ent->v.team != tr.ent->v.team) )
  746.   {
  747.     VectorCopy (pr_global_struct->v_forward, G_VECTOR(OFS_RETURN));
  748.     return;
  749.   }
  750.  
  751.  
  752. // try all possible entities
  753.   VectorCopy (dir, bestdir);
  754.   bestdist = sv_aim.value;
  755.   bestent = NULL;
  756.   
  757.   check = NEXT_EDICT(sv.edicts);
  758.   for (i=1 ; i<sv.num_edicts ; i++, check = NEXT_EDICT(check) )
  759.   {
  760.     if (check->v.takedamage != DAMAGE_AIM)
  761.       continue;
  762.     if (check == ent)
  763.       continue;
  764.     if (teamplay.value && ent->v.team > 0 && ent->v.team == check->v.team)
  765.       continue; // don't aim at teammate
  766.     for (j=0 ; j<3 ; j++)
  767.       end[j] = check->v.origin[j]
  768.       + 0.5*(check->v.mins[j] + check->v.maxs[j]);
  769.     VectorSubtract (end, start, dir);
  770.     VectorNormalize (dir);
  771.     dist = DotProduct (dir, pr_global_struct->v_forward);
  772.     if (dist < bestdist)
  773.       continue; // to far to turn
  774.     tr = SV_Move (start, vec3_origin, vec3_origin, end, false, ent);
  775.     if (tr.ent == check)
  776.     { // can shoot at this one
  777.       bestdist = dist;
  778.       bestent = check;
  779.     }
  780.   }
  781.   
  782.   if (bestent)
  783.   {
  784.     VectorSubtract (bestent->v.origin, ent->v.origin, dir);
  785.     dist = DotProduct (dir, pr_global_struct->v_forward);
  786.     VectorScale (pr_global_struct->v_forward, dist, end);
  787.     end[2] = dir[2];
  788.     VectorNormalize (end);
  789.     VectorCopy (end, G_VECTOR(OFS_RETURN)); 
  790.   }
  791.   else
  792.   {
  793.     VectorCopy (bestdir, G_VECTOR(OFS_RETURN));
  794.   }
  795. }
  796.  
  797. /*
  798. =================
  799. PF_stuffcmd
  800.  
  801. Sends text over to the client's execution buffer
  802.  
  803. stuffcmd (clientent, value)
  804. =================
  805. */
  806. void PF_stuffcmd (void)
  807. {
  808.   int   entnum;
  809.   char  *str;
  810.   client_t  *old;
  811.   
  812.   entnum = G_EDICTNUM(OFS_PARM0);
  813.   if (entnum < 1 || entnum > svs.maxclients)
  814.     PR_RunError ("Parm 0 not a client");
  815.   str = G_STRING(OFS_PARM1);  
  816.   
  817.   old = host_client;
  818.   host_client = &svs.clients[entnum-1];
  819.   Host_ClientCommands ("%s", str);
  820.   host_client = old;
  821. }
  822.  
  823. /*
  824. =================
  825. PF_localcmd
  826.  
  827. Sends text over to the client's execution buffer
  828.  
  829. localcmd (string)
  830. =================
  831. */
  832. void PF_localcmd (void)
  833. {
  834.   char  *str;
  835.   
  836.   str = G_STRING(OFS_PARM0);  
  837.   Cbuf_AddText (str);
  838. }
  839.  
  840. /*
  841. =================
  842. PF_cvar
  843.  
  844. float cvar (string)
  845. =================
  846. */
  847. void PF_cvar (void)
  848. {
  849.   char  *str;
  850.   
  851.   str = G_STRING(OFS_PARM0);
  852.   
  853.   G_FLOAT(OFS_RETURN) = Cvar_VariableValue (str);
  854. }
  855.  
  856. /*
  857. =================
  858. PF_cvar_set
  859.  
  860. float cvar (string)
  861. =================
  862. */
  863. void PF_cvar_set (void)
  864. {
  865.   char  *var, *val;
  866.   
  867.   var = G_STRING(OFS_PARM0);
  868.   val = G_STRING(OFS_PARM1);
  869.   
  870.   Cvar_Set (var, val);
  871. }
  872.  
  873. /*
  874. =================
  875. PF_findradius
  876.  
  877. Returns a chain of entities that have origins within a spherical area
  878.  
  879. findradius (origin, radius)
  880. =================
  881. */
  882. void PF_findradius (void)
  883. {
  884.   edict_t *ent, *chain;
  885.   float rad;
  886.   float *org;
  887.   vec3_t  eorg;
  888.   int   i, j;
  889.  
  890.   chain = (edict_t *)sv.edicts;
  891.   
  892.   org = G_VECTOR(OFS_PARM0);
  893.   rad = G_FLOAT(OFS_PARM1);
  894.  
  895.   ent = NEXT_EDICT(sv.edicts);
  896.   for (i=1 ; i<sv.num_edicts ; i++, ent = NEXT_EDICT(ent))
  897.   {
  898.     if (ent->free)
  899.       continue;
  900.     if (ent->v.solid == SOLID_NOT)
  901.       continue;
  902.     for (j=0 ; j<3 ; j++)
  903.       eorg[j] = org[j] - (ent->v.origin[j] + (ent->v.mins[j] + ent->v.maxs[j])*0.5);      
  904.     if (Length(eorg) > rad)
  905.       continue;
  906.       
  907.     ent->v.chain = EDICT_TO_PROG(chain);
  908.     chain = ent;
  909.   }
  910.  
  911.   RETURN_EDICT(chain);
  912. }
  913.  
  914.  
  915. /*
  916. =========
  917. PF_dprint
  918. =========
  919. */
  920. void PF_dprint (void)
  921. {
  922.   Con_DPrintf ("%s",PF_VarString(0));
  923. }
  924.  
  925. char  pr_string_temp[128];
  926.  
  927. void PF_ftos (void)
  928. {
  929.   float v;
  930.   v = G_FLOAT(OFS_PARM0);
  931.   
  932.   if (v == (int)v)
  933.     sprintf (pr_string_temp, "%d",(int)v);
  934.   else
  935.     sprintf (pr_string_temp, "%5.1f",v);
  936.   G_INT(OFS_RETURN) = pr_string_temp - pr_strings;
  937. }
  938.  
  939. void PF_fabs (void)
  940. {
  941.   float v;
  942.   v = G_FLOAT(OFS_PARM0);
  943.   G_FLOAT(OFS_RETURN) = fabs(v);
  944. }
  945.  
  946. void PF_vtos (void)
  947. {
  948.   sprintf (pr_string_temp, "'%5.1f %5.1f %5.1f'", G_VECTOR(OFS_PARM0)[0], G_VECTOR(OFS_PARM0)[1], G_VECTOR(OFS_PARM0)[2]);
  949.   G_INT(OFS_RETURN) = pr_string_temp - pr_strings;
  950. }
  951.  
  952. #ifdef QUAKE2
  953. void PF_etos (void)
  954. {
  955.   sprintf (pr_string_temp, "entity %i", G_EDICTNUM(OFS_PARM0));
  956.   G_INT(OFS_RETURN) = pr_string_temp - pr_strings;
  957. }
  958. #endif
  959.  
  960. void PF_Spawn (void)
  961. {
  962.   edict_t *ed;
  963.   ed = ED_Alloc();
  964.   RETURN_EDICT(ed);
  965. }
  966.  
  967. void PF_Remove (void)
  968. {
  969.   edict_t *ed;
  970.   
  971.   ed = G_EDICT(OFS_PARM0);
  972.   ED_Free (ed);
  973. }
  974.  
  975.  
  976. // entity (entity start, .string field, string match) find = #5;
  977. void PF_Find (void)
  978. #ifdef QUAKE2
  979. {
  980.   int   e;  
  981.   int   f;
  982.   char  *s, *t;
  983.   edict_t *ed;
  984.   edict_t *first;
  985.   edict_t *second;
  986.   edict_t *last;
  987.  
  988.   first = second = last = (edict_t *)sv.edicts;
  989.   e = G_EDICTNUM(OFS_PARM0);
  990.   f = G_INT(OFS_PARM1);
  991.   s = G_STRING(OFS_PARM2);
  992.   if (!s)
  993.     PR_RunError ("PF_Find: bad search string");
  994.     
  995.   for (e++ ; e < sv.num_edicts ; e++)
  996.   {
  997.     ed = EDICT_NUM(e);
  998.     if (ed->free)
  999.       continue;
  1000.     t = E_STRING(ed,f);
  1001.     if (!t)
  1002.       continue;
  1003.     if (!strcmp(t,s))
  1004.     {
  1005.       if (first == (edict_t *)sv.edicts)
  1006.         first = ed;
  1007.       else if (second == (edict_t *)sv.edicts)
  1008.         second = ed;
  1009.       ed->v.chain = EDICT_TO_PROG(last);
  1010.       last = ed;
  1011.     }
  1012.   }
  1013.  
  1014.   if (first != last)
  1015.   {
  1016.     if (last != second)
  1017.       first->v.chain = last->v.chain;
  1018.     else
  1019.       first->v.chain = EDICT_TO_PROG(last);
  1020.     last->v.chain = EDICT_TO_PROG((edict_t *)sv.edicts);
  1021.     if (second && second != last)
  1022.       second->v.chain = EDICT_TO_PROG(last);
  1023.   }
  1024.   RETURN_EDICT(first);
  1025. }
  1026. #else
  1027. {
  1028.   int   e;  
  1029.   int   f;
  1030.   char  *s, *t;
  1031.   edict_t *ed;
  1032.  
  1033.   e = G_EDICTNUM(OFS_PARM0);
  1034.   f = G_INT(OFS_PARM1);
  1035.   s = G_STRING(OFS_PARM2);
  1036.   if (!s)
  1037.     PR_RunError ("PF_Find: bad search string");
  1038.     
  1039.   for (e++ ; e < sv.num_edicts ; e++)
  1040.   {
  1041.     ed = EDICT_NUM(e);
  1042.     if (ed->free)
  1043.       continue;
  1044.     t = E_STRING(ed,f);
  1045.     if (!t)
  1046.       continue;
  1047.     if (!strcmp(t,s))
  1048.     {
  1049.       RETURN_EDICT(ed);
  1050.       return;
  1051.     }
  1052.   }
  1053.  
  1054.   RETURN_EDICT(sv.edicts);
  1055. }
  1056. #endif
  1057.  
  1058. void PR_CheckEmptyString (char *s)
  1059. {
  1060.   if (s[0] <= ' ')
  1061.     PR_RunError ("Bad string");
  1062. }
  1063.  
  1064. void PF_precache_file (void)
  1065. { // precache_file is only used to copy files with qcc, it does nothing
  1066.   G_INT(OFS_RETURN) = G_INT(OFS_PARM0);
  1067. }
  1068.  
  1069. void PF_precache_sound (void)
  1070. {
  1071.   char  *s;
  1072.   int   i;
  1073.   
  1074.   if (sv.state != ss_loading)
  1075.     PR_RunError ("PF_Precache_*: Precache can only be done in spawn functions");
  1076.     
  1077.   s = G_STRING(OFS_PARM0);
  1078.   G_INT(OFS_RETURN) = G_INT(OFS_PARM0);
  1079.   PR_CheckEmptyString (s);
  1080.   
  1081.   for (i=0 ; i<MAX_SOUNDS ; i++)
  1082.   {
  1083.     if (!sv.sound_precache[i])
  1084.     {
  1085.       sv.sound_precache[i] = s;
  1086.       return;
  1087.     }
  1088.     if (!strcmp(sv.sound_precache[i], s))
  1089.       return;
  1090.   }
  1091.   PR_RunError ("PF_precache_sound: overflow");
  1092. }
  1093.  
  1094. void PF_precache_model (void)
  1095. {
  1096.   char  *s;
  1097.   int   i;
  1098.   
  1099.   if (sv.state != ss_loading)
  1100.     PR_RunError ("PF_Precache_*: Precache can only be done in spawn functions");
  1101.     
  1102.   s = G_STRING(OFS_PARM0);
  1103.   G_INT(OFS_RETURN) = G_INT(OFS_PARM0);
  1104.   PR_CheckEmptyString (s);
  1105.  
  1106.   for (i=0 ; i<MAX_MODELS ; i++)
  1107.   {
  1108.     if (!sv.model_precache[i])
  1109.     {
  1110.       sv.model_precache[i] = s;
  1111.       sv.models[i] = Mod_ForName (s, true);
  1112.       return;
  1113.     }
  1114.     if (!strcmp(sv.model_precache[i], s))
  1115.       return;
  1116.   }
  1117.   PR_RunError ("PF_precache_model: overflow");
  1118. }
  1119.  
  1120.  
  1121. void PF_coredump (void)
  1122. {
  1123.   ED_PrintEdicts ();
  1124. }
  1125.  
  1126. void PF_traceon (void)
  1127. {
  1128.   pr_trace = true;
  1129. }
  1130.  
  1131. void PF_traceoff (void)
  1132. {
  1133.   pr_trace = false;
  1134. }
  1135.  
  1136. void PF_eprint (void)
  1137. {
  1138.   ED_PrintNum (G_EDICTNUM(OFS_PARM0));
  1139. }
  1140.  
  1141. /*
  1142. ===============
  1143. PF_walkmove
  1144.  
  1145. float(float yaw, float dist) walkmove
  1146. ===============
  1147. */
  1148. void PF_walkmove (void)
  1149. {
  1150.   edict_t *ent;
  1151.   float yaw, dist;
  1152.   vec3_t  move;
  1153.   dfunction_t *oldf;
  1154.   int   oldself;
  1155.   
  1156.   ent = PROG_TO_EDICT(pr_global_struct->self);
  1157.   yaw = G_FLOAT(OFS_PARM0);
  1158.   dist = G_FLOAT(OFS_PARM1);
  1159.   
  1160.   if ( !( (int)ent->v.flags & (FL_ONGROUND|FL_FLY|FL_SWIM) ) )
  1161.   {
  1162.     G_FLOAT(OFS_RETURN) = 0;
  1163.     return;
  1164.   }
  1165.  
  1166.   yaw = yaw*M_PI*2 / 360;
  1167.   
  1168.   move[0] = cos(yaw)*dist;
  1169.   move[1] = sin(yaw)*dist;
  1170.   move[2] = 0;
  1171.  
  1172. // save program state, because SV_movestep may call other progs
  1173.   oldf = pr_xfunction;
  1174.   oldself = pr_global_struct->self;
  1175.   
  1176.   G_FLOAT(OFS_RETURN) = SV_movestep(ent, move, true);
  1177.   
  1178.   
  1179. // restore program state
  1180.   pr_xfunction = oldf;
  1181.   pr_global_struct->self = oldself;
  1182. }
  1183.  
  1184. /*
  1185. ===============
  1186. PF_droptofloor
  1187.  
  1188. void() droptofloor
  1189. ===============
  1190. */
  1191. void PF_droptofloor (void)
  1192. {
  1193.   edict_t   *ent;
  1194.   vec3_t    end;
  1195.   trace_t   trace;
  1196.   
  1197.   ent = PROG_TO_EDICT(pr_global_struct->self);
  1198.  
  1199.   VectorCopy (ent->v.origin, end);
  1200.   end[2] -= 256;
  1201.   
  1202.   trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, end, false, ent);
  1203.  
  1204.   if (trace.fraction == 1 || trace.allsolid)
  1205.     G_FLOAT(OFS_RETURN) = 0;
  1206.   else
  1207.   {
  1208.     VectorCopy (trace.endpos, ent->v.origin);
  1209.     SV_LinkEdict (ent, false);
  1210.     ent->v.flags = (int)ent->v.flags | FL_ONGROUND;
  1211.     ent->v.groundentity = EDICT_TO_PROG(trace.ent);
  1212.     G_FLOAT(OFS_RETURN) = 1;
  1213.   }
  1214. }
  1215.  
  1216. /*
  1217. ===============
  1218. PF_lightstyle
  1219.  
  1220. void(float style, string value) lightstyle
  1221. ===============
  1222. */
  1223. void PF_lightstyle (void)
  1224. {
  1225.   int   style;
  1226.   char  *val;
  1227.   client_t  *client;
  1228.   int     j;
  1229.   
  1230.   style = G_FLOAT(OFS_PARM0);
  1231.   val = G_STRING(OFS_PARM1);
  1232.  
  1233. // change the string in sv
  1234.   sv.lightstyles[style] = val;
  1235.   
  1236. // send message to all clients on this server
  1237.   if (sv.state != ss_active)
  1238.     return;
  1239.   
  1240.   for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
  1241.     if (client->active || client->spawned)
  1242.     {
  1243.       MSG_WriteChar (&client->message, svc_lightstyle);
  1244.       MSG_WriteChar (&client->message,style);
  1245.       MSG_WriteString (&client->message, val);
  1246.     }
  1247. }
  1248.  
  1249. void PF_rint (void)
  1250. {
  1251.   float f;
  1252.   f = G_FLOAT(OFS_PARM0);
  1253.   if (f > 0)
  1254.     G_FLOAT(OFS_RETURN) = (int)(f + 0.5);
  1255.   else
  1256.     G_FLOAT(OFS_RETURN) = (int)(f - 0.5);
  1257. }
  1258. void PF_floor (void)
  1259. {
  1260.   G_FLOAT(OFS_RETURN) = floor(G_FLOAT(OFS_PARM0));
  1261. }
  1262. void PF_ceil (void)
  1263. {
  1264.   G_FLOAT(OFS_RETURN) = ceil(G_FLOAT(OFS_PARM0));
  1265. }
  1266.  
  1267.  
  1268. /*
  1269. =============
  1270. PF_checkbottom
  1271. =============
  1272. */
  1273. void PF_checkbottom (void)
  1274. {
  1275.   edict_t *ent;
  1276.   
  1277.   ent = G_EDICT(OFS_PARM0);
  1278.  
  1279.   G_FLOAT(OFS_RETURN) = SV_CheckBottom (ent);
  1280. }
  1281.  
  1282. /*
  1283. =============
  1284. PF_pointcontents
  1285. =============
  1286. */
  1287. void PF_pointcontents (void)
  1288. {
  1289.   float *v;
  1290.   
  1291.   v = G_VECTOR(OFS_PARM0);
  1292.  
  1293.   G_FLOAT(OFS_RETURN) = SV_PointContents (v); 
  1294. }
  1295.  
  1296. /*
  1297. =============
  1298. PF_nextent
  1299.  
  1300. entity nextent(entity)
  1301. =============
  1302. */
  1303. void PF_nextent (void)
  1304. {
  1305.   int   i;
  1306.   edict_t *ent;
  1307.   
  1308.   i = G_EDICTNUM(OFS_PARM0);
  1309.   while (1)
  1310.   {
  1311.     i++;
  1312.     if (i == sv.num_edicts)
  1313.     {
  1314.       RETURN_EDICT(sv.edicts);
  1315.       return;
  1316.     }
  1317.     ent = EDICT_NUM(i);
  1318.     if (!ent->free)
  1319.     {
  1320.       RETURN_EDICT(ent);
  1321.       return;
  1322.     }
  1323.   }
  1324. }
  1325.  
  1326.  
  1327. /*
  1328. ==============
  1329. PF_changeyaw
  1330.  
  1331. This was a major timewaster in progs, so it was converted to C
  1332. ==============
  1333. */
  1334. void PF_changeyaw (void)
  1335. {
  1336.   edict_t   *ent;
  1337.   float   ideal, current, move, speed;
  1338.   
  1339.   ent = PROG_TO_EDICT(pr_global_struct->self);
  1340.   current = anglemod( ent->v.angles[1] );
  1341.   ideal = ent->v.ideal_yaw;
  1342.   speed = ent->v.yaw_speed;
  1343.   
  1344.   if (current == ideal)
  1345.     return;
  1346.   move = ideal - current;
  1347.   if (ideal > current)
  1348.   {
  1349.     if (move >= 180)
  1350.       move = move - 360;
  1351.   }
  1352.   else
  1353.   {
  1354.     if (move <= -180)
  1355.       move = move + 360;
  1356.   }
  1357.   if (move > 0)
  1358.   {
  1359.     if (move > speed)
  1360.       move = speed;
  1361.   }
  1362.   else
  1363.   {
  1364.     if (move < -speed)
  1365.       move = -speed;
  1366.   }
  1367.   
  1368.   ent->v.angles[1] = anglemod (current + move);
  1369. }
  1370.  
  1371. #ifdef QUAKE2
  1372. /*
  1373. ==============
  1374. PF_changepitch
  1375. ==============
  1376. */
  1377. void PF_changepitch (void)
  1378. {
  1379.   edict_t   *ent;
  1380.   float   ideal, current, move, speed;
  1381.   
  1382.   ent = G_EDICT(OFS_PARM0);
  1383.   current = anglemod( ent->v.angles[0] );
  1384.   ideal = ent->v.idealpitch;
  1385.   speed = ent->v.pitch_speed;
  1386.   
  1387.   if (current == ideal)
  1388.     return;
  1389.   move = ideal - current;
  1390.   if (ideal > current)
  1391.   {
  1392.     if (move >= 180)
  1393.       move = move - 360;
  1394.   }
  1395.   else
  1396.   {
  1397.     if (move <= -180)
  1398.       move = move + 360;
  1399.   }
  1400.   if (move > 0)
  1401.   {
  1402.     if (move > speed)
  1403.       move = speed;
  1404.   }
  1405.   else
  1406.   {
  1407.     if (move < -speed)
  1408.       move = -speed;
  1409.   }
  1410.   
  1411.   ent->v.angles[0] = anglemod (current + move);
  1412. }
  1413. #endif
  1414.  
  1415. /*
  1416. ===============================================================================
  1417.  
  1418. MESSAGE WRITING
  1419.  
  1420. ===============================================================================
  1421. */
  1422.  
  1423. #define MSG_BROADCAST 0   // unreliable to all
  1424. #define MSG_ONE     1   // reliable to one (msg_entity)
  1425. #define MSG_ALL     2   // reliable to all
  1426. #define MSG_INIT    3   // write to the init string
  1427.  
  1428. sizebuf_t *WriteDest (void)
  1429. {
  1430.   int   entnum;
  1431.   int   dest;
  1432.   edict_t *ent;
  1433.  
  1434.   dest = G_FLOAT(OFS_PARM0);
  1435.   switch (dest)
  1436.   {
  1437.   case MSG_BROADCAST:
  1438.     return &sv.datagram;
  1439.   
  1440.   case MSG_ONE:
  1441.     ent = PROG_TO_EDICT(pr_global_struct->msg_entity);
  1442.     entnum = NUM_FOR_EDICT(ent);
  1443.     if (entnum < 1 || entnum > svs.maxclients)
  1444.       PR_RunError ("WriteDest: not a client");
  1445.     return &svs.clients[entnum-1].message;
  1446.     
  1447.   case MSG_ALL:
  1448.     return &sv.reliable_datagram;
  1449.   
  1450.   case MSG_INIT:
  1451.     return &sv.signon;
  1452.  
  1453.   default:
  1454.     PR_RunError ("WriteDest: bad destination");
  1455.     break;
  1456.   }
  1457.   
  1458.   return NULL;
  1459. }
  1460.  
  1461. void PF_WriteByte (void)
  1462. {
  1463.   MSG_WriteByte (WriteDest(), G_FLOAT(OFS_PARM1));
  1464. }
  1465.  
  1466. void PF_WriteChar (void)
  1467. {
  1468.   MSG_WriteChar (WriteDest(), G_FLOAT(OFS_PARM1));
  1469. }
  1470.  
  1471. void PF_WriteShort (void)
  1472. {
  1473.   MSG_WriteShort (WriteDest(), G_FLOAT(OFS_PARM1));
  1474. }
  1475.  
  1476. void PF_WriteLong (void)
  1477. {
  1478.   MSG_WriteLong (WriteDest(), G_FLOAT(OFS_PARM1));
  1479. }
  1480.  
  1481. void PF_WriteAngle (void)
  1482. {
  1483.   MSG_WriteAngle (WriteDest(), G_FLOAT(OFS_PARM1));
  1484. }
  1485.  
  1486. void PF_WriteCoord (void)
  1487. {
  1488.   MSG_WriteCoord (WriteDest(), G_FLOAT(OFS_PARM1));
  1489. }
  1490.  
  1491. void PF_WriteString (void)
  1492. {
  1493.   MSG_WriteString (WriteDest(), G_STRING(OFS_PARM1));
  1494. }
  1495.  
  1496.  
  1497. void PF_WriteEntity (void)
  1498. {
  1499.   MSG_WriteShort (WriteDest(), G_EDICTNUM(OFS_PARM1));
  1500. }
  1501.  
  1502. //=============================================================================
  1503.  
  1504. int SV_ModelIndex (char *name);
  1505.  
  1506. void PF_makestatic (void)
  1507. {
  1508.   edict_t *ent;
  1509.   int   i;
  1510.   
  1511.   ent = G_EDICT(OFS_PARM0);
  1512.  
  1513.   MSG_WriteByte (&sv.signon,svc_spawnstatic);
  1514.  
  1515.   MSG_WriteByte (&sv.signon, SV_ModelIndex(pr_strings + ent->v.model));
  1516.  
  1517.   MSG_WriteByte (&sv.signon, ent->v.frame);
  1518.   MSG_WriteByte (&sv.signon, ent->v.colormap);
  1519.   MSG_WriteByte (&sv.signon, ent->v.skin);
  1520.   for (i=0 ; i<3 ; i++)
  1521.   {
  1522.     MSG_WriteCoord(&sv.signon, ent->v.origin[i]);
  1523.     MSG_WriteAngle(&sv.signon, ent->v.angles[i]);
  1524.   }
  1525.  
  1526. // throw the entity away now
  1527.   ED_Free (ent);
  1528. }
  1529.  
  1530. //=============================================================================
  1531.  
  1532. /*
  1533. ==============
  1534. PF_setspawnparms
  1535. ==============
  1536. */
  1537. void PF_setspawnparms (void)
  1538. {
  1539.   edict_t *ent;
  1540.   int   i;
  1541.   client_t  *client;
  1542.  
  1543.   ent = G_EDICT(OFS_PARM0);
  1544.   i = NUM_FOR_EDICT(ent);
  1545.   if (i < 1 || i > svs.maxclients)
  1546.     PR_RunError ("Entity is not a client");
  1547.  
  1548.   // copy spawn parms out of the client_t
  1549.   client = svs.clients + (i-1);
  1550.  
  1551.   for (i=0 ; i< NUM_SPAWN_PARMS ; i++)
  1552.     (&pr_global_struct->parm1)[i] = client->spawn_parms[i];
  1553. }
  1554.  
  1555. /*
  1556. ==============
  1557. PF_changelevel
  1558. ==============
  1559. */
  1560. void PF_changelevel (void)
  1561. {
  1562. #ifdef QUAKE2
  1563.   char  *s1, *s2;
  1564.  
  1565.   if (svs.changelevel_issued)
  1566.     return;
  1567.   svs.changelevel_issued = true;
  1568.  
  1569.   s1 = G_STRING(OFS_PARM0);
  1570.   s2 = G_STRING(OFS_PARM1);
  1571.  
  1572.   if ((int)pr_global_struct->serverflags & (SFL_NEW_UNIT | SFL_NEW_EPISODE))
  1573.     Cbuf_AddText (va("changelevel %s %s\n",s1, s2));
  1574.   else
  1575.     Cbuf_AddText (va("changelevel2 %s %s\n",s1, s2));
  1576. #else
  1577.   char  *s;
  1578.  
  1579. // make sure we don't issue two changelevels
  1580.   if (svs.changelevel_issued)
  1581.     return;
  1582.   svs.changelevel_issued = true;
  1583.   
  1584.   s = G_STRING(OFS_PARM0);
  1585.   Cbuf_AddText (va("changelevel %s\n",s));
  1586. #endif
  1587. }
  1588.  
  1589.  
  1590. #ifdef QUAKE2
  1591.  
  1592. #define CONTENT_WATER -3
  1593. #define CONTENT_SLIME -4
  1594. #define CONTENT_LAVA  -5
  1595.  
  1596. #define FL_IMMUNE_WATER 131072
  1597. #define FL_IMMUNE_SLIME 262144
  1598. #define FL_IMMUNE_LAVA  524288
  1599.  
  1600. #define CHAN_VOICE  2
  1601. #define CHAN_BODY 4
  1602.  
  1603. #define ATTN_NORM 1
  1604.  
  1605. void PF_WaterMove (void)
  1606. {
  1607.   edict_t   *self;
  1608.   int     flags;
  1609.   int     waterlevel;
  1610.   int     watertype;
  1611.   float   drownlevel;
  1612.   float   damage = 0.0;
  1613.  
  1614.   self = PROG_TO_EDICT(pr_global_struct->self);
  1615.  
  1616.   if (self->v.movetype == MOVETYPE_NOCLIP)
  1617.   {
  1618.     self->v.air_finished = sv.time + 12;
  1619.     G_FLOAT(OFS_RETURN) = damage;
  1620.     return;
  1621.   }
  1622.  
  1623.   if (self->v.health < 0)
  1624.   {
  1625.     G_FLOAT(OFS_RETURN) = damage;
  1626.     return;
  1627.   }
  1628.  
  1629.   if (self->v.deadflag == DEAD_NO)
  1630.     drownlevel = 3;
  1631.   else
  1632.     drownlevel = 1;
  1633.  
  1634.   flags = (int)self->v.flags;
  1635.   waterlevel = (int)self->v.waterlevel;
  1636.   watertype = (int)self->v.watertype;
  1637.  
  1638.   if (!(flags & (FL_IMMUNE_WATER + FL_GODMODE)))
  1639.     if (((flags & FL_SWIM) && (waterlevel < drownlevel)) || (waterlevel >= drownlevel))
  1640.     {
  1641.       if (self->v.air_finished < sv.time)
  1642.         if (self->v.pain_finished < sv.time)
  1643.         {
  1644.           self->v.dmg = self->v.dmg + 2;
  1645.           if (self->v.dmg > 15)
  1646.             self->v.dmg = 10;
  1647. //          T_Damage (self, world, world, self.dmg, 0, FALSE);
  1648.           damage = self->v.dmg;
  1649.           self->v.pain_finished = sv.time + 1.0;
  1650.         }
  1651.     }
  1652.     else
  1653.     {
  1654.       if (self->v.air_finished < sv.time)
  1655. //        sound (self, CHAN_VOICE, "player/gasp2.wav", 1, ATTN_NORM);
  1656.         SV_StartSound (self, CHAN_VOICE, "player/gasp2.wav", 255, ATTN_NORM);
  1657.       else if (self->v.air_finished < sv.time + 9)
  1658. //        sound (self, CHAN_VOICE, "player/gasp1.wav", 1, ATTN_NORM);
  1659.         SV_StartSound (self, CHAN_VOICE, "player/gasp1.wav", 255, ATTN_NORM);
  1660.       self->v.air_finished = sv.time + 12.0;
  1661.       self->v.dmg = 2;
  1662.     }
  1663.   
  1664.   if (!waterlevel)
  1665.   {
  1666.     if (flags & FL_INWATER)
  1667.     { 
  1668.       // play leave water sound
  1669. //      sound (self, CHAN_BODY, "misc/outwater.wav", 1, ATTN_NORM);
  1670.       SV_StartSound (self, CHAN_BODY, "misc/outwater.wav", 255, ATTN_NORM);
  1671.       self->v.flags = (float)(flags &~FL_INWATER);
  1672.     }
  1673.     self->v.air_finished = sv.time + 12.0;
  1674.     G_FLOAT(OFS_RETURN) = damage;
  1675.     return;
  1676.   }
  1677.  
  1678.   if (watertype == CONTENT_LAVA)
  1679.   { // do damage
  1680.     if (!(flags & (FL_IMMUNE_LAVA + FL_GODMODE)))
  1681.       if (self->v.dmgtime < sv.time)
  1682.       {
  1683.         if (self->v.radsuit_finished < sv.time)
  1684.           self->v.dmgtime = sv.time + 0.2;
  1685.         else
  1686.           self->v.dmgtime = sv.time + 1.0;
  1687. //        T_Damage (self, world, world, 10*self.waterlevel, 0, TRUE);
  1688.         damage = (float)(10*waterlevel);
  1689.       }
  1690.   }
  1691.   else if (watertype == CONTENT_SLIME)
  1692.   { // do damage
  1693.     if (!(flags & (FL_IMMUNE_SLIME + FL_GODMODE)))
  1694.       if (self->v.dmgtime < sv.time && self->v.radsuit_finished < sv.time)
  1695.       {
  1696.         self->v.dmgtime = sv.time + 1.0;
  1697. //        T_Damage (self, world, world, 4*self.waterlevel, 0, TRUE);
  1698.         damage = (float)(4*waterlevel);
  1699.       }
  1700.   }
  1701.   
  1702.   if ( !(flags & FL_INWATER) )
  1703.   { 
  1704.  
  1705. // player enter water sound
  1706.     if (watertype == CONTENT_LAVA)
  1707. //      sound (self, CHAN_BODY, "player/inlava.wav", 1, ATTN_NORM);
  1708.       SV_StartSound (self, CHAN_BODY, "player/inlava.wav", 255, ATTN_NORM);
  1709.     if (watertype == CONTENT_WATER)
  1710. //      sound (self, CHAN_BODY, "player/inh2o.wav", 1, ATTN_NORM);
  1711.       SV_StartSound (self, CHAN_BODY, "player/inh2o.wav", 255, ATTN_NORM);
  1712.     if (watertype == CONTENT_SLIME)
  1713. //      sound (self, CHAN_BODY, "player/slimbrn2.wav", 1, ATTN_NORM);
  1714.       SV_StartSound (self, CHAN_BODY, "player/slimbrn2.wav", 255, ATTN_NORM);
  1715.  
  1716.     self->v.flags = (float)(flags | FL_INWATER);
  1717.     self->v.dmgtime = 0;
  1718.   }
  1719.   
  1720.   if (! (flags & FL_WATERJUMP) )
  1721.   {
  1722. //    self.velocity = self.velocity - 0.8*self.waterlevel*frametime*self.velocity;
  1723.     VectorMA (self->v.velocity, -0.8 * self->v.waterlevel * host_frametime, self->v.velocity, self->v.velocity);
  1724.   }
  1725.  
  1726.   G_FLOAT(OFS_RETURN) = damage;
  1727. }
  1728.  
  1729.  
  1730. void PF_sin (void)
  1731. {
  1732.   G_FLOAT(OFS_RETURN) = sin(G_FLOAT(OFS_PARM0));
  1733. }
  1734.  
  1735. void PF_cos (void)
  1736. {
  1737.   G_FLOAT(OFS_RETURN) = cos(G_FLOAT(OFS_PARM0));
  1738. }
  1739.  
  1740. void PF_sqrt (void)
  1741. {
  1742.   G_FLOAT(OFS_RETURN) = sqrt(G_FLOAT(OFS_PARM0));
  1743. }
  1744. #endif
  1745.  
  1746. void PF_Fixme (void)
  1747. {
  1748.   PR_RunError ("unimplemented bulitin");
  1749. }
  1750.  
  1751.  
  1752.  
  1753. builtin_t pr_builtin[] =
  1754. {
  1755. PF_Fixme,
  1756. PF_makevectors, // void(entity e) makevectors     = #1;
  1757. PF_setorigin, // void(entity e, vector o) setorigin = #2;
  1758. PF_setmodel,  // void(entity e, string m) setmodel  = #3;
  1759. PF_setsize, // void(entity e, vector min, vector max) setsize = #4;
  1760. PF_Fixme, // void(entity e, vector min, vector max) setabssize = #5;
  1761. PF_break, // void() break           = #6;
  1762. PF_random,  // float() random           = #7;
  1763. PF_sound, // void(entity e, float chan, string samp) sound = #8;
  1764. PF_normalize, // vector(vector v) normalize     = #9;
  1765. PF_error, // void(string e) error       = #10;
  1766. PF_objerror,  // void(string e) objerror        = #11;
  1767. PF_vlen,  // float(vector v) vlen       = #12;
  1768. PF_vectoyaw,  // float(vector v) vectoyaw   = #13;
  1769. PF_Spawn, // entity() spawn           = #14;
  1770. PF_Remove,  // void(entity e) remove        = #15;
  1771. PF_traceline, // float(vector v1, vector v2, float tryents) traceline = #16;
  1772. PF_checkclient, // entity() clientlist          = #17;
  1773. PF_Find,  // entity(entity start, .string fld, string match) find = #18;
  1774. PF_precache_sound,  // void(string s) precache_sound    = #19;
  1775. PF_precache_model,  // void(string s) precache_model    = #20;
  1776. PF_stuffcmd,  // void(entity client, string s)stuffcmd = #21;
  1777. PF_findradius,  // entity(vector org, float rad) findradius = #22;
  1778. PF_bprint,  // void(string s) bprint        = #23;
  1779. PF_sprint,  // void(entity client, string s) sprint = #24;
  1780. PF_dprint,  // void(string s) dprint        = #25;
  1781. PF_ftos,  // void(string s) ftos        = #26;
  1782. PF_vtos,  // void(string s) vtos        = #27;
  1783. PF_coredump,
  1784. PF_traceon,
  1785. PF_traceoff,
  1786. PF_eprint,  // void(entity e) debug print an entire entity
  1787. PF_walkmove, // float(float yaw, float dist) walkmove
  1788. PF_Fixme, // float(float yaw, float dist) walkmove
  1789. PF_droptofloor,
  1790. PF_lightstyle,
  1791. PF_rint,
  1792. PF_floor,
  1793. PF_ceil,
  1794. PF_Fixme,
  1795. PF_checkbottom,
  1796. PF_pointcontents,
  1797. PF_Fixme,
  1798. PF_fabs,
  1799. PF_aim,
  1800. PF_cvar,
  1801. PF_localcmd,
  1802. PF_nextent,
  1803. PF_particle,
  1804. PF_changeyaw,
  1805. PF_Fixme,
  1806. PF_vectoangles,
  1807.  
  1808. PF_WriteByte,
  1809. PF_WriteChar,
  1810. PF_WriteShort,
  1811. PF_WriteLong,
  1812. PF_WriteCoord,
  1813. PF_WriteAngle,
  1814. PF_WriteString,
  1815. PF_WriteEntity,
  1816.  
  1817. #ifdef QUAKE2
  1818. PF_sin,
  1819. PF_cos,
  1820. PF_sqrt,
  1821. PF_changepitch,
  1822. PF_TraceToss,
  1823. PF_etos,
  1824. PF_WaterMove,
  1825. #else
  1826. PF_Fixme,
  1827. PF_Fixme,
  1828. PF_Fixme,
  1829. PF_Fixme,
  1830. PF_Fixme,
  1831. PF_Fixme,
  1832. PF_Fixme,
  1833. #endif
  1834.  
  1835. SV_MoveToGoal,
  1836. PF_precache_file,
  1837. PF_makestatic,
  1838.  
  1839. PF_changelevel,
  1840. PF_Fixme,
  1841.  
  1842. PF_cvar_set,
  1843. PF_centerprint,
  1844.  
  1845. PF_ambientsound,
  1846.  
  1847. PF_precache_model,
  1848. PF_precache_sound,    // precache_sound2 is different only for qcc
  1849. PF_precache_file,
  1850.  
  1851. PF_setspawnparms
  1852. };
  1853.  
  1854. builtin_t *pr_builtins = pr_builtin;
  1855. int pr_numbuiltins = sizeof(pr_builtin)/sizeof(pr_builtin[0]);
  1856.  
  1857.