File: unit.h

package info (click to toggle)
boswars 2.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 96,652 kB
  • sloc: cpp: 57,250; python: 1,715; sh: 25; makefile: 17
file content (1032 lines) | stat: -rw-r--r-- 35,695 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
//     ____                _       __               
//    / __ )____  _____   | |     / /___ ___________
//   / __  / __ \/ ___/   | | /| / / __ `/ ___/ ___/
//  / /_/ / /_/ (__  )    | |/ |/ / /_/ / /  (__  ) 
// /_____/\____/____/     |__/|__/\__,_/_/  /____/  
//                                              
//       A futuristic real-time strategy game.
//          This file is part of Bos Wars.
//
/**@name unit.h - The unit headerfile. */
//
//      (c) Copyright 1998-2008 by Lutz Sammer and Jimmy Salmon
//
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; only version 2 of the License.
//
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
//      02111-1307, USA.

#ifndef __UNIT_H__
#define __UNIT_H__

//@{

/*----------------------------------------------------------------------------
-- Documentation
----------------------------------------------------------------------------*/

/**
**  @class CUnit unit.h
**
**  \#include "unit.h"
**
**  Everything belonging to a unit. FIXME: rearrange for less memory.
**
**  This class contains all information about a unit in game.
**  A unit could be anything: a man, a vehicle, a ship, or a building.
**  Currently only a tile, a unit, or a missile could be placed on the map.
**
**  The unit structure members:
**
**  CUnit::Refs
**
**  The reference counter of the unit. If the pointer to the unit
**  is stored the counter must be incremented and if this reference
**  is destroyed the counter must be decremented. Alternative it
**  would be possible to implement a garbage collector for this.
**
**  CUnit::Slot
**
**  This is the unique slot number. It is not possible that two
**  units have the same slot number at the same time. The slot
**  numbers are reused.
**  This field could be accessed by the macro UnitNumber(Unit *).
**  Maximal 65535 (=#MAX_UNIT_SLOTS) simultaneous units are
**  supported.
**
**  CUnit::UnitSlot
**
**  This is the pointer into #Units[], where the unit pointer is
**  stored.  #Units[] is a table of all units currently active in
**  game. This pointer is only needed to speed up, the remove of
**  the unit pointer from #Units[], it didn't must be searched in
**  the table.
**
**  CUnit::PlayerSlot
**
**  A pointer into Player::Units[], where the unit pointer is
**  stored. Player::Units[] is a table of all units currently
**  belonging to a player. This pointer is only needed to speed
**  up, the remove of the unit pointer from Player::Units[].
**
**  CUnit::Next
**
**  A generic link pointer. This member is currently used, if an
**  unit is on the map, to link all units on the same map field
**  together. This also links corpses and stuff. Also, this is
**  used in memory management to link unused units.
**
**  CUnit::Container
**
**  Pointer to the unit containing it, or NoUnitP if the unit is
**  free. This points to the transporter for units on board, or to
**  the building for peasants inside(when they are mining).
**
**  CUnit::UnitInside
**
**  Pointer to the last unit added inside. Order doesn't really
**  matter. All units inside are kept in a circular linked list.
**  This is NoUnitP if there are no units inside. Multiple levels
**  of inclusion are allowed, though not very usefull right now
**
**  CUnit::NextContained, CUnit::PrevContained
**
**  The next and previous element in the curent container. Bogus
**  values allowed for units not contained.
**
**  CUnit::InsideCount
**
**  The number of units inside the container.
**
**  CUnit::BoardCount
**
**  The number of units transported inside the container. This
**  does not include for instance stuff like harvesters returning
**  cargo.
**
**  CUnit::X CUnit::Y
**
**  The tile map coordinates of the unit. 0,0 is the upper left on
**  the map. To convert the map coordinates into pixels, they
**  must be multiplicated with the #TileSizeX and #TileSizeY.
**  To get the pixel coordinates of a unit, calculate
**  CUnit::X*#TileSizeX+CUnit::IX , CUnit::Y*#TileSizeY+CUnit::IY.
**
**  CUnit::Type
**
**  Pointer to the unit-type (::UnitType). The unit-type contains
**  all informations that all units of the same type shares.
**  (Animations, Name, Stats, ...)
**
**  CUnit::SeenType
**  Pointer to the unit-type that this unit was, when last seen.
**  Currently only used by buildings.
**
**  CUnit::Player
**
**  Pointer to the owner of this unit (::Player). An unit could
**  only be owned by one player.
**
**  CUnit::Stats
**
**  Pointer to the current status (::UnitStats) of a unit. The
**  units of the same player and the same type could share the same
**  stats. The status contains all values which could be different
**  for each player. This f.e. the upgradeable abilities of an
**  unit.  (CUnit::Stats::SightRange, CUnit::Stats::Armor,
**  CUnit::Stats::HitPoints, ...)
**
**  CUnit::CurrentSightRange
**
**  Current sight range of a unit, this changes when a unit enters
**  a transporter or building or exits one of these.
**
**  CUnit::Colors
**
**  Player colors of the unit. Contains the hardware dependent
**  pixel values for the player colors (palette index #208-#211).
**  Setup from the global palette. This is a pointer.
**  @note Index #208-#211 are various SHADES of the team color
**  (#208 is brightest shade, #211 is darkest shade) .... these
**  numbers are NOT red=#208, blue=#209, etc
**
**  CUnit::IX CUnit::IY
**
**  Coordinate displacement in pixels or coordinates inside a tile.
**  If the unit is moving from one tile to an adjacent one.
**  CUnit::IX is between -::TileSizeX and +::TileSizeX,
**  and counts towards 0 during horizontal motion.
**  CUnit::IY is similar.
**
**  CUnit::Frame
**
**  Current graphic image of the animation sequence. The high bit
**  (128) is used to flip this image horizontal (x direction).
**  This also limits the number of different frames/image to 126.
**
**  CUnit::SeenFrame
**
**  Graphic image (see CUnit::Frame) what the player on this
**  computer has last seen. If UnitNotSeen the player haven't seen
**  this unit yet.
**
**  CUnit::Direction
**
**  Contains the binary angle (0-255) in which the direction the
**  unit looks. 0, 32, 64, 128, 160, 192, 224, 256 corresponds to
**  0, 45, 90, 135, 180, 225, 270, 315, 360 or north,
**  north-east, east, south-east, south, south-west, west,
**  north-west, north. Currently only 8 directions are used, this
**  is more for the future.
**
**  CUnit::Attacked
**
**  Last cycle the unit was attacked. 0 means never.
**
**  CUnit::Burning
**
**  If Burning is non-zero, the unit is burning.
**
**  CUnit::VisCount[PlayerMax]
**
**              Used to keep track of visible units on the map, it counts the
**              Number of seen tiles for each player. This is only modified
**              in UnitsMarkSeen and UnitsUnmarkSeen, from fow.
**              We keep track of visilibty for each player, and combine with
**              Shared vision ONLY when querying and such.
**
**  CUnit::SeenByPlayer
**
**              This is a bitmask of 1 and 0 values. SeenByPlayer & (1<<p) is 0
**              If p never saw the unit and 1 if it did. This is important for
**              keeping track of dead units under fog. We only keep track of units
**              that are visible under fog with this.
**
**  CUnit::Destroyed
**
**  Unit is destroyed. pending reference.
**
**  CUnit::Removed
**
**  This flag means the unit is not active on map. This flag
**  have workers set if they are inside a building, units that are
**  on board of a transporter.
**
**  CUnit::Selected
**
**  Unit is selected. (So you can give it orders)
**
**  CUnit::Constructed
**  Set when a building is under construction, and still using the
**  generic building animation.
**
**  CUnit::SeenConstructed
**  Last seen state of construction.  Used to draw correct building
**  frame. See CUnit::Constructed for more information.
**
**  CUnit::SeenState
**  The Seen State of the building.
**  01 The building in being built when last seen.
**  10 The building was been upgraded when last seen.
**
**  CUnit::Boarded
**
**  This is 1 if the unit is on board a transporter.
**
**  CUnit::Kills
**
**  How many units have been killed by the unit.
**
**  CUnit::GroupId
**
**  Number of the group to that the unit belongs. This is the main
**  group showed on map, a unit can belong to many groups.
**
**  CUnit::LastGroup
**
**  Automatic group number, to reselect the same units. When the
**  user selects more than one unit all units is given the next
**  same number. (Used for ALT-CLICK)
**
**  CUnit::Value
**
**  This values hold the amount of resources in a resource or in
**  in a harvester.
**
**  CUnit::SubAction
**
**  This is an action private variable, it is zero on the first
**  entry of an action. Must be set to zero, if an action finishes.
**  It should only be used inside of actions.
**
**  CUnit::Wait
**
**  The unit is forced too wait for that many cycles. Be carefull,
**  setting this to 0 will lock the unit.
**
**  CUnit::State
**
**  Animation state, currently position in the animation script.
**  0 if an animation has just started, it should only be changed
**  inside of actions.
**
**  CUnit::Reset
**
**
**  CUnit::Blink
**
**
**  CUnit::Moving
**
**
**  CUnit::RescuedFrom
**
**  Pointer to the original owner of a unit. It will be NULL if
**  the unit was not rescued.
**
**  CUnit::OrderCount
**
**  The number of the orders unit to process. An unit has atleast
**  one order. CUnit::OrderCount should be a number at least 1.
**  The orders are in CUnit::Orders[].
**
**  CUnit::OrderFlush
**
**  A flag, which tells the unit to stop with the current order
**  and immediately start with the next order.
**
**  CUnit::TotalOrders
**
**  The number of Orders allocated for this unit to use.
**  Default is 4, but is dynamically updated if more orders are
**  given.
**
**  CUnit::Orders
**
**  Contains all orders of the unit. Slot 0 is always used.
**
**  CUnit::SavedOrder
**
**  This order is executed, if the current order is finished.
**  This is used for attacking units, to return to the old
**  place or for patrolling units to return to patrol after
**  killing some enemies. Any new order given to the unit,
**  clears this saved order.
**
**  CUnit::NewOrder
**
**  This field is only used by buildings and this order is
**  assigned to any by this building new trained unit.
**  This is can be used to set the exit or gathering point of a
**  building.
**
**  CUnit::Data
**
**  @todo continue documentation
**
**  CUnit::Goal
**
**  Generic goal pointer. Used by teleporters to point to circle of power.
**
*/

