Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
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.
8 : *
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.
13 : *
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/>.
16 : */
17 :
18 : #ifndef INCLUDED_MESSAGETYPES
19 : #define INCLUDED_MESSAGETYPES
20 :
21 : #include "maths/Fixed.h"
22 : #include "maths/Vector3D.h"
23 : #include "ps/CStr.h"
24 : #include "simulation2/helpers/Pathfinding.h"
25 : #include "simulation2/helpers/Player.h"
26 : #include "simulation2/helpers/Position.h"
27 : #include "simulation2/system/Component.h"
28 : #include "simulation2/system/Entity.h"
29 : #include "simulation2/system/Message.h"
30 :
31 : #include <array>
32 : #include <cstdint>
33 : #include <js/TypeDecls.h>
34 : #include <string>
35 : #include <vector>
36 :
37 : namespace Script { class Request; }
38 :
39 : #define DEFAULT_MESSAGE_IMPL(name) \
40 : virtual int GetType() const { return MT_##name; } \
41 : virtual const char* GetScriptHandlerName() const { return "On" #name; } \
42 : virtual const char* GetScriptGlobalHandlerName() const { return "OnGlobal" #name; } \
43 : virtual JS::Value ToJSVal(const Script::Request& rq) const; \
44 : static CMessage* FromJSVal(const Script::Request&, JS::HandleValue val);
45 :
46 : class SceneCollector;
47 : class CFrustum;
48 :
49 185 : class CMessageTurnStart final : public CMessage
50 : {
51 : public:
52 8943 : DEFAULT_MESSAGE_IMPL(TurnStart)
53 :
54 184 : CMessageTurnStart()
55 184 : {
56 : }
57 : };
58 :
59 : // The update process is split into a number of phases, in an attempt
60 : // to cope with dependencies between components. Each phase is implemented
61 : // as a separate message. Simulation2.cpp sends them in sequence.
62 :
63 : /**
64 : * Generic per-turn update message, for things that don't care much about ordering.
65 : */
66 370 : class CMessageUpdate final : public CMessage
67 : {
68 : public:
69 1917 : DEFAULT_MESSAGE_IMPL(Update)
70 :
71 192 : CMessageUpdate(fixed turnLength) :
72 192 : turnLength(turnLength)
73 : {
74 : }
75 :
76 : fixed turnLength;
77 : };
78 :
79 : /**
80 : * Update phase for formation controller movement (must happen before individual
81 : * units move to follow their formation).
82 : */
83 180 : class CMessageUpdate_MotionFormation final : public CMessage
84 : {
85 : public:
86 720 : DEFAULT_MESSAGE_IMPL(Update_MotionFormation)
87 :
88 180 : CMessageUpdate_MotionFormation(fixed turnLength) :
89 180 : turnLength(turnLength)
90 : {
91 : }
92 :
93 : fixed turnLength;
94 : };
95 :
96 : /**
97 : * Update phase for non-formation-controller unit movement.
98 : */
99 180 : class CMessageUpdate_MotionUnit final : public CMessage
100 : {
101 : public:
102 720 : DEFAULT_MESSAGE_IMPL(Update_MotionUnit)
103 :
104 180 : CMessageUpdate_MotionUnit(fixed turnLength) :
105 180 : turnLength(turnLength)
106 : {
107 : }
108 :
109 : fixed turnLength;
110 : };
111 :
112 : /**
113 : * Final update phase, after all other updates.
114 : */
115 180 : class CMessageUpdate_Final final : public CMessage
116 : {
117 : public:
118 720 : DEFAULT_MESSAGE_IMPL(Update_Final)
119 :
120 180 : CMessageUpdate_Final(fixed turnLength) :
121 180 : turnLength(turnLength)
122 : {
123 : }
124 :
125 : fixed turnLength;
126 : };
127 :
128 : /**
129 : * Prepare for rendering a new frame (set up model positions etc).
130 : */
131 5 : class CMessageInterpolate final : public CMessage
132 : {
133 : public:
134 38 : DEFAULT_MESSAGE_IMPL(Interpolate)
135 :
136 5 : CMessageInterpolate(float deltaSimTime, float offset, float deltaRealTime) :
137 5 : deltaSimTime(deltaSimTime), offset(offset), deltaRealTime(deltaRealTime)
138 : {
139 : }
140 :
141 : /// Elapsed simulation time since previous interpolate, in seconds. This is similar to the elapsed real time, except
142 : /// it is scaled by the current simulation rate (and might indeed be zero).
143 : float deltaSimTime;
144 : /// Range [0, 1] (inclusive); fractional time of current frame between previous/next simulation turns.
145 : float offset;
146 : /// Elapsed real time since previous interpolate, in seconds.
147 : float deltaRealTime;
148 : };
149 :
150 : /**
151 : * Add renderable objects to the scene collector.
152 : * Called after CMessageInterpolate.
153 : */
154 0 : class CMessageRenderSubmit final : public CMessage
155 : {
156 : public:
157 0 : DEFAULT_MESSAGE_IMPL(RenderSubmit)
158 :
159 0 : CMessageRenderSubmit(SceneCollector& collector, const CFrustum& frustum, bool culling) :
160 0 : collector(collector), frustum(frustum), culling(culling)
161 : {
162 : }
163 :
164 : SceneCollector& collector;
165 : const CFrustum& frustum;
166 : bool culling;
167 : };
168 :
169 : /**
170 : * Handle progressive loading of resources.
171 : * A component that listens to this message must do the following:
172 : * - Increase *msg.total by the non-zero number of loading tasks this component can perform.
173 : * - If *msg.progressed == true, return and do nothing.
174 : * - If you've loaded everything, increase *msg.progress by the value you added to .total
175 : * - Otherwise do some loading, set *msg.progressed = true, and increase *msg.progress by a
176 : * value indicating how much progress you've made in total (0 <= p <= what you added to .total)
177 : * In some situations these messages will never be sent - components must ensure they
178 : * load all their data themselves before using it in that case.
179 : */
180 : class CMessageProgressiveLoad final : public CMessage
181 : {
182 : public:
183 9 : DEFAULT_MESSAGE_IMPL(ProgressiveLoad)
184 :
185 : CMessageProgressiveLoad(bool* progressed, int* total, int* progress) :
186 : progressed(progressed), total(total), progress(progress)
187 : {
188 : }
189 :
190 : bool* progressed;
191 : int* total;
192 : int* progress;
193 : };
194 :
195 : /**
196 : * Broadcast after the entire simulation state has been deserialized.
197 : * Components should do all their self-contained work in their Deserialize
198 : * function when possible. But any reinitialisation that depends on other
199 : * components or other entities, that might not be constructed until later
200 : * in the deserialization process, may be done in response to this message
201 : * instead.
202 : */
203 3 : class CMessageDeserialized final : public CMessage
204 : {
205 : public:
206 9 : DEFAULT_MESSAGE_IMPL(Deserialized)
207 :
208 3 : CMessageDeserialized()
209 3 : {
210 : }
211 : };
212 :
213 :
214 : /**
215 : * This is sent immediately after a new entity's components have all been created
216 : * and initialised.
217 : */
218 396 : class CMessageCreate final : public CMessage
219 : {
220 : public:
221 1417 : DEFAULT_MESSAGE_IMPL(Create)
222 :
223 219 : CMessageCreate(entity_id_t entity) :
224 219 : entity(entity)
225 : {
226 : }
227 :
228 : entity_id_t entity;
229 : };
230 :
231 : /**
232 : * This is sent immediately before a destroyed entity is flushed and really destroyed.
233 : * (That is, after CComponentManager::DestroyComponentsSoon and inside FlushDestroyedComponents).
234 : * The entity will still exist at the time this message is sent.
235 : * It's possible for this message to be sent multiple times for one entity, but all its components
236 : * will have been deleted after the first time.
237 : */
238 64 : class CMessageDestroy final : public CMessage
239 : {
240 : public:
241 319 : DEFAULT_MESSAGE_IMPL(Destroy)
242 :
243 34 : CMessageDestroy(entity_id_t entity) :
244 34 : entity(entity)
245 : {
246 : }
247 :
248 : entity_id_t entity;
249 : };
250 :
251 452 : class CMessageOwnershipChanged final : public CMessage
252 : {
253 : public:
254 6997 : DEFAULT_MESSAGE_IMPL(OwnershipChanged)
255 :
256 238 : CMessageOwnershipChanged(entity_id_t entity, player_id_t from, player_id_t to) :
257 238 : entity(entity), from(from), to(to)
258 : {
259 : }
260 :
261 : entity_id_t entity;
262 : player_id_t from;
263 : player_id_t to;
264 : };
265 :
266 : /**
267 : * Sent by CCmpPosition whenever anything has changed that will affect the
268 : * return value of GetPosition2D() or GetRotation().Y
269 : *
270 : * If @c inWorld is false, then the other fields are invalid and meaningless.
271 : * Otherwise they represent the current position.
272 : */
273 16919 : class CMessagePositionChanged final : public CMessage
274 : {
275 : public:
276 72496 : DEFAULT_MESSAGE_IMPL(PositionChanged)
277 :
278 8996 : CMessagePositionChanged(entity_id_t entity, bool inWorld, entity_pos_t x, entity_pos_t z, entity_angle_t a) :
279 8996 : entity(entity), inWorld(inWorld), x(x), z(z), a(a)
280 : {
281 : }
282 :
283 : entity_id_t entity;
284 : bool inWorld;
285 : entity_pos_t x, z;
286 : entity_angle_t a;
287 : };
288 :
289 : /**
290 : * Sent by CCmpPosition whenever anything has changed that will affect the
291 : * return value of GetInterpolatedTransform()
292 : */
293 7165 : class CMessageInterpolatedPositionChanged final : public CMessage
294 : {
295 : public:
296 28638 : DEFAULT_MESSAGE_IMPL(InterpolatedPositionChanged)
297 :
298 7165 : CMessageInterpolatedPositionChanged(entity_id_t entity, bool inWorld, const CVector3D& pos0, const CVector3D& pos1) :
299 7165 : entity(entity), inWorld(inWorld), pos0(pos0), pos1(pos1)
300 : {
301 : }
302 :
303 : entity_id_t entity;
304 : bool inWorld;
305 : CVector3D pos0;
306 : CVector3D pos1;
307 : };
308 :
309 : /**
310 : * Sent by CCmpUnitMotion during Update if an event happened that might interest other components.
311 : */
312 984 : class CMessageMotionUpdate final : public CMessage
313 : {
314 : public:
315 1968 : DEFAULT_MESSAGE_IMPL(MotionUpdate)
316 :
317 : enum UpdateType {
318 : LIKELY_SUCCESS, // UnitMotion considers it is arrived at destination.
319 : LIKELY_FAILURE, // UnitMotion says it cannot reach the destination.
320 : OBSTRUCTED, // UnitMotion was obstructed. This does not mean stuck, but can be a hint to run range checks.
321 : VERY_OBSTRUCTED, // Sent when obstructed several time in a row.
322 : LENGTH
323 : };
324 :
325 : static const std::array<const char*, UpdateType::LENGTH> UpdateTypeStr;
326 :
327 492 : CMessageMotionUpdate(UpdateType ut) : updateType(ut)
328 : {
329 : }
330 :
331 : UpdateType updateType;
332 : };
333 :
334 : /**
335 : * Sent when water height has been changed.
336 : */
337 3 : class CMessageWaterChanged final : public CMessage
338 : {
339 : public:
340 15 : DEFAULT_MESSAGE_IMPL(WaterChanged)
341 :
342 3 : CMessageWaterChanged()
343 3 : {
344 : }
345 : };
346 :
347 : /**
348 : * Sent when terrain (texture or elevation) has been changed.
349 : */
350 4 : class CMessageTerrainChanged final : public CMessage
351 : {
352 : public:
353 22 : DEFAULT_MESSAGE_IMPL(TerrainChanged)
354 :
355 4 : CMessageTerrainChanged(int32_t i0, int32_t j0, int32_t i1, int32_t j1) :
356 4 : i0(i0), j0(j0), i1(i1), j1(j1)
357 : {
358 : }
359 :
360 : int32_t i0, j0, i1, j1; // inclusive lower bound, exclusive upper bound, in tiles
361 : };
362 :
363 : /**
364 : * Sent, at most once per turn, when the visibility of an entity changed
365 : */
366 5426 : class CMessageVisibilityChanged final : public CMessage
367 : {
368 : public:
369 21696 : DEFAULT_MESSAGE_IMPL(VisibilityChanged)
370 :
371 5426 : CMessageVisibilityChanged(player_id_t player, entity_id_t ent, int oldVisibility, int newVisibility) :
372 5426 : player(player), ent(ent), oldVisibility(oldVisibility), newVisibility(newVisibility)
373 : {
374 : }
375 :
376 : player_id_t player;
377 : entity_id_t ent;
378 : int oldVisibility;
379 : int newVisibility;
380 : };
381 :
382 : /**
383 : * Sent when then obstruction of an entity has changed in a manner
384 : * that changes 'block movement' properties.
385 : */
386 0 : class CMessageMovementObstructionChanged final : public CMessage
387 : {
388 : public:
389 0 : DEFAULT_MESSAGE_IMPL(MovementObstructionChanged)
390 :
391 0 : CMessageMovementObstructionChanged()
392 0 : {
393 : }
394 : };
395 :
396 : /**
397 : * Sent when ObstructionManager's view of the shape of the world has changed
398 : * (changing the TILE_OUTOFBOUNDS tiles returned by Rasterise).
399 : */
400 3 : class CMessageObstructionMapShapeChanged final : public CMessage
401 : {
402 : public:
403 15 : DEFAULT_MESSAGE_IMPL(ObstructionMapShapeChanged)
404 :
405 3 : CMessageObstructionMapShapeChanged()
406 3 : {
407 : }
408 : };
409 :
410 : /**
411 : * Sent when territory assignments have changed.
412 : */
413 6 : class CMessageTerritoriesChanged final : public CMessage
414 : {
415 : public:
416 18 : DEFAULT_MESSAGE_IMPL(TerritoriesChanged)
417 :
418 3 : CMessageTerritoriesChanged()
419 3 : {
420 : }
421 : };
422 :
423 : /**
424 : * Sent by CCmpRangeManager at most once per turn, when an active range query
425 : * has had matching units enter/leave the range since the last RangeUpdate.
426 : */
427 : class CMessageRangeUpdate final : public CMessage
428 : {
429 : public:
430 9 : DEFAULT_MESSAGE_IMPL(RangeUpdate)
431 :
432 :
433 :
434 : std::uint32_t tag;
435 : std::vector<entity_id_t> added;
436 : std::vector<entity_id_t> removed;
437 :
438 : // CCmpRangeManager wants to store a vector of messages and wants to
439 : // swap vectors instead of copying (to save on memory allocations),
440 : // so add some constructors for it:
441 :
442 : // don't init tag in empty ctor
443 : CMessageRangeUpdate()
444 : {
445 : }
446 : CMessageRangeUpdate(std::uint32_t tag) : tag(tag)
447 : {
448 : }
449 3 : CMessageRangeUpdate(std::uint32_t tag, const std::vector<entity_id_t>& added, const std::vector<entity_id_t>& removed)
450 3 : : tag(tag), added(added), removed(removed)
451 : {
452 3 : }
453 3 : CMessageRangeUpdate(const CMessageRangeUpdate& other)
454 3 : : CMessage(), tag(other.tag), added(other.added), removed(other.removed)
455 : {
456 3 : }
457 : CMessageRangeUpdate& operator=(const CMessageRangeUpdate& other)
458 : {
459 : tag = other.tag;
460 : added = other.added;
461 : removed = other.removed;
462 : return *this;
463 : }
464 : };
465 :
466 : /**
467 : * Sent by CCmpPathfinder after async path requests.
468 : */
469 : class CMessagePathResult final : public CMessage
470 : {
471 : public:
472 3804 : DEFAULT_MESSAGE_IMPL(PathResult)
473 :
474 951 : CMessagePathResult(std::uint32_t ticket, const WaypointPath& path) :
475 951 : ticket(ticket), path(path)
476 : {
477 951 : }
478 :
479 : std::uint32_t ticket;
480 : WaypointPath path;
481 : };
482 :
483 : /**
484 : * Sent by aura manager when a value of a certain entity's component is changed
485 : */
486 : class CMessageValueModification final : public CMessage
487 : {
488 : public:
489 5616 : DEFAULT_MESSAGE_IMPL(ValueModification)
490 :
491 432 : CMessageValueModification(const std::vector<entity_id_t>& entities, std::wstring component, const std::vector<std::wstring>& valueNames) :
492 432 : entities(entities),
493 432 : component(component),
494 864 : valueNames(valueNames)
495 : {
496 432 : }
497 :
498 : std::vector<entity_id_t> entities;
499 : std::wstring component;
500 : std::vector<std::wstring> valueNames;
501 : };
502 :
503 : /**
504 : * Sent by atlas if the playercolor has been changed.
505 : */
506 : class CMessagePlayerColorChanged final : public CMessage
507 : {
508 : public:
509 0 : DEFAULT_MESSAGE_IMPL(PlayerColorChanged)
510 :
511 0 : CMessagePlayerColorChanged(player_id_t player) :
512 0 : player(player)
513 : {
514 : }
515 :
516 : player_id_t player;
517 : };
518 :
519 : /**
520 : * Sent by the Player component when a specific player has won.
521 : */
522 : class CMessagePlayerWon final : public CMessage
523 : {
524 : public:
525 0 : DEFAULT_MESSAGE_IMPL(PlayerWon)
526 :
527 0 : CMessagePlayerWon(player_id_t playerId) :
528 0 : playerId{playerId}
529 : {
530 : }
531 :
532 : player_id_t playerId;
533 : };
534 :
535 : /**
536 : * Sent by the Player component when an specific player has been defeated.
537 : */
538 : class CMessagePlayerDefeated final : public CMessage
539 : {
540 : public:
541 0 : DEFAULT_MESSAGE_IMPL(PlayerDefeated)
542 :
543 0 : CMessagePlayerDefeated(player_id_t playerId) :
544 0 : playerId{playerId}
545 : {
546 : }
547 :
548 : player_id_t playerId;
549 : };
550 :
551 : /**
552 : * Sent by aura and tech managers when a value of a certain template's component is changed
553 : */
554 : class CMessageTemplateModification final : public CMessage
555 : {
556 : public:
557 1080 : DEFAULT_MESSAGE_IMPL(TemplateModification)
558 :
559 216 : CMessageTemplateModification(player_id_t player, std::wstring component, const std::vector<std::wstring>& valueNames) :
560 216 : player(player),
561 216 : component(component),
562 432 : valueNames(valueNames)
563 : {
564 216 : }
565 :
566 : player_id_t player;
567 : std::wstring component;
568 : std::vector<std::wstring> valueNames;
569 : };
570 :
571 : /**
572 : * Sent by CCmpVision when an entity's vision range changes.
573 : */
574 0 : class CMessageVisionRangeChanged final : public CMessage
575 : {
576 : public:
577 0 : DEFAULT_MESSAGE_IMPL(VisionRangeChanged)
578 :
579 0 : CMessageVisionRangeChanged(entity_id_t entity, entity_pos_t oldRange, entity_pos_t newRange) :
580 0 : entity(entity), oldRange(oldRange), newRange(newRange)
581 : {
582 : }
583 :
584 : entity_id_t entity;
585 : entity_pos_t oldRange;
586 : entity_pos_t newRange;
587 : };
588 :
589 : /**
590 : * Sent by CCmpVision when an entity's vision sharing changes.
591 : */
592 : class CMessageVisionSharingChanged final : public CMessage
593 : {
594 : public:
595 0 : DEFAULT_MESSAGE_IMPL(VisionSharingChanged)
596 :
597 0 : CMessageVisionSharingChanged(entity_id_t entity, player_id_t player, bool add) :
598 0 : entity(entity), player(player), add(add)
599 : {
600 : }
601 :
602 : entity_id_t entity;
603 : player_id_t player;
604 : bool add;
605 : };
606 :
607 : /**
608 : * Sent when an entity pings the minimap
609 : */
610 : class CMessageMinimapPing final : public CMessage
611 : {
612 : public:
613 756 : DEFAULT_MESSAGE_IMPL(MinimapPing)
614 :
615 189 : CMessageMinimapPing()
616 189 : {
617 : }
618 : };
619 :
620 : /**
621 : * Cinematics events
622 : */
623 :
624 : class CMessageCinemaPathEnded final : public CMessage
625 : {
626 : public:
627 18 : DEFAULT_MESSAGE_IMPL(CinemaPathEnded)
628 :
629 5 : CMessageCinemaPathEnded(CStrW name) :
630 5 : name(name)
631 : {
632 5 : }
633 :
634 : CStrW name;
635 : };
636 :
637 8 : class CMessageCinemaQueueEnded final : public CMessage
638 : {
639 : public:
640 18 : DEFAULT_MESSAGE_IMPL(CinemaQueueEnded)
641 : };
642 :
643 : #endif // INCLUDED_MESSAGETYPES
|