]> icculus.org git repositories - divverent/darkplaces.git/blob - cgame.c
Removed warnings in MSVC6. Updated the DSP file.
[divverent/darkplaces.git] / cgame.c
1
2 #include "cgame_api.h"
3 #include "cg_math.h"
4
5
6 #ifndef NULL
7 #define NULL ((void *)0)
8 #endif
9
10 static double gametime, frametime;
11
12 struct localentity_s;
13 typedef struct localentity_s
14 {
15         int active; // true if the entity is alive (not freed)
16         float freetime; // time this entity was freed
17         float dietime;
18         vec3_t velocity;
19         vec3_t avelocity;
20         vec3_t worldmins;
21         vec3_t worldmaxs;
22         vec3_t entitymins;
23         vec3_t entitymaxs;
24         float bouncescale;
25         float airfrictionscale;
26         float gravityscale;
27         void (*framethink)(struct localentity_s *e);
28         //int solid;
29         //void (*touch)(struct localentity_s *self, struct localentity_s *other);
30         //void (*touchnetwork)(struct *localentity_s *self);
31         cgdrawentity_t draw;
32 }
33 localentity_t;
34
35 #define MAX_LOCALENTITIES 1024
36 static localentity_t *localentity;
37
38 static cgphysentity_t *phys_entity;
39 static int phys_entities;
40
41 static float cg_gravity;
42
43 static void readvector(vec3_t v)
44 {
45         v[0] = CGVM_MSG_ReadFloat();
46         v[1] = CGVM_MSG_ReadFloat();
47         v[2] = CGVM_MSG_ReadFloat();
48 }
49
50 static localentity_t *entspawn(void)
51 {
52         int i;
53         localentity_t *l;
54         for (i = 0;i < MAX_LOCALENTITIES;i++)
55         {
56                 l = localentity + i;
57                 if (!l->active && l->freetime < gametime)
58                 {
59                         memset(l, 0, sizeof(*l));
60                         l->active = true;
61                         return l;
62                 }
63         }
64         for (i = 0;i < MAX_LOCALENTITIES;i++)
65         {
66                 l = localentity + i;
67                 if (!l->active)
68                 {
69                         memset(l, 0, sizeof(*l));
70                         l->active = true;
71                         return l;
72                 }
73         }
74         return NULL;
75 }
76
77 static void entremove(localentity_t *e)
78 {
79         memset(e, 0, sizeof(*e));
80         e->freetime = (float)gametime + 1.0f;
81 }
82
83 static void phys_setupphysentities(void)
84 {
85         phys_entities = 0;
86         /*
87         for (i = 0;i < MAX_LOCALENTITIES;i++)
88         {
89                 l = localentities + i;
90                 if (l->active && l->solid)
91                 {
92                 }
93         }
94         */
95 }
96
97 static void phys_moveentities(void)
98 {
99         int i;
100         localentity_t *l;
101         for (i = 0;i < MAX_LOCALENTITIES;i++)
102         {
103                 l = localentity + i;
104                 if (l->active)
105                 {
106                         if (l->framethink)
107                                 l->framethink(l);
108                         if (l->active && l->draw.model)
109                                 CGVM_Draw_Entity(&l->draw);
110                 }
111         }
112 }
113
114 static void phys_updateentities(void)
115 {
116         phys_setupphysentities();
117         phys_moveentities();
118 }
119
120 static void phys_update(localentity_t *e)
121 {
122         vec3_t impactpos, impactnormal, end;
123         int impactentnum;
124         float t, f, frac, bounce;
125         t = (float)frametime;
126         if (t == 0)
127                 return;
128         VectorMA(e->draw.angles, t, e->avelocity, e->draw.angles);
129         VectorMA(e->draw.origin, t, e->velocity, end);
130         frac = CGVM_TracePhysics(e->draw.origin, end, e->worldmins, e->worldmaxs, e->entitymins, e->entitymaxs, phys_entity, phys_entities, impactpos, impactnormal, &impactentnum);
131         VectorCopy(impactpos, e->draw.origin);
132         if (frac < 1)
133         {
134                 bounce = DotProduct(e->velocity, impactnormal) * -e->bouncescale;
135                 VectorMA(e->velocity, bounce, impactnormal, e->velocity);
136                 // FIXME: do some kind of touch code here if physentities get implemented
137
138                 if (impactnormal[2] >= 0.7 && DotProduct(e->velocity, e->velocity) < 100*100)
139                 {
140                         VectorClear(e->velocity);
141                         VectorClear(e->avelocity);
142                 }
143         }
144
145         if (e->airfrictionscale)
146         {
147                 if (DotProduct(e->velocity, e->velocity) < 10*10)
148                 {
149                         VectorClear(e->velocity);
150                         VectorClear(e->avelocity);
151                 }
152                 else
153                 {
154                         f = 1 - (t * e->airfrictionscale);
155                         if (f > 0)
156                         {
157                                 VectorScale(e->velocity, f, e->velocity);
158                                 if (DotProduct(e->avelocity, e->avelocity) < 10*10)
159                                 {
160                                         VectorClear(e->avelocity);
161                                 }
162                                 else
163                                 {
164                                         VectorScale(e->avelocity, f, e->avelocity);
165                                 }
166                         }
167                         else
168                         {
169                                 VectorClear(e->velocity);
170                                 VectorClear(e->avelocity);
171                         }
172                 }
173         }
174         if (e->gravityscale)
175                 e->velocity[2] += cg_gravity * e->gravityscale * t;
176 }
177
178 static void explosiondebris_framethink(localentity_t *self)
179 {
180         if (gametime > self->dietime)
181         {
182                 self->draw.scale -= (float)frametime * 3.0f;
183                 if (self->draw.scale < 0.05f)
184                 {
185                         entremove(self);
186                         return;
187                 }
188         }
189         phys_update(self);
190 }
191
192 static void net_explosion(unsigned char num)
193 {
194         int i;
195         float r;
196         vec3_t org;
197         double time;
198         localentity_t *e;
199         // need the time to know when the rubble should fade
200         time = CGVM_Time();
201         // read the network data
202         readvector(org);
203
204         for (i = 0;i < 40;i++)
205         {
206                 e = entspawn();
207                 if (!e)
208                         return;
209
210                 VectorCopy(org, e->draw.origin);
211                 e->draw.angles[0] = CGVM_RandomRange(0, 360);
212                 e->draw.angles[1] = CGVM_RandomRange(0, 360);
213                 e->draw.angles[2] = CGVM_RandomRange(0, 360);
214                 VectorRandom(e->velocity);
215                 VectorScale(e->velocity, 300, e->velocity);
216                 e->velocity[2] -= (float)cg_gravity * 0.1f;
217                 e->avelocity[0] = CGVM_RandomRange(0, 1440);
218                 e->avelocity[1] = CGVM_RandomRange(0, 1440);
219                 e->avelocity[2] = CGVM_RandomRange(0, 1440);
220                 r = CGVM_RandomRange(0, 3);
221                 if (r < 1)
222                         e->draw.model = CGVM_Model("progs/rubble1.mdl");
223                 else if (r < 2)
224                         e->draw.model = CGVM_Model("progs/rubble2.mdl");
225                 else
226                         e->draw.model = CGVM_Model("progs/rubble3.mdl");
227                 e->draw.alpha = 1;
228                 e->draw.scale = 1;
229                 e->draw.frame1 = 0;
230                 e->draw.frame2 = 0;
231                 e->draw.framelerp = 0;
232                 e->draw.skinnum = 5;
233                 VectorSet(e->worldmins, 0, 0, -8);
234                 VectorSet(e->worldmaxs, 0, 0, -8);
235                 VectorSet(e->entitymins, -8, -8, -8);
236                 VectorSet(e->entitymaxs, 8, 8, 8);
237                 e->bouncescale = 1.4f;
238                 e->gravityscale = 1;
239                 e->airfrictionscale = 1;
240                 e->framethink = explosiondebris_framethink;
241                 e->dietime = (float)time + 5.0f;
242         }
243 }
244
245 // called by engine
246 void CG_Init(void)
247 {
248         localentity = CGVM_Malloc(sizeof(localentity_t) * MAX_LOCALENTITIES);
249         phys_entity = CGVM_Malloc(sizeof(cgphysentity_t) * MAX_LOCALENTITIES);
250         CGVM_RegisterNetworkCode(1, net_explosion);
251         gametime = 0;
252 }
253
254 // called by engine
255 void CG_Frame(double time)
256 {
257         cg_gravity = -CGVM_GetCvarFloat("sv_gravity");
258         frametime = time - gametime;
259         gametime = time;
260         phys_updateentities();
261 }
262