/*----------------------------------------------------------------------------
--  Includes
----------------------------------------------------------------------------*/

#include <vector>
#include "SDL.h"
#include "upgrade_structs.h"

/*----------------------------------------------------------------------------
--  Declarations
----------------------------------------------------------------------------*/

class CUnit;
class CUnitType;
class CUnitStats;
class CPlayer;
class SpellType;
class CUnitColors;
class CConstructionFrame;
class CVariable;
class CBuildRestrictionOnTop;
class CFile;
struct lua_State;
class CViewport;
class CAnimation;

	/// Called whenever the selected unit was updated
extern void SelectedUnitChanged(void);

/**
**  Unit references over network, or for memory saving.
*/
typedef unsigned short UnitRef;

/**
**  All possible unit actions.
**
**  @note  Always change the table ::HandleActionTable
**
**  @see HandleActionTable
*/
typedef enum _unit_action_ {
	UnitActionNone,         /// No valid action

	UnitActionStill,        /// unit stand still, does nothing
	UnitActionStandGround,  /// unit stands ground
	UnitActionFollow,       /// unit follows units
	UnitActionMove,         /// unit moves to position/unit
	UnitActionAttack,       /// unit attacks position/unit
	UnitActionAttackGround, /// unit attacks ground
	UnitActionDie,          /// unit dies

	UnitActionSpellCast,    /// unit casts spell

	UnitActionTrain,        /// building is training
	UnitActionBuilt,      /// building is under construction

// Compound actions
	UnitActionBoard,        /// unit entering transporter
	UnitActionUnload,       /// unit leaving transporter
	UnitActionPatrol,       /// unit paroling area
	UnitActionBuild,        /// unit builds building

	UnitActionRepair,       /// unit repairing
	UnitActionResource,     /// unit harvesting resources
} UnitAction;

