pr_cmds.c 36 KB

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