ScummVM API documentation
actor.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef GRIM_ACTOR_H
23 #define GRIM_ACTOR_H
24 
25 #include "engines/grim/pool.h"
26 #include "engines/grim/object.h"
27 #include "engines/grim/color.h"
28 
29 #include "math/vector3d.h"
30 #include "math/angle.h"
31 #include "math/quat.h"
32 
33 namespace Grim {
34 
35 class TextObject;
36 class Sector;
37 class Costume;
38 class LipSync;
39 class Font;
40 class Set;
41 class Material;
42 struct SetShadow;
43 struct Joint;
44 class EMIModel;
45 
46 struct Plane {
47  Common::String setName;
48  Sector *sector;
49 };
50 
52 
53 #define MAX_SHADOWS 8
54 
55 struct Shadow {
56  Shadow();
57 
58  Common::String name;
59  Math::Vector3d pos;
60  SectorListType planeList;
61  bool active;
62  bool dontNegate;
63  Color color;
64  void *userData;
65 };
66 
72 class Actor : public PoolObject<Actor> {
73 public:
74  enum CollisionMode {
75  CollisionOff = 0,
76  CollisionBox = 1,
77  CollisionSphere = 2
78  };
79 
80  enum AlphaMode {
81  AlphaOff = -1,
82  AlphaReplace = 2,
83  AlphaModulate = 3 // Seems to be unused
84  };
85 
86  enum LightMode {
87  LightStatic = 0,
88  LightFastDyn = 1,
89  LightNormDyn = 2,
90  LightNone = 3
91  };
92 
96  Actor();
101  ~Actor();
102 
103  static int32 getStaticTag() { return MKTAG('A', 'C', 'T', 'R'); }
104 
110  void saveState(SaveGame *savedState) const;
116  bool restoreState(SaveGame *savedState);
117 
121  const Common::String &getName() const { return _name; }
122 
128  void setName(const Common::String &name) { _name = name; }
129 
136  void setTalkColor(const Color &color) { _talkColor = color; }
142  Color getTalkColor() const { return _talkColor; }
143 
150  void setPos(const Math::Vector3d &position);
156  Math::Vector3d getPos() const { return _pos; }
157  inline Math::Vector3d getDestPos() const { return _destPos; }
158 
168  void walkTo(const Math::Vector3d &position);
175  void stopWalking() { _walking = false; }
182  bool isWalking() const;
183 
200  void turnTo(const Math::Angle &pitch, const Math::Angle &yaw, const Math::Angle &roll, bool snap = false);
211  void turnTo(const Math::Vector3d &pos, bool snap = false);
222  bool singleTurnTo(const Math::Vector3d &pos);
228  bool isTurning() const;
232  void stopTurning();
247  void setRot(const Math::Angle &pitch, const Math::Angle &yaw, const Math::Angle &roll);
256  void setRot(const Math::Vector3d &pos);
269  void turn(int dir);
280  Math::Angle getPitch() const { return _pitch; }
291  Math::Angle getYaw() const { return _yaw; }
302  Math::Angle getRoll() const { return _roll; }
303 
310  Math::Angle getYawTo(const Actor *actor) const;
317  Math::Angle getYawTo(const Math::Vector3d &p) const;
318 
325  void setVisibility(bool val) { _visible = val; }
331  bool isVisible() const { return _visible; }
332 
339  void setScale(float scale) { _scale = scale; }
340  float getScale() const { return _scale; }
341 
350  void setTimeScale(float scale) { _timeScale = scale; }
356  float getTimeScale() const { return _timeScale; }
357 
363  void putInSet(const Common::String &setName);
370  bool isDrawableInSet(const Common::String &setName) const;
377  bool isInSet(const Common::String &setName) const;
378 
385  void setTurnRate(float rate) { _turnRate = rate; }
391  float getTurnRate() const { return _turnRate; }
398  void setWalkRate(float rate) { _walkRate = rate; }
404  float getWalkRate() const { return _walkRate; }
405 
406  void setLooking(bool lookingMode) { _lookingMode = lookingMode; }
407 
415  void walkForward();
416  void moveTo(const Math::Vector3d &pos);
422  void setRunning(bool running) { _running = running; }
423  void setReflection(float angle) { _reflectionAngle = angle; }
428  Math::Vector3d getPuckVector() const;
429 
430  void setPuckOrient(bool orient);
431 
442  void sayLine(const char *msgId, bool background, float x, float y);
443  // When we clean all text objects we don't want the actors to clean their
444  // objects again since they're already freed
445  void lineCleanup();
452  void shutUp();
459  bool isTalking();
460 
461  void setRestChore(int choreNumber, Costume *cost);
462  int getRestChore() const;
463  void setWalkChore(int choreNumber, Costume *cost);
464  void setTurnChores(int left_chore, int right_chore, Costume *cost);
465  void setTalkChore(int index, int choreNumber, Costume *cost);
466  int getTalkChore(int index) const;
467  Costume *getTalkCostume(int index) const;
468  void setMumbleChore(int choreNumber, Costume *cost);
469  void stopAllChores(bool ignoreLoopingChores = false);
470  void setColormap(const char *map);
471  void pushCostume(const char *name);
472  void setCostume(const char *name);
473  void popCostume();
474  void clearCostumes();
475  Costume *getCurrentCostume() const;
476  void setLocalAlphaMode(unsigned int vertex, AlphaMode alphamode);
477  void setLocalAlpha(unsigned int vertex, float alpha);
478  bool hasLocalAlpha() const;
479  float getLocalAlpha(unsigned int vertex) const;
480  Costume *findCostume(const Common::String &name);
481  int getCostumeStackDepth() const {
482  return _costumeStack.size();
483  }
484  const Common::List<Costume *> &getCostumes() const { return _costumeStack; }
485 
486  void setActiveShadow(int shadowId);
487  void setShadowPoint(const Math::Vector3d &pos);
488  void setShadowColor(const Color &color);
489  void setShadowPlane(const char *name);
490  void addShadowPlane(const char *name);
491  void clearShadowPlanes();
492  void clearShadowPlane(int i);
493  void setShadowValid(int);
494  void setActivateShadow(int, bool);
495 
496  void setFollowBoxes(bool follow);
497  bool hasFollowBoxes() const { return _followBoxes; }
498  bool hasFollowedBoxes() const { return _hasFollowedBoxes; }
499  void update(uint frameTime);
503  bool updateTalk(uint frameTime);
504  void draw();
505 
506  bool isLookAtVectorZero() {
507  return _lookAtVector.isZero();
508  }
509  void setLookAtVectorZero() {
510  _lookAtVector.set(0.f, 0.f, 0.f);
511  _lookAtActor = 0;
512  }
513  void setLookAtVector(const Math::Vector3d &vector) {
514  _lookAtVector = vector;
515  _lookAtActor = 0;
516  }
517  Math::Vector3d getLookAtVector() {
518  return _lookAtVector;
519  }
520  void setLookAtActor(Actor *other) { _lookAtActor = other->getId(); }
521  void setLookAtRate(float rate);
522  float getLookAtRate() const;
523  void setHead(int joint1, int joint2, int joint3, float maxRoll, float maxPitch, float maxYaw);
524  void setHead(const char *joint, const Math::Vector3d &offset);
525  void setHeadLimits(float yawRange, float maxPitch, float minPitch);
526 
527  void setCollisionMode(CollisionMode mode);
528  void setCollisionScale(float scale);
529 
530  bool handleCollisionWith(Actor *actor, CollisionMode mode, Math::Vector3d *vec) const;
531 
532  static void saveStaticState(SaveGame *state);
533  static void restoreStaticState(SaveGame *state);
534 
535  bool isAttached() const { return _attachedActor != 0; }
536  Math::Vector3d getWorldPos() const;
537  void attachToActor(Actor *other, const char *joint);
538  void detach();
539  Math::Quaternion getRotationQuat() const;
540  const Math::Matrix4 getFinalMatrix() const;
541  Math::Vector3d getHeadPos() const;
542 
543  void setInOverworld(bool inOverworld) { _inOverworld = inOverworld; }
544  bool isInOverworld() const { return _inOverworld; }
545 
546  float getGlobalAlpha() const { return _globalAlpha; }
547  AlphaMode getAlphaMode() const { return _alphaMode; }
548  float getEffectiveAlpha() const { return _alphaMode != AlphaOff ? _globalAlpha : 1.f; }
549  void setGlobalAlpha(float alpha, const Common::String &mesh);
550  void setAlphaMode(AlphaMode mode, const Common::String &mesh);
551 
552  int getSortOrder() const;
553  void setSortOrder(const int order);
554  int getEffectiveSortOrder() const;
555 
556  void activateShadow(bool active, const char *shadowName);
557  void activateShadow(bool active, SetShadow *shadow);
558 
559  void drawToCleanBuffer();
560 
561  bool isTalkingForeground() const;
562 
563  LightMode getLightMode() const { return _lightMode; }
564  void setLightMode(LightMode lightMode) { _lightMode = lightMode; }
565 
566  ObjectPtr<Material> loadMaterial(const Common::String &name, bool clamp);
567  ObjectPtr<Material> findMaterial(const Common::String &name);
568 
569  void getBBoxInfo(Math::Vector3d &bboxPos, Math::Vector3d &bboxSize) const;
570 
571 private:
572  void costumeMarkerCallback(int marker);
573  void collisionHandlerCallback(Actor *other) const;
574  void updateWalk();
575  void addShadowPlane(const char *n, Set *scene, int shadowId);
576  bool shouldDrawShadow(int shadowId);
577  void stopTalking();
578  bool stopMumbleChore();
579  void drawCostume(Costume *costume);
584  Math::Vector3d handleCollisionTo(const Math::Vector3d &from, const Math::Vector3d &pos) const;
590  Math::Vector3d getTangentPos(const Math::Vector3d &pos, const Math::Vector3d &dest) const;
591 
592  Math::Vector3d getSimplePuckVector() const;
593  void calculateOrientation(const Math::Vector3d &pos, Math::Angle *pitch, Math::Angle *yaw, Math::Angle *roll);
594 
595  bool getSphereInfo(bool adjustZ, float &size, Math::Vector3d &pos) const;
596  EMIModel *findModelWithMesh(const Common::String &mesh);
597 
598  Common::String _name;
599  Common::String _setName; // The actual current set
600 
601  Color _talkColor;
602  Math::Vector3d _pos;
603  Math::Angle _pitch, _yaw, _roll;
604  float _walkRate, _turnRate;
605 
606  bool _followBoxes; // Constrain to walkboxes
607  bool _hasFollowedBoxes;
608  float _reflectionAngle; // Maximum angle to turn by at walls
609  bool _visible;
610  float _scale;
611  float _timeScale;
612  bool _lookingMode;
613  Common::String _talkSoundName;
614  bool _talking;
615  bool _backgroundTalk;
616  ObjectPtr<LipSync> _lipSync;
617  Common::List<Costume *> _costumeStack;
618 
619  // Variables for gradual turning
620  bool _turning;
621  bool _singleTurning;
622  // NOTE: The movement direction is separate from the direction
623  // the actor's model is facing. The model's direction is gradually
624  // updated to match the movement direction. This produces a smooth
625  // turning animation while still allowing the actor to move in a
626  // new direction immediately after reflecting off a wall.
627  Math::Angle _moveYaw;
628  Math::Angle _movePitch;
629  Math::Angle _moveRoll;
630  // This is used to increase momentarily the turn rate when needed
631  float _turnRateMultiplier;
632 
633  // Variables for walking to a point
634  bool _walking;
635  Math::Vector3d _destPos;
636 
637  //chores
638  class ActionChore {
639  public:
640  ActionChore();
641  ActionChore(Costume *cost, int chore);
642 
643  void play(bool fade = false, unsigned int time = fadeTime);
644  void playLooping(bool fade = false, unsigned int time = fadeTime);
645  void stop(bool fade = false, unsigned int time = fadeTime);
646  void setLastFrame();
647 
648  inline bool isValid() const { return _chore > -1 && _costume != nullptr; }
649  bool isPlaying() const;
650  inline bool equals(const Costume *cost, int chore) const {
651  return (_costume == cost && _chore == chore);
652  }
653 
654  void saveState(SaveGame *state) const;
655  void restoreState(SaveGame *state, Actor *actor);
656 
657  Costume *_costume;
658  int _chore;
659 
660  static const unsigned int fadeTime;
661  static const unsigned int talkFadeTime;
662  };
663  ActionChore _restChore;
664 
665  ActionChore _walkChore;
666  bool _walkedLast, _walkedCur;
667  bool _running;
668 
669  ActionChore _leftTurnChore, _rightTurnChore;
670  int _lastTurnDir, _currTurnDir;
671 
672  ActionChore _talkChore[10];
673  int _talkAnim;
674 
675  ActionChore _mumbleChore;
676 
677  Shadow *_shadowArray;
678  int _activeShadowSlot;
679 
680  static ObjectPtr<Font> _sayLineFont;
681  int _sayLineText;
682  bool _mustPlaceText;
683 
684  ActionChore *getTurnChore(int dir) {
685  return (dir > 0 ? &_leftTurnChore : &_rightTurnChore);
686  }
687 
688  void freeCostume(Costume *costume);
689  void freeCostumeChore(const Costume *toFree, ActionChore *chore);
690 
691  // lookAt
692  Math::Vector3d _lookAtVector;
693 
694  // struct used for path finding
695  struct PathNode {
696  Sector *sect;
697  PathNode *parent;
698  Math::Vector3d pos;
699  float dist;
700  float cost;
701  };
703 
704  CollisionMode _collisionMode;
705  float _collisionScale;
706 
707  bool _puckOrient;
708 
709  static bool _isTalkingBackground;
710  int _talkDelay;
711  int _attachedActor;
712  int _lookAtActor;
713  Common::String _attachedJoint;
714  AlphaMode _alphaMode;
715  float _globalAlpha;
716 
717  bool _inOverworld;
718 
719  int _sortOrder;
720  int _sectorSortOrder;
721  bool _useParentSortOrder;
722 
723  bool _fakeUnbound;
724  bool _drawnToClean;
725 
726  LightMode _lightMode;
727 
729 
730  // Highest vertex used in EMI
731  const static unsigned int MAX_LOCAL_ALPHA_VERTICES = 48;
732  Common::Array<float> _localAlpha;
733  Common::Array<int> _localAlphaMode;
734 };
735 
736 } // end of namespace Grim
737 
738 #endif
float getTimeScale() const
Definition: actor.h:356
Definition: str.h:59
Definition: actor.h:55
void setVisibility(bool val)
Definition: actor.h:325
void setWalkRate(float rate)
Definition: actor.h:398
Definition: modelemi.h:82
Math::Angle getPitch() const
Definition: actor.h:280
void setTalkColor(const Color &color)
Definition: actor.h:136
float getWalkRate() const
Definition: actor.h:404
Math::Angle getRoll() const
Definition: actor.h:302
Definition: savegame.h:33
Definition: actor.h:33
void setScale(float scale)
Definition: actor.h:339
void stopWalking()
Definition: actor.h:175
Graphics::Surface * scale(const Graphics::Surface &srcImage, int xSize, int ySize)
bool isVisible() const
Definition: actor.h:331
Definition: set.h:216
void setRunning(bool running)
Definition: actor.h:422
float getTurnRate() const
Definition: actor.h:391
#define MKTAG(a0, a1, a2, a3)
Definition: endian.h:188
Definition: object.h:69
Definition: pool.h:42
Actor represents a 3D character on screen.
Definition: actor.h:72
void setTurnRate(float rate)
Definition: actor.h:385
void setTimeScale(float scale)
Definition: actor.h:350
Math::Angle getYaw() const
Definition: actor.h:291
void setName(const Common::String &name)
Definition: actor.h:128
Definition: actor.h:46
Math::Vector3d getPos() const
Definition: actor.h:156
Definition: color.h:29
Definition: costume.h:45
Color getTalkColor() const
Definition: actor.h:142
Definition: sector.h:40
const Common::String & getName() const
Definition: actor.h:121
Definition: set.h:44