/**
**  Unit order structure.
*/
class COrder {
public:
	COrder() : Action(UnitActionNone), Range(0), MinRange(0), Width(0),
		Height(0), Goal(NULL), X(-1), Y(-1), Type(NULL)
	{
		memset(&Arg1, 0, sizeof(Arg1));
	};

	void Init() {
		Action = UnitActionNone;
		Range = 0;
		MinRange = 0;
		Width = 0;
		Height = 0;
		Assert(!Goal);
		X = -1; Y = -1;
		Type = NULL;
		memset(&Arg1, 0, sizeof(Arg1));
	};

	unsigned char Action;   /// global action
	int Range;              /// How far away
	unsigned int  MinRange; /// How far away minimum
	unsigned char Width;    /// Goal Width (used when Goal is not)
	unsigned char Height;   /// Goal Height (used when Goal is not)

	CUnit *Goal;            /// goal of the order (if any)
	int X;                  /// or X tile coordinate of destination
	int Y;                  /// or Y tile coordinate of destination
	CUnitType *Type;        /// Unit-type argument

	union {
		struct {
			int X;                    /// X position for patroling.
			int Y;                    /// Y position for patroling.
		} Patrol;                     /// position.
		SpellType *Spell;             /// spell when casting.
	} Arg1;             /// Extra command argument.
};

