forked from xonotic/darkplaces
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clvm_cmds.c
5669 lines (5106 loc) · 194 KB
/
clvm_cmds.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "quakedef.h"
#include "prvm_cmds.h"
#include "csprogs.h"
#include "cl_collision.h"
#include "r_shadow.h"
#include "jpeg.h"
#include "image.h"
//============================================================================
// Client
//[515]: unsolved PROBLEMS
//- finish player physics code (cs_runplayerphysics)
//- EntWasFreed ?
//- RF_DEPTHHACK is not like it should be
//- add builtin that sets cl.viewangles instead of reading "input_angles" global
//- finish lines support for R_Polygon***
//- insert selecttraceline into traceline somehow
//4 feature darkplaces csqc: add builtin to clientside qc for reading triangles of model meshes (useful to orient a ui along a triangle of a model mesh)
//4 feature darkplaces csqc: add builtins to clientside qc for gl calls
extern cvar_t v_flipped;
r_refdef_view_t csqc_original_r_refdef_view;
r_refdef_view_t csqc_main_r_refdef_view;
// #1 void(vector ang) makevectors
static void VM_CL_makevectors (prvm_prog_t *prog)
{
vec3_t angles, forward, right, up;
VM_SAFEPARMCOUNT(1, VM_CL_makevectors);
VectorCopy(PRVM_G_VECTOR(OFS_PARM0), angles);
AngleVectors(angles, forward, right, up);
VectorCopy(forward, PRVM_clientglobalvector(v_forward));
VectorCopy(right, PRVM_clientglobalvector(v_right));
VectorCopy(up, PRVM_clientglobalvector(v_up));
}
// #2 void(entity e, vector o) setorigin
static void VM_CL_setorigin (prvm_prog_t *prog)
{
prvm_edict_t *e;
prvm_vec_t *org;
VM_SAFEPARMCOUNT(2, VM_CL_setorigin);
e = PRVM_G_EDICT(OFS_PARM0);
if (e == prog->edicts)
{
VM_Warning(prog, "setorigin: can not modify world entity\n");
return;
}
if (e->free)
{
VM_Warning(prog, "setorigin: can not modify free entity\n");
return;
}
org = PRVM_G_VECTOR(OFS_PARM1);
VectorCopy (org, PRVM_clientedictvector(e, origin));
if(e->priv.required->mark == PRVM_EDICT_MARK_WAIT_FOR_SETORIGIN)
e->priv.required->mark = PRVM_EDICT_MARK_SETORIGIN_CAUGHT;
CL_LinkEdict(e);
}
static void SetMinMaxSizePRVM (prvm_prog_t *prog, prvm_edict_t *e, prvm_vec_t *min, prvm_vec_t *max)
{
int i;
for (i=0 ; i<3 ; i++)
if (min[i] > max[i])
prog->error_cmd("SetMinMaxSize: backwards mins/maxs");
// set derived values
VectorCopy (min, PRVM_clientedictvector(e, mins));
VectorCopy (max, PRVM_clientedictvector(e, maxs));
VectorSubtract (max, min, PRVM_clientedictvector(e, size));
CL_LinkEdict (e);
}
static void SetMinMaxSize (prvm_prog_t *prog, prvm_edict_t *e, const vec_t *min, const vec_t *max)
{
prvm_vec3_t mins, maxs;
VectorCopy(min, mins);
VectorCopy(max, maxs);
SetMinMaxSizePRVM(prog, e, mins, maxs);
}
// #3 void(entity e, string m) setmodel
static void VM_CL_setmodel (prvm_prog_t *prog)
{
prvm_edict_t *e;
const char *m;
model_t *mod;
int i;
VM_SAFEPARMCOUNT(2, VM_CL_setmodel);
e = PRVM_G_EDICT(OFS_PARM0);
PRVM_clientedictfloat(e, modelindex) = 0;
PRVM_clientedictstring(e, model) = 0;
m = PRVM_G_STRING(OFS_PARM1);
mod = NULL;
for (i = 0;i < MAX_MODELS && cl.csqc_model_precache[i];i++)
{
if (!strcmp(cl.csqc_model_precache[i]->name, m))
{
mod = cl.csqc_model_precache[i];
PRVM_clientedictstring(e, model) = PRVM_SetEngineString(prog, mod->name);
PRVM_clientedictfloat(e, modelindex) = -(i+1);
break;
}
}
if( !mod ) {
for (i = 0;i < MAX_MODELS;i++)
{
mod = cl.model_precache[i];
if (mod && !strcmp(mod->name, m))
{
PRVM_clientedictstring(e, model) = PRVM_SetEngineString(prog, mod->name);
PRVM_clientedictfloat(e, modelindex) = i;
break;
}
}
}
if( mod ) {
// TODO: check if this breaks needed consistency and maybe add a cvar for it too?? [1/10/2008 Black]
// LadyHavoc: erm you broke it by commenting this out - setmodel must do setsize or else the qc can't find out the model size, and ssqc does this by necessity, consistency.
SetMinMaxSize (prog, e, mod->normalmins, mod->normalmaxs);
}
else
{
SetMinMaxSize (prog, e, vec3_origin, vec3_origin);
VM_Warning(prog, "setmodel: model '%s' not precached\n", m);
}
}
// #4 void(entity e, vector min, vector max) setsize
static void VM_CL_setsize (prvm_prog_t *prog)
{
prvm_edict_t *e;
vec3_t mins, maxs;
VM_SAFEPARMCOUNT(3, VM_CL_setsize);
e = PRVM_G_EDICT(OFS_PARM0);
if (e == prog->edicts)
{
VM_Warning(prog, "setsize: can not modify world entity\n");
return;
}
if (e->free)
{
VM_Warning(prog, "setsize: can not modify free entity\n");
return;
}
VectorCopy(PRVM_G_VECTOR(OFS_PARM1), mins);
VectorCopy(PRVM_G_VECTOR(OFS_PARM2), maxs);
SetMinMaxSize( prog, e, mins, maxs );
CL_LinkEdict(e);
}
// #8 void(entity e, float chan, string samp, float volume, float atten[, float pitchchange[, float flags]]) sound
static void VM_CL_sound (prvm_prog_t *prog)
{
const char *sample;
int channel;
prvm_edict_t *entity;
float fvolume;
float attenuation;
float pitchchange;
float startposition;
int flags;
vec3_t org;
VM_SAFEPARMCOUNTRANGE(5, 7, VM_CL_sound);
entity = PRVM_G_EDICT(OFS_PARM0);
channel = (int)PRVM_G_FLOAT(OFS_PARM1);
sample = PRVM_G_STRING(OFS_PARM2);
fvolume = PRVM_G_FLOAT(OFS_PARM3);
attenuation = PRVM_G_FLOAT(OFS_PARM4);
if (fvolume < 0 || fvolume > 1)
{
VM_Warning(prog, "VM_CL_sound: volume must be in range 0-1\n");
return;
}
if (attenuation < 0 || attenuation > 4)
{
VM_Warning(prog, "VM_CL_sound: attenuation must be in range 0-4\n");
return;
}
if (prog->argc < 6)
pitchchange = 0;
else
pitchchange = PRVM_G_FLOAT(OFS_PARM5);
if (prog->argc < 7)
flags = 0;
else
{
// LadyHavoc: we only let the qc set certain flags, others are off-limits
flags = (int)PRVM_G_FLOAT(OFS_PARM6) & (CHANNELFLAG_RELIABLE | CHANNELFLAG_FORCELOOP | CHANNELFLAG_PAUSED | CHANNELFLAG_FULLVOLUME);
}
// sound_starttime exists instead of sound_startposition because in a
// networking sense you might not know when something is being received,
// so making sounds match up in sync would be impossible if relative
// position was sent
if (PRVM_clientglobalfloat(sound_starttime))
startposition = cl.time - PRVM_clientglobalfloat(sound_starttime);
else
startposition = 0;
if (!IS_CHAN(channel))
{
VM_Warning(prog, "VM_CL_sound: channel must be in range 0-127\n");
return;
}
CL_VM_GetEntitySoundOrigin(MAX_EDICTS + PRVM_NUM_FOR_EDICT(entity), org);
S_StartSound_StartPosition_Flags(MAX_EDICTS + PRVM_NUM_FOR_EDICT(entity), channel, S_FindName(sample), org, fvolume, attenuation, startposition, flags, pitchchange > 0.0f ? pitchchange * 0.01f : 1.0f);
}
// #483 void(vector origin, string sample, float volume, float attenuation) pointsound
static void VM_CL_pointsound(prvm_prog_t *prog)
{
const char *sample;
float fvolume;
float attenuation;
vec3_t org;
VM_SAFEPARMCOUNT(4, VM_CL_pointsound);
VectorCopy( PRVM_G_VECTOR(OFS_PARM0), org);
sample = PRVM_G_STRING(OFS_PARM1);
fvolume = PRVM_G_FLOAT(OFS_PARM2);
attenuation = PRVM_G_FLOAT(OFS_PARM3);
if (fvolume < 0 || fvolume > 1)
{
VM_Warning(prog, "VM_CL_pointsound: volume must be in range 0-1\n");
return;
}
if (attenuation < 0 || attenuation > 4)
{
VM_Warning(prog, "VM_CL_pointsound: attenuation must be in range 0-4\n");
return;
}
// Send World Entity as Entity to Play Sound (for CSQC, that is MAX_EDICTS)
S_StartSound(MAX_EDICTS, 0, S_FindName(sample), org, fvolume, attenuation);
}
// #14 entity() spawn
static void VM_CL_spawn (prvm_prog_t *prog)
{
prvm_edict_t *ed;
ed = PRVM_ED_Alloc(prog);
VM_RETURN_EDICT(ed);
}
static void CL_VM_SetTraceGlobals(prvm_prog_t *prog, const trace_t *trace, int svent)
{
VM_SetTraceGlobals(prog, trace);
PRVM_clientglobalfloat(trace_networkentity) = svent;
}
#define CL_HitNetworkBrushModels(move) !((move) == MOVE_WORLDONLY)
#define CL_HitNetworkPlayers(move) !((move) == MOVE_WORLDONLY || (move) == MOVE_NOMONSTERS)
// #16 void(vector v1, vector v2, float movetype, entity ignore) traceline
static void VM_CL_traceline (prvm_prog_t *prog)
{
vec3_t v1, v2;
trace_t trace;
int move, svent;
prvm_edict_t *ent;
// R_TimeReport("pretraceline");
VM_SAFEPARMCOUNTRANGE(4, 4, VM_CL_traceline);
prog->xfunction->builtinsprofile += 30;
VectorCopy(PRVM_G_VECTOR(OFS_PARM0), v1);
VectorCopy(PRVM_G_VECTOR(OFS_PARM1), v2);
move = (int)PRVM_G_FLOAT(OFS_PARM2);
ent = PRVM_G_EDICT(OFS_PARM3);
if (isnan(v1[0]) || isnan(v1[1]) || isnan(v1[2]) || isnan(v2[0]) || isnan(v2[1]) || isnan(v2[2]))
prog->error_cmd("%s: NAN errors detected in traceline('%f %f %f', '%f %f %f', %i, entity %i)\n", prog->name, v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], move, PRVM_EDICT_TO_PROG(ent));
trace = CL_TraceLine(v1, v2, move, ent, CL_GenericHitSuperContentsMask(ent), 0, 0, collision_extendtracelinelength.value, CL_HitNetworkBrushModels(move), CL_HitNetworkPlayers(move), &svent, true, false);
CL_VM_SetTraceGlobals(prog, &trace, svent);
// R_TimeReport("traceline");
}
/*
=================
VM_CL_tracebox
Used for use tracing and shot targeting
Traces are blocked by bbox and exact bsp entityes, and also slide box entities
if the tryents flag is set.
tracebox (vector1, vector mins, vector maxs, vector2, tryents)
=================
*/
// LadyHavoc: added this for my own use, VERY useful, similar to traceline
static void VM_CL_tracebox (prvm_prog_t *prog)
{
vec3_t v1, v2, m1, m2;
trace_t trace;
int move, svent;
prvm_edict_t *ent;
// R_TimeReport("pretracebox");
VM_SAFEPARMCOUNTRANGE(6, 8, VM_CL_tracebox); // allow more parameters for future expansion
prog->xfunction->builtinsprofile += 30;
VectorCopy(PRVM_G_VECTOR(OFS_PARM0), v1);
VectorCopy(PRVM_G_VECTOR(OFS_PARM1), m1);
VectorCopy(PRVM_G_VECTOR(OFS_PARM2), m2);
VectorCopy(PRVM_G_VECTOR(OFS_PARM3), v2);
move = (int)PRVM_G_FLOAT(OFS_PARM4);
ent = PRVM_G_EDICT(OFS_PARM5);
if (isnan(v1[0]) || isnan(v1[1]) || isnan(v1[2]) || isnan(v2[0]) || isnan(v2[1]) || isnan(v2[2]))
prog->error_cmd("%s: NAN errors detected in tracebox('%f %f %f', '%f %f %f', '%f %f %f', '%f %f %f', %i, entity %i)\n", prog->name, v1[0], v1[1], v1[2], m1[0], m1[1], m1[2], m2[0], m2[1], m2[2], v2[0], v2[1], v2[2], move, PRVM_EDICT_TO_PROG(ent));
trace = CL_TraceBox(v1, m1, m2, v2, move, ent, CL_GenericHitSuperContentsMask(ent), 0, 0, collision_extendtraceboxlength.value, CL_HitNetworkBrushModels(move), CL_HitNetworkPlayers(move), &svent, true);
CL_VM_SetTraceGlobals(prog, &trace, svent);
// R_TimeReport("tracebox");
}
static trace_t CL_Trace_Toss (prvm_prog_t *prog, prvm_edict_t *tossent, prvm_edict_t *ignore, int *svent)
{
int i;
float gravity;
vec3_t start, end, mins, maxs, move;
vec3_t original_origin;
vec3_t original_velocity;
vec3_t original_angles;
vec3_t original_avelocity;
trace_t trace;
VectorCopy(PRVM_clientedictvector(tossent, origin) , original_origin );
VectorCopy(PRVM_clientedictvector(tossent, velocity) , original_velocity );
VectorCopy(PRVM_clientedictvector(tossent, angles) , original_angles );
VectorCopy(PRVM_clientedictvector(tossent, avelocity), original_avelocity);
gravity = PRVM_clientedictfloat(tossent, gravity);
if (!gravity)
gravity = 1.0f;
gravity *= cl.movevars_gravity * 0.05;
for (i = 0;i < 200;i++) // LadyHavoc: sanity check; never trace more than 10 seconds
{
PRVM_clientedictvector(tossent, velocity)[2] -= gravity;
VectorMA (PRVM_clientedictvector(tossent, angles), 0.05, PRVM_clientedictvector(tossent, avelocity), PRVM_clientedictvector(tossent, angles));
VectorScale (PRVM_clientedictvector(tossent, velocity), 0.05, move);
VectorAdd (PRVM_clientedictvector(tossent, origin), move, end);
VectorCopy(PRVM_clientedictvector(tossent, origin), start);
VectorCopy(PRVM_clientedictvector(tossent, mins), mins);
VectorCopy(PRVM_clientedictvector(tossent, maxs), maxs);
trace = CL_TraceBox(start, mins, maxs, end, MOVE_NORMAL, tossent, CL_GenericHitSuperContentsMask(tossent), 0, 0, collision_extendmovelength.value, true, true, NULL, true);
VectorCopy (trace.endpos, PRVM_clientedictvector(tossent, origin));
if (trace.fraction < 1)
break;
}
VectorCopy(original_origin , PRVM_clientedictvector(tossent, origin) );
VectorCopy(original_velocity , PRVM_clientedictvector(tossent, velocity) );
VectorCopy(original_angles , PRVM_clientedictvector(tossent, angles) );
VectorCopy(original_avelocity, PRVM_clientedictvector(tossent, avelocity));
return trace;
}
static void VM_CL_tracetoss (prvm_prog_t *prog)
{
trace_t trace;
prvm_edict_t *ent;
prvm_edict_t *ignore;
int svent = 0;
prog->xfunction->builtinsprofile += 600;
VM_SAFEPARMCOUNT(2, VM_CL_tracetoss);
ent = PRVM_G_EDICT(OFS_PARM0);
if (ent == prog->edicts)
{
VM_Warning(prog, "tracetoss: can not use world entity\n");
return;
}
ignore = PRVM_G_EDICT(OFS_PARM1);
trace = CL_Trace_Toss (prog, ent, ignore, &svent);
CL_VM_SetTraceGlobals(prog, &trace, svent);
}
// #20 void(string s) precache_model
static void VM_CL_precache_model (prvm_prog_t *prog)
{
const char *name;
int i;
model_t *m;
VM_SAFEPARMCOUNT(1, VM_CL_precache_model);
name = PRVM_G_STRING(OFS_PARM0);
for (i = 0;i < MAX_MODELS && cl.csqc_model_precache[i];i++)
{
if(!strcmp(cl.csqc_model_precache[i]->name, name))
{
PRVM_G_FLOAT(OFS_RETURN) = -(i+1);
return;
}
}
PRVM_G_FLOAT(OFS_RETURN) = 0;
m = Mod_ForName(name, false, false, name[0] == '*' ? cl.model_name[1] : NULL);
if(m && m->loaded)
{
for (i = 0;i < MAX_MODELS;i++)
{
if (!cl.csqc_model_precache[i])
{
cl.csqc_model_precache[i] = (model_t*)m;
PRVM_G_FLOAT(OFS_RETURN) = -(i+1);
return;
}
}
VM_Warning(prog, "VM_CL_precache_model: no free models\n");
return;
}
VM_Warning(prog, "VM_CL_precache_model: model \"%s\" not found\n", name);
}
// #22 entity(vector org, float rad) findradius
static void VM_CL_findradius (prvm_prog_t *prog)
{
prvm_edict_t *ent, *chain;
vec_t radius, radius2;
vec3_t org, eorg, mins, maxs;
int i, numtouchedicts;
static prvm_edict_t *touchedicts[MAX_EDICTS];
int chainfield;
VM_SAFEPARMCOUNTRANGE(2, 3, VM_CL_findradius);
if(prog->argc == 3)
chainfield = PRVM_G_INT(OFS_PARM2);
else
chainfield = prog->fieldoffsets.chain;
if(chainfield < 0)
prog->error_cmd("VM_CL_findradius: %s doesnt have the specified chain field !", prog->name);
chain = (prvm_edict_t *)prog->edicts;
VectorCopy(PRVM_G_VECTOR(OFS_PARM0), org);
radius = PRVM_G_FLOAT(OFS_PARM1);
radius2 = radius * radius;
mins[0] = org[0] - (radius + 1);
mins[1] = org[1] - (radius + 1);
mins[2] = org[2] - (radius + 1);
maxs[0] = org[0] + (radius + 1);
maxs[1] = org[1] + (radius + 1);
maxs[2] = org[2] + (radius + 1);
numtouchedicts = World_EntitiesInBox(&cl.world, mins, maxs, MAX_EDICTS, touchedicts);
if (numtouchedicts > MAX_EDICTS)
{
// this never happens //[515]: for what then ?
Con_Printf("CSQC_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
numtouchedicts = MAX_EDICTS;
}
for (i = 0;i < numtouchedicts;i++)
{
ent = touchedicts[i];
// Quake did not return non-solid entities but darkplaces does
// (note: this is the reason you can't blow up fallen zombies)
if (PRVM_clientedictfloat(ent, solid) == SOLID_NOT && !sv_gameplayfix_blowupfallenzombies.integer)
continue;
// LadyHavoc: compare against bounding box rather than center so it
// doesn't miss large objects, and use DotProduct instead of Length
// for a major speedup
VectorSubtract(org, PRVM_clientedictvector(ent, origin), eorg);
if (sv_gameplayfix_findradiusdistancetobox.integer)
{
eorg[0] -= bound(PRVM_clientedictvector(ent, mins)[0], eorg[0], PRVM_clientedictvector(ent, maxs)[0]);
eorg[1] -= bound(PRVM_clientedictvector(ent, mins)[1], eorg[1], PRVM_clientedictvector(ent, maxs)[1]);
eorg[2] -= bound(PRVM_clientedictvector(ent, mins)[2], eorg[2], PRVM_clientedictvector(ent, maxs)[2]);
}
else
VectorMAMAM(1, eorg, -0.5f, PRVM_clientedictvector(ent, mins), -0.5f, PRVM_clientedictvector(ent, maxs), eorg);
if (DotProduct(eorg, eorg) < radius2)
{
PRVM_EDICTFIELDEDICT(ent, chainfield) = PRVM_EDICT_TO_PROG(chain);
chain = ent;
}
}
VM_RETURN_EDICT(chain);
}
// #566 entity(vector mins, vector maxs) findbox
// #566 entity(vector mins, vector maxs, .entity tofield) findbox_tofield
static void VM_CL_findbox (prvm_prog_t *prog)
{
prvm_edict_t *chain;
int i, numtouchedicts;
static prvm_edict_t *touchedicts[MAX_EDICTS];
int chainfield;
VM_SAFEPARMCOUNTRANGE(2, 3, VM_CL_findbox);
if(prog->argc == 3)
chainfield = PRVM_G_INT(OFS_PARM2);
else
chainfield = prog->fieldoffsets.chain;
if(chainfield < 0)
prog->error_cmd("VM_CL_findbox: %s doesnt have the specified chain field !", prog->name);
chain = (prvm_edict_t *)prog->edicts;
numtouchedicts = World_EntitiesInBox(&cl.world, PRVM_G_VECTOR(OFS_PARM0), PRVM_G_VECTOR(OFS_PARM1), MAX_EDICTS, touchedicts);
if (numtouchedicts > MAX_EDICTS)
{
// this never happens //[515]: for what then ?
Con_Printf("World_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
numtouchedicts = MAX_EDICTS;
}
for (i = 0; i < numtouchedicts; ++i)
{
PRVM_EDICTFIELDEDICT(touchedicts[i], chainfield) = PRVM_EDICT_TO_PROG(chain);
chain = touchedicts[i];
}
VM_RETURN_EDICT(chain);
}
// #34 float() droptofloor
static void VM_CL_droptofloor (prvm_prog_t *prog)
{
prvm_edict_t *ent;
vec3_t start, end, mins, maxs;
trace_t trace;
VM_SAFEPARMCOUNTRANGE(0, 2, VM_CL_droptofloor); // allow 2 parameters because the id1 defs.qc had an incorrect prototype
// assume failure if it returns early
PRVM_G_FLOAT(OFS_RETURN) = 0;
ent = PRVM_PROG_TO_EDICT(PRVM_clientglobaledict(self));
if (ent == prog->edicts)
{
VM_Warning(prog, "droptofloor: can not modify world entity\n");
return;
}
if (ent->free)
{
VM_Warning(prog, "droptofloor: can not modify free entity\n");
return;
}
VectorCopy(PRVM_clientedictvector(ent, origin), start);
VectorCopy(PRVM_clientedictvector(ent, mins), mins);
VectorCopy(PRVM_clientedictvector(ent, maxs), maxs);
VectorCopy(PRVM_clientedictvector(ent, origin), end);
if (cl.worldmodel->brush.isq3bsp)
end[2] -= 4096;
else if (cl.worldmodel->brush.isq2bsp)
end[2] -= 128;
else
end[2] -= 256; // Quake, QuakeWorld
trace = CL_TraceBox(start, mins, maxs, end, MOVE_NORMAL, ent, CL_GenericHitSuperContentsMask(ent), 0, 0, collision_extendmovelength.value, true, true, NULL, true);
if (trace.fraction != 1)
{
VectorCopy (trace.endpos, PRVM_clientedictvector(ent, origin));
PRVM_clientedictfloat(ent, flags) = (int)PRVM_clientedictfloat(ent, flags) | FL_ONGROUND;
PRVM_clientedictedict(ent, groundentity) = PRVM_EDICT_TO_PROG(trace.ent);
PRVM_G_FLOAT(OFS_RETURN) = 1;
// if support is destroyed, keep suspended (gross hack for floating items in various maps)
// ent->priv.server->suspendedinairflag = true;
}
}
// #35 void(float style, string value) lightstyle
static void VM_CL_lightstyle (prvm_prog_t *prog)
{
int i;
const char *c;
VM_SAFEPARMCOUNT(2, VM_CL_lightstyle);
i = (int)PRVM_G_FLOAT(OFS_PARM0);
c = PRVM_G_STRING(OFS_PARM1);
if (i >= cl.max_lightstyle)
{
VM_Warning(prog, "VM_CL_lightstyle >= MAX_LIGHTSTYLES\n");
return;
}
dp_strlcpy (cl.lightstyle[i].map, c, sizeof (cl.lightstyle[i].map));
cl.lightstyle[i].map[MAX_STYLESTRING - 1] = 0;
cl.lightstyle[i].length = (int)strlen(cl.lightstyle[i].map);
}
// #40 float(entity e) checkbottom
static void VM_CL_checkbottom (prvm_prog_t *prog)
{
static int cs_yes, cs_no;
prvm_edict_t *ent;
vec3_t mins, maxs, start, stop;
trace_t trace;
int x, y;
float mid, bottom;
VM_SAFEPARMCOUNT(1, VM_CL_checkbottom);
ent = PRVM_G_EDICT(OFS_PARM0);
PRVM_G_FLOAT(OFS_RETURN) = 0;
VectorAdd (PRVM_clientedictvector(ent, origin), PRVM_clientedictvector(ent, mins), mins);
VectorAdd (PRVM_clientedictvector(ent, origin), PRVM_clientedictvector(ent, maxs), maxs);
// if all of the points under the corners are solid world, don't bother
// with the tougher checks
// the corners must be within 16 of the midpoint
start[2] = mins[2] - 1;
for (x=0 ; x<=1 ; x++)
for (y=0 ; y<=1 ; y++)
{
start[0] = x ? maxs[0] : mins[0];
start[1] = y ? maxs[1] : mins[1];
if (!(CL_PointSuperContents(start) & (SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY)))
goto realcheck;
}
cs_yes++;
PRVM_G_FLOAT(OFS_RETURN) = true;
return; // we got out easy
realcheck:
cs_no++;
//
// check it for real...
//
start[2] = mins[2];
// the midpoint must be within 16 of the bottom
start[0] = stop[0] = (mins[0] + maxs[0])*0.5;
start[1] = stop[1] = (mins[1] + maxs[1])*0.5;
stop[2] = start[2] - 2*sv_stepheight.value;
trace = CL_TraceLine(start, stop, MOVE_NORMAL, ent, CL_GenericHitSuperContentsMask(ent), 0, 0, collision_extendmovelength.value, true, true, NULL, true, false);
if (trace.fraction == 1.0)
return;
mid = bottom = trace.endpos[2];
// the corners must be within 16 of the midpoint
for (x=0 ; x<=1 ; x++)
for (y=0 ; y<=1 ; y++)
{
start[0] = stop[0] = x ? maxs[0] : mins[0];
start[1] = stop[1] = y ? maxs[1] : mins[1];
trace = CL_TraceLine(start, stop, MOVE_NORMAL, ent, CL_GenericHitSuperContentsMask(ent), 0, 0, collision_extendmovelength.value, true, true, NULL, true, false);
if (trace.fraction != 1.0 && trace.endpos[2] > bottom)
bottom = trace.endpos[2];
if (trace.fraction == 1.0 || mid - trace.endpos[2] > sv_stepheight.value)
return;
}
cs_yes++;
PRVM_G_FLOAT(OFS_RETURN) = true;
}
// #41 float(vector v) pointcontents
static void VM_CL_pointcontents (prvm_prog_t *prog)
{
vec3_t point;
VM_SAFEPARMCOUNT(1, VM_CL_pointcontents);
VectorCopy(PRVM_G_VECTOR(OFS_PARM0), point);
PRVM_G_FLOAT(OFS_RETURN) = Mod_Q1BSP_NativeContentsFromSuperContents(CL_PointSuperContents(point));
}
// #48 void(vector o, vector d, float color, float count) particle
static void VM_CL_particle (prvm_prog_t *prog)
{
vec3_t org, dir;
int count;
unsigned char color;
VM_SAFEPARMCOUNT(4, VM_CL_particle);
VectorCopy(PRVM_G_VECTOR(OFS_PARM0), org);
VectorCopy(PRVM_G_VECTOR(OFS_PARM1), dir);
color = (int)PRVM_G_FLOAT(OFS_PARM2);
count = (int)PRVM_G_FLOAT(OFS_PARM3);
CL_ParticleEffect(EFFECT_SVC_PARTICLE, count, org, org, dir, dir, NULL, color);
}
// #74 void(vector pos, string samp, float vol, float atten) ambientsound
static void VM_CL_ambientsound (prvm_prog_t *prog)
{
vec3_t f;
sfx_t *s;
VM_SAFEPARMCOUNT(4, VM_CL_ambientsound);
VectorCopy(PRVM_G_VECTOR(OFS_PARM0), f);
s = S_FindName(PRVM_G_STRING(OFS_PARM1));
S_StaticSound (s, f, PRVM_G_FLOAT(OFS_PARM2), PRVM_G_FLOAT(OFS_PARM3)*64);
}
// #92 vector(vector org[, float lpflag]) getlight (DP_QC_GETLIGHT)
static void VM_CL_getlight (prvm_prog_t *prog)
{
vec3_t ambientcolor, diffusecolor, diffusenormal;
vec3_t p;
int flags = prog->argc >= 2 ? PRVM_G_FLOAT(OFS_PARM1) : LP_LIGHTMAP;
VM_SAFEPARMCOUNTRANGE(1, 3, VM_CL_getlight);
VectorCopy(PRVM_G_VECTOR(OFS_PARM0), p);
R_CompleteLightPoint(ambientcolor, diffusecolor, diffusenormal, p, flags, r_refdef.scene.lightmapintensity, r_refdef.scene.ambientintensity);
VectorMA(ambientcolor, 0.5, diffusecolor, PRVM_G_VECTOR(OFS_RETURN));
if (PRVM_clientglobalvector(getlight_ambient))
VectorCopy(ambientcolor, PRVM_clientglobalvector(getlight_ambient));
if (PRVM_clientglobalvector(getlight_diffuse))
VectorCopy(diffusecolor, PRVM_clientglobalvector(getlight_diffuse));
if (PRVM_clientglobalvector(getlight_dir))
VectorCopy(diffusenormal, PRVM_clientglobalvector(getlight_dir));
}
//============================================================================
//[515]: SCENE MANAGER builtins
extern cvar_t v_yshearing;
void CSQC_R_RecalcView (void)
{
extern matrix4x4_t viewmodelmatrix_nobob;
extern matrix4x4_t viewmodelmatrix_withbob;
Matrix4x4_CreateFromQuakeEntity(&r_refdef.view.matrix, cl.csqc_vieworigin[0], cl.csqc_vieworigin[1], cl.csqc_vieworigin[2], cl.csqc_viewangles[0], cl.csqc_viewangles[1], cl.csqc_viewangles[2], 1);
if (v_yshearing.value > 0)
Matrix4x4_QuakeToDuke3D(&r_refdef.view.matrix, &r_refdef.view.matrix, v_yshearing.value);
Matrix4x4_Copy(&viewmodelmatrix_nobob, &r_refdef.view.matrix);
Matrix4x4_ConcatScale(&viewmodelmatrix_nobob, cl_viewmodel_scale.value);
Matrix4x4_Concat(&viewmodelmatrix_withbob, &r_refdef.view.matrix, &cl.csqc_viewmodelmatrixfromengine);
}
//#300 void() clearscene (EXT_CSQC)
static void VM_CL_R_ClearScene (prvm_prog_t *prog)
{
VM_SAFEPARMCOUNT(0, VM_CL_R_ClearScene);
// clear renderable entity and light lists
r_refdef.scene.numentities = 0;
r_refdef.scene.numlights = 0;
// restore the view settings to the values that VM_CL_UpdateView received from the client code
r_refdef.view = csqc_original_r_refdef_view;
// polygonbegin without draw2d arg has to guess
prog->polygonbegin_guess2d = false;
VectorCopy(cl.csqc_vieworiginfromengine, cl.csqc_vieworigin);
VectorCopy(cl.csqc_viewanglesfromengine, cl.csqc_viewangles);
cl.csqc_vidvars.drawworld = r_drawworld.integer != 0;
cl.csqc_vidvars.drawenginesbar = false;
cl.csqc_vidvars.drawcrosshair = false;
CSQC_R_RecalcView();
// clear the CL_Mesh_Scene() used for CSQC polygons and engine effects, they will be added by CSQC_RelinkAllEntities and manually created by CSQC
CL_MeshEntities_Scene_Clear();
}
//#301 void(float mask) addentities (EXT_CSQC)
static void VM_CL_R_AddEntities (prvm_prog_t *prog)
{
double t = Sys_DirtyTime();
int i, drawmask;
prvm_edict_t *ed;
VM_SAFEPARMCOUNT(1, VM_CL_R_AddEntities);
drawmask = (int)PRVM_G_FLOAT(OFS_PARM0);
CSQC_RelinkAllEntities(drawmask);
PRVM_clientglobalfloat(time) = cl.time;
for(i=1;i<prog->num_edicts;i++)
{
// so we can easily check if CSQC entity #edictnum is currently drawn
cl.csqcrenderentities[i].entitynumber = 0;
ed = &prog->edicts[i];
if(ed->free)
continue;
CSQC_Think(ed);
if(ed->free)
continue;
// note that for RF_USEAXIS entities, Predraw sets v_forward/v_right/v_up globals that are read by CSQC_AddRenderEdict
CSQC_Predraw(ed);
if(ed->free)
continue;
if(!((int)PRVM_clientedictfloat(ed, drawmask) & drawmask))
continue;
CSQC_AddRenderEdict(ed, i);
}
// callprofile fixing hack: do not include this time in what is counted for CSQC_UpdateView
t = Sys_DirtyTime() - t;if (t < 0 || t >= 1800) t = 0;
prog->functions[PRVM_clientfunction(CSQC_UpdateView)].totaltime -= t;
}
//#302 void(entity ent) addentity (EXT_CSQC)
static void VM_CL_R_AddEntity (prvm_prog_t *prog)
{
double t = Sys_DirtyTime();
VM_SAFEPARMCOUNT(1, VM_CL_R_AddEntity);
CSQC_AddRenderEdict(PRVM_G_EDICT(OFS_PARM0), 0);
t = Sys_DirtyTime() - t;if (t < 0 || t >= 1800) t = 0;
prog->functions[PRVM_clientfunction(CSQC_UpdateView)].totaltime -= t;
}
//#303 float(float property, ...) setproperty (EXT_CSQC)
//#303 float(float property) getproperty
//#303 vector(float property) getpropertyvec
//#309 float(float property) getproperty
//#309 vector(float property) getpropertyvec
// VorteX: make this function be able to return previously set property if new value is not given
static void VM_CL_R_SetView (prvm_prog_t *prog)
{
int c;
prvm_vec_t *f;
float k;
VM_SAFEPARMCOUNTRANGE(1, 3, VM_CL_R_SetView);
c = (int)PRVM_G_FLOAT(OFS_PARM0);
// return value?
if (prog->argc < 2)
{
switch(c)
{
case VF_MIN:
VectorSet(PRVM_G_VECTOR(OFS_RETURN), r_refdef.view.x, r_refdef.view.y, 0);
break;
case VF_MIN_X:
PRVM_G_FLOAT(OFS_RETURN) = r_refdef.view.x;
break;
case VF_MIN_Y:
PRVM_G_FLOAT(OFS_RETURN) = r_refdef.view.y;
break;
case VF_SIZE:
VectorSet(PRVM_G_VECTOR(OFS_RETURN), r_refdef.view.width, r_refdef.view.height, 0);
break;
case VF_SIZE_X:
PRVM_G_FLOAT(OFS_RETURN) = r_refdef.view.width;
break;
case VF_SIZE_Y:
PRVM_G_FLOAT(OFS_RETURN) = r_refdef.view.height;
break;
case VF_VIEWPORT:
VM_Warning(prog, "VM_CL_R_GetView : VF_VIEWPORT can't be retrieved, use VF_MIN/VF_SIZE instead\n");
break;
case VF_FOV:
VectorSet(PRVM_G_VECTOR(OFS_RETURN), r_refdef.view.ortho_x, r_refdef.view.ortho_y, 0);
break;
case VF_FOVX:
PRVM_G_FLOAT(OFS_RETURN) = r_refdef.view.ortho_x;
break;
case VF_FOVY:
PRVM_G_FLOAT(OFS_RETURN) = r_refdef.view.ortho_y;
break;
case VF_ORIGIN:
VectorCopy(cl.csqc_vieworigin, PRVM_G_VECTOR(OFS_RETURN));
break;
case VF_ORIGIN_X:
PRVM_G_FLOAT(OFS_RETURN) = cl.csqc_vieworigin[0];
break;
case VF_ORIGIN_Y:
PRVM_G_FLOAT(OFS_RETURN) = cl.csqc_vieworigin[1];
break;
case VF_ORIGIN_Z:
PRVM_G_FLOAT(OFS_RETURN) = cl.csqc_vieworigin[2];
break;
case VF_ANGLES:
VectorCopy(cl.csqc_viewangles, PRVM_G_VECTOR(OFS_RETURN));
break;
case VF_ANGLES_X:
PRVM_G_FLOAT(OFS_RETURN) = cl.csqc_viewangles[0];
break;
case VF_ANGLES_Y:
PRVM_G_FLOAT(OFS_RETURN) = cl.csqc_viewangles[1];
break;
case VF_ANGLES_Z:
PRVM_G_FLOAT(OFS_RETURN) = cl.csqc_viewangles[2];
break;
case VF_DRAWWORLD:
PRVM_G_FLOAT(OFS_RETURN) = cl.csqc_vidvars.drawworld;
break;
case VF_DRAWENGINESBAR:
PRVM_G_FLOAT(OFS_RETURN) = cl.csqc_vidvars.drawenginesbar;
break;
case VF_DRAWCROSSHAIR:
PRVM_G_FLOAT(OFS_RETURN) = cl.csqc_vidvars.drawcrosshair;
break;
case VF_CL_VIEWANGLES:
VectorCopy(cl.viewangles, PRVM_G_VECTOR(OFS_RETURN));;
break;
case VF_CL_VIEWANGLES_X:
PRVM_G_FLOAT(OFS_RETURN) = cl.viewangles[0];
break;
case VF_CL_VIEWANGLES_Y:
PRVM_G_FLOAT(OFS_RETURN) = cl.viewangles[1];
break;
case VF_CL_VIEWANGLES_Z:
PRVM_G_FLOAT(OFS_RETURN) = cl.viewangles[2];
break;
case VF_PERSPECTIVE:
PRVM_G_FLOAT(OFS_RETURN) = r_refdef.view.useperspective;
break;
case VF_CLEARSCREEN:
PRVM_G_FLOAT(OFS_RETURN) = r_refdef.view.isoverlay;
break;
case VF_MAINVIEW:
PRVM_G_FLOAT(OFS_RETURN) = r_refdef.view.ismain;
break;
case VF_FOG_DENSITY:
PRVM_G_FLOAT(OFS_RETURN) = r_refdef.fog_density;
break;
case VF_FOG_COLOR:
PRVM_G_VECTOR(OFS_RETURN)[0] = r_refdef.fog_red;
PRVM_G_VECTOR(OFS_RETURN)[1] = r_refdef.fog_green;
PRVM_G_VECTOR(OFS_RETURN)[2] = r_refdef.fog_blue;
break;
case VF_FOG_COLOR_R:
PRVM_G_VECTOR(OFS_RETURN)[0] = r_refdef.fog_red;
break;
case VF_FOG_COLOR_G:
PRVM_G_VECTOR(OFS_RETURN)[1] = r_refdef.fog_green;
break;
case VF_FOG_COLOR_B:
PRVM_G_VECTOR(OFS_RETURN)[2] = r_refdef.fog_blue;
break;
case VF_FOG_ALPHA:
PRVM_G_FLOAT(OFS_RETURN) = r_refdef.fog_alpha;
break;
case VF_FOG_START:
PRVM_G_FLOAT(OFS_RETURN) = r_refdef.fog_start;
break;
case VF_FOG_END:
PRVM_G_FLOAT(OFS_RETURN) = r_refdef.fog_end;
break;
case VF_FOG_HEIGHT:
PRVM_G_FLOAT(OFS_RETURN) = r_refdef.fog_height;
break;
case VF_FOG_FADEDEPTH:
PRVM_G_FLOAT(OFS_RETURN) = r_refdef.fog_fadedepth;
break;
case VF_MINFPS_QUALITY:
PRVM_G_FLOAT(OFS_RETURN) = r_refdef.view.quality;
break;
default:
PRVM_G_FLOAT(OFS_RETURN) = 0;
VM_Warning(prog, "VM_CL_R_GetView : unknown parm %i\n", c);
return;
}
if (csqc_lowres.integer)
{
switch(c)
{
case VF_MIN: case VF_MIN_X: case VF_MIN_Y: case VF_SIZE: case VF_SIZE_X: case VF_SIZE_Y: case VF_VIEWPORT:
VectorScale(PRVM_G_VECTOR(OFS_RETURN), vid_conwidth.value / vid.mode.width, PRVM_G_VECTOR(OFS_RETURN));
}
}
return;
}
if (csqc_lowres.integer)
{
float scale = vid.mode.width / vid_conwidth.value;
switch(c)
{
case VF_MIN: case VF_MIN_X: case VF_MIN_Y: case VF_SIZE: case VF_SIZE_X: case VF_SIZE_Y: case VF_VIEWPORT:
VectorScale(PRVM_G_VECTOR(OFS_PARM1), scale, PRVM_G_VECTOR(OFS_PARM1));
VectorScale(PRVM_G_VECTOR(OFS_PARM2), scale, PRVM_G_VECTOR(OFS_PARM2));
}
}
f = PRVM_G_VECTOR(OFS_PARM1);