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  Grim::Joint *getAttachedJoint(Actor *parent) const;
538  void attachToActor(Actor *other, const char *joint);
539  void detach();
540  Math::Quaternion getRotationQuat() const;
541  const Math::Matrix4 getFinalMatrix() const;
542  Math::Vector3d getHeadPos() const;
543 
544  void setInOverworld(bool inOverworld) { _inOverworld = inOverworld; }
545  bool isInOverworld() const { return _inOverworld; }
546 
547  float getGlobalAlpha() const { return _globalAlpha; }
548  AlphaMode getAlphaMode() const { return _alphaMode; }
549  float getEffectiveAlpha() const { return _alphaMode != AlphaOff ? _globalAlpha : 1.f; }
550  void setGlobalAlpha(float alpha, const Common::String &mesh);
551  void setAlphaMode(AlphaMode mode, const Common::String &mesh);
552 
553  int getSortOrder() const;
554  void setSortOrder(const int order);
555  int getEffectiveSortOrder() const;
556 
557  void activateShadow(bool active, const char *shadowName);
558  void activateShadow(bool active, SetShadow *shadow);
559 
560  void drawToCleanBuffer();
561 
562  bool isTalkingForeground() const;
563 
564  LightMode getLightMode() const { return _lightMode; }
565  void setLightMode(LightMode lightMode) { _lightMode = lightMode; }
566 
567  ObjectPtr<Material> loadMaterial(const Common::String &name, bool clamp);
568  ObjectPtr<Material> findMaterial(const Common::String &name);
569 
570  void getBBoxInfo(Math::Vector3d &bboxPos, Math::Vector3d &bboxSize) const;
571 
572 private:
573  void costumeMarkerCallback(int marker);
574  void collisionHandlerCallback(Actor *other) const;
575  void updateWalk();
576  void addShadowPlane(const char *n, Set *scene, int shadowId);
577  bool shouldDrawShadow(int shadowId);
578  void stopTalking();
579  bool stopMumbleChore();
580  void drawCostume(Costume *costume);
585  Math::Vector3d handleCollisionTo(const Math::Vector3d &from, const Math::Vector3d &pos) const;
591  Math::Vector3d getTangentPos(const Math::Vector3d &pos, const Math::Vector3d &dest) const;
592 
593  Math::Vector3d getSimplePuckVector() const;
594  void calculateOrientation(const Math::Vector3d &pos, Math::Angle *pitch, Math::Angle *yaw, Math::Angle *roll);
595 
596  bool getSphereInfo(bool adjustZ, float &size, Math::Vector3d &pos) const;
597  EMIModel *findModelWithMesh(const Common::String &mesh);
598 
599  Common::String _name;
600  Common::String _setName; // The actual current set
601 
602  Color _talkColor;
603  Math::Vector3d _pos;
604  Math::Angle _pitch, _yaw, _roll;
605  float _walkRate, _turnRate;
606 
607  bool _followBoxes; // Constrain to walkboxes
608  bool _hasFollowedBoxes;
609  float _reflectionAngle; // Maximum angle to turn by at walls
610  bool _visible;
611  float _scale;
612  float _timeScale;
613  bool _lookingMode;
614  Common::String _talkSoundName;
615  bool _talking;
616  bool _backgroundTalk;
617  ObjectPtr<LipSync> _lipSync;
618  Common::List<Costume *> _costumeStack;
619 
620  // Variables for gradual turning
621  bool _turning;
622  bool _singleTurning;
623  // NOTE: The movement direction is separate from the direction
624  // the actor's model is facing. The model's direction is gradually
625  // updated to match the movement direction. This produces a smooth
626  // turning animation while still allowing the actor to move in a
627  // new direction immediately after reflecting off a wall.
628  Math::Angle _moveYaw;
629  Math::Angle _movePitch;
630  Math::Angle _moveRoll;
631  // This is used to increase momentarily the turn rate when needed
632  float _turnRateMultiplier;
633 
634  // Variables for walking to a point
635  bool _walking;
636  Math::Vector3d _destPos;
637 
638  //chores
639  class ActionChore {
640  public:
641  ActionChore();
642  ActionChore(Costume *cost, int chore);
643 
644  void play(bool fade = false, unsigned int time = fadeTime);
645  void playLooping(bool fade = false, unsigned int time = fadeTime);
646  void stop(bool fade = false, unsigned int time = fadeTime);
647  void setLastFrame();
648 
649  inline bool isValid() const { return _chore > -1 && _costume != nullptr; }
650  bool isPlaying() const;
651  inline bool equals(const Costume *cost, int chore) const {
652  return (_costume == cost && _chore == chore);
653  }
654 
655  void saveState(SaveGame *state) const;
656  void restoreState(SaveGame *state, Actor *actor);
657 
658  Costume *_costume;
659  int _chore;
660 
661  static const unsigned int fadeTime;
662  static const unsigned int talkFadeTime;
663  };
664  ActionChore _restChore;
665 
666  ActionChore _walkChore;
667  bool _walkedLast, _walkedCur;
668  bool _running;
669 
670  ActionChore _leftTurnChore, _rightTurnChore;
671  int _lastTurnDir, _currTurnDir;
672 
673  ActionChore _talkChore[10];
674  int _talkAnim;
675 
676  ActionChore _mumbleChore;
677 
678  Shadow *_shadowArray;
679  int _activeShadowSlot;
680 
681  static ObjectPtr<Font> _sayLineFont;
682  int _sayLineText;
683  bool _mustPlaceText;
684 
685  ActionChore *getTurnChore(int dir) {
686  return (dir > 0 ? &_leftTurnChore : &_rightTurnChore);
687  }
688 
689  void freeCostume(Costume *costume);
690  void freeCostumeChore(const Costume *toFree, ActionChore *chore);
691 
692  // lookAt
693  Math::Vector3d _lookAtVector;
694 
695  // struct used for path finding
696  struct PathNode {
697  Sector *sect;
698  PathNode *parent;
699  Math::Vector3d pos;
700  float dist;
701  float cost;
702  };
704 
705  CollisionMode _collisionMode;
706  float _collisionScale;
707 
708  bool _puckOrient;
709 
710  static bool _isTalkingBackground;
711  int _talkDelay;
712  int _attachedActor;
713  int _lookAtActor;
714  Common::String _attachedJoint;
715  AlphaMode _alphaMode;
716  float _globalAlpha;
717 
718  bool _inOverworld;
719 
720  int _sortOrder;
721  int _sectorSortOrder;
722  bool _useParentSortOrder;
723 
724  bool _fakeUnbound;
725  bool _drawnToClean;
726 
727  LightMode _lightMode;
728 
730 
731  // Highest vertex used in EMI
732  const static unsigned int MAX_LOCAL_ALPHA_VERTICES = 48;
733  Common::Array<float> _localAlpha;
734  Common::Array<int> _localAlphaMode;
735 };
736 
737 } // end of namespace Grim
738 
739 #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
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
Definition: skeleton.h:43