/**
**  Voice groups for a unit
*/
enum UnitVoiceGroup {
	VoiceSelected,          /// If selected
	VoiceAcknowledging,     /// Acknowledge command
	VoiceReady,             /// Command completed
	VoiceHelpMe,            /// If attacked
	VoiceDying,             /// If killed
	VoiceBuilding,          /// only for building under construction
	VoiceDocking,           /// only for transport reaching coast
};

/**
**  Unit/Missile headings.
**          N
**  NW              NE
**  W                E
**  SW              SE
**          S
*/
enum _directions_ {
	LookingN  = 0 * 32,      /// Unit looking north
	LookingNE = 1 * 32,      /// Unit looking north east
	LookingE  = 2 * 32,      /// Unit looking east
	LookingSE = 3 * 32,      /// Unit looking south east
	LookingS  = 4 * 32,      /// Unit looking south
	LookingSW = 5 * 32,      /// Unit looking south west
	LookingW  = 6 * 32,      /// Unit looking west
	LookingNW = 7 * 32,      /// Unit looking north west
};

#define NextDirection 32        /// Next direction N->NE->E...
#define UnitNotSeen 0x7fffffff  /// Unit not seen, used by CUnit::SeenFrame

	/// The big unit structure
class CUnit {
public:
	CUnit() { Init(); }

	void Init() {
		Refs = 0;
		Slot = 0;
		UnitSlot = NULL;
		PlayerSlot = NULL;
		Next = NULL;
		InsideCount = 0;
		BoardCount = 0;
		UnitInside = NULL;
		Container = NULL;
		NextContained = NULL;
		PrevContained = NULL;
		X = 0;
		Y = 0;
		Type = NULL;
		Player = NULL;
		Stats = NULL;
		CurrentSightRange = 0;
		Colors = NULL;
		IX = 0;
		IY = 0;
		Frame = 0;
		Direction = 0;
		Attacked = 0;
		Burning = 0;
		Destroyed = 0;
		Removed = 0;
		Selected = 0;
		TeamSelected = 0;
		Constructed = 0;
		Boarded = 0;
		RescuedFrom = NULL;
		memset(VisCount, 0, sizeof(VisCount));
		memset(&Seen, 0, sizeof(Seen));
		Variable = NULL;
		TTL = 0;
		GroupId = 0;
		LastGroup = 0;
		memset(ResourcesHeld, 0, sizeof(ResourcesHeld));
		ProductionEfficiency = 100;
		SubAction = 0;
		Wait = 0;
		State = 0;
		Blink = 0;
		Moving = 0;
		ReCast = 0;
		memset(&Anim, 0, sizeof(Anim));
		OrderCount = 0;
		OrderFlush = 0;
		Orders.clear();
		SavedOrder.Init();
		NewOrder.Init();
		AutoCastSpell = NULL;
		AutoRepair = 0;
		memset(&Data, 0, sizeof(Data));
		Goal = NULL;
	}

	// @note int is faster than shorts
	unsigned long Refs;   /// Reference counter
	int Slot;             /// Assigned slot number
	CUnit **UnitSlot;     /// Slot pointer of Units
	CUnit **PlayerSlot;   /// Slot pointer of Player->Units

	CUnit   *Next;          /// Generic link pointer (on map)

	int    InsideCount;   /// Number of units inside.
	int    BoardCount;    /// Number of units transported inside.
	CUnit *UnitInside;    /// Pointer to one of the units inside.
	CUnit *Container;     /// Pointer to the unit containing it (or 0)
	CUnit *NextContained; /// Next unit in the container.
	CUnit *PrevContained; /// Previous unit in the container.

	int X;                /// Map position X
	int Y;                /// Map position Y

	CUnitType  *Type;              /// Pointer to unit-type (peon,...)
	CPlayer    *Player;            /// Owner of this unit
	CUnitStats *Stats;             /// Current unit stats
	int         CurrentSightRange; /// Unit's Current Sight Range

// DISPLAY:
	CUnitColors *Colors;    /// Player colors
	signed char IX;         /// X image displacement to map position
	signed char IY;         /// Y image displacement to map position
	int         Frame;      /// Image frame: <0 is mirrored

	unsigned Direction : 8; /// angle (0-255) unit looking

	unsigned long Attacked; /// gamecycle unit was last attacked

	unsigned Burning : 1;   /// unit is burning
	unsigned Destroyed : 1; /// unit is destroyed pending reference
	unsigned Removed : 1;   /// unit is removed (not on map)
	unsigned Selected : 1;  /// unit is selected

