|
5 | 5 | "time"
|
6 | 6 |
|
7 | 7 | "github.com/golang/geo/r3"
|
| 8 | + "github.com/pkg/errors" |
8 | 9 |
|
9 | 10 | "github.com/markus-wa/demoinfocs-golang/v3/pkg/demoinfocs/constants"
|
10 | 11 | st "github.com/markus-wa/demoinfocs-golang/v3/pkg/demoinfocs/sendtables"
|
@@ -343,20 +344,24 @@ func (pf PlayerFlags) OnGround() bool {
|
343 | 344 | }
|
344 | 345 |
|
345 | 346 | // Ducking returns true if the player is/was fully crouched.
|
346 |
| -// Fully ducked: Ducking() && DuckingKeyPressed() |
347 |
| -// Previously fully ducked, unducking in progress: Ducking() && !DuckingKeyPressed() |
348 |
| -// Fully unducked: !Ducking() && !DuckingKeyPressed() |
349 |
| -// Previously fully unducked, ducking in progress: !Ducking() && DuckingKeyPressed() |
| 347 | +// |
| 348 | +// Fully ducked: Ducking() && DuckingKeyPressed() |
| 349 | +// Previously fully ducked, unducking in progress: Ducking() && !DuckingKeyPressed() |
| 350 | +// Fully unducked: !Ducking() && !DuckingKeyPressed() |
| 351 | +// Previously fully unducked, ducking in progress: !Ducking() && DuckingKeyPressed() |
| 352 | +// |
350 | 353 | // See m_fFlags FL_DUCKING https://github.com/ValveSoftware/source-sdk-2013/blob/master/mp/src/public/const.h#L146-L188
|
351 | 354 | func (pf PlayerFlags) Ducking() bool {
|
352 | 355 | return pf.Get(flDucking)
|
353 | 356 | }
|
354 | 357 |
|
355 | 358 | // DuckingKeyPressed returns true if the player is holding the crouch key pressed.
|
356 |
| -// Fully ducked: Ducking() && DuckingKeyPressed() |
357 |
| -// Previously fully ducked, unducking in progress: Ducking() && !DuckingKeyPressed() |
358 |
| -// Fully unducked: !Ducking() && !DuckingKeyPressed() |
359 |
| -// Previously fully unducked, ducking in progress: !Ducking() && DuckingKeyPressed() |
| 359 | +// |
| 360 | +// Fully ducked: Ducking() && DuckingKeyPressed() |
| 361 | +// Previously fully ducked, unducking in progress: Ducking() && !DuckingKeyPressed() |
| 362 | +// Fully unducked: !Ducking() && !DuckingKeyPressed() |
| 363 | +// Previously fully unducked, ducking in progress: !Ducking() && DuckingKeyPressed() |
| 364 | +// |
360 | 365 | // See m_fFlags FL_ANIMDUCKING https://github.com/ValveSoftware/source-sdk-2013/blob/master/mp/src/public/const.h#L146-L188
|
361 | 366 | func (pf PlayerFlags) DuckingKeyPressed() bool {
|
362 | 367 | return pf.Get(flAnimDucking)
|
@@ -406,9 +411,43 @@ func (p *Player) Score() int {
|
406 | 411 | return getInt(p.resourceEntity(), "m_iScore."+p.entityIDStr())
|
407 | 412 | }
|
408 | 413 |
|
409 |
| -// Color returns the players color as shown on the match. |
| 414 | +// Color returns the players color as shown on the minimap. |
| 415 | +// It will return Grey (-1) if the resource entity does not exist when the function is called or when the demo does not support player colors. |
| 416 | +// Deprecated: Use ColorOrErr() instead. |
410 | 417 | func (p *Player) Color() Color {
|
411 |
| - return Color(getInt(p.resourceEntity(), "m_iCompTeammateColor."+p.entityIDStr())) |
| 418 | + resourceEnt := p.resourceEntity() |
| 419 | + if resourceEnt == nil { |
| 420 | + return Grey |
| 421 | + } |
| 422 | + |
| 423 | + n, ok := resourceEnt.PropertyValue("m_iCompTeammateColor." + p.entityIDStr()) |
| 424 | + if !ok { |
| 425 | + return Grey |
| 426 | + } |
| 427 | + |
| 428 | + return Color(n.IntVal) |
| 429 | +} |
| 430 | + |
| 431 | +var ( |
| 432 | + ErrDataNotAvailable = errors.New("some data is not (yet) available (reading the same data later during parsing may work)") |
| 433 | + ErrNotSupportedByDemo = errors.New("this data is not supported by the demo (this may be because the demos is too old)") |
| 434 | +) |
| 435 | + |
| 436 | +// ColorOrErr returns the players color as shown on the minimap. |
| 437 | +// Returns ErrDataNotAvailable if the resource entity does not exist (it may exist later during parsing). |
| 438 | +// Returns ErrNotSupportedByDemo if the demo does not support player colors (e.g. very old demos). |
| 439 | +func (p *Player) ColorOrErr() (Color, error) { |
| 440 | + resourceEnt := p.resourceEntity() |
| 441 | + if resourceEnt == nil { |
| 442 | + return Grey, errors.Wrap(ErrDataNotAvailable, "player resource entity is nil") |
| 443 | + } |
| 444 | + |
| 445 | + colorVal, ok := resourceEnt.PropertyValue("m_iCompTeammateColor." + p.entityIDStr()) |
| 446 | + if !ok { |
| 447 | + return Grey, errors.Wrap(ErrNotSupportedByDemo, "failed to get player color from resource entity") |
| 448 | + } |
| 449 | + |
| 450 | + return Color(colorVal.IntVal), nil |
412 | 451 | }
|
413 | 452 |
|
414 | 453 | // Kills returns the amount of kills the player has as shown on the scoreboard.
|
|
0 commit comments