]> icculus.org git repositories - divverent/darkplaces.git/blob - world.c
no longer stripping darkplaces release executables, because debugging
[divverent/darkplaces.git] / world.c
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 // world.c -- world query functions
21
22 #include "quakedef.h"
23
24 /*
25
26 entities never clip against themselves, or their owner
27
28 line of sight checks trace->inopen and trace->inwater, but bullets don't
29
30 */
31
32 void World_Init(void)
33 {
34         Collision_Init();
35 }
36
37 //============================================================================
38
39 // World_ClearLink is used for new headnodes
40 void World_ClearLink (link_t *l)
41 {
42         l->entitynumber = 0;
43         l->prev = l->next = l;
44 }
45
46 void World_RemoveLink (link_t *l)
47 {
48         l->next->prev = l->prev;
49         l->prev->next = l->next;
50 }
51
52 void World_InsertLinkBefore (link_t *l, link_t *before, int entitynumber)
53 {
54         l->entitynumber = entitynumber;
55         l->next = before;
56         l->prev = before->prev;
57         l->prev->next = l;
58         l->next->prev = l;
59 }
60
61 /*
62 ===============================================================================
63
64 ENTITY AREA CHECKING
65
66 ===============================================================================
67 */
68
69 void World_PrintAreaStats(world_t *world, const char *worldname)
70 {
71         Con_Printf("%s areagrid check stats: %d calls %d nodes (%f per call) %d entities (%f per call)\n", worldname, world->areagrid_stats_calls, world->areagrid_stats_nodechecks, (double) world->areagrid_stats_nodechecks / (double) world->areagrid_stats_calls, world->areagrid_stats_entitychecks, (double) world->areagrid_stats_entitychecks / (double) world->areagrid_stats_calls);
72         world->areagrid_stats_calls = 0;
73         world->areagrid_stats_nodechecks = 0;
74         world->areagrid_stats_entitychecks = 0;
75 }
76
77 /*
78 ===============
79 World_SetSize
80
81 ===============
82 */
83 void World_SetSize(world_t *world, const char *filename, const vec3_t mins, const vec3_t maxs)
84 {
85         int i;
86
87         strlcpy(world->filename, filename, sizeof(world->filename));
88         VectorCopy(mins, world->mins);
89         VectorCopy(maxs, world->maxs);
90
91         // the areagrid_marknumber is not allowed to be 0
92         if (world->areagrid_marknumber < 1)
93                 world->areagrid_marknumber = 1;
94         // choose either the world box size, or a larger box to ensure the grid isn't too fine
95         world->areagrid_size[0] = max(world->areagrid_maxs[0] - world->areagrid_mins[0], AREA_GRID * sv_areagrid_mingridsize.value);
96         world->areagrid_size[1] = max(world->areagrid_maxs[1] - world->areagrid_mins[1], AREA_GRID * sv_areagrid_mingridsize.value);
97         world->areagrid_size[2] = max(world->areagrid_maxs[2] - world->areagrid_mins[2], AREA_GRID * sv_areagrid_mingridsize.value);
98         // figure out the corners of such a box, centered at the center of the world box
99         world->areagrid_mins[0] = (world->areagrid_mins[0] + world->areagrid_maxs[0] - world->areagrid_size[0]) * 0.5f;
100         world->areagrid_mins[1] = (world->areagrid_mins[1] + world->areagrid_maxs[1] - world->areagrid_size[1]) * 0.5f;
101         world->areagrid_mins[2] = (world->areagrid_mins[2] + world->areagrid_maxs[2] - world->areagrid_size[2]) * 0.5f;
102         world->areagrid_maxs[0] = (world->areagrid_mins[0] + world->areagrid_maxs[0] + world->areagrid_size[0]) * 0.5f;
103         world->areagrid_maxs[1] = (world->areagrid_mins[1] + world->areagrid_maxs[1] + world->areagrid_size[1]) * 0.5f;
104         world->areagrid_maxs[2] = (world->areagrid_mins[2] + world->areagrid_maxs[2] + world->areagrid_size[2]) * 0.5f;
105         // now calculate the actual useful info from that
106         VectorNegate(world->areagrid_mins, world->areagrid_bias);
107         world->areagrid_scale[0] = AREA_GRID / world->areagrid_size[0];
108         world->areagrid_scale[1] = AREA_GRID / world->areagrid_size[1];
109         world->areagrid_scale[2] = AREA_GRID / world->areagrid_size[2];
110         World_ClearLink(&world->areagrid_outside);
111         for (i = 0;i < AREA_GRIDNODES;i++)
112                 World_ClearLink(&world->areagrid[i]);
113         if (developer.integer >= 10)
114                 Con_Printf("areagrid settings: divisions %ix%ix1 : box %f %f %f : %f %f %f size %f %f %f grid %f %f %f (mingrid %f)\n", AREA_GRID, AREA_GRID, world->areagrid_mins[0], world->areagrid_mins[1], world->areagrid_mins[2], world->areagrid_maxs[0], world->areagrid_maxs[1], world->areagrid_maxs[2], world->areagrid_size[0], world->areagrid_size[1], world->areagrid_size[2], 1.0f / world->areagrid_scale[0], 1.0f / world->areagrid_scale[1], 1.0f / world->areagrid_scale[2], sv_areagrid_mingridsize.value);
115 }
116
117 /*
118 ===============
119 World_UnlinkAll
120
121 ===============
122 */
123 void World_UnlinkAll(world_t *world)
124 {
125         int i;
126         link_t *grid;
127         // unlink all entities one by one
128         grid = &world->areagrid_outside;
129         while (grid->next != grid)
130                 World_UnlinkEdict(PRVM_EDICT_NUM(grid->next->entitynumber));
131         for (i = 0, grid = world->areagrid;i < AREA_GRIDNODES;i++, grid++)
132                 while (grid->next != grid)
133                         World_UnlinkEdict(PRVM_EDICT_NUM(grid->next->entitynumber));
134 }
135
136 /*
137 ===============
138
139 ===============
140 */
141 void World_UnlinkEdict(prvm_edict_t *ent)
142 {
143         int i;
144         for (i = 0;i < ENTITYGRIDAREAS;i++)
145         {
146                 if (ent->priv.server->areagrid[i].prev)
147                 {
148                         World_RemoveLink (&ent->priv.server->areagrid[i]);
149                         ent->priv.server->areagrid[i].prev = ent->priv.server->areagrid[i].next = NULL;
150                 }
151         }
152 }
153
154 int World_EntitiesInBox(world_t *world, const vec3_t mins, const vec3_t maxs, int maxlist, prvm_edict_t **list)
155 {
156         int numlist;
157         link_t *grid;
158         link_t *l;
159         prvm_edict_t *ent;
160         int igrid[3], igridmins[3], igridmaxs[3];
161
162         // FIXME: if areagrid_marknumber wraps, all entities need their
163         // ent->priv.server->areagridmarknumber reset
164         world->areagrid_stats_calls++;
165         world->areagrid_marknumber++;
166         igridmins[0] = (int) floor((mins[0] + world->areagrid_bias[0]) * world->areagrid_scale[0]);
167         igridmins[1] = (int) floor((mins[1] + world->areagrid_bias[1]) * world->areagrid_scale[1]);
168         //igridmins[2] = (int) ((mins[2] + world->areagrid_bias[2]) * world->areagrid_scale[2]);
169         igridmaxs[0] = (int) floor((maxs[0] + world->areagrid_bias[0]) * world->areagrid_scale[0]) + 1;
170         igridmaxs[1] = (int) floor((maxs[1] + world->areagrid_bias[1]) * world->areagrid_scale[1]) + 1;
171         //igridmaxs[2] = (int) ((maxs[2] + world->areagrid_bias[2]) * world->areagrid_scale[2]) + 1;
172         igridmins[0] = max(0, igridmins[0]);
173         igridmins[1] = max(0, igridmins[1]);
174         //igridmins[2] = max(0, igridmins[2]);
175         igridmaxs[0] = min(AREA_GRID, igridmaxs[0]);
176         igridmaxs[1] = min(AREA_GRID, igridmaxs[1]);
177         //igridmaxs[2] = min(AREA_GRID, igridmaxs[2]);
178
179         numlist = 0;
180         // add entities not linked into areagrid because they are too big or
181         // outside the grid bounds
182         if (world->areagrid_outside.next != &world->areagrid_outside)
183         {
184                 grid = &world->areagrid_outside;
185                 for (l = grid->next;l != grid;l = l->next)
186                 {
187                         ent = PRVM_EDICT_NUM(l->entitynumber);
188                         if (ent->priv.server->areagridmarknumber != world->areagrid_marknumber)
189                         {
190                                 ent->priv.server->areagridmarknumber = world->areagrid_marknumber;
191                                 if (!ent->priv.server->free && BoxesOverlap(mins, maxs, ent->priv.server->areamins, ent->priv.server->areamaxs))
192                                 {
193                                         if (numlist < maxlist)
194                                                 list[numlist] = ent;
195                                         numlist++;
196                                 }
197                                 world->areagrid_stats_entitychecks++;
198                         }
199                 }
200         }
201         // add grid linked entities
202         for (igrid[1] = igridmins[1];igrid[1] < igridmaxs[1];igrid[1]++)
203         {
204                 grid = world->areagrid + igrid[1] * AREA_GRID + igridmins[0];
205                 for (igrid[0] = igridmins[0];igrid[0] < igridmaxs[0];igrid[0]++, grid++)
206                 {
207                         if (grid->next != grid)
208                         {
209                                 for (l = grid->next;l != grid;l = l->next)
210                                 {
211                                         ent = PRVM_EDICT_NUM(l->entitynumber);
212                                         if (ent->priv.server->areagridmarknumber != world->areagrid_marknumber)
213                                         {
214                                                 ent->priv.server->areagridmarknumber = world->areagrid_marknumber;
215                                                 if (!ent->priv.server->free && BoxesOverlap(mins, maxs, ent->priv.server->areamins, ent->priv.server->areamaxs))
216                                                 {
217                                                         if (numlist < maxlist)
218                                                                 list[numlist] = ent;
219                                                         numlist++;
220                                                 }
221                                                 //Con_Printf("%d %f %f %f %f %f %f : %d : %f %f %f %f %f %f\n", BoxesOverlap(mins, maxs, ent->priv.server->areamins, ent->priv.server->areamaxs), ent->priv.server->areamins[0], ent->priv.server->areamins[1], ent->priv.server->areamins[2], ent->priv.server->areamaxs[0], ent->priv.server->areamaxs[1], ent->priv.server->areamaxs[2], PRVM_NUM_FOR_EDICT(ent), mins[0], mins[1], mins[2], maxs[0], maxs[1], maxs[2]);
222                                         }
223                                         world->areagrid_stats_entitychecks++;
224                                 }
225                         }
226                 }
227         }
228         return numlist;
229 }
230
231 void World_LinkEdict_AreaGrid(world_t *world, prvm_edict_t *ent)
232 {
233         link_t *grid;
234         int igrid[3], igridmins[3], igridmaxs[3], gridnum, entitynumber = PRVM_NUM_FOR_EDICT(ent);
235
236         if (entitynumber <= 0 || entitynumber >= prog->max_edicts || PRVM_EDICT_NUM(entitynumber) != ent)
237         {
238                 Con_Printf ("SV_LinkEdict_AreaGrid: invalid edict %p (edicts is %p, edict compared to prog->edicts is %i)\n", ent, prog->edicts, entitynumber);
239                 return;
240         }
241
242         igridmins[0] = (int) floor((ent->priv.server->areamins[0] + world->areagrid_bias[0]) * world->areagrid_scale[0]);
243         igridmins[1] = (int) floor((ent->priv.server->areamins[1] + world->areagrid_bias[1]) * world->areagrid_scale[1]);
244         //igridmins[2] = (int) floor((ent->priv.server->areamins[2] + world->areagrid_bias[2]) * world->areagrid_scale[2]);
245         igridmaxs[0] = (int) floor((ent->priv.server->areamaxs[0] + world->areagrid_bias[0]) * world->areagrid_scale[0]) + 1;
246         igridmaxs[1] = (int) floor((ent->priv.server->areamaxs[1] + world->areagrid_bias[1]) * world->areagrid_scale[1]) + 1;
247         //igridmaxs[2] = (int) floor((ent->priv.server->areamaxs[2] + world->areagrid_bias[2]) * world->areagrid_scale[2]) + 1;
248         if (igridmins[0] < 0 || igridmaxs[0] > AREA_GRID || igridmins[1] < 0 || igridmaxs[1] > AREA_GRID || ((igridmaxs[0] - igridmins[0]) * (igridmaxs[1] - igridmins[1])) > ENTITYGRIDAREAS)
249         {
250                 // wow, something outside the grid, store it as such
251                 World_InsertLinkBefore (&ent->priv.server->areagrid[0], &world->areagrid_outside, entitynumber);
252                 return;
253         }
254
255         gridnum = 0;
256         for (igrid[1] = igridmins[1];igrid[1] < igridmaxs[1];igrid[1]++)
257         {
258                 grid = world->areagrid + igrid[1] * AREA_GRID + igridmins[0];
259                 for (igrid[0] = igridmins[0];igrid[0] < igridmaxs[0];igrid[0]++, grid++, gridnum++)
260                         World_InsertLinkBefore (&ent->priv.server->areagrid[gridnum], grid, entitynumber);
261         }
262 }
263
264 /*
265 ===============
266 World_LinkEdict
267
268 ===============
269 */
270 void World_LinkEdict(world_t *world, prvm_edict_t *ent, const vec3_t mins, const vec3_t maxs)
271 {
272         // unlink from old position first
273         if (ent->priv.server->areagrid[0].prev)
274                 World_UnlinkEdict(ent);
275
276         // don't add the world
277         if (ent == prog->edicts)
278                 return;
279
280         // don't add free entities
281         if (ent->priv.server->free)
282                 return;
283
284         VectorCopy(mins, ent->priv.server->areamins);
285         VectorCopy(maxs, ent->priv.server->areamaxs);
286         World_LinkEdict_AreaGrid(world, ent);
287 }