	unsigned Constructed : 1;    /// Unit is in construction
	unsigned Boarded : 1;        /// Unit is on board a transporter.
	unsigned TeamSelected;       /// unit is selected by a team member.
	CPlayer *RescuedFrom;        /// The original owner of a rescued unit.
							     /// NULL if the unit was not rescued.
	/* Seen stuff. */
	int VisCount[PlayerMax];     /// Unit visibility counts
	struct _unit_seen_ {
		unsigned            ByPlayer : PlayerMax;    /// Track unit seen by player
		int                 Frame;                   /// last seen frame/stage of buildings
		CUnitType          *Type;                    /// Pointer to last seen unit-type
		int                 X;                       /// Last unit->X Seen
		int                 Y;                       /// Last unit->Y Seen
		signed char         IX;                      /// Seen X image displacement to map position
		signed char         IY;                      /// seen Y image displacement to map position
		unsigned            Constructed : 1;         /// Unit seen construction
		unsigned            State : 3;               /// Unit seen build/upgrade state
		unsigned            Destroyed : PlayerMax;   /// Unit seen destroyed or not
		CConstructionFrame *CFrame;                  /// Seen construction frame
	} Seen;

	CVariable *Variable; /// array of User Defined variables.

	unsigned long TTL;   /// time to live

	int GroupId;         /// unit belongs to this group id
	int LastGroup;       /// unit belongs to this last group

	int ResourcesHeld[MaxCosts];       /// Resources held by a unit
	unsigned ProductionEfficiency : 8; /// Production efficiency

	unsigned SubAction : 8; /// sub-action of unit
	unsigned Wait;          /// action counter
	unsigned State : 8;     /// action state
	unsigned Blink : 3;     /// Let selection rectangle blink
	unsigned Moving : 1;    /// The unit is moving
	unsigned ReCast : 1;    /// Recast again next cycle

	struct _unit_anim_ {
		const CAnimation *Anim;     /// Anim
		const CAnimation *CurrAnim; /// CurrAnim
		int Wait;                   /// Wait
		int Unbreakable;            /// Unbreakable
	} Anim;

	char OrderCount;              /// how many orders in queue
	char OrderFlush;              /// cancel current order, take next
	std::vector<COrder *> Orders; /// orders to process
	COrder SavedOrder;            /// order to continue after current
	COrder NewOrder;              /// order for new trained units
	char *AutoCastSpell;          /// spells to auto cast
	unsigned AutoRepair : 1;      /// True if unit tries to repair on still action.

	union _order_data_ {
	struct _order_move_ {
		char Fast;                  /// Flag fast move (one step)
		char Length;                /// stored path length
#define MAX_PATH_LENGTH 28          /// max length of precalculated path
		char Path[MAX_PATH_LENGTH]; /// directions of stored path
	} Move; /// ActionMove,...
	struct _order_built_ {
		int Progress;               /// Progress counter, in 1/100 cycles.
		int Cancel;                 /// Cancel construction
		CConstructionFrame *Frame;  /// Construction frame
	} Built; /// ActionBuilt,...
	struct _order_harvest_ {
		int CurrentProduction[MaxCosts];
	} Harvest; /// Harvest
	struct _order_train_ {
		int Ticks;                  /// Ticks to complete
	} Train; /// Train units action
	struct _order_repair_ {
		int Progress;               /// Progress counter
	} Repair; /// Repair
	} Data; /// Storage room for different commands

	CUnit *Goal; /// Generic goal pointer


	inline bool IsIdle() const {
		return Orders[0]->Action == UnitActionStill && OrderCount == 1;
	}

	inline void ClearAction() {
		Orders[0]->Action = UnitActionStill;
		SubAction = 0;
		if (Selected) {
			SelectedUnitChanged();
		}
	}

	/// Increase a unit's reference count
	void RefsIncrease();
	/// Decrease a unit's reference count
	void RefsDecrease();

	/// Initialize unit structure with default values
	void Init(CUnitType *type);
	/// Assign unit to player
	void AssignToPlayer(CPlayer *player);

	/// Draw a single unit
	void Draw() const;
	/// Place a unit on map
	void Place(int x, int y);

	/// Move unit to tile(x, y). (Do special stuff : vision, cachelist, pathfinding)
	void MoveToXY(int x, int y);
	/// Add a unit inside a container. Only deal with list stuff.
	void AddInContainer(CUnit *host);
	/// Change owner of unit
	void ChangeOwner(CPlayer *newplayer);

	/// Remove unit from map/groups/...
	void Remove(CUnit *host);
	/// Release a unit
	void Release();

