]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/func/conveyor.qc
375a6adb867bca7c02d800a7a26c05b43548b31d
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / triggers / func / conveyor.qc
1 #include "conveyor.qh"
2 REGISTER_NET_LINKED(ENT_CLIENT_CONVEYOR)
3
4 void conveyor_think(entity this)
5 {
6 #ifdef CSQC
7         // TODO: check if this is what is causing the glitchiness when switching between them
8         float dt = time - this.move_time;
9         this.move_time = time;
10         if(dt <= 0) { return; }
11 #endif
12
13         // set myself as current conveyor where possible
14         IL_EACH(g_conveyed, it.conveyor == this,
15         {
16                 it.conveyor = NULL;
17                 IL_REMOVE(g_conveyed, it);
18         });
19
20         if(this.state)
21         {
22                 FOREACH_ENTITY_RADIUS((this.absmin + this.absmax) * 0.5, vlen(this.absmax - this.absmin) * 0.5 + 1, !it.conveyor.state && isPushable(it),
23                 {
24                         vector emin = it.absmin;
25                         vector emax = it.absmax;
26                         if(this.solid == SOLID_BSP)
27                         {
28                                 emin -= '1 1 1';
29                                 emax += '1 1 1';
30                         }
31                         if(boxesoverlap(emin, emax, this.absmin, this.absmax)) // quick
32                                 if(WarpZoneLib_BoxTouchesBrush(emin, emax, this, it)) // accurate
33                                 {
34                                         if(!it.conveyor)
35                                                 IL_PUSH(g_conveyed, it);
36                                         it.conveyor = this;
37                                 }
38                 });
39
40                 IL_EACH(g_conveyed, it.conveyor == this,
41                 {
42                         if(IS_CLIENT(it)) // doing it via velocity has quite some advantages
43                                 continue; // done in SV_PlayerPhysics   continue;
44
45                         setorigin(it, it.origin + this.movedir * PHYS_INPUT_FRAMETIME);
46                         move_out_of_solid(it);
47 #ifdef SVQC
48                         UpdateCSQCProjectile(it);
49 #endif
50                         /*
51                         // stupid conveyor code
52                         tracebox(it.origin, it.mins, it.maxs, it.origin + this.movedir * sys_frametime, MOVE_NORMAL, it);
53                         if(trace_fraction > 0)
54                                 setorigin(it, trace_endpos);
55                         */
56                 });
57         }
58
59 #ifdef SVQC
60         this.nextthink = time;
61 #endif
62 }
63
64 #ifdef SVQC
65
66 void conveyor_use(entity this, entity actor, entity trigger)
67 {
68         this.state = !this.state;
69
70         this.SendFlags |= SF_TRIGGER_UPDATE;
71 }
72
73 void conveyor_reset(entity this)
74 {
75         if(this.spawnflags & CONVEYOR_START_ENABLED)
76         {
77                 this.state = STATE_ON;
78         }
79
80         this.SendFlags |= SF_TRIGGER_UPDATE;
81 }
82
83 bool conveyor_send(entity this, entity to, int sendflags)
84 {
85         WriteHeader(MSG_ENTITY, ENT_CLIENT_CONVEYOR);
86         WriteByte(MSG_ENTITY, sendflags);
87
88         if(sendflags & SF_TRIGGER_INIT)
89         {
90                 WriteByte(MSG_ENTITY, this.warpzone_isboxy);
91                 WriteVector(MSG_ENTITY, this.origin);
92
93                 WriteVector(MSG_ENTITY, this.mins);
94                 WriteVector(MSG_ENTITY, this.maxs);
95
96                 WriteVector(MSG_ENTITY, this.movedir);
97
98                 WriteByte(MSG_ENTITY, this.speed);
99                 WriteByte(MSG_ENTITY, this.state);
100
101                 WriteString(MSG_ENTITY, this.targetname);
102                 WriteString(MSG_ENTITY, this.target);
103         }
104
105         if(sendflags & SF_TRIGGER_UPDATE)
106                 WriteByte(MSG_ENTITY, this.state);
107
108         return true;
109 }
110
111 void conveyor_init(entity this)
112 {
113         if (!this.speed) this.speed = 200;
114         this.movedir *= this.speed;
115         setthink(this, conveyor_think);
116         this.nextthink = time;
117         IFTARGETED
118         {
119                 this.use = conveyor_use;
120                 this.reset = conveyor_reset;
121                 this.reset(this);
122         }
123         else
124                 this.state = STATE_ON;
125
126         FixSize(this);
127
128         Net_LinkEntity(this, 0, false, conveyor_send);
129
130         this.SendFlags |= SF_TRIGGER_INIT;
131 }
132
133 spawnfunc(trigger_conveyor)
134 {
135         SetMovedir(this);
136         EXACTTRIGGER_INIT;
137         conveyor_init(this);
138 }
139
140 spawnfunc(func_conveyor)
141 {
142         SetMovedir(this);
143         InitMovingBrushTrigger(this);
144         set_movetype(this, MOVETYPE_NONE);
145         conveyor_init(this);
146 }
147
148 #elif defined(CSQC)
149
150 void conveyor_draw(entity this) { conveyor_think(this); }
151
152 void conveyor_init(entity this, bool isnew)
153 {
154         if(isnew)
155                 IL_PUSH(g_drawables, this);
156         this.draw = conveyor_draw;
157         this.drawmask = MASK_NORMAL;
158
159         set_movetype(this, MOVETYPE_NONE);
160         this.model = "";
161         this.solid = SOLID_TRIGGER;
162         this.move_time = time;
163 }
164
165 NET_HANDLE(ENT_CLIENT_CONVEYOR, bool isnew)
166 {
167         int sendflags = ReadByte();
168
169         if(sendflags & SF_TRIGGER_INIT)
170         {
171                 this.warpzone_isboxy = ReadByte();
172                 this.origin = ReadVector();
173                 setorigin(this, this.origin);
174
175                 this.mins = ReadVector();
176                 this.maxs = ReadVector();
177                 setsize(this, this.mins, this.maxs);
178
179                 this.movedir = ReadVector();
180
181                 this.speed = ReadByte();
182                 this.state = ReadByte();
183
184                 this.targetname = strzone(ReadString());
185                 this.target = strzone(ReadString());
186
187                 conveyor_init(this, isnew);
188         }
189
190         if(sendflags & SF_TRIGGER_UPDATE)
191                 this.state = ReadByte();
192
193         return true;
194 }
195 #endif