OpenMoHAA 0.82.0
Loading...
Searching...
No Matches
camera.h
1/*
2===========================================================================
3Copyright (C) 2015 the OpenMoHAA team
4
5This file is part of OpenMoHAA source code.
6
7OpenMoHAA source code is free software; you can redistribute it
8and/or modify it under the terms of the GNU General Public License as
9published by the Free Software Foundation; either version 2 of the License,
10or (at your option) any later version.
11
12OpenMoHAA source code is distributed in the hope that it will be
13useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with OpenMoHAA source code; if not, write to the Free Software
19Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20===========================================================================
21*/
22
23// camera.h: Camera. Duh.
24//
25
26#pragma once
27
28#include "g_local.h"
29#include "entity.h"
30#include "bspline.h"
31#include "container.h"
32#include "level.h"
33
34#define CAMERA_SWITCHTIME 0.5f
35
36#define ORBIT (1 << 0)
37#define START_ON (1 << 1)
38#define AUTOMATIC (1 << 2)
39#define NO_TRACE (1 << 3)
40#define NO_WATCH (1 << 4)
41#define LEVEL_EXIT (1 << 5)
42
43extern Event EV_Camera_CameraThink;
44extern Event EV_Camera_StartMoving;
45extern Event EV_Camera_Pause;
46extern Event EV_Camera_Continue;
47extern Event EV_Camera_StopMoving;
48extern Event EV_Camera_SetSpeed;
49extern Event EV_Camera_SetFollowDistance;
50extern Event EV_Camera_SetOrbitHeight;
51extern Event EV_Camera_SetYaw;
52extern Event EV_Camera_AbsoluteYaw;
53extern Event EV_Camera_RelativeYaw;
54extern Event EV_Camera_SetFOV;
55extern Event EV_Camera_Orbit;
56extern Event EV_Camera_Follow;
57extern Event EV_Camera_Watch;
58extern Event EV_Camera_WatchPath;
59extern Event EV_Camera_LookAt;
60extern Event EV_Camera_TurnTo;
61extern Event EV_Camera_MoveToEntity;
62extern Event EV_Camera_MoveToPos;
63extern Event EV_Camera_NoWatch;
64extern Event EV_Camera_FadeTime;
65extern Event EV_Camera_Cut;
66
67class Camera;
68
69class CameraMoveState : public Class
70{
71public:
72 Vector pos;
73 Vector movedir; // direction of travel
74 Vector angles; // angles from spline camera
75
76 BSpline cameraPath;
77 SplinePathPtr splinePath;
78 SplinePathPtr currentNode;
79 SplinePathPtr loopNode;
80
81 float cameraTime;
82 int lastTime;
83 int newTime;
84
85 qboolean followingpath;
86 EntityPtr followEnt;
87 EntityPtr orbitEnt;
88
89 void operator=(CameraMoveState& newstate);
90 void Evaluate(Camera *camera);
91 void Initialize(Camera *camera);
92 void DoNodeEvents(Camera *camera);
93 void Archive(Archiver& arc) override;
94};
95
96inline void CameraMoveState::operator=(CameraMoveState& newstate)
97
98{
99 movedir = newstate.movedir;
100 pos = newstate.pos;
101 angles = newstate.angles;
102
103 cameraPath = newstate.cameraPath;
104 splinePath = newstate.splinePath;
105 currentNode = newstate.currentNode;
106 loopNode = newstate.loopNode;
107
108 cameraTime = newstate.cameraTime;
109 lastTime = newstate.lastTime;
110 newTime = newstate.newTime;
111
112 followEnt = newstate.followEnt;
113 orbitEnt = newstate.orbitEnt;
114
115 followingpath = newstate.followingpath;
116}
117
118inline void CameraMoveState::Archive(Archiver& arc)
119{
120 arc.ArchiveVector(&movedir);
121 arc.ArchiveVector(&pos);
122 arc.ArchiveVector(&angles);
123
124 cameraPath.Archive(arc);
125
126 arc.ArchiveSafePointer(&splinePath);
127 arc.ArchiveSafePointer(&currentNode);
128 arc.ArchiveSafePointer(&loopNode);
129
130 arc.ArchiveFloat(&cameraTime);
131 arc.ArchiveInteger(&lastTime);
132 arc.ArchiveInteger(&newTime);
133
134 arc.ArchiveSafePointer(&followEnt);
135 arc.ArchiveSafePointer(&orbitEnt);
136
137 arc.ArchiveBoolean(&followingpath);
138}
139
140class CameraWatchState : public Class
141{
142public:
143 Vector watchAngles;
144
145 EntityPtr watchEnt;
146 qboolean watchNodes;
147 qboolean watchPath;
148
149 void Evaluate(Camera *camera, CameraMoveState *move);
150 void Initialize(Camera *camera);
151 void Archive(Archiver& arc) override;
152};
153
154inline void CameraWatchState::Archive(Archiver& arc)
155{
156 arc.ArchiveVector(&watchAngles);
157 arc.ArchiveSafePointer(&watchEnt);
158 arc.ArchiveBoolean(&watchNodes);
159 arc.ArchiveBoolean(&watchPath);
160}
161
162class CameraState : public Class
163{
164public:
165 CameraMoveState move;
166 CameraWatchState watch;
167 float fov;
168
169 void Evaluate(Camera *camera);
170 void Initialize(Camera *camera);
171 void Archive(Archiver& arc) override;
172};
173
174inline void CameraState::Archive(Archiver& arc)
175{
176 move.Archive(arc);
177 watch.Archive(arc);
178 arc.ArchiveFloat(&fov);
179}
180
181class Camera : public Entity
182{
183private:
184 friend class CameraState;
185 friend class CameraWatchState;
186 friend class CameraMoveState;
187 //
188 // follow parameters
189 //
190 float follow_yaw;
191 qboolean follow_yaw_fixed;
192 float follow_dist;
193 int follow_mask;
194
195 // camera speed
196 float camera_speed;
197 // current camera fov
198 float camera_fov;
199 // orbit height
200 float orbit_height;
201 // orbit_dotrace
202 qboolean orbit_dotrace;
203 // whether or not auto calculate fov, a non-zero value means yes
204 float auto_fov;
205
206 // automatic variables
207 float automatic_startTime;
208 float automatic_stopTime;
209 float automatic_radius;
210 float automatic_maxFOV;
211 qboolean automatic_active;
212 Container<str> automatic_states;
213
214 // Miscellaneous
215 Vector m_vPosOffset;
216
217protected:
218 CameraState currentstate;
219 CameraState newstate;
220
221 float watchTime; // if non-zero, camera view is transitioning
222 float followTime; // if non-zero, camera position is tranisitioning
223 float fovTime; // if non-zero, fov is being lerped
224
225 float fadeTime; // time to transition over
226 float fovFadeTime; // time for fov transition
227 float followFadeTime; // time for fov transition
228 float watchFadeTime; // time for fov transition
229
230 bool m_bShowquakes;
231
232 str nextCamera;
233
234 qboolean showcamera;
235
236 void SetupCamera(Event *ev);
237 void CameraThink(Event *ev);
238 void CreateOrbit(Vector pos, float radius, Vector &forward, Vector &left);
239 void CreatePath(SplinePath *path, splinetype_t type);
240 void UpdateStates(void);
241 Vector CalculatePosition(void);
242 Vector CalculateOrientation(void);
243 float CalculateFov(void);
244 virtual void bind(Entity *master, qboolean use_my_angles = false) override;
245 void unbind(void) override;
246
247public:
248 CLASS_PROTOTYPE(Camera);
249
250 Camera();
251 void Stop(void);
252 void FollowPath(SplinePath *path, qboolean loop, Entity *watch);
253 void Orbit(Entity *ent, float dist, Entity *watch, float yaw_offset = 0, qboolean dotrace = qtrue);
254 void FollowEntity(Entity *ent, float dist, int mask, Entity *watch = NULL);
255 void Watch(str watch, float time);
256 void Watch(Entity *ent, float time);
257 void SetFOV(float fov, float time);
258
259 void StartMoving(Event *ev);
260 void StopMoving(Event *ev);
261 void Pause(Event *ev);
262 void Continue(Event *ev);
263 void SetAnglesEvent(Event *ev);
264 void SetSpeed(Event *ev);
265 void SetFollowDistance(Event *ev);
266 void SetOrbitHeight(float height);
267 void SetOrbitHeight(Event *ev);
268 void SetFollowYaw(Event *ev);
269 void AbsoluteYaw(Event *ev);
270 void RelativeYaw(Event *ev);
271 void SetFOV(Event *ev);
272 void OrbitEvent(Event *ev);
273 void FollowEvent(Event *ev);
274 void WatchEvent(Event *ev);
275 void WatchStringEvent(Event *ev);
276 void WatchPathEvent(Event *ev);
277 void WatchNodesEvent(Event *ev);
278 void NoWatchEvent(Event *ev);
279 void EventShowQuakes(Event *ev);
280 void LookAt(Event *ev);
281 void MoveToEntity(Event *ev);
282 void MoveToPos(Event *ev);
283 void Cut(Event *ev);
284 void FadeTime(Event *ev);
285 void TurnTo(Event *ev);
286 void SetNextCamera(Event *ev);
287 float CalculateScore(Entity *player, str state);
288 float AutomaticStart(Entity *player);
289 float AutomaticStop(Entity *player);
290 qboolean IsAutomatic(void);
291 qboolean IsLevelExit(void);
292 void SetAutoStateEvent(Event *ev);
293 void SetAutoRadiusEvent(Event *ev);
294 void SetAutoStartTimeEvent(Event *ev);
295 void SetAutoStopTimeEvent(Event *ev);
296 void SetMaximumAutoFOVEvent(Event *ev);
297 void SetAutoActiveEvent(Event *ev);
298
299 str & NextCamera(void);
300 float Fov(void);
301
302 void Reset(Vector org, Vector ang);
303
304 bool ShowQuakes(void) const;
305 Vector GetPositionOffset(void);
306 void SetPositionOffset(Vector vNewOfs);
307
308 void Archive(Archiver& arc) override;
309};
310
311inline bool Camera::ShowQuakes(void) const
312{
313 return m_bShowquakes;
314}
315
316inline Vector Camera::GetPositionOffset(void)
317{
318 return m_vPosOffset;
319}
320
321inline void Camera::SetPositionOffset(Vector vNewOfs)
322{
323 m_vPosOffset = vNewOfs;
324}
325
326inline void Camera::Archive(Archiver& arc)
327{
328 Entity::Archive(arc);
329
330 arc.ArchiveFloat(&follow_yaw);
331 arc.ArchiveBoolean(&follow_yaw_fixed);
332 arc.ArchiveFloat(&follow_dist);
333 arc.ArchiveInteger(&follow_mask);
334
335 arc.ArchiveFloat(&camera_speed);
336 arc.ArchiveFloat(&camera_fov);
337 arc.ArchiveFloat(&orbit_height);
338 arc.ArchiveBoolean(&orbit_dotrace);
339 arc.ArchiveFloat(&auto_fov);
340
341 arc.ArchiveFloat(&automatic_startTime);
342 arc.ArchiveFloat(&automatic_stopTime);
343 arc.ArchiveFloat(&automatic_radius);
344 arc.ArchiveFloat(&automatic_maxFOV);
345 arc.ArchiveBoolean(&automatic_active);
346
347 automatic_states.Archive(arc);
348
349 // currentstate
350 currentstate.Archive(arc);
351 // newstate
352 newstate.Archive(arc);
353
354 arc.ArchiveFloat(&watchTime);
355 arc.ArchiveFloat(&followTime);
356 arc.ArchiveFloat(&fovTime);
357 arc.ArchiveFloat(&fadeTime);
358 arc.ArchiveFloat(&fovFadeTime);
359 arc.ArchiveFloat(&followFadeTime);
360 arc.ArchiveFloat(&watchFadeTime);
361
362 arc.ArchiveString(&nextCamera);
363
364 arc.ArchiveBoolean(&showcamera);
365
366 if (arc.Loading()) {
367 if (spawnflags & AUTOMATIC) {
368 level.AddAutomaticCamera(this);
369 }
370 }
371}
372
373void SetCamera(Entity *ent, float switchTime);
374Entity *GetWatchEntity(str watch);
375
376typedef SafePtr<Camera> CameraPtr;
377
378class CameraManager : public Listener
379{
380protected:
381 Container<str> pathList;
382 BSpline cameraPath;
383 SplinePathPtr path;
384 SplinePathPtr current;
385 float speed;
386 int watch;
387 str pathName;
388 CameraPtr cam;
389 qboolean cameraPath_dirty;
390
391 void NewPath(Event *ev);
392 void SetPath(Event *ev);
393 void SetTargetName(Event *ev);
394 void SetTarget(Event *ev);
395 void AddPoint(Event *ev);
396 void ReplacePoint(Event *ev);
397 void DeletePoint(Event *ev);
398 void MovePlayer(Event *ev);
399 void NextPoint(Event *ev);
400 void PreviousPoint(Event *ev);
401 void ShowingPath(Event *ev);
402 void ShowPath(Event *ev);
403 void HidePath(Event *ev);
404 void StopPlayback(Event *ev);
405 void PlayPath(Event *ev);
406 void LoopPath(Event *ev);
407 void Watch(Event *ev);
408 void NoWatch(Event *ev);
409 void Fov(Event *ev);
410 void FadeTime(Event *ev);
411 void Speed(Event *ev);
412 void Save(Event *ev);
413 void Load(Event *ev);
414 void SaveMap(Event *ev);
415 void UpdateEvent(Event *ev);
416 void NextPath(Event *ev);
417 void PreviousPath(Event *ev);
418 void RenamePath(Event *ev);
419
420 void ShowPath(void);
421 void UpdateUI(void);
422 void SetPathName(str name);
423 void SavePath(str pathName);
424
425public:
426 CLASS_PROTOTYPE(CameraManager);
427
428 CameraManager();
429 void SetPath(str pathName);
430 void Archive(Archiver& arc) override;
431};
432
433inline void CameraManager::Archive(Archiver& arc)
434
435{
436 Listener::Archive(arc);
437
438 pathList.Archive(arc);
439
440 // no need to archive the cameraPath
441 arc.ArchiveSafePointer(&path);
442 arc.ArchiveSafePointer(&current);
443 arc.ArchiveFloat(&speed);
444 arc.ArchiveInteger(&watch);
445 arc.ArchiveString(&pathName);
446 arc.ArchiveSafePointer(&cam);
447
448 // make sure the cameraPath gets rebuilt
449 cameraPath_dirty = qtrue;
450}
451
452extern CameraManager CameraMan;
Definition archive.h:86
Definition bspline.h:237
Definition camera.h:379
Definition camera.h:70
Definition camera.h:163
Definition camera.h:141
Definition camera.h:182
Definition container.h:85
Definition entity.h:203
Definition listener.h:246
Definition safeptr.h:160
Definition bspline.h:492
Definition vector.h:61
Definition str.h:77
Definition puff.c:88