git-svn-id: http://bladebattles.com/kurok/SVN@11 20cd92bb-ff49-0410-b73e-96a06e42c3b9
[kurok.git] / r_efrag.c
blobf502d8601469161ae194e03d3d723ab1527616e2
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
20 // r_efrag.c
22 #include "quakedef.h"
23 #include "r_local.h"
25 mnode_t *r_pefragtopnode;
28 //===========================================================================
31 ===============================================================================
33 ENTITY FRAGMENT FUNCTIONS
35 ===============================================================================
38 efrag_t **lastlink;
40 vec3_t r_emins, r_emaxs;
42 entity_t *r_addent;
46 ================
47 R_RemoveEfrags
49 Call when removing an object from the world or moving it to another position
50 ================
52 void R_RemoveEfrags (entity_t *ent)
54 efrag_t *ef, *old, *walk, **prev;
56 ef = ent->efrag;
58 while (ef)
60 prev = &ef->leaf->efrags;
61 while (1)
63 walk = *prev;
64 if (!walk)
65 break;
66 if (walk == ef)
67 { // remove this fragment
68 *prev = ef->leafnext;
69 break;
71 else
72 prev = &walk->leafnext;
75 old = ef;
76 ef = ef->entnext;
78 // put it on the free list
79 old->entnext = cl.free_efrags;
80 cl.free_efrags = old;
83 ent->efrag = NULL;
87 ===================
88 R_SplitEntityOnNode
89 ===================
91 void R_SplitEntityOnNode (mnode_t *node)
93 efrag_t *ef;
94 mplane_t *splitplane;
95 mleaf_t *leaf;
96 int sides;
98 if (node->contents == CONTENTS_SOLID)
100 return;
103 // add an efrag if the node is a leaf
105 if ( node->contents < 0)
107 if (!r_pefragtopnode)
108 r_pefragtopnode = node;
110 leaf = (mleaf_t *)node;
112 // grab an efrag off the free list
113 ef = cl.free_efrags;
114 if (!ef)
116 Con_Printf ("Too many efrags!\n");
117 return; // no free fragments...
119 cl.free_efrags = cl.free_efrags->entnext;
121 ef->entity = r_addent;
123 // add the entity link
124 *lastlink = ef;
125 lastlink = &ef->entnext;
126 ef->entnext = NULL;
128 // set the leaf links
129 ef->leaf = leaf;
130 ef->leafnext = leaf->efrags;
131 leaf->efrags = ef;
133 return;
136 // NODE_MIXED
138 splitplane = node->plane;
139 sides = BOX_ON_PLANE_SIDE(r_emins, r_emaxs, splitplane);
141 if (sides == 3)
143 // split on this plane
144 // if this is the first splitter of this bmodel, remember it
145 if (!r_pefragtopnode)
146 r_pefragtopnode = node;
149 // recurse down the contacted sides
150 if (sides & 1)
151 R_SplitEntityOnNode (node->children[0]);
153 if (sides & 2)
154 R_SplitEntityOnNode (node->children[1]);
159 ===================
160 R_SplitEntityOnNode2
161 ===================
163 void R_SplitEntityOnNode2 (mnode_t *node)
165 mplane_t *splitplane;
166 int sides;
168 if (node->visframe != r_visframecount)
169 return;
171 if (node->contents < 0)
173 if (node->contents != CONTENTS_SOLID)
174 r_pefragtopnode = node; // we've reached a non-solid leaf, so it's
175 // visible and not BSP clipped
176 return;
179 splitplane = node->plane;
180 sides = BOX_ON_PLANE_SIDE(r_emins, r_emaxs, splitplane);
182 if (sides == 3)
184 // remember first splitter
185 r_pefragtopnode = node;
186 return;
189 // not split yet; recurse down the contacted side
190 if (sides & 1)
191 R_SplitEntityOnNode2 (node->children[0]);
192 else
193 R_SplitEntityOnNode2 (node->children[1]);
198 ===========
199 R_AddEfrags
200 ===========
202 void R_AddEfrags (entity_t *ent)
204 model_t *entmodel;
205 int i;
207 if (!ent->model)
208 return;
210 if (ent == cl_entities)
211 return; // never add the world
213 r_addent = ent;
215 lastlink = &ent->efrag;
216 r_pefragtopnode = NULL;
218 entmodel = ent->model;
220 for (i=0 ; i<3 ; i++)
222 r_emins[i] = ent->origin[i] + entmodel->mins[i];
223 r_emaxs[i] = ent->origin[i] + entmodel->maxs[i];
226 R_SplitEntityOnNode (cl.worldmodel->nodes);
228 ent->topnode = r_pefragtopnode;
233 ================
234 R_StoreEfrags
236 // FIXME: a lot of this goes away with edge-based
237 ================
239 void R_StoreEfrags (efrag_t **ppefrag)
241 entity_t *pent;
242 model_t *clmodel;
243 efrag_t *pefrag;
246 while ((pefrag = *ppefrag) != NULL)
248 pent = pefrag->entity;
249 clmodel = pent->model;
251 switch (clmodel->type)
253 case mod_alias:
254 case mod_brush:
255 case mod_sprite:
256 pent = pefrag->entity;
258 if ((pent->visframe != r_framecount) &&
259 (cl_numvisedicts < MAX_VISEDICTS))
261 cl_visedicts[cl_numvisedicts++] = pent;
263 // mark that we've recorded this entity for this frame
264 pent->visframe = r_framecount;
267 ppefrag = &pefrag->leafnext;
268 break;
270 default:
271 Sys_Error ("R_StoreEfrags: Bad entity type %d\n", clmodel->type);