sv_ents.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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 "qwsvdef.h"
  16. /*
  17. =============================================================================
  18. The PVS must include a small area around the client to allow head bobbing
  19. or other small motion on the client side. Otherwise, a bob might cause an
  20. entity that should be visible to not show up, especially when the bob
  21. crosses a waterline.
  22. =============================================================================
  23. */
  24. int fatbytes;
  25. byte fatpvs[MAX_MAP_LEAFS/8];
  26. void SV_AddToFatPVS (vec3_t org, mnode_t *node)
  27. {
  28. int i;
  29. byte *pvs;
  30. mplane_t *plane;
  31. float d;
  32. while (1)
  33. {
  34. // if this is a leaf, accumulate the pvs bits
  35. if (node->contents < 0)
  36. {
  37. if (node->contents != CONTENTS_SOLID)
  38. {
  39. pvs = Mod_LeafPVS ( (mleaf_t *)node, sv.worldmodel);
  40. for (i=0 ; i<fatbytes ; i++)
  41. fatpvs[i] |= pvs[i];
  42. }
  43. return;
  44. }
  45. plane = node->plane;
  46. d = DotProduct (org, plane->normal) - plane->dist;
  47. if (d > 8)
  48. node = node->children[0];
  49. else if (d < -8)
  50. node = node->children[1];
  51. else
  52. { // go down both
  53. SV_AddToFatPVS (org, node->children[0]);
  54. node = node->children[1];
  55. }
  56. }
  57. }
  58. /*
  59. =============
  60. SV_FatPVS
  61. Calculates a PVS that is the inclusive or of all leafs within 8 pixels of the
  62. given point.
  63. =============
  64. */
  65. byte *SV_FatPVS (vec3_t org)
  66. {
  67. fatbytes = (sv.worldmodel->numleafs+31)>>3;
  68. Q_memset (fatpvs, 0, fatbytes);
  69. SV_AddToFatPVS (org, sv.worldmodel->nodes);
  70. return fatpvs;
  71. }
  72. //=============================================================================
  73. // because there can be a lot of nails, there is a special
  74. // network protocol for them
  75. #define MAX_NAILS 32
  76. edict_t *nails[MAX_NAILS];
  77. int numnails;
  78. extern int sv_nailmodel, sv_supernailmodel, sv_playermodel;
  79. qboolean SV_AddNailUpdate (edict_t *ent)
  80. {
  81. if (ent->v.modelindex != sv_nailmodel
  82. && ent->v.modelindex != sv_supernailmodel)
  83. return false;
  84. if (numnails == MAX_NAILS)
  85. return true;
  86. nails[numnails] = ent;
  87. numnails++;
  88. return true;
  89. }
  90. void SV_EmitNailUpdate (sizebuf_t *msg)
  91. {
  92. byte bits[6]; // [48 bits] xyzpy 12 12 12 4 8
  93. int n, i;
  94. edict_t *ent;
  95. int x, y, z, p, yaw;
  96. if (!numnails)
  97. return;
  98. MSG_WriteByte (msg, svc_nails);
  99. MSG_WriteByte (msg, numnails);
  100. for (n=0 ; n<numnails ; n++)
  101. {
  102. ent = nails[n];
  103. x = (int)(ent->v.origin[0]+4096)>>1;
  104. y = (int)(ent->v.origin[1]+4096)>>1;
  105. z = (int)(ent->v.origin[2]+4096)>>1;
  106. p = (int)(16*ent->v.angles[0]/360)&15;
  107. yaw = (int)(256*ent->v.angles[1]/360)&255;
  108. bits[0] = x;
  109. bits[1] = (x>>8) | (y<<4);
  110. bits[2] = (y>>4);
  111. bits[3] = z;
  112. bits[4] = (z>>8) | (p<<4);
  113. bits[5] = yaw;
  114. for (i=0 ; i<6 ; i++)
  115. MSG_WriteByte (msg, bits[i]);
  116. }
  117. }
  118. //=============================================================================
  119. /*
  120. ==================
  121. SV_WriteDelta
  122. Writes part of a packetentities message.
  123. Can delta from either a baseline or a previous packet_entity
  124. ==================
  125. */
  126. void SV_WriteDelta (entity_state_t *from, entity_state_t *to, sizebuf_t *msg, qboolean force)
  127. {
  128. int bits;
  129. int i;
  130. float miss;
  131. // send an update
  132. bits = 0;
  133. for (i=0 ; i<3 ; i++)
  134. {
  135. miss = to->origin[i] - from->origin[i];
  136. if ( miss < -0.1 || miss > 0.1 )
  137. bits |= U_ORIGIN1<<i;
  138. }
  139. if ( to->angles[0] != from->angles[0] )
  140. bits |= U_ANGLE1;
  141. if ( to->angles[1] != from->angles[1] )
  142. bits |= U_ANGLE2;
  143. if ( to->angles[2] != from->angles[2] )
  144. bits |= U_ANGLE3;
  145. if ( to->colormap != from->colormap )
  146. bits |= U_COLORMAP;
  147. if ( to->skinnum != from->skinnum )
  148. bits |= U_SKIN;
  149. if ( to->frame != from->frame )
  150. bits |= U_FRAME;
  151. if ( to->effects != from->effects )
  152. bits |= U_EFFECTS;
  153. if ( to->modelindex != from->modelindex )
  154. bits |= U_MODEL;
  155. if (bits & 511)
  156. bits |= U_MOREBITS;
  157. if (to->flags & U_SOLID)
  158. bits |= U_SOLID;
  159. //
  160. // write the message
  161. //
  162. if (!to->number)
  163. SV_Error ("Unset entity number");
  164. if (to->number >= 512)
  165. SV_Error ("Entity number >= 512");
  166. if (!bits && !force)
  167. return; // nothing to send!
  168. i = to->number | (bits&~511);
  169. if (i & U_REMOVE)
  170. Sys_Error ("U_REMOVE");
  171. MSG_WriteShort (msg, i);
  172. if (bits & U_MOREBITS)
  173. MSG_WriteByte (msg, bits&255);
  174. if (bits & U_MODEL)
  175. MSG_WriteByte (msg, to->modelindex);
  176. if (bits & U_FRAME)
  177. MSG_WriteByte (msg, to->frame);
  178. if (bits & U_COLORMAP)
  179. MSG_WriteByte (msg, to->colormap);
  180. if (bits & U_SKIN)
  181. MSG_WriteByte (msg, to->skinnum);
  182. if (bits & U_EFFECTS)
  183. MSG_WriteByte (msg, to->effects);
  184. if (bits & U_ORIGIN1)
  185. MSG_WriteCoord (msg, to->origin[0]);
  186. if (bits & U_ANGLE1)
  187. MSG_WriteAngle(msg, to->angles[0]);
  188. if (bits & U_ORIGIN2)
  189. MSG_WriteCoord (msg, to->origin[1]);
  190. if (bits & U_ANGLE2)
  191. MSG_WriteAngle(msg, to->angles[1]);
  192. if (bits & U_ORIGIN3)
  193. MSG_WriteCoord (msg, to->origin[2]);
  194. if (bits & U_ANGLE3)
  195. MSG_WriteAngle(msg, to->angles[2]);
  196. }
  197. /*
  198. =============
  199. SV_EmitPacketEntities
  200. Writes a delta update of a packet_entities_t to the message.
  201. =============
  202. */
  203. void SV_EmitPacketEntities (client_t *client, packet_entities_t *to, sizebuf_t *msg)
  204. {
  205. edict_t *ent;
  206. client_frame_t *fromframe;
  207. packet_entities_t *from;
  208. int oldindex, newindex;
  209. int oldnum, newnum;
  210. int oldmax;
  211. // this is the frame that we are going to delta update from
  212. if (client->delta_sequence != -1)
  213. {
  214. fromframe = &client->frames[client->delta_sequence & UPDATE_MASK];
  215. from = &fromframe->entities;
  216. oldmax = from->num_entities;
  217. MSG_WriteByte (msg, svc_deltapacketentities);
  218. MSG_WriteByte (msg, client->delta_sequence);
  219. }
  220. else
  221. {
  222. oldmax = 0; // no delta update
  223. from = NULL;
  224. MSG_WriteByte (msg, svc_packetentities);
  225. }
  226. newindex = 0;
  227. oldindex = 0;
  228. //Con_Printf ("---%i to %i ----\n", client->delta_sequence & UPDATE_MASK
  229. // , client->netchan.outgoing_sequence & UPDATE_MASK);
  230. while (newindex < to->num_entities || oldindex < oldmax)
  231. {
  232. newnum = newindex >= to->num_entities ? 9999 : to->entities[newindex].number;
  233. oldnum = oldindex >= oldmax ? 9999 : from->entities[oldindex].number;
  234. if (newnum == oldnum)
  235. { // delta update from old position
  236. //Con_Printf ("delta %i\n", newnum);
  237. SV_WriteDelta (&from->entities[oldindex], &to->entities[newindex], msg, false);
  238. oldindex++;
  239. newindex++;
  240. continue;
  241. }
  242. if (newnum < oldnum)
  243. { // this is a new entity, send it from the baseline
  244. ent = EDICT_NUM(newnum);
  245. //Con_Printf ("baseline %i\n", newnum);
  246. SV_WriteDelta (&ent->baseline, &to->entities[newindex], msg, true);
  247. newindex++;
  248. continue;
  249. }
  250. if (newnum > oldnum)
  251. { // the old entity isn't present in the new message
  252. //Con_Printf ("remove %i\n", oldnum);
  253. MSG_WriteShort (msg, oldnum | U_REMOVE);
  254. oldindex++;
  255. continue;
  256. }
  257. }
  258. MSG_WriteShort (msg, 0); // end of packetentities
  259. }
  260. /*
  261. =============
  262. SV_WritePlayersToClient
  263. =============
  264. */
  265. void SV_WritePlayersToClient (client_t *client, edict_t *clent, byte *pvs, sizebuf_t *msg)
  266. {
  267. int i, j;
  268. client_t *cl;
  269. edict_t *ent;
  270. int msec;
  271. usercmd_t cmd;
  272. int pflags;
  273. for (j=0,cl=svs.clients ; j<MAX_CLIENTS ; j++,cl++)
  274. {
  275. if (cl->state != cs_spawned)
  276. continue;
  277. ent = cl->edict;
  278. // ZOID visibility tracking
  279. if (ent != clent &&
  280. !(client->spec_track && client->spec_track - 1 == j))
  281. {
  282. if (cl->spectator)
  283. continue;
  284. // ignore if not touching a PV leaf
  285. for (i=0 ; i < ent->num_leafs ; i++)
  286. if (pvs[ent->leafnums[i] >> 3] & (1 << (ent->leafnums[i]&7) ))
  287. break;
  288. if (i == ent->num_leafs)
  289. continue; // not visible
  290. }
  291. pflags = PF_MSEC | PF_COMMAND;
  292. if (ent->v.modelindex != sv_playermodel)
  293. pflags |= PF_MODEL;
  294. for (i=0 ; i<3 ; i++)
  295. if (ent->v.velocity[i])
  296. pflags |= PF_VELOCITY1<<i;
  297. if (ent->v.effects)
  298. pflags |= PF_EFFECTS;
  299. if (ent->v.skin)
  300. pflags |= PF_SKINNUM;
  301. if (ent->v.health <= 0)
  302. pflags |= PF_DEAD;
  303. if (ent->v.mins[2] != -24)
  304. pflags |= PF_GIB;
  305. if (cl->spectator)
  306. { // only sent origin and velocity to spectators
  307. pflags &= PF_VELOCITY1 | PF_VELOCITY2 | PF_VELOCITY3;
  308. }
  309. else if (ent == clent)
  310. { // don't send a lot of data on personal entity
  311. pflags &= ~(PF_MSEC|PF_COMMAND);
  312. if (ent->v.weaponframe)
  313. pflags |= PF_WEAPONFRAME;
  314. }
  315. if (client->spec_track && client->spec_track - 1 == j &&
  316. ent->v.weaponframe)
  317. pflags |= PF_WEAPONFRAME;
  318. MSG_WriteByte (msg, svc_playerinfo);
  319. MSG_WriteByte (msg, j);
  320. MSG_WriteShort (msg, pflags);
  321. for (i=0 ; i<3 ; i++)
  322. MSG_WriteCoord (msg, ent->v.origin[i]);
  323. MSG_WriteByte (msg, ent->v.frame);
  324. if (pflags & PF_MSEC)
  325. {
  326. msec = 1000*(sv.time - cl->localtime);
  327. if (msec > 255)
  328. msec = 255;
  329. MSG_WriteByte (msg, msec);
  330. }
  331. if (pflags & PF_COMMAND)
  332. {
  333. cmd = cl->lastcmd;
  334. if (ent->v.health <= 0)
  335. { // don't show the corpse looking around...
  336. cmd.angles[0] = 0;
  337. cmd.angles[1] = ent->v.angles[1];
  338. cmd.angles[0] = 0;
  339. }
  340. cmd.buttons = 0; // never send buttons
  341. cmd.impulse = 0; // never send impulses
  342. MSG_WriteDeltaUsercmd (msg, &nullcmd, &cmd);
  343. }
  344. for (i=0 ; i<3 ; i++)
  345. if (pflags & (PF_VELOCITY1<<i) )
  346. MSG_WriteShort (msg, ent->v.velocity[i]);
  347. if (pflags & PF_MODEL)
  348. MSG_WriteByte (msg, ent->v.modelindex);
  349. if (pflags & PF_SKINNUM)
  350. MSG_WriteByte (msg, ent->v.skin);
  351. if (pflags & PF_EFFECTS)
  352. MSG_WriteByte (msg, ent->v.effects);
  353. if (pflags & PF_WEAPONFRAME)
  354. MSG_WriteByte (msg, ent->v.weaponframe);
  355. }
  356. }
  357. /*
  358. =============
  359. SV_WriteEntitiesToClient
  360. Encodes the current state of the world as
  361. a svc_packetentities messages and possibly
  362. a svc_nails message and
  363. svc_playerinfo messages
  364. =============
  365. */
  366. void SV_WriteEntitiesToClient (client_t *client, sizebuf_t *msg)
  367. {
  368. int e, i;
  369. byte *pvs;
  370. vec3_t org;
  371. edict_t *ent;
  372. packet_entities_t *pack;
  373. edict_t *clent;
  374. client_frame_t *frame;
  375. entity_state_t *state;
  376. // this is the frame we are creating
  377. frame = &client->frames[client->netchan.incoming_sequence & UPDATE_MASK];
  378. // find the client's PVS
  379. clent = client->edict;
  380. VectorAdd (clent->v.origin, clent->v.view_ofs, org);
  381. pvs = SV_FatPVS (org);
  382. // send over the players in the PVS
  383. SV_WritePlayersToClient (client, clent, pvs, msg);
  384. // put other visible entities into either a packet_entities or a nails message
  385. pack = &frame->entities;
  386. pack->num_entities = 0;
  387. numnails = 0;
  388. for (e=MAX_CLIENTS+1, ent=EDICT_NUM(e) ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
  389. {
  390. // ignore ents without visible models
  391. if (!ent->v.modelindex || !*PR_GetString(ent->v.model))
  392. continue;
  393. // ignore if not touching a PV leaf
  394. for (i=0 ; i < ent->num_leafs ; i++)
  395. if (pvs[ent->leafnums[i] >> 3] & (1 << (ent->leafnums[i]&7) ))
  396. break;
  397. if (i == ent->num_leafs)
  398. continue; // not visible
  399. if (SV_AddNailUpdate (ent))
  400. continue; // added to the special update list
  401. // add to the packetentities
  402. if (pack->num_entities == MAX_PACKET_ENTITIES)
  403. continue; // all full
  404. state = &pack->entities[pack->num_entities];
  405. pack->num_entities++;
  406. state->number = e;
  407. state->flags = 0;
  408. VectorCopy (ent->v.origin, state->origin);
  409. VectorCopy (ent->v.angles, state->angles);
  410. state->modelindex = ent->v.modelindex;
  411. state->frame = ent->v.frame;
  412. state->colormap = ent->v.colormap;
  413. state->skinnum = ent->v.skin;
  414. state->effects = ent->v.effects;
  415. }
  416. // encode the packet entities as a delta from the
  417. // last packetentities acknowledged by the client
  418. SV_EmitPacketEntities (client, pack, msg);
  419. // now add the specialized nail update
  420. SV_EmitNailUpdate (msg);
  421. }