	/// Returns true, if unit is directly seen by an allied unit.
	bool IsVisible(const CPlayer *player) const;
	/// Returns true, if unit is visible as a goal.
	bool IsVisibleAsGoal(const CPlayer *player) const;
	/// Returns true, if the unit is alive and on the map.
	bool IsAliveOnMap() const;
	/// Returns true, if unit is Visible for game logic on the map.
	bool IsVisibleOnMap(const CPlayer *player) const;
	/// Returns true if unit is visible on minimap. Only for ThisPlayer.
	bool IsVisibleOnMinimap() const;
	/// Returns true if unit is visible in an viewport. Only for ThisPlayer.
	bool IsVisibleInViewport(const CViewport *vp) const;
	/// Returns true, if unit is visible on current map view (any viewport).
	bool IsVisibleOnScreen() const;

	/// @todo more docu
	void GetMapArea(int *sx, int *sy, int *ex, int *ey) const;

	bool IsEnemy(const CPlayer *x) const;
	bool IsEnemy(const CUnit *x) const;
	bool IsAllied(const CPlayer *x) const;
	bool IsAllied(const CUnit *x) const;
	bool IsSharedVision(const CPlayer *x) const;
	bool IsSharedVision(const CUnit *x) const;
	bool IsBothSharedVision(const CPlayer *x) const;
	bool IsBothSharedVision(const CUnit *x) const;
	bool IsTeamed(const CPlayer *x) const;
	bool IsTeamed(const CUnit *x) const;

	bool IsUnusable() const;
};

#define NoUnitP (CUnit *)0        /// return value: for no unit found
#define InfiniteDistance INT_MAX  /// the distance is unreachable

#define FlushCommands 1           /// Flush commands in queue

#define MAX_UNIT_SLOTS 65535      /// Maximal number of used slots

/**
**  Returns unit number (unique to this unit)
*/
#define UnitNumber(unit) ((unit)->Slot)

/**
**  How many groups supported
*/
#define NUM_GROUPS 10

/**
**  User preference.
*/
class CPreference {
public:
	CPreference() : ShowSightRange(false), ShowReactionRange(false),
		ShowAttackRange(false), ShowOrders(0) {};

	bool ShowSightRange;     /// Show right range.
	bool ShowReactionRange;  /// Show reaction range.
	bool ShowAttackRange;    /// Show attack range.
	int  ShowOrders;         /// How many second show orders of unit on map.
};

extern CPreference Preference;

/*----------------------------------------------------------------------------
-- Variables
----------------------------------------------------------------------------*/

extern CUnit *Units[MAX_UNIT_SLOTS]; /// Units used
extern int NumUnits;                 /// Number of units used

// in unit_draw.cpp
/// @todo could be moved into the user interface ?
extern unsigned long ShowOrdersCount;   /// Show orders for some time
extern bool EnableBuildingCapture;      /// Config: building capture enabled
extern const CViewport *CurrentViewport;/// CurrentViewport
extern void DrawUnitSelection(const CUnit *);
extern void DrawSelection(Uint32 color, int x1, int y1, int x2, int y2);
extern int MaxSelectable;                   /// How many units could be selected

extern CUnit **Selected;                    /// currently selected units
extern CUnit **TeamSelected[PlayerMax];     /// teams currently selected units
extern int     NumSelected;                 /// how many units selected
extern int     TeamNumSelected[PlayerMax];  /// Number of Units a team member has selected

/*----------------------------------------------------------------------------
-- Functions
----------------------------------------------------------------------------*/

	/// Mark the field with the FieldFlags.
void MarkUnitFieldFlags(const CUnit *unit);
	/// Unmark the field with the FieldFlags.
void UnmarkUnitFieldFlags(const CUnit *unit);
	/// Update unit->CurrentSightRange.
void UpdateUnitSightRange(CUnit *unit);
	/// Create a new unit
extern CUnit *MakeUnit(CUnitType *type, CPlayer *player);
	/// Create a new unit and place on map
extern CUnit *MakeUnitAndPlace(int x, int y, CUnitType *type, CPlayer *player);
	/// Handle the loss of a unit (food,...)
extern void UnitLost(CUnit *unit);
	/// Remove the Orders of a Unit
extern void UnitClearOrders(CUnit *unit);
	/// @todo more docu
extern void UpdateForNewUnit(const CUnit *unit, int upgrade);
	/// @todo more docu
extern void NearestOfUnit(const CUnit *unit, int tx, int ty, int *dx, int *dy);

	/// Call when an Unit goes under fog.
extern void UnitGoesUnderFog(CUnit *unit, const CPlayer *player);
	/// Call when an Unit goes out of fog.
extern void UnitGoesOutOfFog(CUnit *unit, const CPlayer *player);
	/// Marks a unit as seen
