1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#include "items.h"
4
5#include <engine/demo.h>
6#include <engine/graphics.h>
7#include <engine/shared/config.h>
8
9#include <generated/client_data.h>
10#include <generated/protocol.h>
11
12#include <game/client/components/effects.h>
13#include <game/client/gameclient.h>
14#include <game/client/laser_data.h>
15#include <game/client/pickup_data.h>
16#include <game/client/prediction/entities/laser.h>
17#include <game/client/prediction/entities/pickup.h>
18#include <game/client/prediction/entities/projectile.h>
19#include <game/client/projectile_data.h>
20#include <game/mapitems.h>
21
22void CItems::RenderProjectile(const CProjectileData *pCurrent, int ItemId, const CScreenRect &ScreenRect)
23{
24 int CurWeapon = std::clamp(val: pCurrent->m_Type, lo: 0, hi: NUM_WEAPONS - 1);
25
26 // get positions
27 float Curvature = 0;
28 float Speed = 0;
29 const CTuningParams *pTuning = GameClient()->GetTuning(i: pCurrent->m_TuneZone);
30 if(CurWeapon == WEAPON_GRENADE)
31 {
32 Curvature = pTuning->m_GrenadeCurvature;
33 Speed = pTuning->m_GrenadeSpeed;
34 }
35 else if(CurWeapon == WEAPON_SHOTGUN)
36 {
37 Curvature = pTuning->m_ShotgunCurvature;
38 Speed = pTuning->m_ShotgunSpeed;
39 }
40 else if(CurWeapon == WEAPON_GUN)
41 {
42 Curvature = pTuning->m_GunCurvature;
43 Speed = pTuning->m_GunSpeed;
44 }
45
46 bool LocalPlayerInGame = false;
47
48 if(GameClient()->m_Snap.m_pLocalInfo)
49 LocalPlayerInGame = GameClient()->m_aClients[GameClient()->m_Snap.m_pLocalInfo->m_ClientId].m_Team != TEAM_SPECTATORS;
50
51 static float s_LastGameTickTime = Client()->GameTickTime(Conn: g_Config.m_ClDummy);
52 if(!GameClient()->IsWorldPaused() && !GameClient()->IsDemoPlaybackPaused())
53 s_LastGameTickTime = Client()->GameTickTime(Conn: g_Config.m_ClDummy);
54
55 bool IsOtherTeam = (pCurrent->m_ExtraInfo && pCurrent->m_Owner >= 0 && GameClient()->IsOtherTeam(ClientId: pCurrent->m_Owner));
56
57 int PredictionTick = Client()->GetPredictionTick();
58
59 float Ct;
60 if(GameClient()->Predict() && GameClient()->AntiPingGrenade() && LocalPlayerInGame && !IsOtherTeam)
61 Ct = ((float)(PredictionTick - 1 - pCurrent->m_StartTick) + Client()->PredIntraGameTick(Conn: g_Config.m_ClDummy)) / (float)Client()->GameTickSpeed();
62 else
63 Ct = (Client()->PrevGameTick(Conn: g_Config.m_ClDummy) - pCurrent->m_StartTick) / (float)Client()->GameTickSpeed() + s_LastGameTickTime;
64 if(Ct < 0)
65 {
66 if(Ct > -s_LastGameTickTime / 2)
67 {
68 // Fixup the timing which might be screwed during demo playback because
69 // s_LastGameTickTime depends on the system timer, while the other part
70 // (Client()->PrevGameTick(g_Config.m_ClDummy) - pCurrent->m_StartTick) / (float)Client()->GameTickSpeed()
71 // is virtually constant (for projectiles fired on the current game tick):
72 // (x - (x+2)) / 50 = -0.04
73 //
74 // We have a strict comparison for the passed time being more than the time between ticks
75 // if(CurtickStart > m_Info.m_CurrentTime) in CDemoPlayer::Update()
76 // so on the practice the typical value of s_LastGameTickTime varies from 0.02386 to 0.03999
77 // which leads to Ct from -0.00001 to -0.01614.
78 // Round up those to 0.0 to fix missing rendering of the projectile.
79 Ct = 0;
80 }
81 else
82 {
83 return; // projectile haven't been shot yet
84 }
85 }
86
87 vec2 Pos = CalcPos(Pos: pCurrent->m_StartPos, Velocity: pCurrent->m_StartVel, Curvature, Speed, Time: Ct);
88 if(!ScreenRect.Inside(Position: Pos))
89 return;
90 vec2 PrevPos = CalcPos(Pos: pCurrent->m_StartPos, Velocity: pCurrent->m_StartVel, Curvature, Speed, Time: Ct - 0.001f);
91
92 float Alpha = 1.f;
93 if(IsOtherTeam)
94 {
95 Alpha = g_Config.m_ClShowOthersAlpha / 100.0f;
96 }
97
98 vec2 Vel = Pos - PrevPos;
99
100 // add particle for this projectile
101 // don't check for validity of the projectile for the current weapon here, so particle effects are rendered for mod compatibility
102 if(CurWeapon == WEAPON_GRENADE)
103 {
104 GameClient()->m_Effects.SmokeTrail(Pos, Vel: Vel * -1, Alpha, TimePassed: 0.0f);
105 static float s_Time = 0.0f;
106 static float s_LastLocalTime = LocalTime();
107 s_Time += (LocalTime() - s_LastLocalTime) * GameClient()->GetAnimationPlaybackSpeed();
108 Graphics()->QuadsSetRotation(Angle: s_Time * pi * 2 * 2 + ItemId);
109 s_LastLocalTime = LocalTime();
110 }
111 else
112 {
113 GameClient()->m_Effects.BulletTrail(Pos, Alpha, TimePassed: 0.0f);
114
115 if(length(a: Vel) > 0.00001f)
116 Graphics()->QuadsSetRotation(Angle: angle(a: Vel));
117 else
118 Graphics()->QuadsSetRotation(Angle: 0);
119 }
120
121 if(GameClient()->m_GameSkin.m_aSpriteWeaponProjectiles[CurWeapon].IsValid())
122 {
123 Graphics()->TextureSet(Texture: GameClient()->m_GameSkin.m_aSpriteWeaponProjectiles[CurWeapon]);
124 Graphics()->SetColor(r: 1.f, g: 1.f, b: 1.f, a: Alpha);
125 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_ItemsQuadContainerIndex, QuadOffset: m_aProjectileOffset[CurWeapon], X: Pos.x, Y: Pos.y);
126 }
127}
128
129void CItems::RenderPickup(const CNetObj_Pickup *pPrev, const CNetObj_Pickup *pCurrent, bool IsPredicted, int Flags)
130{
131 int CurWeapon = std::clamp(val: pCurrent->m_Subtype, lo: 0, hi: NUM_WEAPONS - 1);
132 int QuadOffset = 2;
133 float IntraTick = IsPredicted ? Client()->PredIntraGameTick(Conn: g_Config.m_ClDummy) : Client()->IntraGameTick(Conn: g_Config.m_ClDummy);
134 vec2 Pos = mix(a: vec2(pPrev->m_X, pPrev->m_Y), b: vec2(pCurrent->m_X, pCurrent->m_Y), amount: IntraTick);
135 if(pCurrent->m_Type == POWERUP_HEALTH)
136 {
137 QuadOffset = m_PickupHealthOffset;
138 Graphics()->TextureSet(Texture: GameClient()->m_GameSkin.m_SpritePickupHealth);
139 }
140 else if(pCurrent->m_Type == POWERUP_FREEZE)
141 {
142 QuadOffset = m_PickupFreezeOffset;
143 Graphics()->TextureSet(Texture: GameClient()->m_GameSkin.m_SpritePickupFreeze);
144 }
145 else if(pCurrent->m_Type == POWERUP_ARMOR)
146 {
147 QuadOffset = m_PickupArmorOffset;
148 Graphics()->TextureSet(Texture: GameClient()->m_GameSkin.m_SpritePickupArmor);
149 }
150 else if(pCurrent->m_Type == POWERUP_WEAPON)
151 {
152 QuadOffset = m_aPickupWeaponOffset[CurWeapon];
153 Graphics()->TextureSet(Texture: GameClient()->m_GameSkin.m_aSpritePickupWeapons[CurWeapon]);
154 }
155 else if(pCurrent->m_Type == POWERUP_NINJA)
156 {
157 QuadOffset = m_PickupNinjaOffset;
158 if(Flags & PICKUPFLAG_ROTATE)
159 GameClient()->m_Effects.PowerupShine(Pos, Size: vec2(18, 96), Alpha: 1.0f);
160 else
161 GameClient()->m_Effects.PowerupShine(Pos, Size: vec2(96, 18), Alpha: 1.0f);
162
163 Graphics()->TextureSet(Texture: GameClient()->m_GameSkin.m_SpritePickupNinja);
164 }
165 else if(pCurrent->m_Type >= POWERUP_ARMOR_SHOTGUN && pCurrent->m_Type <= POWERUP_ARMOR_LASER)
166 {
167 QuadOffset = m_aPickupWeaponArmorOffset[pCurrent->m_Type - POWERUP_ARMOR_SHOTGUN];
168 Graphics()->TextureSet(Texture: GameClient()->m_GameSkin.m_aSpritePickupWeaponArmor[pCurrent->m_Type - POWERUP_ARMOR_SHOTGUN]);
169 }
170 else
171 {
172 return;
173 }
174 Graphics()->QuadsSetRotation(Angle: 0);
175 Graphics()->SetColor(r: 1.f, g: 1.f, b: 1.f, a: 1.f);
176
177 vec2 Scale = vec2(1, 1);
178 if(Flags & PICKUPFLAG_XFLIP)
179 Scale.x = -Scale.x;
180
181 if(Flags & PICKUPFLAG_YFLIP)
182 Scale.y = -Scale.y;
183
184 if(Flags & PICKUPFLAG_ROTATE)
185 {
186 Graphics()->QuadsSetRotation(Angle: 90.f * (pi / 180));
187 std::swap(a&: Scale.x, b&: Scale.y);
188
189 if(pCurrent->m_Type == POWERUP_NINJA)
190 {
191 if(Flags & PICKUPFLAG_XFLIP)
192 Pos.y += 10.0f;
193 else
194 Pos.y -= 10.0f;
195 }
196 }
197 else
198 {
199 if(pCurrent->m_Type == POWERUP_NINJA)
200 {
201 if(Flags & PICKUPFLAG_XFLIP)
202 Pos.x += 10.0f;
203 else
204 Pos.x -= 10.0f;
205 }
206 }
207
208 static float s_Time = 0.0f;
209 static float s_LastLocalTime = LocalTime();
210 float Offset = Pos.y / 32.0f + Pos.x / 32.0f;
211 s_Time += (LocalTime() - s_LastLocalTime) * GameClient()->GetAnimationPlaybackSpeed();
212 Pos += direction(angle: s_Time * 2.0f + Offset) * 2.5f;
213 s_LastLocalTime = LocalTime();
214
215 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_ItemsQuadContainerIndex, QuadOffset, X: Pos.x, Y: Pos.y, ScaleX: Scale.x, ScaleY: Scale.y);
216 Graphics()->QuadsSetRotation(Angle: 0);
217}
218
219void CItems::RenderFlags()
220{
221 for(int Flag = 0; Flag < GameClient()->m_Snap.m_NumFlags; ++Flag)
222 {
223 RenderFlag(pPrev: GameClient()->m_Snap.m_apPrevFlags[Flag], pCurrent: GameClient()->m_Snap.m_apFlags[Flag],
224 pPrevGameData: GameClient()->m_Snap.m_pPrevGameDataObj, pCurGameData: GameClient()->m_Snap.m_pGameDataObj);
225 }
226}
227
228void CItems::RenderFlag(const CNetObj_Flag *pPrev, const CNetObj_Flag *pCurrent, const CNetObj_GameData *pPrevGameData, const CNetObj_GameData *pCurGameData)
229{
230 vec2 Pos = mix(a: vec2(pPrev->m_X, pPrev->m_Y), b: vec2(pCurrent->m_X, pCurrent->m_Y), amount: Client()->IntraGameTick(Conn: g_Config.m_ClDummy));
231 if(pCurGameData)
232 {
233 int FlagCarrier = (pCurrent->m_Team == TEAM_RED) ? pCurGameData->m_FlagCarrierRed : pCurGameData->m_FlagCarrierBlue;
234 // use the flagcarriers position if available
235 if(FlagCarrier >= 0 && GameClient()->m_Snap.m_aCharacters[FlagCarrier].m_Active)
236 Pos = GameClient()->m_aClients[FlagCarrier].m_RenderPos;
237
238 // make sure that the flag isn't interpolated between capture and return
239 if(pPrevGameData &&
240 ((pCurrent->m_Team == TEAM_RED && pPrevGameData->m_FlagCarrierRed != pCurGameData->m_FlagCarrierRed) ||
241 (pCurrent->m_Team == TEAM_BLUE && pPrevGameData->m_FlagCarrierBlue != pCurGameData->m_FlagCarrierBlue)))
242 Pos = vec2(pCurrent->m_X, pCurrent->m_Y);
243 }
244
245 float Size = 42.0f;
246 int QuadOffset;
247 if(pCurrent->m_Team == TEAM_RED)
248 {
249 Graphics()->TextureSet(Texture: GameClient()->m_GameSkin.m_SpriteFlagRed);
250 QuadOffset = m_RedFlagOffset;
251 }
252 else
253 {
254 Graphics()->TextureSet(Texture: GameClient()->m_GameSkin.m_SpriteFlagBlue);
255 QuadOffset = m_BlueFlagOffset;
256 }
257 Graphics()->QuadsSetRotation(Angle: 0.0f);
258 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f);
259 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_ItemsQuadContainerIndex, QuadOffset, X: Pos.x, Y: Pos.y - Size * 0.75f);
260}
261
262void CItems::RenderLaser(const CLaserData *pCurrent, bool IsPredicted)
263{
264 int Type = std::clamp(val: pCurrent->m_Type, lo: -1, hi: NUM_LASERTYPES - 1);
265 int ColorIn, ColorOut;
266 switch(Type)
267 {
268 case LASERTYPE_RIFLE:
269 ColorOut = g_Config.m_ClLaserRifleOutlineColor;
270 ColorIn = g_Config.m_ClLaserRifleInnerColor;
271 break;
272 case LASERTYPE_SHOTGUN:
273 ColorOut = g_Config.m_ClLaserShotgunOutlineColor;
274 ColorIn = g_Config.m_ClLaserShotgunInnerColor;
275 break;
276 case LASERTYPE_DOOR:
277 ColorOut = g_Config.m_ClLaserDoorOutlineColor;
278 ColorIn = g_Config.m_ClLaserDoorInnerColor;
279 break;
280 case LASERTYPE_FREEZE:
281 ColorOut = g_Config.m_ClLaserFreezeOutlineColor;
282 ColorIn = g_Config.m_ClLaserFreezeInnerColor;
283 break;
284 case LASERTYPE_DRAGGER:
285 ColorOut = g_Config.m_ClLaserDraggerOutlineColor;
286 ColorIn = g_Config.m_ClLaserDraggerInnerColor;
287 break;
288 case LASERTYPE_GUN:
289 case LASERTYPE_PLASMA:
290 if(pCurrent->m_Subtype == LASERGUNTYPE_FREEZE || pCurrent->m_Subtype == LASERGUNTYPE_EXPFREEZE)
291 {
292 ColorOut = g_Config.m_ClLaserFreezeOutlineColor;
293 ColorIn = g_Config.m_ClLaserFreezeInnerColor;
294 }
295 else
296 {
297 ColorOut = g_Config.m_ClLaserRifleOutlineColor;
298 ColorIn = g_Config.m_ClLaserRifleInnerColor;
299 }
300 break;
301 default:
302 ColorOut = g_Config.m_ClLaserRifleOutlineColor;
303 ColorIn = g_Config.m_ClLaserRifleInnerColor;
304 }
305
306 bool IsOtherTeam = (pCurrent->m_ExtraInfo && pCurrent->m_Owner >= 0 && GameClient()->IsOtherTeam(ClientId: pCurrent->m_Owner));
307
308 float Alpha = IsOtherTeam ? g_Config.m_ClShowOthersAlpha / 100.0f : 1.f;
309
310 const ColorRGBA OuterColor = color_cast<ColorRGBA>(hsl: ColorHSLA(ColorOut).WithAlpha(alpha: Alpha));
311 const ColorRGBA InnerColor = color_cast<ColorRGBA>(hsl: ColorHSLA(ColorIn).WithAlpha(alpha: Alpha));
312
313 float Ticks;
314 float TicksHead = Client()->GameTick(Conn: g_Config.m_ClDummy);
315 if(Type == LASERTYPE_DOOR)
316 {
317 Ticks = 1.0f;
318 }
319 else if(IsPredicted)
320 {
321 int PredictionTick = Client()->GetPredictionTick();
322 Ticks = (float)(PredictionTick - pCurrent->m_StartTick) + Client()->PredIntraGameTick(Conn: g_Config.m_ClDummy);
323 TicksHead += Client()->PredIntraGameTick(Conn: g_Config.m_ClDummy);
324 }
325 else
326 {
327 Ticks = (float)(Client()->GameTick(Conn: g_Config.m_ClDummy) - pCurrent->m_StartTick) + Client()->IntraGameTick(Conn: g_Config.m_ClDummy);
328 TicksHead += Client()->IntraGameTick(Conn: g_Config.m_ClDummy);
329 }
330
331 if(Type == LASERTYPE_DRAGGER)
332 {
333 TicksHead *= (((pCurrent->m_Subtype >> 1) % 3) * 4.0f) + 1;
334 TicksHead *= (pCurrent->m_Subtype & 1) ? -1 : 1;
335 }
336 RenderLaser(From: pCurrent->m_From, Pos: pCurrent->m_To, OuterColor, InnerColor, TicksBody: Ticks, TicksHead, Type);
337}
338
339void CItems::RenderLaser(vec2 From, vec2 Pos, ColorRGBA OuterColor, ColorRGBA InnerColor, float TicksBody, float TicksHead, int Type) const
340{
341 float Len = distance(a: Pos, b: From);
342
343 if(Len > 0)
344 {
345 if(Type == LASERTYPE_DRAGGER)
346 {
347 // rubber band effect
348 float Thickness = std::sqrt(x: Len) / 5.f;
349 TicksBody = std::clamp(val: Thickness, lo: 1.0f, hi: 5.0f);
350 }
351 vec2 Dir = normalize_pre_length(v: Pos - From, len: Len);
352
353 float Ms = TicksBody * 1000.0f / Client()->GameTickSpeed();
354 float a;
355 if(Type == LASERTYPE_RIFLE || Type == LASERTYPE_SHOTGUN)
356 {
357 int TuneZone = (Client()->State() == IClient::STATE_ONLINE && GameClient()->m_GameWorld.m_WorldConfig.m_UseTuneZones) ? Collision()->IsTune(Index: Collision()->GetMapIndex(Pos: From)) : 0;
358 a = Ms / GameClient()->GetTuning(i: TuneZone)->m_LaserBounceDelay;
359 }
360 else
361 {
362 a = Ms / CTuningParams::DEFAULT.m_LaserBounceDelay;
363 }
364 a = std::clamp(val: a, lo: 0.0f, hi: 1.0f);
365 float Ia = 1 - a;
366
367 Graphics()->TextureClear();
368 Graphics()->QuadsBegin();
369
370 // do outline
371 Graphics()->SetColor(OuterColor);
372 vec2 Out = vec2(Dir.y, -Dir.x) * (7.0f * Ia);
373
374 IGraphics::CFreeformItem Freeform(
375 From - Out, From + Out,
376 Pos - Out, Pos + Out);
377 Graphics()->QuadsDrawFreeform(pArray: &Freeform, Num: 1);
378
379 // do inner
380 Out = vec2(Dir.y, -Dir.x) * (5.0f * Ia);
381 vec2 ExtraOutlinePos = Dir;
382 vec2 ExtraOutlineFrom = Type == LASERTYPE_DOOR ? vec2(0, 0) : Dir;
383 Graphics()->SetColor(InnerColor); // center
384
385 Freeform = IGraphics::CFreeformItem(
386 From - Out + ExtraOutlineFrom, From + Out + ExtraOutlineFrom,
387 Pos - Out - ExtraOutlinePos, Pos + Out - ExtraOutlinePos);
388 Graphics()->QuadsDrawFreeform(pArray: &Freeform, Num: 1);
389
390 Graphics()->QuadsEnd();
391 }
392
393 // render head
394 if(Type == LASERTYPE_DOOR)
395 {
396 Graphics()->TextureClear();
397 Graphics()->QuadsSetRotation(Angle: 0);
398 Graphics()->SetColor(OuterColor);
399 Graphics()->RenderQuadContainerEx(ContainerIndex: m_ItemsQuadContainerIndex, QuadOffset: m_DoorHeadOffset, QuadDrawNum: 1, X: Pos.x - 8.0f, Y: Pos.y - 8.0f);
400 Graphics()->SetColor(InnerColor);
401 Graphics()->RenderQuadContainerEx(ContainerIndex: m_ItemsQuadContainerIndex, QuadOffset: m_DoorHeadOffset, QuadDrawNum: 1, X: Pos.x - 6.0f, Y: Pos.y - 6.0f, ScaleX: 6.f / 8.f, ScaleY: 6.f / 8.f);
402 }
403 else if(Type == LASERTYPE_DRAGGER)
404 {
405 Graphics()->TextureSet(Texture: GameClient()->m_ExtrasSkin.m_SpritePulley);
406 for(int Inner = 0; Inner < 2; ++Inner)
407 {
408 Graphics()->SetColor(Inner ? InnerColor : OuterColor);
409
410 float Size = Inner ? 4.f / 5.f : 1.f;
411
412 // circle at laser end
413 if(Len > 0)
414 {
415 Graphics()->QuadsSetRotation(Angle: 0);
416 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_ItemsQuadContainerIndex, QuadOffset: m_PulleyHeadOffset, X: From.x, Y: From.y, ScaleX: Size, ScaleY: Size);
417 }
418
419 //rotating orbs
420 Size = Inner ? 0.75f - 1.f / 5.f : 0.75f;
421 for(int Orb = 0; Orb < 3; ++Orb)
422 {
423 vec2 Offset(10.f, 0);
424 Offset = rotate(a: Offset, angle: Orb * 120 + TicksHead);
425 Graphics()->QuadsSetRotation(Angle: TicksHead + Orb * pi * 2.f / 3.f); // rotate the sprite as well, as it might be customized
426 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_ItemsQuadContainerIndex, QuadOffset: m_PulleyHeadOffset, X: From.x + Offset.x, Y: From.y + Offset.y, ScaleX: Size, ScaleY: Size);
427 }
428 }
429 }
430 else if(Type == LASERTYPE_FREEZE)
431 {
432 float Pulsation = 6.f / 5.f + 1.f / 10.f * std::sin(x: TicksHead / 2.f);
433 float Angle = angle(a: Pos - From);
434 Graphics()->TextureSet(Texture: GameClient()->m_ExtrasSkin.m_SpriteHectagon);
435 Graphics()->QuadsSetRotation(Angle);
436 Graphics()->SetColor(OuterColor);
437 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_ItemsQuadContainerIndex, QuadOffset: m_FreezeHeadOffset, X: Pos.x, Y: Pos.y, ScaleX: 6.f / 5.f * Pulsation, ScaleY: 6.f / 5.f * Pulsation);
438 Graphics()->TextureSet(Texture: GameClient()->m_ExtrasSkin.m_SpriteParticleSnowflake);
439 // snowflakes are white
440 Graphics()->SetColor(ColorRGBA(1.f, 1.f, 1.f));
441 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_ItemsQuadContainerIndex, QuadOffset: m_FreezeHeadOffset, X: Pos.x, Y: Pos.y, ScaleX: Pulsation, ScaleY: Pulsation);
442 }
443 else
444 {
445 int CurParticle = (int)TicksHead % 3;
446 Graphics()->TextureSet(Texture: GameClient()->m_ParticlesSkin.m_aSpriteParticleSplat[CurParticle]);
447 Graphics()->QuadsSetRotation(Angle: (int)TicksHead);
448 Graphics()->SetColor(OuterColor);
449 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_ItemsQuadContainerIndex, QuadOffset: m_aParticleSplatOffset[CurParticle], X: Pos.x, Y: Pos.y);
450 Graphics()->SetColor(InnerColor);
451 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_ItemsQuadContainerIndex, QuadOffset: m_aParticleSplatOffset[CurParticle], X: Pos.x, Y: Pos.y, ScaleX: 20.f / 24.f, ScaleY: 20.f / 24.f);
452 }
453}
454
455void CItems::OnRender()
456{
457 if(Client()->State() != IClient::STATE_ONLINE && Client()->State() != IClient::STATE_DEMOPLAYBACK)
458 return;
459
460 bool IsSuper = GameClient()->IsLocalCharSuper();
461 int Ticks = Client()->GameTick(Conn: g_Config.m_ClDummy) % Client()->GameTickSpeed();
462 bool BlinkingPickup = (Ticks % 22) < 4;
463 bool BlinkingGun = (Ticks % 22) < 4;
464 bool BlinkingDragger = (Ticks % 22) < 4;
465 bool BlinkingProj = (Ticks % 20) < 2;
466 bool BlinkingProjEx = (Ticks % 6) < 2;
467 bool BlinkingLight = (Ticks % 6) < 2;
468 int SwitcherTeam = GameClient()->SwitchStateTeam();
469 int DraggerStartTick = std::max(a: (Client()->GameTick(Conn: g_Config.m_ClDummy) / 7) * 7, b: Client()->GameTick(Conn: g_Config.m_ClDummy) - 4);
470 int GunStartTick = (Client()->GameTick(Conn: g_Config.m_ClDummy) / 7) * 7;
471
472 bool UsePredicted = GameClient()->Predict() && GameClient()->AntiPingGunfire();
473 auto &aSwitchers = GameClient()->Switchers();
474
475 CScreenRect ScreenRectLaser = Graphics()->GetScreen();
476 CScreenRect ScreenRectProjectile = ScreenRectLaser;
477 CScreenRect ScreenRectPickup = ScreenRectLaser;
478
479 constexpr float TileSize = 64.0f;
480 ScreenRectProjectile.Expand(Size: TileSize);
481 ScreenRectLaser.Expand(Size: TileSize / 2.0f);
482 ScreenRectPickup.Expand(Width: 1.75f * TileSize, Height: 0.75f * TileSize);
483
484 auto IsLaserInside = [&](const CLaserData &LaserData) -> bool {
485 const vec2 &From = LaserData.m_From;
486 const vec2 &To = LaserData.m_To;
487 return !((From.x < ScreenRectLaser.m_TopLeft.x && To.x < ScreenRectLaser.m_TopLeft.x) || (From.x > ScreenRectLaser.m_BottomRight.x && To.x > ScreenRectLaser.m_BottomRight.x) ||
488 (From.y < ScreenRectLaser.m_TopLeft.y && To.y < ScreenRectLaser.m_TopLeft.y) || (From.y > ScreenRectLaser.m_BottomRight.y && To.y > ScreenRectLaser.m_BottomRight.y));
489 };
490
491 if(UsePredicted)
492 {
493 {
494 CGameWorld::CEntityIterator Iterator(&GameClient()->m_PrevPredictedWorld, CGameWorld::ENTTYPE_PROJECTILE);
495 while(CProjectile *pProj = static_cast<CProjectile *>(Iterator.Next()))
496 {
497 if(!IsSuper && pProj->m_Number > 0 && pProj->m_Number < (int)aSwitchers.size() && !aSwitchers[pProj->m_Number].m_aStatus[SwitcherTeam] && (pProj->m_Explosive ? BlinkingProjEx : BlinkingProj))
498 continue;
499
500 CProjectileData Data = pProj->GetData();
501 RenderProjectile(pCurrent: &Data, ItemId: pProj->GetId(), ScreenRect: ScreenRectProjectile);
502 }
503 }
504 {
505 CGameWorld::CEntityIterator Iterator(&GameClient()->m_PrevPredictedWorld, CGameWorld::ENTTYPE_LASER);
506 while(CLaser *pLaser = static_cast<CLaser *>(Iterator.Next()))
507 {
508 if(pLaser->GetOwner() < 0 || !GameClient()->m_aClients[pLaser->GetOwner()].m_IsPredictedLocal)
509 continue;
510 CLaserData Data = pLaser->GetData();
511 if(!IsLaserInside(Data))
512 continue;
513 RenderLaser(pCurrent: &Data, IsPredicted: true);
514 }
515 }
516 {
517 CGameWorld::CEntityIterator Iterator(&GameClient()->m_PrevPredictedWorld, CGameWorld::ENTTYPE_PICKUP);
518 while(CPickup *pPickup = static_cast<CPickup *>(Iterator.Next()))
519 {
520 if(!IsSuper && pPickup->m_Layer == LAYER_SWITCH && pPickup->m_Number > 0 && pPickup->m_Number < (int)aSwitchers.size() && !aSwitchers[pPickup->m_Number].m_aStatus[SwitcherTeam] && BlinkingPickup)
521 continue;
522
523 if(pPickup->InDDNetTile())
524 {
525 if(auto *pPrev = (CPickup *)GameClient()->m_PrevPredictedWorld.GetEntity(Id: pPickup->GetId(), EntityType: CGameWorld::ENTTYPE_PICKUP))
526 {
527 CNetObj_Pickup Data, Prev;
528 pPickup->FillInfo(pPickup: &Data);
529 pPrev->FillInfo(pPickup: &Prev);
530 RenderPickup(pPrev: &Prev, pCurrent: &Data, IsPredicted: true, Flags: pPickup->Flags());
531 }
532 }
533 }
534 }
535 }
536
537 for(const CSnapEntities &Ent : GameClient()->SnapEntities())
538 {
539 const IClient::CSnapItem Item = Ent.m_Item;
540 const void *pData = Item.m_pData;
541 const CNetObj_EntityEx *pEntEx = Ent.m_pDataEx;
542
543 if(Item.m_Type == NETOBJTYPE_PROJECTILE || Item.m_Type == NETOBJTYPE_DDRACEPROJECTILE || Item.m_Type == NETOBJTYPE_DDNETPROJECTILE)
544 {
545 CProjectileData Data = ExtractProjectileInfo(NetObjType: Item.m_Type, pData, pGameWorld: &GameClient()->m_GameWorld, pEntEx);
546 bool Inactive = !IsSuper && Data.m_SwitchNumber > 0 && Data.m_SwitchNumber < (int)aSwitchers.size() && !aSwitchers[Data.m_SwitchNumber].m_aStatus[SwitcherTeam];
547 if(Inactive && (Data.m_Explosive ? BlinkingProjEx : BlinkingProj))
548 continue;
549 if(UsePredicted)
550
551 {
552 if(auto *pProj = (CProjectile *)GameClient()->m_GameWorld.FindMatch(ObjId: Item.m_Id, ObjType: Item.m_Type, pObjData: pData))
553 {
554 bool IsOtherTeam = GameClient()->IsOtherTeam(ClientId: pProj->GetOwner());
555 if(pProj->m_LastRenderTick <= 0 && (pProj->m_Type != WEAPON_SHOTGUN || (!pProj->m_Freeze && !pProj->m_Explosive)) // skip ddrace shotgun bullets
556 && (pProj->m_Type == WEAPON_SHOTGUN || absolute(a: length(a: pProj->m_Direction) - 1.f) < 0.02f) // workaround to skip grenades on ball mod
557 && (pProj->GetOwner() < 0 || !GameClient()->m_aClients[pProj->GetOwner()].m_IsPredictedLocal || IsOtherTeam) // skip locally predicted projectiles
558 && !Client()->SnapFindItem(SnapId: IClient::SNAP_PREV, Type: Item.m_Type, Id: Item.m_Id))
559 {
560 ReconstructSmokeTrail(pCurrent: &Data, DestroyTick: pProj->m_DestroyTick);
561 }
562 pProj->m_LastRenderTick = Client()->GameTick(Conn: g_Config.m_ClDummy);
563 if(!IsOtherTeam)
564 continue;
565 }
566 }
567 RenderProjectile(pCurrent: &Data, ItemId: Item.m_Id, ScreenRect: ScreenRectProjectile);
568 }
569 else if(Item.m_Type == NETOBJTYPE_PICKUP || Item.m_Type == NETOBJTYPE_DDNETPICKUP)
570 {
571 CPickupData Data = ExtractPickupInfo(NetObjType: Item.m_Type, pData, pEntEx);
572 if(!ScreenRectPickup.Inside(Position: Data.m_Pos))
573 continue;
574 bool Inactive = !IsSuper && Data.m_SwitchNumber > 0 && Data.m_SwitchNumber < (int)aSwitchers.size() && !aSwitchers[Data.m_SwitchNumber].m_aStatus[SwitcherTeam];
575
576 if(Inactive && BlinkingPickup)
577 continue;
578 if(UsePredicted)
579 {
580 auto *pPickup = (CPickup *)GameClient()->m_GameWorld.FindMatch(ObjId: Item.m_Id, ObjType: Item.m_Type, pObjData: pData);
581 if(pPickup && pPickup->InDDNetTile())
582 continue;
583 }
584 const void *pPrev = Client()->SnapFindItem(SnapId: IClient::SNAP_PREV, Type: Item.m_Type, Id: Item.m_Id);
585 if(pPrev)
586 RenderPickup(pPrev: (const CNetObj_Pickup *)pPrev, pCurrent: (const CNetObj_Pickup *)pData, IsPredicted: false, Flags: Data.m_Flags);
587 }
588 else if(Item.m_Type == NETOBJTYPE_LASER || Item.m_Type == NETOBJTYPE_DDNETLASER)
589 {
590 if(UsePredicted)
591 {
592 auto *pLaser = dynamic_cast<CLaser *>(GameClient()->m_GameWorld.FindMatch(ObjId: Item.m_Id, ObjType: Item.m_Type, pObjData: pData));
593 if(pLaser && pLaser->GetOwner() >= 0 && GameClient()->m_aClients[pLaser->GetOwner()].m_IsPredictedLocal)
594 continue;
595 }
596
597 CLaserData Data = ExtractLaserInfo(NetObjType: Item.m_Type, pData, pGameWorld: &GameClient()->m_GameWorld, pEntEx);
598 if(!IsLaserInside(Data))
599 continue;
600 bool Inactive = !IsSuper && Data.m_SwitchNumber > 0 && Data.m_SwitchNumber < (int)aSwitchers.size() && !aSwitchers[Data.m_SwitchNumber].m_aStatus[SwitcherTeam];
601
602 bool IsEntBlink = false;
603 int EntStartTick = -1;
604 if(Data.m_Type == LASERTYPE_FREEZE)
605 {
606 IsEntBlink = BlinkingLight;
607 EntStartTick = DraggerStartTick;
608 }
609 else if(Data.m_Type == LASERTYPE_GUN)
610 {
611 IsEntBlink = BlinkingGun;
612 EntStartTick = GunStartTick;
613 }
614 else if(Data.m_Type == LASERTYPE_DRAGGER)
615 {
616 IsEntBlink = BlinkingDragger;
617 EntStartTick = DraggerStartTick;
618 }
619 else if(Data.m_Type == LASERTYPE_DOOR)
620 {
621 if(Data.m_Predict && (Inactive || IsSuper))
622 {
623 Data.m_From.x = Data.m_To.x;
624 Data.m_From.y = Data.m_To.y;
625 }
626 EntStartTick = Client()->GameTick(Conn: g_Config.m_ClDummy);
627 }
628 else
629 {
630 IsEntBlink = BlinkingDragger;
631 EntStartTick = Client()->GameTick(Conn: g_Config.m_ClDummy);
632 }
633
634 if(Data.m_Predict && Inactive && IsEntBlink)
635 {
636 continue;
637 }
638
639 if(Data.m_StartTick <= 0 && EntStartTick != -1)
640 {
641 Data.m_StartTick = EntStartTick;
642 }
643
644 RenderLaser(pCurrent: &Data);
645 }
646 }
647
648 RenderFlags();
649
650 Graphics()->QuadsSetRotation(Angle: 0);
651 Graphics()->SetColor(r: 1.f, g: 1.f, b: 1.f, a: 1.f);
652}
653
654void CItems::OnInit()
655{
656 Graphics()->QuadsSetRotation(Angle: 0);
657 Graphics()->SetColor(r: 1.f, g: 1.f, b: 1.f, a: 1.f);
658
659 m_ItemsQuadContainerIndex = Graphics()->CreateQuadContainer(AutomaticUpload: false);
660
661 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 0, BottomRightU: 1, BottomRightV: 1);
662 m_RedFlagOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_ItemsQuadContainerIndex, X: -21.f, Y: -42.f, Width: 42.f, Height: 84.f);
663 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 0, BottomRightU: 1, BottomRightV: 1);
664 m_BlueFlagOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_ItemsQuadContainerIndex, X: -21.f, Y: -42.f, Width: 42.f, Height: 84.f);
665
666 float ScaleX, ScaleY;
667 Graphics()->GetSpriteScale(Id: SPRITE_PICKUP_HEALTH, ScaleX, ScaleY);
668 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 0, BottomRightU: 1, BottomRightV: 1);
669 m_PickupHealthOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_ItemsQuadContainerIndex, Width: 64.f * ScaleX, Height: 64.f * ScaleY);
670 Graphics()->GetSpriteScale(Id: SPRITE_PICKUP_ARMOR, ScaleX, ScaleY);
671 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 0, BottomRightU: 1, BottomRightV: 1);
672 m_PickupArmorOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_ItemsQuadContainerIndex, Width: 64.f * ScaleX, Height: 64.f * ScaleY);
673 Graphics()->GetSpriteScale(Id: SPRITE_PICKUP_FREEZE, ScaleX, ScaleY);
674 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 0, BottomRightU: 1, BottomRightV: 1);
675 m_PickupFreezeOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_ItemsQuadContainerIndex, Width: 64.f * ScaleX, Height: 64.f * ScaleY);
676
677 for(int i = 0; i < NUM_WEAPONS; ++i)
678 {
679 Graphics()->GetSpriteScale(pSprite: g_pData->m_Weapons.m_aId[i].m_pSpriteBody, ScaleX, ScaleY);
680 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 0, BottomRightU: 1, BottomRightV: 1);
681 m_aPickupWeaponOffset[i] = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_ItemsQuadContainerIndex, Width: g_pData->m_Weapons.m_aId[i].m_VisualSize * ScaleX, Height: g_pData->m_Weapons.m_aId[i].m_VisualSize * ScaleY);
682 }
683 Graphics()->GetSpriteScale(Id: SPRITE_PICKUP_NINJA, ScaleX, ScaleY);
684 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 0, BottomRightU: 1, BottomRightV: 1);
685 m_PickupNinjaOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_ItemsQuadContainerIndex, Width: 128.f * ScaleX, Height: 128.f * ScaleY);
686
687 for(int i = 0; i < 4; i++)
688 {
689 Graphics()->GetSpriteScale(Id: SPRITE_PICKUP_ARMOR_SHOTGUN + i, ScaleX, ScaleY);
690 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 0, BottomRightU: 1, BottomRightV: 1);
691 m_aPickupWeaponArmorOffset[i] = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_ItemsQuadContainerIndex, Width: 64.f * ScaleX, Height: 64.f * ScaleY);
692 }
693
694 for(int &ProjectileOffset : m_aProjectileOffset)
695 {
696 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 0, BottomRightU: 1, BottomRightV: 1);
697 ProjectileOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_ItemsQuadContainerIndex, Size: 32.f);
698 }
699
700 for(int &ParticleSplatOffset : m_aParticleSplatOffset)
701 {
702 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 0, BottomRightU: 1, BottomRightV: 1);
703 ParticleSplatOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_ItemsQuadContainerIndex, Size: 24.f);
704 }
705
706 Graphics()->GetSpriteScale(Id: SPRITE_PART_PULLEY, ScaleX, ScaleY);
707 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 0, BottomRightU: 1, BottomRightV: 1);
708 m_PulleyHeadOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_ItemsQuadContainerIndex, Size: 20.f * ScaleX);
709
710 Graphics()->GetSpriteScale(Id: SPRITE_PART_HECTAGON, ScaleX, ScaleY);
711 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 0, BottomRightU: 1, BottomRightV: 1);
712 m_FreezeHeadOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_ItemsQuadContainerIndex, Size: 20.f * ScaleX);
713
714 IGraphics::CQuadItem Brick(0, 0, 16.0f, 16.0f);
715 m_DoorHeadOffset = Graphics()->QuadContainerAddQuads(ContainerIndex: m_ItemsQuadContainerIndex, pArray: &Brick, Num: 1);
716
717 Graphics()->QuadContainerUpload(ContainerIndex: m_ItemsQuadContainerIndex);
718}
719
720void CItems::ReconstructSmokeTrail(const CProjectileData *pCurrent, int DestroyTick)
721{
722 bool LocalPlayerInGame = false;
723
724 if(GameClient()->m_Snap.m_pLocalInfo)
725 LocalPlayerInGame = GameClient()->m_aClients[GameClient()->m_Snap.m_pLocalInfo->m_ClientId].m_Team != TEAM_SPECTATORS;
726 if(!GameClient()->AntiPingGunfire() || !LocalPlayerInGame)
727 return;
728
729 int PredictionTick = Client()->GetPredictionTick();
730
731 if(PredictionTick == pCurrent->m_StartTick)
732 return;
733
734 // get positions
735 float Curvature = 0;
736 float Speed = 0;
737 const CTuningParams *pTuning = GameClient()->GetTuning(i: pCurrent->m_TuneZone);
738
739 if(pCurrent->m_Type == WEAPON_GRENADE)
740 {
741 Curvature = pTuning->m_GrenadeCurvature;
742 Speed = pTuning->m_GrenadeSpeed;
743 }
744 else if(pCurrent->m_Type == WEAPON_SHOTGUN)
745 {
746 Curvature = pTuning->m_ShotgunCurvature;
747 Speed = pTuning->m_ShotgunSpeed;
748 }
749 else if(pCurrent->m_Type == WEAPON_GUN)
750 {
751 Curvature = pTuning->m_GunCurvature;
752 Speed = pTuning->m_GunSpeed;
753 }
754
755 float Pt = ((float)(PredictionTick - pCurrent->m_StartTick) + Client()->PredIntraGameTick(Conn: g_Config.m_ClDummy)) / (float)Client()->GameTickSpeed();
756 if(Pt < 0)
757 return; // projectile haven't been shot yet
758
759 float Gt = (Client()->PrevGameTick(Conn: g_Config.m_ClDummy) - pCurrent->m_StartTick) / (float)Client()->GameTickSpeed() + Client()->GameTickTime(Conn: g_Config.m_ClDummy);
760
761 float Alpha = 1.f;
762 if(pCurrent->m_ExtraInfo && pCurrent->m_Owner >= 0 && GameClient()->IsOtherTeam(ClientId: pCurrent->m_Owner))
763 {
764 Alpha = g_Config.m_ClShowOthersAlpha / 100.0f;
765 }
766
767 float T = Pt;
768 if(DestroyTick >= 0)
769 T = std::min(a: Pt, b: ((float)(DestroyTick - 1 - pCurrent->m_StartTick) + Client()->PredIntraGameTick(Conn: g_Config.m_ClDummy)) / (float)Client()->GameTickSpeed());
770
771 float MinTrailSpan = 0.4f * ((pCurrent->m_Type == WEAPON_GRENADE) ? 0.5f : 0.25f);
772 float Step = std::max(a: Client()->FrameTimeAverage(), b: (pCurrent->m_Type == WEAPON_GRENADE) ? 0.02f : 0.01f);
773 for(int i = 1 + (int)(Gt / Step); i < (int)(T / Step); i++)
774 {
775 float t = Step * (float)i + 0.4f * Step * random_float(min: -0.5f, max: 0.5f);
776 vec2 Pos = CalcPos(Pos: pCurrent->m_StartPos, Velocity: pCurrent->m_StartVel, Curvature, Speed, Time: t);
777 vec2 PrevPos = CalcPos(Pos: pCurrent->m_StartPos, Velocity: pCurrent->m_StartVel, Curvature, Speed, Time: t - 0.001f);
778 vec2 Vel = Pos - PrevPos;
779 float TimePassed = Pt - t;
780 if(Pt - MinTrailSpan > 0.01f)
781 TimePassed = std::min(a: TimePassed, b: (TimePassed - MinTrailSpan) / (Pt - MinTrailSpan) * (MinTrailSpan * 0.5f) + MinTrailSpan);
782 // add particle for this projectile
783 if(pCurrent->m_Type == WEAPON_GRENADE)
784 GameClient()->m_Effects.SmokeTrail(Pos, Vel: Vel * -1, Alpha, TimePassed);
785 else
786 GameClient()->m_Effects.BulletTrail(Pos, Alpha, TimePassed);
787 }
788}
789