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
4#include "players.h"
5
6#include <base/color.h>
7#include <base/math.h>
8
9#include <engine/client/enums.h>
10#include <engine/demo.h>
11#include <engine/graphics.h>
12#include <engine/shared/config.h>
13
14#include <generated/client_data.h>
15#include <generated/client_data7.h>
16#include <generated/protocol.h>
17
18#include <game/client/animstate.h>
19#include <game/client/components/controls.h>
20#include <game/client/components/effects.h>
21#include <game/client/components/flow.h>
22#include <game/client/components/skins.h>
23#include <game/client/components/sounds.h>
24#include <game/client/gameclient.h>
25#include <game/gamecore.h>
26#include <game/mapitems.h>
27
28static float CalculateHandAngle(vec2 Dir, float AngleOffset)
29{
30 const float Angle = angle(a: Dir);
31 if(Dir.x < 0.0f)
32 {
33 return Angle - AngleOffset;
34 }
35 else
36 {
37 return Angle + AngleOffset;
38 }
39}
40
41static vec2 CalculateHandPosition(vec2 CenterPos, vec2 Dir, vec2 PostRotOffset)
42{
43 vec2 DirY = vec2(-Dir.y, Dir.x);
44 if(Dir.x < 0.0f)
45 {
46 DirY = -DirY;
47 }
48 return CenterPos + Dir + Dir * PostRotOffset.x + DirY * PostRotOffset.y;
49}
50
51void CPlayers::RenderHand(const CTeeRenderInfo *pInfo, vec2 CenterPos, vec2 Dir, float AngleOffset, vec2 PostRotOffset, float Alpha)
52{
53 const vec2 HandPos = CalculateHandPosition(CenterPos, Dir, PostRotOffset);
54 const float HandAngle = CalculateHandAngle(Dir, AngleOffset);
55 if(pInfo->m_aSixup[g_Config.m_ClDummy].PartTexture(Part: protocol7::SKINPART_HANDS).IsValid())
56 {
57 RenderHand7(pInfo, HandPos, HandAngle, Alpha);
58 }
59 else
60 {
61 RenderHand6(pInfo, HandPos, HandAngle, Alpha);
62 }
63}
64
65void CPlayers::RenderHand7(const CTeeRenderInfo *pInfo, vec2 HandPos, float HandAngle, float Alpha)
66{
67 // in-game hand size is 15 when tee size is 64
68 const float BaseSize = 15.0f * (pInfo->m_Size / 64.0f);
69 IGraphics::CQuadItem QuadOutline(HandPos.x, HandPos.y, 2 * BaseSize, 2 * BaseSize);
70 IGraphics::CQuadItem QuadHand = QuadOutline;
71
72 Graphics()->TextureSet(Texture: pInfo->m_aSixup[g_Config.m_ClDummy].PartTexture(Part: protocol7::SKINPART_HANDS));
73 Graphics()->QuadsBegin();
74 Graphics()->SetColor(pInfo->m_aSixup[g_Config.m_ClDummy].m_aColors[protocol7::SKINPART_HANDS].WithAlpha(alpha: Alpha));
75 Graphics()->QuadsSetRotation(Angle: HandAngle);
76 Graphics()->SelectSprite7(Id: client_data7::SPRITE_TEE_HAND_OUTLINE);
77 Graphics()->QuadsDraw(pArray: &QuadOutline, Num: 1);
78 Graphics()->SelectSprite7(Id: client_data7::SPRITE_TEE_HAND);
79 Graphics()->QuadsDraw(pArray: &QuadHand, Num: 1);
80 Graphics()->QuadsEnd();
81}
82
83void CPlayers::RenderHand6(const CTeeRenderInfo *pInfo, vec2 HandPos, float HandAngle, float Alpha)
84{
85 const CSkin::CSkinTextures *pSkinTextures = pInfo->m_CustomColoredSkin ? &pInfo->m_ColorableRenderSkin : &pInfo->m_OriginalRenderSkin;
86
87 Graphics()->SetColor(pInfo->m_ColorBody.WithAlpha(alpha: Alpha));
88 Graphics()->QuadsSetRotation(Angle: HandAngle);
89 Graphics()->TextureSet(Texture: pSkinTextures->m_HandsOutline);
90 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_WeaponEmoteQuadContainerIndex, QuadOffset: NUM_WEAPONS * 2, X: HandPos.x, Y: HandPos.y);
91 Graphics()->TextureSet(Texture: pSkinTextures->m_Hands);
92 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_WeaponEmoteQuadContainerIndex, QuadOffset: NUM_WEAPONS * 2 + 1, X: HandPos.x, Y: HandPos.y);
93}
94
95float CPlayers::GetPlayerTargetAngle(
96 const CNetObj_Character *pPrevChar,
97 const CNetObj_Character *pPlayerChar,
98 int ClientId,
99 float Intra)
100{
101 if(GameClient()->PredictDummy() && GameClient()->m_aLocalIds[!g_Config.m_ClDummy] == ClientId)
102 {
103 const CNetObj_PlayerInput &Input = g_Config.m_ClDummyHammer ? GameClient()->m_HammerInput : GameClient()->m_DummyInput;
104 return angle(a: vec2(Input.m_TargetX, Input.m_TargetY));
105 }
106
107 // with dummy copy, use the same angle as local player
108 if((GameClient()->m_Snap.m_LocalClientId == ClientId || (GameClient()->PredictDummy() && g_Config.m_ClDummyCopyMoves && GameClient()->m_aLocalIds[!g_Config.m_ClDummy] == ClientId)) &&
109 !GameClient()->m_Snap.m_SpecInfo.m_Active && Client()->State() != IClient::STATE_DEMOPLAYBACK)
110 {
111 // calculate what would be sent to the server from our current input
112 vec2 Direction = normalize(v: vec2((int)GameClient()->m_Controls.m_aMousePos[g_Config.m_ClDummy].x, (int)GameClient()->m_Controls.m_aMousePos[g_Config.m_ClDummy].y));
113
114 // fix direction if mouse is exactly in the center
115 if(Direction == vec2(0.0f, 0.0f))
116 Direction = vec2(1.0f, 0.0f);
117
118 return angle(a: Direction);
119 }
120
121 // using unpredicted angle when rendering other players in-game
122 if(ClientId >= 0)
123 Intra = Client()->IntraGameTick(Conn: g_Config.m_ClDummy);
124
125 if(ClientId >= 0 && GameClient()->m_Snap.m_aCharacters[ClientId].m_HasExtendedDisplayInfo)
126 {
127 const CNetObj_DDNetCharacter *pExtendedData = &GameClient()->m_Snap.m_aCharacters[ClientId].m_ExtendedData;
128 const CNetObj_DDNetCharacter *pPrevExtendedData = GameClient()->m_Snap.m_aCharacters[ClientId].m_pPrevExtendedData;
129 if(pPrevExtendedData)
130 {
131 float MixX = mix(a: (float)pPrevExtendedData->m_TargetX, b: (float)pExtendedData->m_TargetX, amount: Intra);
132 float MixY = mix(a: (float)pPrevExtendedData->m_TargetY, b: (float)pExtendedData->m_TargetY, amount: Intra);
133 return angle(a: vec2(MixX, MixY));
134 }
135 else
136 {
137 return angle(a: vec2(pExtendedData->m_TargetX, pExtendedData->m_TargetY));
138 }
139 }
140 else
141 {
142 // If the player moves their weapon through top, then change
143 // the end angle by 2*Pi, so that the mix function will use the
144 // short path and not the long one.
145 if(pPlayerChar->m_Angle > (256.0f * pi) && pPrevChar->m_Angle < 0)
146 {
147 return mix(a: (float)pPrevChar->m_Angle, b: (float)(pPlayerChar->m_Angle - 256.0f * 2 * pi), amount: Intra) / 256.0f;
148 }
149 else if(pPlayerChar->m_Angle < 0 && pPrevChar->m_Angle > (256.0f * pi))
150 {
151 return mix(a: (float)pPrevChar->m_Angle, b: (float)(pPlayerChar->m_Angle + 256.0f * 2 * pi), amount: Intra) / 256.0f;
152 }
153 else
154 {
155 return mix(a: (float)pPrevChar->m_Angle, b: (float)pPlayerChar->m_Angle, amount: Intra) / 256.0f;
156 }
157 }
158}
159
160void CPlayers::RenderHookCollLine(
161 const CNetObj_Character *pPrevChar,
162 const CNetObj_Character *pPlayerChar,
163 int ClientId)
164{
165 CNetObj_Character Prev;
166 CNetObj_Character Player;
167 Prev = *pPrevChar;
168 Player = *pPlayerChar;
169
170 dbg_assert(in_range(ClientId, MAX_CLIENTS - 1), "invalid client id (%d)", ClientId);
171
172 if(!GameClient()->m_GameInfo.m_AllowHookColl)
173 return;
174
175 bool Local = GameClient()->m_Snap.m_LocalClientId == ClientId;
176
177#if defined(CONF_VIDEORECORDER)
178 if(IVideo::Current() && !g_Config.m_ClVideoShowHookCollOther && !Local)
179 return;
180#endif
181
182 bool Aim = (Player.m_PlayerFlags & PLAYERFLAG_AIM);
183 if(!Client()->ServerCapAnyPlayerFlag())
184 {
185 for(int i = 0; i < NUM_DUMMIES; i++)
186 {
187 if(ClientId == GameClient()->m_aLocalIds[i])
188 {
189 Aim = GameClient()->m_Controls.m_aShowHookColl[i];
190 break;
191 }
192 }
193 }
194
195 if(GameClient()->PredictDummy() && g_Config.m_ClDummyCopyMoves && GameClient()->m_aLocalIds[!g_Config.m_ClDummy] == ClientId)
196 Aim = false; // don't use unpredicted with copy moves
197
198 bool AlwaysRenderHookColl = (Local ? g_Config.m_ClShowHookCollOwn : g_Config.m_ClShowHookCollOther) == 2;
199 bool RenderHookCollPlayer = Aim && (Local ? g_Config.m_ClShowHookCollOwn : g_Config.m_ClShowHookCollOther) > 0;
200 if(Local && Client()->State() != IClient::STATE_DEMOPLAYBACK)
201 RenderHookCollPlayer = GameClient()->m_Controls.m_aShowHookColl[g_Config.m_ClDummy] && g_Config.m_ClShowHookCollOwn > 0;
202
203 if(GameClient()->PredictDummy() && g_Config.m_ClDummyCopyMoves &&
204 GameClient()->m_aLocalIds[!g_Config.m_ClDummy] == ClientId && GameClient()->m_Controls.m_aShowHookColl[g_Config.m_ClDummy] &&
205 Client()->State() != IClient::STATE_DEMOPLAYBACK)
206 {
207 RenderHookCollPlayer = g_Config.m_ClShowHookCollOther > 0;
208 }
209
210 if(!AlwaysRenderHookColl && !RenderHookCollPlayer)
211 return;
212
213 float Intra = GameClient()->m_aClients[ClientId].m_IsPredicted ? Client()->PredIntraGameTick(Conn: g_Config.m_ClDummy) : Client()->IntraGameTick(Conn: g_Config.m_ClDummy);
214 float Angle = GetPlayerTargetAngle(pPrevChar: &Prev, pPlayerChar: &Player, ClientId, Intra);
215
216 vec2 Direction = direction(angle: Angle);
217 vec2 Position = GameClient()->m_aClients[ClientId].m_RenderPos;
218
219 static constexpr float HOOK_START_DISTANCE = CCharacterCore::PhysicalSize() * 1.5f;
220 float HookLength = (float)GameClient()->m_aClients[ClientId].m_Predicted.m_Tuning.m_HookLength;
221 float HookFireSpeed = (float)GameClient()->m_aClients[ClientId].m_Predicted.m_Tuning.m_HookFireSpeed;
222
223 // janky physics
224 if(HookLength < HOOK_START_DISTANCE || HookFireSpeed <= 0.0f)
225 return;
226
227 vec2 QuantizedDirection = Direction;
228 vec2 StartOffset = Direction * HOOK_START_DISTANCE;
229 vec2 BasePos = Position;
230 vec2 LineStartPos = BasePos + StartOffset;
231 vec2 SegmentStartPos = LineStartPos;
232
233 ColorRGBA HookCollColor = color_cast<ColorRGBA>(hsl: ColorHSLA(g_Config.m_ClHookCollColorNoColl));
234 std::vector<IGraphics::CLineItem> vLineSegments;
235
236 const int MaxHookTicks = 5 * Client()->GameTickSpeed(); // calculating above 5 seconds is very expensive and unlikely to happen
237
238 auto AddHookPlayerSegment = [&](const vec2 &StartPos, const vec2 &EndPos, const vec2 &HookablePlayerPosition, const vec2 &HitPos) {
239 HookCollColor = color_cast<ColorRGBA>(hsl: ColorHSLA(g_Config.m_ClHookCollColorTeeColl));
240
241 // stop hookline at player circle so it looks better
242 vec2 aIntersections[2];
243 int NumIntersections = intersect_line_circle(LineStart: StartPos, LineEnd: EndPos, CircleCenter: HookablePlayerPosition, Radius: CCharacterCore::PhysicalSize() * 1.45f / 2.0f, aIntersections);
244 if(NumIntersections == 2)
245 {
246 if(distance(a: Position, b: aIntersections[0]) < distance(a: Position, b: aIntersections[1]))
247 vLineSegments.emplace_back(args: StartPos, args&: aIntersections[0]);
248 else
249 vLineSegments.emplace_back(args: StartPos, args&: aIntersections[1]);
250 }
251 else if(NumIntersections == 1)
252 vLineSegments.emplace_back(args: StartPos, args&: aIntersections[0]);
253 else
254 vLineSegments.emplace_back(args: StartPos, args: HitPos);
255 };
256
257 // simulate the hook into the future
258 int HookTick;
259 bool HookEnteredTelehook = false;
260 std::optional<IGraphics::CLineItem> HookTipLineSegment;
261 for(HookTick = 0; HookTick < MaxHookTicks; ++HookTick)
262 {
263 int Tele;
264 vec2 HitPos, IntersectedPlayerPosition;
265 vec2 SegmentEndPos = SegmentStartPos + QuantizedDirection * HookFireSpeed;
266
267 // check if a hook would enter retracting state in this tick
268 if(distance(a: BasePos, b: SegmentEndPos) > HookLength)
269 {
270 // check if the retracting hook hits a player
271 if(!HookEnteredTelehook)
272 {
273 vec2 RetractingHookEndPos = BasePos + normalize(v: SegmentEndPos - BasePos) * HookLength;
274 // you can't hook a player, if the hook is behind solids, however you miss the solids as well
275 int Hit = Collision()->IntersectLineTeleHook(Pos0: SegmentStartPos, Pos1: RetractingHookEndPos, pOutCollision: &HitPos, pOutBeforeCollision: nullptr, pTeleNr: &Tele);
276
277 if(GameClient()->IntersectCharacter(HookPos: SegmentStartPos, NewPos: HitPos, NewPos2&: RetractingHookEndPos, OwnId: ClientId, pPlayerPosition: &IntersectedPlayerPosition) != -1)
278 {
279 AddHookPlayerSegment(LineStartPos, SegmentEndPos, IntersectedPlayerPosition, RetractingHookEndPos);
280 break;
281 }
282
283 // Retracting hooks don't go through hook teleporters
284 if(Hit && Hit != TILE_TELEINHOOK)
285 {
286 // The hook misses the player, but also misses the solid
287 vLineSegments.emplace_back(args&: LineStartPos, args&: SegmentStartPos);
288
289 // The player hook misses due to a solid
290 HookTipLineSegment = IGraphics::CLineItem(SegmentStartPos, HitPos);
291 break;
292 }
293
294 // we are missing the player, the solid hookline stopped already, but we want this extra line segment
295 // the player-hooking-hook is only longer, if we didn't go through a tele hook
296 HookTipLineSegment = IGraphics::CLineItem(SegmentStartPos, RetractingHookEndPos);
297 }
298
299 // the line is too long here, and the hook retracts, use old position
300 vLineSegments.emplace_back(args&: LineStartPos, args&: SegmentStartPos);
301 break;
302 }
303
304 // check for map collisions
305 int Hit = Collision()->IntersectLineTeleHook(Pos0: SegmentStartPos, Pos1: SegmentEndPos, pOutCollision: &HitPos, pOutBeforeCollision: nullptr, pTeleNr: &Tele);
306
307 // check if we intersect a player
308 if(GameClient()->IntersectCharacter(HookPos: SegmentStartPos, NewPos: HitPos, NewPos2&: SegmentEndPos, OwnId: ClientId, pPlayerPosition: &IntersectedPlayerPosition) != -1)
309 {
310 AddHookPlayerSegment(LineStartPos, HitPos, IntersectedPlayerPosition, SegmentEndPos);
311 break;
312 }
313
314 // we hit nothing, continue calculating segments
315 if(!Hit)
316 {
317 SegmentStartPos = SegmentEndPos;
318 SegmentStartPos.x = round_to_int(f: SegmentStartPos.x);
319 SegmentStartPos.y = round_to_int(f: SegmentStartPos.y);
320
321 // direction is always the same after the first tick quantization
322 if(HookTick == 0)
323 {
324 QuantizedDirection.x = round_to_int(f: QuantizedDirection.x * 256.0f) / 256.0f;
325 QuantizedDirection.y = round_to_int(f: QuantizedDirection.y * 256.0f) / 256.0f;
326 }
327 continue;
328 }
329
330 // we hit a solid / hook stopper
331 if(Hit != TILE_TELEINHOOK)
332 {
333 if(Hit != TILE_NOHOOK)
334 HookCollColor = color_cast<ColorRGBA>(hsl: ColorHSLA(g_Config.m_ClHookCollColorHookableColl));
335 vLineSegments.emplace_back(args&: LineStartPos, args&: HitPos);
336 break;
337 }
338
339 // we are hitting TILE_TELEINHOOK
340 vLineSegments.emplace_back(args&: LineStartPos, args&: HitPos);
341 HookEnteredTelehook = true;
342
343 // check tele outs
344 const std::vector<vec2> &vTeleOuts = Collision()->TeleOuts(Number: Tele - 1);
345 if(vTeleOuts.empty())
346 {
347 // the hook gets stuck, this is a feature or a bug
348 HookCollColor = color_cast<ColorRGBA>(hsl: ColorHSLA(g_Config.m_ClHookCollColorHookableColl));
349 break;
350 }
351 else if(vTeleOuts.size() > 1)
352 {
353 // we don't know which teleout the hook takes, just invert the color
354 HookCollColor = color_invert(col: color_cast<ColorRGBA>(hsl: ColorHSLA(g_Config.m_ClHookCollColorTeeColl)));
355 break;
356 }
357
358 // go through one teleout, update positions and continue
359 BasePos = vTeleOuts[0];
360 LineStartPos = BasePos; // make the line start in the teleporter to prevent a gap
361 SegmentStartPos = BasePos + Direction * HOOK_START_DISTANCE;
362 SegmentStartPos.x = round_to_int(f: SegmentStartPos.x);
363 SegmentStartPos.y = round_to_int(f: SegmentStartPos.y);
364
365 // direction is always the same after the first tick quantization
366 if(HookTick == 0)
367 {
368 QuantizedDirection.x = round_to_int(f: QuantizedDirection.x * 256.0f) / 256.0f;
369 QuantizedDirection.y = round_to_int(f: QuantizedDirection.y * 256.0f) / 256.0f;
370 }
371 }
372
373 // The hook line is too expensive to calculate and didn't hit anything before, just set a straight line
374 if(HookTick >= MaxHookTicks && vLineSegments.empty())
375 {
376 // we simply don't know if we hit anything or not
377 HookCollColor = color_invert(col: color_cast<ColorRGBA>(hsl: ColorHSLA(g_Config.m_ClHookCollColorTeeColl)));
378 vLineSegments.emplace_back(args&: LineStartPos, args: BasePos + QuantizedDirection * HookLength);
379 }
380
381 // add a line from the player to the start position to prevent a visual gap
382 vLineSegments.emplace_back(args&: Position, args: Position + StartOffset);
383
384 if(AlwaysRenderHookColl && RenderHookCollPlayer)
385 {
386 // invert the hook coll colors when using cl_show_hook_coll_always and +showhookcoll is pressed
387 HookCollColor = color_invert(col: HookCollColor);
388 }
389
390 // Render hook coll line
391 const int HookCollSize = Local ? g_Config.m_ClHookCollSize : g_Config.m_ClHookCollSizeOther;
392
393 float Alpha = GameClient()->IsOtherTeam(ClientId) ? g_Config.m_ClShowOthersAlpha / 100.0f : 1.0f;
394 Alpha *= (float)g_Config.m_ClHookCollAlpha / 100;
395 if(Alpha <= 0.0f)
396 return;
397 ColorRGBA HookCollTipColor = color_cast<ColorRGBA>(hsl: ColorHSLA(g_Config.m_ClHookCollTipColor, true));
398
399 Graphics()->TextureClear();
400 if(HookCollSize > 0)
401 {
402 std::vector<IGraphics::CFreeformItem> vLineQuadSegments;
403 vLineQuadSegments.reserve(n: vLineSegments.size());
404
405 float LineWidth = 0.5f + (float)(HookCollSize - 1) * 0.25f;
406 const vec2 PerpToAngle = normalize(v: vec2(Direction.y, -Direction.x)) * GameClient()->m_Camera.m_Zoom;
407
408 auto ConvertLineSegments = [&](const IGraphics::CLineItem &LineSegment) {
409 vec2 DrawInitPos(LineSegment.m_X0, LineSegment.m_Y0);
410 vec2 DrawFinishPos(LineSegment.m_X1, LineSegment.m_Y1);
411 vec2 Pos0 = DrawFinishPos + PerpToAngle * -LineWidth;
412 vec2 Pos1 = DrawFinishPos + PerpToAngle * LineWidth;
413 vec2 Pos2 = DrawInitPos + PerpToAngle * -LineWidth;
414 vec2 Pos3 = DrawInitPos + PerpToAngle * LineWidth;
415 vLineQuadSegments.emplace_back(args&: Pos0.x, args&: Pos0.y, args&: Pos1.x, args&: Pos1.y, args&: Pos2.x, args&: Pos2.y, args&: Pos3.x, args&: Pos3.y);
416 };
417
418 for(const auto &LineSegment : vLineSegments)
419 {
420 ConvertLineSegments(LineSegment);
421 }
422
423 vLineSegments.clear();
424
425 Graphics()->QuadsBegin();
426 Graphics()->SetColor(HookCollColor.WithAlpha(alpha: Alpha));
427 Graphics()->QuadsDrawFreeform(pArray: vLineQuadSegments.data(), Num: vLineQuadSegments.size());
428 if(HookTipLineSegment.has_value() && HookCollTipColor.a > 0.0f)
429 {
430 vLineQuadSegments.clear();
431 ConvertLineSegments(HookTipLineSegment.value());
432 Graphics()->SetColor(HookCollTipColor.WithMultipliedAlpha(alpha: Alpha));
433 Graphics()->QuadsDrawFreeform(pArray: vLineQuadSegments.data(), Num: vLineQuadSegments.size());
434 }
435 Graphics()->QuadsEnd();
436 }
437 else
438 {
439 Graphics()->LinesBegin();
440 Graphics()->SetColor(HookCollColor.WithAlpha(alpha: Alpha));
441 Graphics()->LinesDraw(pArray: vLineSegments.data(), Num: vLineSegments.size());
442 if(HookTipLineSegment.has_value() && HookCollTipColor.a > 0.0f)
443 {
444 Graphics()->SetColor(HookCollTipColor.WithMultipliedAlpha(alpha: Alpha));
445 Graphics()->LinesDraw(pArray: &HookTipLineSegment.value(), Num: 1);
446 }
447 Graphics()->LinesEnd();
448 }
449}
450
451void CPlayers::RenderHook(
452 const CNetObj_Character *pPrevChar,
453 const CNetObj_Character *pPlayerChar,
454 const CTeeRenderInfo *pRenderInfo,
455 int ClientId,
456 float Intra)
457{
458 if(pPrevChar->m_HookState <= 0 || pPlayerChar->m_HookState <= 0)
459 return;
460
461 CNetObj_Character Prev;
462 CNetObj_Character Player;
463 Prev = *pPrevChar;
464 Player = *pPlayerChar;
465
466 CTeeRenderInfo RenderInfo = *pRenderInfo;
467
468 // don't render hooks to not active character cores
469 if(pPlayerChar->m_HookedPlayer != -1 && !GameClient()->m_Snap.m_aCharacters[pPlayerChar->m_HookedPlayer].m_Active)
470 return;
471
472 if(ClientId >= 0)
473 Intra = GameClient()->m_aClients[ClientId].m_IsPredicted ? Client()->PredIntraGameTick(Conn: g_Config.m_ClDummy) : Client()->IntraGameTick(Conn: g_Config.m_ClDummy);
474
475 bool OtherTeam = GameClient()->IsOtherTeam(ClientId);
476 float Alpha = (OtherTeam || ClientId < 0) ? g_Config.m_ClShowOthersAlpha / 100.0f : 1.0f;
477 if(ClientId == -2) // ghost
478 Alpha = g_Config.m_ClRaceGhostAlpha / 100.0f;
479
480 RenderInfo.m_Size = 64.0f;
481
482 vec2 Position;
483 if(in_range(a: ClientId, upper: MAX_CLIENTS - 1))
484 Position = GameClient()->m_aClients[ClientId].m_RenderPos;
485 else
486 Position = mix(a: vec2(Prev.m_X, Prev.m_Y), b: vec2(Player.m_X, Player.m_Y), amount: Intra);
487
488 // draw hook
489 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f);
490 if(ClientId < 0)
491 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 0.5f);
492
493 vec2 Pos = Position;
494 vec2 HookPos;
495
496 if(in_range(a: pPlayerChar->m_HookedPlayer, upper: MAX_CLIENTS - 1))
497 HookPos = GameClient()->m_aClients[pPlayerChar->m_HookedPlayer].m_RenderPos;
498 else
499 HookPos = mix(a: vec2(Prev.m_HookX, Prev.m_HookY), b: vec2(Player.m_HookX, Player.m_HookY), amount: Intra);
500
501 float d = distance(a: Pos, b: HookPos);
502 vec2 Dir = normalize(v: Pos - HookPos);
503
504 Graphics()->TextureSet(Texture: GameClient()->m_GameSkin.m_SpriteHookHead);
505 Graphics()->QuadsSetRotation(Angle: angle(a: Dir) + pi);
506 // render head
507 int QuadOffset = NUM_WEAPONS * 2 + 2;
508 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: Alpha);
509 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_WeaponEmoteQuadContainerIndex, QuadOffset, X: HookPos.x, Y: HookPos.y);
510
511 // render chain
512 ++QuadOffset;
513 static IGraphics::SRenderSpriteInfo s_aHookChainRenderInfo[1024];
514 int HookChainCount = 0;
515 for(float f = 24; f < d && HookChainCount < 1024; f += 24, ++HookChainCount)
516 {
517 vec2 p = HookPos + Dir * f;
518 s_aHookChainRenderInfo[HookChainCount].m_Pos[0] = p.x;
519 s_aHookChainRenderInfo[HookChainCount].m_Pos[1] = p.y;
520 s_aHookChainRenderInfo[HookChainCount].m_Scale = 1;
521 s_aHookChainRenderInfo[HookChainCount].m_Rotation = angle(a: Dir) + pi;
522 }
523 Graphics()->TextureSet(Texture: GameClient()->m_GameSkin.m_SpriteHookChain);
524 Graphics()->RenderQuadContainerAsSpriteMultiple(ContainerIndex: m_WeaponEmoteQuadContainerIndex, QuadOffset, DrawCount: HookChainCount, pRenderInfo: s_aHookChainRenderInfo);
525
526 Graphics()->QuadsSetRotation(Angle: 0);
527 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f);
528
529 RenderHand(pInfo: &RenderInfo, CenterPos: Position, Dir: normalize(v: HookPos - Pos), AngleOffset: -pi / 2, PostRotOffset: vec2(20, 0), Alpha);
530}
531
532void CPlayers::RenderPlayer(
533 const CNetObj_Character *pPrevChar,
534 const CNetObj_Character *pPlayerChar,
535 const CTeeRenderInfo *pRenderInfo,
536 int ClientId,
537 float Intra)
538{
539 CNetObj_Character Prev;
540 CNetObj_Character Player;
541 Prev = *pPrevChar;
542 Player = *pPlayerChar;
543
544 CTeeRenderInfo RenderInfo = *pRenderInfo;
545
546 bool Local = GameClient()->m_Snap.m_LocalClientId == ClientId;
547 bool OtherTeam = GameClient()->IsOtherTeam(ClientId);
548 float Alpha = (OtherTeam || ClientId < 0) ? g_Config.m_ClShowOthersAlpha / 100.0f : 1.0f;
549 if(ClientId == -2) // ghost
550 Alpha = g_Config.m_ClRaceGhostAlpha / 100.0f;
551 // TODO: snd_game_volume_others
552 const float Volume = 1.0f;
553
554 // set size
555 RenderInfo.m_Size = 64.0f;
556
557 if(ClientId >= 0)
558 Intra = GameClient()->m_aClients[ClientId].m_IsPredicted ? Client()->PredIntraGameTick(Conn: g_Config.m_ClDummy) : Client()->IntraGameTick(Conn: g_Config.m_ClDummy);
559
560 static float s_LastGameTickTime = Client()->GameTickTime(Conn: g_Config.m_ClDummy);
561 static float s_LastPredIntraTick = Client()->PredIntraGameTick(Conn: g_Config.m_ClDummy);
562 if(GameClient()->m_Snap.m_pGameInfoObj && !(GameClient()->m_Snap.m_pGameInfoObj->m_GameStateFlags & GAMESTATEFLAG_PAUSED))
563 {
564 s_LastGameTickTime = Client()->GameTickTime(Conn: g_Config.m_ClDummy);
565 s_LastPredIntraTick = Client()->PredIntraGameTick(Conn: g_Config.m_ClDummy);
566 }
567
568 bool PredictLocalWeapons = false;
569 float AttackTime = (Client()->PrevGameTick(Conn: g_Config.m_ClDummy) - Player.m_AttackTick) / (float)Client()->GameTickSpeed() + Client()->GameTickTime(Conn: g_Config.m_ClDummy);
570 float LastAttackTime = (Client()->PrevGameTick(Conn: g_Config.m_ClDummy) - Player.m_AttackTick) / (float)Client()->GameTickSpeed() + s_LastGameTickTime;
571 if(ClientId >= 0 && GameClient()->m_aClients[ClientId].m_IsPredictedLocal && GameClient()->AntiPingGunfire())
572 {
573 PredictLocalWeapons = true;
574 AttackTime = (Client()->PredIntraGameTick(Conn: g_Config.m_ClDummy) + (Client()->PredGameTick(Conn: g_Config.m_ClDummy) - 1 - Player.m_AttackTick)) / (float)Client()->GameTickSpeed();
575 LastAttackTime = (s_LastPredIntraTick + (Client()->PredGameTick(Conn: g_Config.m_ClDummy) - 1 - Player.m_AttackTick)) / (float)Client()->GameTickSpeed();
576 }
577 float AttackTicksPassed = AttackTime * (float)Client()->GameTickSpeed();
578
579 float Angle = GetPlayerTargetAngle(pPrevChar: &Prev, pPlayerChar: &Player, ClientId, Intra);
580
581 vec2 Direction = direction(angle: Angle);
582 vec2 Position;
583 if(in_range(a: ClientId, upper: MAX_CLIENTS - 1))
584 Position = GameClient()->m_aClients[ClientId].m_RenderPos;
585 else
586 Position = mix(a: vec2(Prev.m_X, Prev.m_Y), b: vec2(Player.m_X, Player.m_Y), amount: Intra);
587 vec2 Vel = mix(a: vec2(Prev.m_VelX / 256.0f, Prev.m_VelY / 256.0f), b: vec2(Player.m_VelX / 256.0f, Player.m_VelY / 256.0f), amount: Intra);
588
589 GameClient()->m_Flow.Add(Pos: Position, Vel: Vel * 100.0f, Size: 10.0f);
590
591 RenderInfo.m_GotAirJump = Player.m_Jumped & 2 ? false : true;
592
593 RenderInfo.m_FeetFlipped = false;
594
595 bool Stationary = Player.m_VelX <= 1 && Player.m_VelX >= -1;
596 bool InAir = !Collision()->CheckPoint(x: Player.m_X, y: Player.m_Y + 16);
597 bool Running = Player.m_VelX >= 5000 || Player.m_VelX <= -5000;
598 bool WantOtherDir = (Player.m_Direction == -1 && Vel.x > 0) || (Player.m_Direction == 1 && Vel.x < 0);
599 bool Inactive = ClientId >= 0 && (GameClient()->m_aClients[ClientId].m_Afk || GameClient()->m_aClients[ClientId].m_Paused);
600
601 // evaluate animation
602 float WalkTime = std::fmod(x: Position.x, y: 100.0f) / 100.0f;
603 float RunTime = std::fmod(x: Position.x, y: 200.0f) / 200.0f;
604
605 // Don't do a moon walk outside the left border
606 if(WalkTime < 0.0f)
607 WalkTime += 1.0f;
608 if(RunTime < 0.0f)
609 RunTime += 1.0f;
610
611 CAnimState State;
612 State.Set(pAnim: &g_pData->m_aAnimations[ANIM_BASE], Time: 0.0f);
613
614 if(InAir)
615 State.Add(pAnim: &g_pData->m_aAnimations[ANIM_INAIR], Time: 0.0f, Amount: 1.0f); // TODO: some sort of time here
616 else if(Stationary)
617 {
618 if(Inactive)
619 {
620 State.Add(pAnim: Direction.x < 0.0f ? &g_pData->m_aAnimations[ANIM_SIT_LEFT] : &g_pData->m_aAnimations[ANIM_SIT_RIGHT], Time: 0.0f, Amount: 1.0f); // TODO: some sort of time here
621 RenderInfo.m_FeetFlipped = true;
622 }
623 else
624 State.Add(pAnim: &g_pData->m_aAnimations[ANIM_IDLE], Time: 0.0f, Amount: 1.0f); // TODO: some sort of time here
625 }
626 else if(!WantOtherDir)
627 {
628 if(Running)
629 State.Add(pAnim: Player.m_VelX < 0 ? &g_pData->m_aAnimations[ANIM_RUN_LEFT] : &g_pData->m_aAnimations[ANIM_RUN_RIGHT], Time: RunTime, Amount: 1.0f);
630 else
631 State.Add(pAnim: &g_pData->m_aAnimations[ANIM_WALK], Time: WalkTime, Amount: 1.0f);
632 }
633
634 const float HammerAnimationTimeScale = 5.0f;
635 if(Player.m_Weapon == WEAPON_HAMMER)
636 State.Add(pAnim: &g_pData->m_aAnimations[ANIM_HAMMER_SWING], Time: std::clamp(val: LastAttackTime * HammerAnimationTimeScale, lo: 0.0f, hi: 1.0f), Amount: 1.0f);
637 if(Player.m_Weapon == WEAPON_NINJA)
638 State.Add(pAnim: &g_pData->m_aAnimations[ANIM_NINJA_SWING], Time: std::clamp(val: LastAttackTime * 2.0f, lo: 0.0f, hi: 1.0f), Amount: 1.0f);
639
640 // do skidding
641 if(!InAir && WantOtherDir && length(a: Vel * 50) > 500.0f)
642 GameClient()->m_Effects.SkidTrail(Pos: Position, Vel, Direction: Player.m_Direction, Alpha, Volume);
643
644 // draw gun
645 if(Player.m_Weapon >= 0)
646 {
647 if(!(RenderInfo.m_TeeRenderFlags & TEE_NO_WEAPON))
648 {
649 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: Alpha);
650
651 // normal weapons
652 int CurrentWeapon = std::clamp(val: Player.m_Weapon, lo: 0, hi: NUM_WEAPONS - 1);
653 Graphics()->TextureSet(Texture: GameClient()->m_GameSkin.m_aSpriteWeapons[CurrentWeapon]);
654 int QuadOffset = CurrentWeapon * 2 + (Direction.x < 0.0f ? 1 : 0);
655
656 float Recoil = 0.0f;
657 vec2 WeaponPosition;
658 bool IsSit = Inactive && !InAir && Stationary;
659
660 if(Player.m_Weapon == WEAPON_HAMMER)
661 {
662 // static position for hammer
663 WeaponPosition = Position + vec2(State.GetAttach()->m_X, State.GetAttach()->m_Y);
664 WeaponPosition.y += g_pData->m_Weapons.m_aId[CurrentWeapon].m_Offsety;
665 if(Direction.x < 0.0f)
666 WeaponPosition.x -= g_pData->m_Weapons.m_aId[CurrentWeapon].m_Offsetx;
667 if(IsSit)
668 WeaponPosition.y += 3.0f;
669
670 // if active and attack is under way, bash stuffs
671 if(!Inactive || LastAttackTime * HammerAnimationTimeScale < 1.0f)
672 {
673 if(Direction.x < 0)
674 Graphics()->QuadsSetRotation(Angle: -pi / 2.0f - State.GetAttach()->m_Angle * pi * 2.0f);
675 else
676 Graphics()->QuadsSetRotation(Angle: -pi / 2.0f + State.GetAttach()->m_Angle * pi * 2.0f);
677 }
678 else
679 Graphics()->QuadsSetRotation(Angle: Direction.x < 0.0f ? 100.0f : 500.0f);
680
681 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_WeaponEmoteQuadContainerIndex, QuadOffset, X: WeaponPosition.x, Y: WeaponPosition.y);
682 }
683 else if(Player.m_Weapon == WEAPON_NINJA)
684 {
685 WeaponPosition = Position;
686 WeaponPosition.y += g_pData->m_Weapons.m_aId[CurrentWeapon].m_Offsety;
687 if(IsSit)
688 WeaponPosition.y += 3.0f;
689
690 if(Direction.x < 0.0f)
691 {
692 Graphics()->QuadsSetRotation(Angle: -pi / 2 - State.GetAttach()->m_Angle * pi * 2.0f);
693 WeaponPosition.x -= g_pData->m_Weapons.m_aId[CurrentWeapon].m_Offsetx;
694 GameClient()->m_Effects.PowerupShine(Pos: WeaponPosition + vec2(32.0f, 0.0f), Size: vec2(32.0f, 12.0f), Alpha);
695 }
696 else
697 {
698 Graphics()->QuadsSetRotation(Angle: -pi / 2 + State.GetAttach()->m_Angle * pi * 2.0f);
699 GameClient()->m_Effects.PowerupShine(Pos: WeaponPosition - vec2(32.0f, 0.0f), Size: vec2(32.0f, 12.0f), Alpha);
700 }
701 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_WeaponEmoteQuadContainerIndex, QuadOffset, X: WeaponPosition.x, Y: WeaponPosition.y);
702
703 // HADOKEN
704 if(AttackTime <= 1.0f / 6.0f && g_pData->m_Weapons.m_aId[CurrentWeapon].m_NumSpriteMuzzles)
705 {
706 int IteX = rand() % g_pData->m_Weapons.m_aId[CurrentWeapon].m_NumSpriteMuzzles;
707 static int s_LastIteX = IteX;
708 if(Client()->State() == IClient::STATE_DEMOPLAYBACK)
709 {
710 const IDemoPlayer::CInfo *pInfo = DemoPlayer()->BaseInfo();
711 if(pInfo->m_Paused)
712 IteX = s_LastIteX;
713 else
714 s_LastIteX = IteX;
715 }
716 else
717 {
718 if(GameClient()->m_Snap.m_pGameInfoObj && GameClient()->m_Snap.m_pGameInfoObj->m_GameStateFlags & GAMESTATEFLAG_PAUSED)
719 IteX = s_LastIteX;
720 else
721 s_LastIteX = IteX;
722 }
723 if(g_pData->m_Weapons.m_aId[CurrentWeapon].m_aSpriteMuzzles[IteX])
724 {
725 vec2 HadokenDirection;
726 if(PredictLocalWeapons || ClientId < 0)
727 HadokenDirection = vec2(pPlayerChar->m_X, pPlayerChar->m_Y) - vec2(pPrevChar->m_X, pPrevChar->m_Y);
728 else
729 HadokenDirection = vec2(GameClient()->m_Snap.m_aCharacters[ClientId].m_Cur.m_X, GameClient()->m_Snap.m_aCharacters[ClientId].m_Cur.m_Y) - vec2(GameClient()->m_Snap.m_aCharacters[ClientId].m_Prev.m_X, GameClient()->m_Snap.m_aCharacters[ClientId].m_Prev.m_Y);
730 float HadokenAngle = 0.0f;
731 if(absolute(a: HadokenDirection.x) > 0.0001f || absolute(a: HadokenDirection.y) > 0.0001f)
732 {
733 HadokenDirection = normalize(v: HadokenDirection);
734 HadokenAngle = angle(a: HadokenDirection);
735 }
736 else
737 {
738 HadokenDirection = vec2(1.0f, 0.0f);
739 }
740 Graphics()->QuadsSetRotation(Angle: HadokenAngle);
741 QuadOffset = IteX * 2;
742 WeaponPosition = Position;
743 float OffsetX = g_pData->m_Weapons.m_aId[CurrentWeapon].m_Muzzleoffsetx;
744 WeaponPosition -= HadokenDirection * OffsetX;
745 Graphics()->TextureSet(Texture: GameClient()->m_GameSkin.m_aaSpriteWeaponsMuzzles[CurrentWeapon][IteX]);
746 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_aWeaponSpriteMuzzleQuadContainerIndex[CurrentWeapon], QuadOffset, X: WeaponPosition.x, Y: WeaponPosition.y);
747 }
748 }
749 }
750 else
751 {
752 // TODO: should be an animation
753 Recoil = 0.0f;
754 float a = AttackTicksPassed / 5.0f;
755 if(a < 1.0f)
756 Recoil = std::sin(x: a * pi);
757 WeaponPosition = Position + Direction * g_pData->m_Weapons.m_aId[CurrentWeapon].m_Offsetx - Direction * Recoil * 10.0f;
758 WeaponPosition.y += g_pData->m_Weapons.m_aId[CurrentWeapon].m_Offsety;
759 if(IsSit)
760 WeaponPosition.y += 3.0f;
761 if(Player.m_Weapon == WEAPON_GUN && g_Config.m_ClOldGunPosition)
762 WeaponPosition.y -= 8.0f;
763 Graphics()->QuadsSetRotation(Angle: State.GetAttach()->m_Angle * pi * 2.0f + Angle);
764 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_WeaponEmoteQuadContainerIndex, QuadOffset, X: WeaponPosition.x, Y: WeaponPosition.y);
765 }
766
767 if(Player.m_Weapon == WEAPON_GUN || Player.m_Weapon == WEAPON_SHOTGUN)
768 {
769 // check if we're firing stuff
770 if(g_pData->m_Weapons.m_aId[CurrentWeapon].m_NumSpriteMuzzles) // prev.attackticks)
771 {
772 float AlphaMuzzle = 0.0f;
773 if(AttackTicksPassed < g_pData->m_Weapons.m_aId[CurrentWeapon].m_Muzzleduration + 3.0f)
774 {
775 float t = AttackTicksPassed / g_pData->m_Weapons.m_aId[CurrentWeapon].m_Muzzleduration;
776 AlphaMuzzle = mix(a: 2.0f, b: 0.0f, amount: minimum(a: 1.0f, b: maximum(a: 0.0f, b: t)));
777 }
778
779 int IteX = rand() % g_pData->m_Weapons.m_aId[CurrentWeapon].m_NumSpriteMuzzles;
780 static int s_LastIteX = IteX;
781 if(Client()->State() == IClient::STATE_DEMOPLAYBACK)
782 {
783 const IDemoPlayer::CInfo *pInfo = DemoPlayer()->BaseInfo();
784 if(pInfo->m_Paused)
785 IteX = s_LastIteX;
786 else
787 s_LastIteX = IteX;
788 }
789 else
790 {
791 if(GameClient()->m_Snap.m_pGameInfoObj && GameClient()->m_Snap.m_pGameInfoObj->m_GameStateFlags & GAMESTATEFLAG_PAUSED)
792 IteX = s_LastIteX;
793 else
794 s_LastIteX = IteX;
795 }
796 if(AlphaMuzzle > 0.0f && g_pData->m_Weapons.m_aId[CurrentWeapon].m_aSpriteMuzzles[IteX])
797 {
798 float OffsetY = -g_pData->m_Weapons.m_aId[CurrentWeapon].m_Muzzleoffsety;
799 QuadOffset = IteX * 2 + (Direction.x < 0.0f ? 1 : 0);
800 if(Direction.x < 0.0f)
801 OffsetY = -OffsetY;
802
803 vec2 DirectionY(-Direction.y, Direction.x);
804 vec2 MuzzlePos = WeaponPosition + Direction * g_pData->m_Weapons.m_aId[CurrentWeapon].m_Muzzleoffsetx + DirectionY * OffsetY;
805 Graphics()->TextureSet(Texture: GameClient()->m_GameSkin.m_aaSpriteWeaponsMuzzles[CurrentWeapon][IteX]);
806 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_aWeaponSpriteMuzzleQuadContainerIndex[CurrentWeapon], QuadOffset, X: MuzzlePos.x, Y: MuzzlePos.y);
807 }
808 }
809 }
810 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f);
811 Graphics()->QuadsSetRotation(Angle: 0.0f);
812
813 switch(Player.m_Weapon)
814 {
815 case WEAPON_GUN: RenderHand(pInfo: &RenderInfo, CenterPos: WeaponPosition, Dir: Direction, AngleOffset: -3.0f * pi / 4.0f, PostRotOffset: vec2(-15.0f, 4.0f), Alpha); break;
816 case WEAPON_SHOTGUN: RenderHand(pInfo: &RenderInfo, CenterPos: WeaponPosition, Dir: Direction, AngleOffset: -pi / 2.0f, PostRotOffset: vec2(-5.0f, 4.0f), Alpha); break;
817 case WEAPON_GRENADE: RenderHand(pInfo: &RenderInfo, CenterPos: WeaponPosition, Dir: Direction, AngleOffset: -pi / 2.0f, PostRotOffset: vec2(-4.0f, 7.0f), Alpha); break;
818 }
819 }
820 }
821
822 // render the "shadow" tee
823 if(g_Config.m_ClUnpredictedShadow == 3 || (Local && g_Config.m_ClUnpredictedShadow == 1) || (!Local && g_Config.m_ClUnpredictedShadow == 2))
824 {
825 vec2 ShadowPosition = Position;
826 if(ClientId >= 0)
827 ShadowPosition = mix(
828 a: vec2(GameClient()->m_Snap.m_aCharacters[ClientId].m_Prev.m_X, GameClient()->m_Snap.m_aCharacters[ClientId].m_Prev.m_Y),
829 b: vec2(GameClient()->m_Snap.m_aCharacters[ClientId].m_Cur.m_X, GameClient()->m_Snap.m_aCharacters[ClientId].m_Cur.m_Y),
830 amount: Client()->IntraGameTick(Conn: g_Config.m_ClDummy));
831
832 RenderTools()->RenderTee(pAnim: &State, pInfo: &RenderInfo, Emote: Player.m_Emote, Dir: Direction, Pos: ShadowPosition, Alpha: g_Config.m_ClUnpredictedShadowAlpha / 100.f); // render ghost
833 }
834
835 RenderTools()->RenderTee(pAnim: &State, pInfo: &RenderInfo, Emote: Player.m_Emote, Dir: Direction, Pos: Position, Alpha);
836
837 float TeeAnimScale, TeeBaseSize;
838 CRenderTools::GetRenderTeeAnimScaleAndBaseSize(pInfo: &RenderInfo, AnimScale&: TeeAnimScale, BaseSize&: TeeBaseSize);
839 vec2 BodyPos = Position + vec2(State.GetBody()->m_X, State.GetBody()->m_Y) * TeeAnimScale;
840 if(RenderInfo.m_TeeRenderFlags & TEE_EFFECT_FROZEN)
841 {
842 GameClient()->m_Effects.FreezingFlakes(Pos: BodyPos, Size: vec2(32, 32), Alpha);
843 }
844 if(RenderInfo.m_TeeRenderFlags & TEE_EFFECT_SPARKLE)
845 {
846 GameClient()->m_Effects.SparkleTrail(Pos: BodyPos, Alpha);
847 }
848
849 if(ClientId < 0)
850 return;
851
852 int QuadOffsetToEmoticon = NUM_WEAPONS * 2 + 2 + 2;
853 if((Player.m_PlayerFlags & PLAYERFLAG_CHATTING) && !GameClient()->m_aClients[ClientId].m_Afk)
854 {
855 int CurEmoticon = (SPRITE_DOTDOT - SPRITE_OOP);
856 Graphics()->TextureSet(Texture: GameClient()->m_EmoticonsSkin.m_aSpriteEmoticons[CurEmoticon]);
857 int QuadOffset = QuadOffsetToEmoticon + CurEmoticon;
858 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: Alpha);
859 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_WeaponEmoteQuadContainerIndex, QuadOffset, X: Position.x + 24.f, Y: Position.y - 40.f);
860
861 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f);
862 Graphics()->QuadsSetRotation(Angle: 0);
863 }
864
865 if(g_Config.m_ClAfkEmote && GameClient()->m_aClients[ClientId].m_Afk && ClientId != GameClient()->m_aLocalIds[!g_Config.m_ClDummy])
866 {
867 int CurEmoticon = (SPRITE_ZZZ - SPRITE_OOP);
868 Graphics()->TextureSet(Texture: GameClient()->m_EmoticonsSkin.m_aSpriteEmoticons[CurEmoticon]);
869 int QuadOffset = QuadOffsetToEmoticon + CurEmoticon;
870 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: Alpha);
871 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_WeaponEmoteQuadContainerIndex, QuadOffset, X: Position.x + 24.f, Y: Position.y - 40.f);
872
873 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f);
874 Graphics()->QuadsSetRotation(Angle: 0);
875 }
876
877 if(g_Config.m_ClShowEmotes && !GameClient()->m_aClients[ClientId].m_EmoticonIgnore && GameClient()->m_aClients[ClientId].m_EmoticonStartTick != -1)
878 {
879 float SinceStart = (Client()->GameTick(Conn: g_Config.m_ClDummy) - GameClient()->m_aClients[ClientId].m_EmoticonStartTick) + (Client()->IntraGameTickSincePrev(Conn: g_Config.m_ClDummy) - GameClient()->m_aClients[ClientId].m_EmoticonStartFraction);
880 float FromEnd = (2 * Client()->GameTickSpeed()) - SinceStart;
881
882 if(0 <= SinceStart && FromEnd > 0)
883 {
884 float a = 1;
885
886 if(FromEnd < Client()->GameTickSpeed() / 5)
887 a = FromEnd / (Client()->GameTickSpeed() / 5.0f);
888
889 float h = 1;
890 if(SinceStart < Client()->GameTickSpeed() / 10)
891 h = SinceStart / (Client()->GameTickSpeed() / 10.0f);
892
893 float Wiggle = 0;
894 if(SinceStart < Client()->GameTickSpeed() / 5)
895 Wiggle = SinceStart / (Client()->GameTickSpeed() / 5.0f);
896
897 float WiggleAngle = std::sin(x: 5 * Wiggle);
898
899 Graphics()->QuadsSetRotation(Angle: pi / 6 * WiggleAngle);
900
901 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: a * Alpha);
902 // client_datas::emoticon is an offset from the first emoticon
903 int QuadOffset = QuadOffsetToEmoticon + GameClient()->m_aClients[ClientId].m_Emoticon;
904 Graphics()->TextureSet(Texture: GameClient()->m_EmoticonsSkin.m_aSpriteEmoticons[GameClient()->m_aClients[ClientId].m_Emoticon]);
905 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_WeaponEmoteQuadContainerIndex, QuadOffset, X: Position.x, Y: Position.y - 23.f - 32.f * h, ScaleX: 1.f, ScaleY: (64.f * h) / 64.f);
906
907 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f);
908 Graphics()->QuadsSetRotation(Angle: 0);
909 }
910 }
911}
912
913inline bool CPlayers::IsPlayerInfoAvailable(int ClientId) const
914{
915 return GameClient()->m_Snap.m_aCharacters[ClientId].m_Active &&
916 GameClient()->m_Snap.m_apPrevPlayerInfos[ClientId] != nullptr &&
917 GameClient()->m_Snap.m_apPlayerInfos[ClientId] != nullptr;
918}
919
920void CPlayers::OnRender()
921{
922 if(Client()->State() != IClient::STATE_ONLINE && Client()->State() != IClient::STATE_DEMOPLAYBACK)
923 return;
924
925 // update render info for ninja
926 CTeeRenderInfo aRenderInfo[MAX_CLIENTS];
927 const bool IsTeamPlay = GameClient()->IsTeamPlay();
928 for(int i = 0; i < MAX_CLIENTS; ++i)
929 {
930 aRenderInfo[i] = GameClient()->m_aClients[i].m_RenderInfo;
931 aRenderInfo[i].m_TeeRenderFlags = 0;
932
933 // predict freeze skin only for local players
934 bool Frozen = false;
935 if(i == GameClient()->m_aLocalIds[0] || i == GameClient()->m_aLocalIds[1])
936 {
937 if(GameClient()->m_aClients[i].m_Predicted.m_FreezeEnd != 0)
938 aRenderInfo[i].m_TeeRenderFlags |= TEE_EFFECT_FROZEN | TEE_NO_WEAPON;
939 if(GameClient()->m_aClients[i].m_Predicted.m_LiveFrozen)
940 aRenderInfo[i].m_TeeRenderFlags |= TEE_EFFECT_FROZEN;
941 if(GameClient()->m_aClients[i].m_Predicted.m_Invincible)
942 aRenderInfo[i].m_TeeRenderFlags |= TEE_EFFECT_SPARKLE;
943
944 Frozen = GameClient()->m_aClients[i].m_Predicted.m_FreezeEnd != 0;
945 }
946 else
947 {
948 if(GameClient()->m_aClients[i].m_FreezeEnd != 0)
949 aRenderInfo[i].m_TeeRenderFlags |= TEE_EFFECT_FROZEN | TEE_NO_WEAPON;
950 if(GameClient()->m_aClients[i].m_LiveFrozen)
951 aRenderInfo[i].m_TeeRenderFlags |= TEE_EFFECT_FROZEN;
952 if(GameClient()->m_aClients[i].m_Invincible)
953 aRenderInfo[i].m_TeeRenderFlags |= TEE_EFFECT_SPARKLE;
954
955 Frozen = GameClient()->m_Snap.m_aCharacters[i].m_HasExtendedData && GameClient()->m_Snap.m_aCharacters[i].m_ExtendedData.m_FreezeEnd != 0;
956 }
957
958 if((GameClient()->m_aClients[i].m_RenderCur.m_Weapon == WEAPON_NINJA || (Frozen && !GameClient()->m_GameInfo.m_NoSkinChangeForFrozen)) && g_Config.m_ClShowNinja)
959 {
960 // change the skin for the player to the ninja
961 aRenderInfo[i].m_aSixup[g_Config.m_ClDummy].Reset();
962 aRenderInfo[i].ApplySkin(TeeRenderInfo: NinjaTeeRenderInfo()->TeeRenderInfo());
963 aRenderInfo[i].m_CustomColoredSkin = IsTeamPlay;
964 if(!IsTeamPlay)
965 {
966 aRenderInfo[i].m_ColorBody = ColorRGBA(1, 1, 1);
967 aRenderInfo[i].m_ColorFeet = ColorRGBA(1, 1, 1);
968 }
969 }
970 }
971
972 // get screen edges to avoid rendering offscreen
973 float ScreenX0, ScreenY0, ScreenX1, ScreenY1;
974 Graphics()->GetScreen(pTopLeftX: &ScreenX0, pTopLeftY: &ScreenY0, pBottomRightX: &ScreenX1, pBottomRightY: &ScreenY1);
975 // expand the edges to prevent popping in/out onscreen
976 //
977 // it is assumed that the tee, all its weapons, and emotes fit into a 200x200 box centered on the tee
978 // this may need to be changed or calculated differently in the future
979 float BorderBuffer = 100;
980 ScreenX0 -= BorderBuffer;
981 ScreenX1 += BorderBuffer;
982 ScreenY0 -= BorderBuffer;
983 ScreenY1 += BorderBuffer;
984
985 // render everyone else's hook, then our own
986 const int LocalClientId = GameClient()->m_Snap.m_LocalClientId;
987 for(int ClientId = 0; ClientId < MAX_CLIENTS; ClientId++)
988 {
989 if(ClientId == LocalClientId || !IsPlayerInfoAvailable(ClientId))
990 {
991 continue;
992 }
993 RenderHook(pPrevChar: &GameClient()->m_aClients[ClientId].m_RenderPrev, pPlayerChar: &GameClient()->m_aClients[ClientId].m_RenderCur, pRenderInfo: &aRenderInfo[ClientId], ClientId);
994 }
995 if(LocalClientId != -1 && IsPlayerInfoAvailable(ClientId: LocalClientId))
996 {
997 const CGameClient::CClientData *pLocalClientData = &GameClient()->m_aClients[LocalClientId];
998 RenderHook(pPrevChar: &pLocalClientData->m_RenderPrev, pPlayerChar: &pLocalClientData->m_RenderCur, pRenderInfo: &aRenderInfo[LocalClientId], ClientId: LocalClientId);
999 }
1000
1001 // render spectating players
1002 for(const auto &Client : GameClient()->m_aClients)
1003 {
1004 if(!Client.m_SpecCharPresent)
1005 {
1006 continue;
1007 }
1008
1009 const int ClientId = Client.ClientId();
1010 float Alpha = (GameClient()->IsOtherTeam(ClientId) || ClientId < 0) ? g_Config.m_ClShowOthersAlpha / 100.f : 1.f;
1011 if(ClientId == -2) // ghost
1012 {
1013 Alpha = g_Config.m_ClRaceGhostAlpha / 100.f;
1014 }
1015 RenderTools()->RenderTee(pAnim: CAnimState::GetIdle(), pInfo: &SpectatorTeeRenderInfo()->TeeRenderInfo(), Emote: EMOTE_BLINK, Dir: vec2(1, 0), Pos: Client.m_SpecChar, Alpha);
1016 }
1017
1018 // render everyone else's tee, then either our own or the tee we are spectating.
1019 const int RenderLastId = (GameClient()->m_Snap.m_SpecInfo.m_SpectatorId != SPEC_FREEVIEW && GameClient()->m_Snap.m_SpecInfo.m_Active) ? GameClient()->m_Snap.m_SpecInfo.m_SpectatorId : LocalClientId;
1020
1021 for(int ClientId = 0; ClientId < MAX_CLIENTS; ClientId++)
1022 {
1023 if(ClientId == RenderLastId || !IsPlayerInfoAvailable(ClientId))
1024 {
1025 continue;
1026 }
1027
1028 RenderHookCollLine(pPrevChar: &GameClient()->m_aClients[ClientId].m_RenderPrev, pPlayerChar: &GameClient()->m_aClients[ClientId].m_RenderCur, ClientId);
1029
1030 if(!in_range(a: GameClient()->m_aClients[ClientId].m_RenderPos.x, lower: ScreenX0, upper: ScreenX1) || !in_range(a: GameClient()->m_aClients[ClientId].m_RenderPos.y, lower: ScreenY0, upper: ScreenY1))
1031 {
1032 continue;
1033 }
1034 RenderPlayer(pPrevChar: &GameClient()->m_aClients[ClientId].m_RenderPrev, pPlayerChar: &GameClient()->m_aClients[ClientId].m_RenderCur, pRenderInfo: &aRenderInfo[ClientId], ClientId);
1035 }
1036 if(RenderLastId != -1 && IsPlayerInfoAvailable(ClientId: RenderLastId))
1037 {
1038 const CGameClient::CClientData *pClientData = &GameClient()->m_aClients[RenderLastId];
1039 RenderHookCollLine(pPrevChar: &pClientData->m_RenderPrev, pPlayerChar: &pClientData->m_RenderCur, ClientId: RenderLastId);
1040 RenderPlayer(pPrevChar: &pClientData->m_RenderPrev, pPlayerChar: &pClientData->m_RenderCur, pRenderInfo: &aRenderInfo[RenderLastId], ClientId: RenderLastId);
1041 }
1042}
1043
1044void CPlayers::CreateNinjaTeeRenderInfo()
1045{
1046 CTeeRenderInfo NinjaTeeRenderInfo;
1047 NinjaTeeRenderInfo.m_Size = 64.0f;
1048 CSkinDescriptor NinjaSkinDescriptor;
1049 NinjaSkinDescriptor.m_Flags |= CSkinDescriptor::FLAG_SIX;
1050 str_copy(dst&: NinjaSkinDescriptor.m_aSkinName, src: "x_ninja");
1051 m_pNinjaTeeRenderInfo = GameClient()->CreateManagedTeeRenderInfo(TeeRenderInfo: NinjaTeeRenderInfo, SkinDescriptor: NinjaSkinDescriptor);
1052}
1053
1054void CPlayers::CreateSpectatorTeeRenderInfo()
1055{
1056 CTeeRenderInfo SpectatorTeeRenderInfo;
1057 SpectatorTeeRenderInfo.m_Size = 64.0f;
1058 CSkinDescriptor SpectatorSkinDescriptor;
1059 SpectatorSkinDescriptor.m_Flags |= CSkinDescriptor::FLAG_SIX;
1060 str_copy(dst&: SpectatorSkinDescriptor.m_aSkinName, src: "x_spec");
1061 m_pSpectatorTeeRenderInfo = GameClient()->CreateManagedTeeRenderInfo(TeeRenderInfo: SpectatorTeeRenderInfo, SkinDescriptor: SpectatorSkinDescriptor);
1062}
1063
1064void CPlayers::OnInit()
1065{
1066 m_WeaponEmoteQuadContainerIndex = Graphics()->CreateQuadContainer(AutomaticUpload: false);
1067
1068 Graphics()->SetColor(r: 1.f, g: 1.f, b: 1.f, a: 1.f);
1069
1070 for(int i = 0; i < NUM_WEAPONS; ++i)
1071 {
1072 float ScaleX, ScaleY;
1073 Graphics()->GetSpriteScale(pSprite: g_pData->m_Weapons.m_aId[i].m_pSpriteBody, ScaleX, ScaleY);
1074 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 0, BottomRightU: 1, BottomRightV: 1);
1075 Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_WeaponEmoteQuadContainerIndex, Width: g_pData->m_Weapons.m_aId[i].m_VisualSize * ScaleX, Height: g_pData->m_Weapons.m_aId[i].m_VisualSize * ScaleY);
1076 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 1, BottomRightU: 1, BottomRightV: 0);
1077 Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_WeaponEmoteQuadContainerIndex, Width: g_pData->m_Weapons.m_aId[i].m_VisualSize * ScaleX, Height: g_pData->m_Weapons.m_aId[i].m_VisualSize * ScaleY);
1078 }
1079 float ScaleX, ScaleY;
1080
1081 // at the end the hand
1082 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 0, BottomRightU: 1, BottomRightV: 1);
1083 Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_WeaponEmoteQuadContainerIndex, Size: 20.f);
1084 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 0, BottomRightU: 1, BottomRightV: 1);
1085 Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_WeaponEmoteQuadContainerIndex, Size: 20.f);
1086
1087 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 0, BottomRightU: 1, BottomRightV: 1);
1088 Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_WeaponEmoteQuadContainerIndex, X: -12.f, Y: -8.f, Width: 24.f, Height: 16.f);
1089 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 0, BottomRightU: 1, BottomRightV: 1);
1090 Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_WeaponEmoteQuadContainerIndex, X: -12.f, Y: -8.f, Width: 24.f, Height: 16.f);
1091
1092 for(int i = 0; i < NUM_EMOTICONS; ++i)
1093 {
1094 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 0, BottomRightU: 1, BottomRightV: 1);
1095 Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_WeaponEmoteQuadContainerIndex, Size: 64.f);
1096 }
1097 Graphics()->QuadContainerUpload(ContainerIndex: m_WeaponEmoteQuadContainerIndex);
1098
1099 for(int i = 0; i < NUM_WEAPONS; ++i)
1100 {
1101 m_aWeaponSpriteMuzzleQuadContainerIndex[i] = Graphics()->CreateQuadContainer(AutomaticUpload: false);
1102 for(int n = 0; n < g_pData->m_Weapons.m_aId[i].m_NumSpriteMuzzles; ++n)
1103 {
1104 if(g_pData->m_Weapons.m_aId[i].m_aSpriteMuzzles[n])
1105 {
1106 if(i == WEAPON_GUN || i == WEAPON_SHOTGUN)
1107 {
1108 // TODO: hardcoded for now to get the same particle size as before
1109 Graphics()->GetSpriteScaleImpl(Width: 96, Height: 64, ScaleX, ScaleY);
1110 }
1111 else
1112 Graphics()->GetSpriteScale(pSprite: g_pData->m_Weapons.m_aId[i].m_aSpriteMuzzles[n], ScaleX, ScaleY);
1113 }
1114
1115 float SWidth = (g_pData->m_Weapons.m_aId[i].m_VisualSize * ScaleX) * (4.0f / 3.0f);
1116 float SHeight = g_pData->m_Weapons.m_aId[i].m_VisualSize * ScaleY;
1117
1118 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 0, BottomRightU: 1, BottomRightV: 1);
1119 if(WEAPON_NINJA == i)
1120 Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_aWeaponSpriteMuzzleQuadContainerIndex[i], Width: 160.f * ScaleX, Height: 160.f * ScaleY);
1121 else
1122 Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_aWeaponSpriteMuzzleQuadContainerIndex[i], Width: SWidth, Height: SHeight);
1123
1124 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 1, BottomRightU: 1, BottomRightV: 0);
1125 if(WEAPON_NINJA == i)
1126 Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_aWeaponSpriteMuzzleQuadContainerIndex[i], Width: 160.f * ScaleX, Height: 160.f * ScaleY);
1127 else
1128 Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_aWeaponSpriteMuzzleQuadContainerIndex[i], Width: SWidth, Height: SHeight);
1129 }
1130 Graphics()->QuadContainerUpload(ContainerIndex: m_aWeaponSpriteMuzzleQuadContainerIndex[i]);
1131 }
1132
1133 Graphics()->QuadsSetSubset(TopLeftU: 0.f, TopLeftV: 0.f, BottomRightU: 1.f, BottomRightV: 1.f);
1134 Graphics()->QuadsSetRotation(Angle: 0.f);
1135
1136 CreateNinjaTeeRenderInfo();
1137 CreateSpectatorTeeRenderInfo();
1138}
1139