extern void UnitsOnTileMarkSeen(const CPlayer *player, int x, int y);
	/// Unmarks a unit as seen
extern void UnitsOnTileUnmarkSeen(const CPlayer *player, int x, int y);
	/// Does a recount for VisCount
extern void UnitCountSeen(CUnit *unit);

	/// Check for rescue each second
extern void RescueUnits(void);

	/// Convert direction (dx,dy) to heading (0-255)
extern int DirectionToHeading(int, int);
	/// Update frame from heading
extern void UnitUpdateHeading(CUnit *unit);
	/// Heading and frame from delta direction x,y
extern void UnitHeadingFromDeltaXY(CUnit *unit, int x, int y);

	/// @todo more docu
extern void DropOutOnSide(CUnit *unit, int heading, int addx, int addy);
	/// @todo more docu
extern void DropOutNearest(CUnit *unit, int x, int y, int addx, int addy);
	/// Drop out all units in the unit
extern void DropOutAll(const CUnit *unit);

	/// Return the rule used to build this building.
extern CBuildRestrictionOnTop *OnTopDetails(const CUnit *unit, const CUnitType *parent);
	/// @todo more docu
extern CUnit *CanBuildHere(const CUnit *unit, const CUnitType *type, int x, int y);
	/// @todo more docu
extern bool CanBuildOn(int x, int y, int mask);
	/// FIXME: more docu
extern CUnit *CanBuildUnitType(const CUnit *unit, const CUnitType *type, int x, int y, int real);

	/// Holds resources
extern bool UnitHoldsResources(const CUnit *unit);
	/// Find resource
extern CUnit *UnitFindResource(const CUnit *unit, int x, int y, int range, int resource = -1);
	/// Find the next idle worker
extern CUnit *FindIdleWorker(const CPlayer *player);

	/// @todo more docu
extern CUnit *UnitOnScreen(int x, int y);
	/// Check if a unit should be removed from UnitsConsumingResources
extern void UnitRemoveConsumingResources(CUnit *unit);

	/// Let a unit die
extern void LetUnitDie(CUnit *unit);
	/// Destory all units inside another unit
extern void DestroyAllInside(CUnit *source);
	/// Hit unit with damage, if destroyed give attacker the points
extern void HitUnit(CUnit *attacker, CUnit *target, int damage);

	/// Returns the map distance between two points
extern int MapDistance(int x1, int y1, int x2, int y2);
	/// Returns the map distance between two points with unit-type
extern int MapDistanceToType(int x1, int y1, const CUnitType *type, int x2, int y2);
	/// Returns the map distance to unit
extern int MapDistanceToUnit(int x, int y, const CUnit *dest);
	/// Returns the map diestance between to unittype as locations
extern int MapDistanceBetweenTypes(const CUnitType *src, int x1, int y1, const CUnitType *dst, int x2, int y2);
	/// Returns the map distance between two units
extern int MapDistanceBetweenUnits(const CUnit *src, const CUnit *dst);

	/// Calculate the distance from current view point to coordinate
extern int ViewPointDistance(int x, int y);
	/// Calculate the distance from current view point to unit
extern int ViewPointDistanceToUnit(const CUnit *dest);

	/// Can this unit-type attack the other (destination)
extern int CanTarget(const CUnitType *type, const CUnitType *dest);
	/// Can transporter transport the other unit
extern int CanTransport(const CUnit *transporter, const CUnit *unit);

	/// Check if unit can move.
extern bool CanMove(const CUnit *unit);

	/// Generate a unit reference, a printable unique string for unit
extern std::string UnitReference(const CUnit *unit);
	/// Save an order
extern void SaveOrder(const COrder *order, CFile *file);
	/// save unit-structure
extern void SaveUnit(const CUnit *unit, CFile *file);
	/// save all units
extern void SaveUnits(CFile *file);

	/// Initialize unit module
extern void InitUnits(void);
	/// Clean unit module
extern void CleanUnits(void);

// in unit_draw.cpp
	/// Register CCL decorations features
extern void DecorationCclRegister(void);
	/// Load the decorations (health,charge) of units
extern void LoadDecorations(void);
	/// Clean the decorations (health,charge) of units
extern void CleanDecorations(void);

	/// Draw unit's shadow
extern void DrawShadow(const CUnit *unit, const CUnitType *type,
	int frame, int x, int y);
	/// Draw all units visible on map in viewport
extern int FindAndSortUnits(const CViewport *vp, CUnit **table, int tablesize);
	/// Show a unit's orders.
extern void ShowOrder(const CUnit *unit);

// in unit_find.cpp
	/// Find all units of this type
