1 /* Copyright (C) 2022 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
18 #ifndef INCLUDED_MESSAGETYPES
19 #define INCLUDED_MESSAGETYPES
21 #include "simulation2/system/Components.h"
22 #include "simulation2/system/Entity.h"
23 #include "simulation2/system/Message.h"
25 #include "simulation2/helpers/Player.h"
26 #include "simulation2/helpers/Position.h"
28 #include "simulation2/components/ICmpPathfinder.h"
30 #include "maths/Vector3D.h"
34 #define DEFAULT_MESSAGE_IMPL(name) \
35 virtual int GetType() const { return MT_##name; } \
36 virtual const char* GetScriptHandlerName() const { return "On" #name; } \
37 virtual const char* GetScriptGlobalHandlerName() const { return "OnGlobal" #name; } \
38 virtual JS::Value ToJSVal(const ScriptInterface& scriptInterface) const; \
39 static CMessage* FromJSVal(const ScriptInterface&, JS::HandleValue val);
44 class CMessageTurnStart final
: public CMessage
47 DEFAULT_MESSAGE_IMPL(TurnStart
)
54 // The update process is split into a number of phases, in an attempt
55 // to cope with dependencies between components. Each phase is implemented
56 // as a separate message. Simulation2.cpp sends them in sequence.
59 * Generic per-turn update message, for things that don't care much about ordering.
61 class CMessageUpdate final
: public CMessage
64 DEFAULT_MESSAGE_IMPL(Update
)
66 CMessageUpdate(fixed turnLength
) :
67 turnLength(turnLength
)
75 * Update phase for formation controller movement (must happen before individual
76 * units move to follow their formation).
78 class CMessageUpdate_MotionFormation final
: public CMessage
81 DEFAULT_MESSAGE_IMPL(Update_MotionFormation
)
83 CMessageUpdate_MotionFormation(fixed turnLength
) :
84 turnLength(turnLength
)
92 * Update phase for non-formation-controller unit movement.
94 class CMessageUpdate_MotionUnit final
: public CMessage
97 DEFAULT_MESSAGE_IMPL(Update_MotionUnit
)
99 CMessageUpdate_MotionUnit(fixed turnLength
) :
100 turnLength(turnLength
)
108 * Final update phase, after all other updates.
110 class CMessageUpdate_Final final
: public CMessage
113 DEFAULT_MESSAGE_IMPL(Update_Final
)
115 CMessageUpdate_Final(fixed turnLength
) :
116 turnLength(turnLength
)
124 * Prepare for rendering a new frame (set up model positions etc).
126 class CMessageInterpolate final
: public CMessage
129 DEFAULT_MESSAGE_IMPL(Interpolate
)
131 CMessageInterpolate(float deltaSimTime
, float offset
, float deltaRealTime
) :
132 deltaSimTime(deltaSimTime
), offset(offset
), deltaRealTime(deltaRealTime
)
136 /// Elapsed simulation time since previous interpolate, in seconds. This is similar to the elapsed real time, except
137 /// it is scaled by the current simulation rate (and might indeed be zero).
139 /// Range [0, 1] (inclusive); fractional time of current frame between previous/next simulation turns.
141 /// Elapsed real time since previous interpolate, in seconds.
146 * Add renderable objects to the scene collector.
147 * Called after CMessageInterpolate.
149 class CMessageRenderSubmit final
: public CMessage
152 DEFAULT_MESSAGE_IMPL(RenderSubmit
)
154 CMessageRenderSubmit(SceneCollector
& collector
, const CFrustum
& frustum
, bool culling
) :
155 collector(collector
), frustum(frustum
), culling(culling
)
159 SceneCollector
& collector
;
160 const CFrustum
& frustum
;
165 * Handle progressive loading of resources.
166 * A component that listens to this message must do the following:
167 * - Increase *msg.total by the non-zero number of loading tasks this component can perform.
168 * - If *msg.progressed == true, return and do nothing.
169 * - If you've loaded everything, increase *msg.progress by the value you added to .total
170 * - Otherwise do some loading, set *msg.progressed = true, and increase *msg.progress by a
171 * value indicating how much progress you've made in total (0 <= p <= what you added to .total)
172 * In some situations these messages will never be sent - components must ensure they
173 * load all their data themselves before using it in that case.
175 class CMessageProgressiveLoad final
: public CMessage
178 DEFAULT_MESSAGE_IMPL(ProgressiveLoad
)
180 CMessageProgressiveLoad(bool* progressed
, int* total
, int* progress
) :
181 progressed(progressed
), total(total
), progress(progress
)
191 * Broadcast after the entire simulation state has been deserialized.
192 * Components should do all their self-contained work in their Deserialize
193 * function when possible. But any reinitialisation that depends on other
194 * components or other entities, that might not be constructed until later
195 * in the deserialization process, may be done in response to this message
198 class CMessageDeserialized final
: public CMessage
201 DEFAULT_MESSAGE_IMPL(Deserialized
)
203 CMessageDeserialized()
210 * This is sent immediately after a new entity's components have all been created
213 class CMessageCreate final
: public CMessage
216 DEFAULT_MESSAGE_IMPL(Create
)
218 CMessageCreate(entity_id_t entity
) :
227 * This is sent immediately before a destroyed entity is flushed and really destroyed.
228 * (That is, after CComponentManager::DestroyComponentsSoon and inside FlushDestroyedComponents).
229 * The entity will still exist at the time this message is sent.
230 * It's possible for this message to be sent multiple times for one entity, but all its components
231 * will have been deleted after the first time.
233 class CMessageDestroy final
: public CMessage
236 DEFAULT_MESSAGE_IMPL(Destroy
)
238 CMessageDestroy(entity_id_t entity
) :
246 class CMessageOwnershipChanged final
: public CMessage
249 DEFAULT_MESSAGE_IMPL(OwnershipChanged
)
251 CMessageOwnershipChanged(entity_id_t entity
, player_id_t from
, player_id_t to
) :
252 entity(entity
), from(from
), to(to
)
262 * Sent by CCmpPosition whenever anything has changed that will affect the
263 * return value of GetPosition2D() or GetRotation().Y
265 * If @c inWorld is false, then the other fields are invalid and meaningless.
266 * Otherwise they represent the current position.
268 class CMessagePositionChanged final
: public CMessage
271 DEFAULT_MESSAGE_IMPL(PositionChanged
)
273 CMessagePositionChanged(entity_id_t entity
, bool inWorld
, entity_pos_t x
, entity_pos_t z
, entity_angle_t a
) :
274 entity(entity
), inWorld(inWorld
), x(x
), z(z
), a(a
)
285 * Sent by CCmpPosition whenever anything has changed that will affect the
286 * return value of GetInterpolatedTransform()
288 class CMessageInterpolatedPositionChanged final
: public CMessage
291 DEFAULT_MESSAGE_IMPL(InterpolatedPositionChanged
)
293 CMessageInterpolatedPositionChanged(entity_id_t entity
, bool inWorld
, const CVector3D
& pos0
, const CVector3D
& pos1
) :
294 entity(entity
), inWorld(inWorld
), pos0(pos0
), pos1(pos1
)
304 /*Sent whenever the territory type (neutral,own,enemy) differs from the former type*/
305 class CMessageTerritoryPositionChanged final
: public CMessage
308 DEFAULT_MESSAGE_IMPL(TerritoryPositionChanged
)
310 CMessageTerritoryPositionChanged(entity_id_t entity
, player_id_t newTerritory
) :
311 entity(entity
), newTerritory(newTerritory
)
316 player_id_t newTerritory
;
320 * Sent by CCmpUnitMotion during Update if an event happened that might interest other components.
322 class CMessageMotionUpdate final
: public CMessage
325 DEFAULT_MESSAGE_IMPL(MotionUpdate
)
328 LIKELY_SUCCESS
, // UnitMotion considers it is arrived at destination.
329 LIKELY_FAILURE
, // UnitMotion says it cannot reach the destination.
330 OBSTRUCTED
, // UnitMotion was obstructed. This does not mean stuck, but can be a hint to run range checks.
331 VERY_OBSTRUCTED
, // Sent when obstructed several time in a row.
335 static const std::array
<const char*, UpdateType::LENGTH
> UpdateTypeStr
;
337 CMessageMotionUpdate(UpdateType ut
) : updateType(ut
)
341 UpdateType updateType
;
345 * Sent when water height has been changed.
347 class CMessageWaterChanged final
: public CMessage
350 DEFAULT_MESSAGE_IMPL(WaterChanged
)
352 CMessageWaterChanged()
358 * Sent when terrain (texture or elevation) has been changed.
360 class CMessageTerrainChanged final
: public CMessage
363 DEFAULT_MESSAGE_IMPL(TerrainChanged
)
365 CMessageTerrainChanged(int32_t i0
, int32_t j0
, int32_t i1
, int32_t j1
) :
366 i0(i0
), j0(j0
), i1(i1
), j1(j1
)
370 int32_t i0
, j0
, i1
, j1
; // inclusive lower bound, exclusive upper bound, in tiles
374 * Sent, at most once per turn, when the visibility of an entity changed
376 class CMessageVisibilityChanged final
: public CMessage
379 DEFAULT_MESSAGE_IMPL(VisibilityChanged
)
381 CMessageVisibilityChanged(player_id_t player
, entity_id_t ent
, int oldVisibility
, int newVisibility
) :
382 player(player
), ent(ent
), oldVisibility(oldVisibility
), newVisibility(newVisibility
)
393 * Sent when then obstruction of an entity has changed in a manner
394 * that changes 'block movement' properties.
396 class CMessageMovementObstructionChanged final
: public CMessage
399 DEFAULT_MESSAGE_IMPL(MovementObstructionChanged
)
401 CMessageMovementObstructionChanged()
407 * Sent when ObstructionManager's view of the shape of the world has changed
408 * (changing the TILE_OUTOFBOUNDS tiles returned by Rasterise).
410 class CMessageObstructionMapShapeChanged final
: public CMessage
413 DEFAULT_MESSAGE_IMPL(ObstructionMapShapeChanged
)
415 CMessageObstructionMapShapeChanged()
421 * Sent when territory assignments have changed.
423 class CMessageTerritoriesChanged final
: public CMessage
426 DEFAULT_MESSAGE_IMPL(TerritoriesChanged
)
428 CMessageTerritoriesChanged()
434 * Sent by CCmpRangeManager at most once per turn, when an active range query
435 * has had matching units enter/leave the range since the last RangeUpdate.
437 class CMessageRangeUpdate final
: public CMessage
440 DEFAULT_MESSAGE_IMPL(RangeUpdate
)
445 std::vector
<entity_id_t
> added
;
446 std::vector
<entity_id_t
> removed
;
448 // CCmpRangeManager wants to store a vector of messages and wants to
449 // swap vectors instead of copying (to save on memory allocations),
450 // so add some constructors for it:
452 // don't init tag in empty ctor
453 CMessageRangeUpdate()
456 CMessageRangeUpdate(u32 tag
) : tag(tag
)
459 CMessageRangeUpdate(u32 tag
, const std::vector
<entity_id_t
>& added
, const std::vector
<entity_id_t
>& removed
)
460 : tag(tag
), added(added
), removed(removed
)
463 CMessageRangeUpdate(const CMessageRangeUpdate
& other
)
464 : CMessage(), tag(other
.tag
), added(other
.added
), removed(other
.removed
)
467 CMessageRangeUpdate
& operator=(const CMessageRangeUpdate
& other
)
471 removed
= other
.removed
;
477 * Sent by CCmpPathfinder after async path requests.
479 class CMessagePathResult final
: public CMessage
482 DEFAULT_MESSAGE_IMPL(PathResult
)
484 CMessagePathResult(u32 ticket
, const WaypointPath
& path
) :
485 ticket(ticket
), path(path
)
494 * Sent by aura manager when a value of a certain entity's component is changed
496 class CMessageValueModification final
: public CMessage
499 DEFAULT_MESSAGE_IMPL(ValueModification
)
501 CMessageValueModification(const std::vector
<entity_id_t
>& entities
, std::wstring component
, const std::vector
<std::wstring
>& valueNames
) :
503 component(component
),
504 valueNames(valueNames
)
508 std::vector
<entity_id_t
> entities
;
509 std::wstring component
;
510 std::vector
<std::wstring
> valueNames
;
514 * Sent by atlas if the playercolor has been changed.
516 class CMessagePlayerColorChanged final
: public CMessage
519 DEFAULT_MESSAGE_IMPL(PlayerColorChanged
)
521 CMessagePlayerColorChanged(player_id_t player
) :
530 * Sent by aura and tech managers when a value of a certain template's component is changed
532 class CMessageTemplateModification final
: public CMessage
535 DEFAULT_MESSAGE_IMPL(TemplateModification
)
537 CMessageTemplateModification(player_id_t player
, std::wstring component
, const std::vector
<std::wstring
>& valueNames
) :
539 component(component
),
540 valueNames(valueNames
)
545 std::wstring component
;
546 std::vector
<std::wstring
> valueNames
;
550 * Sent by CCmpVision when an entity's vision range changes.
552 class CMessageVisionRangeChanged final
: public CMessage
555 DEFAULT_MESSAGE_IMPL(VisionRangeChanged
)
557 CMessageVisionRangeChanged(entity_id_t entity
, entity_pos_t oldRange
, entity_pos_t newRange
) :
558 entity(entity
), oldRange(oldRange
), newRange(newRange
)
563 entity_pos_t oldRange
;
564 entity_pos_t newRange
;
568 * Sent by CCmpVision when an entity's vision sharing changes.
570 class CMessageVisionSharingChanged final
: public CMessage
573 DEFAULT_MESSAGE_IMPL(VisionSharingChanged
)
575 CMessageVisionSharingChanged(entity_id_t entity
, player_id_t player
, bool add
) :
576 entity(entity
), player(player
), add(add
)
586 * Sent when an entity pings the minimap
588 class CMessageMinimapPing final
: public CMessage
591 DEFAULT_MESSAGE_IMPL(MinimapPing
)
593 CMessageMinimapPing()
602 class CMessageCinemaPathEnded final
: public CMessage
605 DEFAULT_MESSAGE_IMPL(CinemaPathEnded
)
607 CMessageCinemaPathEnded(CStrW name
) :
615 class CMessageCinemaQueueEnded final
: public CMessage
618 DEFAULT_MESSAGE_IMPL(CinemaQueueEnded
)
621 #endif // INCLUDED_MESSAGETYPES