extern int FindUnitsByType(const CUnitType *type, CUnit **table, int tablesize);
	/// Find all units of this type of the player
extern int FindPlayerUnitsByType(const CPlayer *, const CUnitType *, CUnit **, int);
	/// Return any unit on that map tile
extern CUnit *UnitOnMapTile(int tx, int ty, unsigned type = (unsigned)-1);
	/// Return possible attack target on that map area
extern CUnit *TargetOnMap(const CUnit *unit, int x1, int y1, int x2, int y2);

	/// Return resource, if on map tile
extern CUnit *ResourceOnMap(int tx, int ty, int resource = -1);

	/// Find best enemy in numeric range to attack
extern CUnit *AttackUnitsInDistance(const CUnit *unit, int range);
	/// Find best enemy in attack range to attack
extern CUnit *AttackUnitsInRange(const CUnit *unit);
	/// Find best enemy in reaction range to attack
extern CUnit *AttackUnitsInReactRange(const CUnit *unit);

// in groups.cpp
	/// Initialize data structures for groups
extern void InitGroups(void);
	/// Save groups
extern void SaveGroups(CFile *file);
	/// Cleanup groups
extern void CleanGroups(void);

	// 2 functions to conseal the groups internal data structures...
	/// Get the number of units in a particular group
extern int GetNumberUnitsOfGroup(int num);
	/// Get the array of units of a particular group
extern CUnit **GetUnitsOfGroup(int num);

	/// Remove all units from a group
extern void ClearGroup(int num);
	/// Add the array of units to the group
extern void AddToGroup(CUnit **units, int nunits, int num);
	/// Set the contents of a particular group with an array of units
extern void SetGroup(CUnit **units, int nunits, int num);
	/// Remove a unit from a group
extern void RemoveUnitFromGroups(CUnit *unit);
	/// Register CCL group features
extern void GroupCclRegister(void);

// in selection.cpp
	/// Check if unit is the currently only selected
#define IsOnlySelected(unit) (NumSelected == 1 && Selected[0] == (unit))

	///  Save selection to restore after.
extern void SaveSelection(void);
	///  Restore selection.
extern void RestoreSelection(void);
	/// Clear current selection
extern void UnSelectAll(void);
	/// Select group as selection
extern void ChangeSelectedUnits(CUnit **units, int num_units);
	/// Changed TeamUnit Selection
extern void ChangeTeamSelectedUnits(CPlayer *player, CUnit **units, int adjust, int count);
	/// Add a unit to selection
extern int SelectUnit(CUnit *unit);
	/// Select one unit as selection
extern void SelectSingleUnit(CUnit *unit);
	/// Remove a unit from selection
extern void UnSelectUnit(CUnit *unit);
	/// Add a unit to selected if not already selected, remove it otherwise
extern int ToggleSelectUnit(CUnit *unit);
	/// Select units from the same type (if selectable by rectangle)
extern int SelectUnitsByType(CUnit *base);
	/// Toggle units from the same type (if selectable by rectangle)
extern int ToggleUnitsByType(CUnit *base);
	/// Select the units belonging to a particular group
extern int SelectGroup(int group_number);
	/// Add the units from the same group as the one in parameter
extern int AddGroupFromUnitToSelection(CUnit *unit);
	/// Select the units from the same group as the one in parameter
extern int SelectGroupFromUnit(CUnit *unit);
	/// Select the units in the selection rectangle
extern int SelectUnitsInRectangle(int tx, int ty, int w, int h);
	/// Select ground units in the selection rectangle
extern int SelectGroundUnitsInRectangle(int tx, int ty, int w, int h);
	/// Select flying units in the selection rectangle
extern int SelectAirUnitsInRectangle(int tx, int ty, int w, int h);
	/// Add the units in the selection rectangle to the current selection
extern int AddSelectedUnitsInRectangle(int tx, int ty, int w, int h);
	/// Add ground units in the selection rectangle to the current selection
extern int AddSelectedGroundUnitsInRectangle(int tx, int ty, int w, int h);
	/// Add flying units in the selection rectangle to the current selection
extern int AddSelectedAirUnitsInRectangle(int tx, int ty, int w, int h);

	/// Init selections
extern void InitSelections(void);
	/// Save current selection state
extern void SaveSelections(CFile *file);
	/// Clean up selections
extern void CleanSelections(void);
	/// Register CCL selection features
extern void SelectionCclRegister(void);

// in script_unit.cpp
	/// Parse order
extern void CclParseOrder(lua_State *l, COrder *order);
	/// register CCL units features
extern void UnitCclRegister(void);

//@}

#endif // !__UNIT_H__