Debian Codegraph @

engine/action/command.cpp

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name command.cpp - Give units a command. */
11 //
12 // (c) Copyright 1998-2008 by Lutz Sammer and Jimmy Salmon
13 //
14 // This program is free software; you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation; only version 2 of the License.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 // 02111-1307, USA.
27 //
28
29 //@{
30
31 /*----------------------------------------------------------------------------
32 -- Includes
33 ----------------------------------------------------------------------------*/
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "stratagus.h"
40 #include "unittype.h"
41 #include "player.h"
42 #include "unit.h"
43 #include "actions.h"
44 #include "map.h"
45 #include "upgrade.h"
46 #include "pathfinder.h"
47 #include "spells.h"
48 #include "interface.h"
49 #include "ui.h"
50
51 /*----------------------------------------------------------------------------
52 -- Functions
53 ----------------------------------------------------------------------------*/
54
55 /**
56 ** Release an order.
57 **
58 ** @param order Pointer to order.
59 */
60 static void ReleaseOrder(COrder *order)
61 {
62 if (order->Goal) {
63 order->Goal->RefsDecrease();
64 order->Goal = NoUnitP;
65 }
66 }
67
68 /**
69 ** Release all orders of a unit.
70 **
71 ** @param unit Pointer to unit.
72 */
73 static void ReleaseOrders(CUnit *unit)
74 {
75 int n;
76
77 if ((n = unit->OrderCount) > 1) {
78 while (--n) {
79 ReleaseOrder(unit->Orders[n]);
80 delete unit->Orders[n];
81 unit->Orders.pop_back();
82 }
83 unit->OrderCount = 1;
84 }
85 unit->OrderFlush = 1;
86 // Order 0 must be stopped in the action loop.
87 }
88
89 /**
90 ** Get next free order slot.
91 **
92 ** @param unit pointer to unit.
93 ** @param flush if true, flush order queue.
94 **
95 ** @return Pointer to next free order slot.
96 */
97 static COrder *GetNextOrder(CUnit *unit, int flush)
98 {
99 if (flush) {
100 // empty command queue
101 ReleaseOrders(unit);
102 }
103 if (unit->OrderCount == 0x7F) {
104 return NULL;
105 }
106
107 unit->Orders.push_back(new COrder);
108
109 return unit->Orders[(int)unit->OrderCount++];
110 }
111
112 /**
113 ** Remove an order from the list of orders pending
114 **
115 ** @param unit pointer to unit
116 ** @param order number of the order to remove
117 */
118 static void RemoveOrder(CUnit *unit, int order)
119 {
120 int i;
121
122 Assert(0 <= order && order < unit->OrderCount);
123 if (order != 0) {
124 delete unit->Orders[order];
125 }
126 i = order;
127 while (i < unit->OrderCount - 1) {
128 unit->Orders[i] = unit->Orders[i + 1];
129 ++i;
130 }
131
132 if (unit->OrderCount > 1) {
133 unit->Orders.pop_back();
134 --unit->OrderCount;
135 if (order == 0) {
136 unit->SubAction = 0;
137 }
138 } else {
139 Assert(i == 0);
140 unit->Orders[0]->Init();
141 unit->Orders[0]->Action = UnitActionStill;
142 unit->SubAction = 0;
143 }
144 }
145
146 /**
147 ** Clear the saved action.
148 **
149 ** @param unit Unit pointer, that get the saved action cleared.
150 **
151 ** @note If we make a new order, we must clear any saved actions.
152 ** @note Internal functions, must protect it, if needed.
153 */
154 static void ClearSavedAction(CUnit *unit)
155 {
156 ReleaseOrder(&unit->SavedOrder);
157
158 unit->SavedOrder.Init();
159 unit->SavedOrder.Action = UnitActionStill; // clear saved action
160 }
161
162 /*----------------------------------------------------------------------------
163 -- Commands
164 ----------------------------------------------------------------------------*/
165
166 /**
167 ** Stop unit.
168 **
169 ** @param unit pointer to unit.
170 */
171 void CommandStopUnit(CUnit *unit)
172 {
173 COrder *order;
174
175 // Ignore that the unit could be removed.
176
177 order = GetNextOrder(unit, FlushCommands); // Flush them.
178 Assert(order);
179 order->Init();
180
181 order->Action = UnitActionStill;
182 ReleaseOrder(&unit->SavedOrder);
183 ReleaseOrder(&unit->NewOrder);
184 unit->SavedOrder = unit->NewOrder = *order;
185 }
186
187 /**
188 ** Order an already formed Order structure
189 **
190 ** @param unit pointer to unit
191 ** @param cpyorder pointer to valid order
192 ** @param flush if true, flush command queue.
193 */
194 void CommandAnyOrder(CUnit *unit, COrder *cpyorder, int flush)
195 {
196 COrder *order;
197
198 if (!(order = GetNextOrder(unit, flush))) {
199 return;
200 }
201
202 *order = *cpyorder;
203 if (order->Goal) {
204 order->Goal->RefsIncrease();
205 }
206 ClearSavedAction(unit);
207 }
208
209 /**
210 ** Move an order in the order queue.
211 ** ( Cannot move the order 0 ! )
212 **
213 ** @param unit pointer to unit
214 ** @param src the order to move
215 ** @param dst the new position of the order
216 */
217 void CommandMoveOrder(CUnit *unit, int src, int dst)
218 {
219 COrder *tmp;
220 int i;
221
222 Assert(src != 0 && dst != 0 && src < unit->OrderCount && dst < unit->OrderCount);
223
224 if (src == dst) {
225 return;
226 }
227
228 if (src < dst) {
229 tmp = unit->Orders[src];
230 for (i = src; i < dst; ++i) {
231 unit->Orders[i] = unit->Orders[i+1];
232 }
233 unit->Orders[dst] = tmp;
234 } else {
235 // dst < src
236 tmp = unit->Orders[src];
237 for (i = src - 1 ; i >= dst; --i) {
238 unit->Orders[i + 1] = unit->Orders[i];
239 }
240 unit->Orders[dst] = tmp;
241 }
242 }
243
244 /**
245 ** Stand ground.
246 **
247 ** @param unit pointer to unit.
248 ** @param flush if true, flush command queue.
249 */
250 void CommandStandGround(CUnit *unit, int flush)
251 {
252 COrder *order;
253
254 // Ignore that the unit could be removed.
255
256 if (unit->Type->Building) {
257 // FIXME: should find a better way for pending orders.
258 order = &unit->NewOrder;
259 ReleaseOrder(order);
260 } else if (!(order = GetNextOrder(unit, flush))) {
261 return;
262 }
263 order->Init();
264 order->Action = UnitActionStandGround;
265 ClearSavedAction(unit);
266 }
267
268 /**
269 ** Follow unit to new position
270 **
271 ** @param unit pointer to unit.
272 ** @param dest unit to be followed
273 ** @param flush if true, flush command queue.
274 */
275 void CommandFollow(CUnit *unit, CUnit *dest, int flush)
276 {
277 COrder *order;
278
279 //
280 // Check if unit is still valid? (NETWORK!)
281 //
282 if (!unit->Removed && unit->Orders[0]->Action != UnitActionDie) {
283 if (!CanMove(unit)) {
284 // FIXME: should find a better way for pending orders.
285 order = &unit->NewOrder;
286 ReleaseOrder(order);
287 } else if (!(order = GetNextOrder(unit, flush))) {
288 return;
289 }
290 order->Init();
291
292 order->Action = UnitActionFollow;
293 //
294 // Destination could be killed.
295 // Should be handled in action, but is not possible!
296 // Unit::Refs is used as timeout counter.
297 //
298 if (dest->Destroyed) {
299 order->X = dest->X + dest->Type->TileWidth / 2;
300 order->Y = dest->Y + dest->Type->TileHeight / 2;
301 } else {
302 order->Goal = dest;
303 dest->RefsIncrease();
304 order->Range = 1;
305 }
306 }
307 ClearSavedAction(unit);
308 }
309
310 /**
311 ** Move unit to new position
312 **
313 ** @param unit pointer to unit.
314 ** @param x X map position to move to.
315 ** @param y Y map position to move to.
316 ** @param flush if true, flush command queue.
317 */
318 void CommandMove(CUnit *unit, int x, int y, int flush)
319 {
320 COrder *order;
321
322 Assert(x >= 0 && y >= 0 && x < Map.Info.MapWidth && y < Map.Info.MapHeight);
323
324 //
325 // Check if unit is still valid? (NETWORK!)
326 //
327 if (!unit->Removed && unit->Orders[0]->Action != UnitActionDie) {
328 if (!CanMove(unit)) {
329 // FIXME: should find a better way for pending orders.
330 order = &unit->NewOrder;
331 ReleaseOrder(order);
332 } else if (!(order = GetNextOrder(unit, flush))) {
333 return;
334 }
335 order->Init();
336
337 order->Action = UnitActionMove;
338 order->X = x;
339 order->Y = y;
340 }
341 ClearSavedAction(unit);
342 }
343
344 /**
345 ** Repair unit
346 **
347 ** @param unit pointer to unit.
348 ** @param x X map position to repair.
349 ** @param y Y map position to repair.
350 ** @param dest or unit to be repaired. FIXME: not supported
351 ** @param flush if true, flush command queue.
352 */
353 void CommandRepair(CUnit *unit, int x, int y, CUnit *dest, int flush)
354 {
355 COrder *order;
356
357 //
358 // Check if unit is still valid? (NETWORK!)
359 //
360 if (!unit->Removed && unit->Orders[0]->Action != UnitActionDie) {
361 if (unit->Type->Building) {
362 // FIXME: should find a better way for pending orders.
363 order = &unit->NewOrder;
364 ReleaseOrder(order);
365 } else if (!(order = GetNextOrder(unit, flush))) {
366 return;
367 }
368 order->Init();
369
370 order->Action = UnitActionRepair;
371 //
372 // Destination could be killed.
373 // Should be handled in action, but is not possible!
374 // Unit::Refs is used as timeout counter.
375 //
376 if (dest) {
377 if (dest->Destroyed) {
378 order->X = dest->X + dest->Type->TileWidth / 2;
379 order->Y = dest->Y + dest->Type->TileHeight / 2;
380 } else {
381 order->Goal = dest;
382 dest->RefsIncrease();
383 order->Range = unit->Type->RepairRange;
384 }
385 } else {
386 order->X = x;
387 order->Y = y;
388 }
389 }
390 ClearSavedAction(unit);
391 }
392
393 /**
394 ** Auto repair.
395 **
396 ** @param unit pointer to unit.
397 ** @param on 1 for auto repair on, 0 for off.
398 */
399 void CommandAutoRepair(CUnit *unit, int on)
400 {
401 //
402 // Check if unit is still valid? (NETWORK!)
403 //
404 if (!unit->Removed && unit->Orders[0]->Action != UnitActionDie) {
405 unit->AutoRepair = on;
406 }
407 }
408
409 /**
410 ** Attack with unit at new position
411 **
412 ** @param unit pointer to unit.
413 ** @param x X map position to attack.
414 ** @param y Y map position to attack.
415 ** @param attack or unit to be attacked.
416 ** @param flush if true, flush command queue.
417 */
418 void CommandAttack(CUnit *unit, int x, int y, CUnit *attack, int flush)
419 {
420 COrder *order;
421
422 Assert(x >= 0 && y >= 0 && x < Map.Info.MapWidth && y < Map.Info.MapHeight);
423
424 //
425 // Check if unit is still valid? (NETWORK!)
426 //
427 if (!unit->Removed && unit->Orders[0]->Action != UnitActionDie) {
428 if (!unit->Type->CanAttack) {
429 // FIXME: should find a better way for pending orders.
430 order = &unit->NewOrder;
431 ReleaseOrder(order);
432 } else if (!(order = GetNextOrder(unit, flush))) {
433 return;
434 }
435 order->Init();
436
437 order->Action = UnitActionAttack;
438 if (attack) {
439 //
440 // Destination could be killed.
441 // Should be handled in action, but is not possible!
442 // Unit::Refs is used as timeout counter.
443 //
444 if (attack->Destroyed) {
445 order->X = attack->X + attack->Type->TileWidth / 2;
446 order->Y = attack->Y + attack->Type->TileHeight / 2;
447 } else {
448 // Removed, Dying handled by action routine.
449 order->Goal = attack;
450 attack->RefsIncrease();
451 order->Range = unit->Stats->Variables[ATTACKRANGE_INDEX].Max;
452 order->MinRange = unit->Type->MinAttackRange;
453 }
454 } else {
455 order->X = x;
456 order->Y = y;
457 }
458 }
459 ClearSavedAction(unit);
460 }
461
462 /**
463 ** Attack ground with unit.
464 **
465 ** @param unit pointer to unit.
466 ** @param x X map position to fire on.
467 ** @param y Y map position to fire on.
468 ** @param flush if true, flush command queue.
469 */
470 void CommandAttackGround(CUnit *unit, int x, int y, int flush)
471 {
472 COrder *order;
473
474 Assert(x >= 0 && y >= 0 && x < Map.Info.MapWidth && y < Map.Info.MapHeight);
475
476 //
477 // Check if unit is still valid? (NETWORK!)
478 //
479 if (!unit->Removed && unit->Orders[0]->Action != UnitActionDie) {
480 if (unit->Type->Building) {
481 // FIXME: should find a better way for pending orders.
482 order = &unit->NewOrder;
483 ReleaseOrder(order);
484 } else if (!(order = GetNextOrder(unit, flush))) {
485 return;
486 }
487 order->Init();
488
489 order->Action = UnitActionAttackGround;
490 order->X = x;
491 order->Y = y;
492 order->Range = unit->Stats->Variables[ATTACKRANGE_INDEX].Max;
493 order->MinRange = unit->Type->MinAttackRange;
494
495 DebugPrint("FIXME this next\n");
496 }
497 ClearSavedAction(unit);
498 }
499
500 /**
501 ** Let a unit patrol from current to new position
502 **
503 ** FIXME: want to support patroling between units.
504 **
505 ** @param unit pointer to unit.
506 ** @param x X map position to patrol between.
507 ** @param y Y map position to patrol between.
508 ** @param flush if true, flush command queue.
509 */
510 void CommandPatrolUnit(CUnit *unit, int x, int y, int flush)
511 {
512 COrder *order;
513
514 Assert(x >= 0 && y >= 0 && x < Map.Info.MapWidth && y < Map.Info.MapHeight);
515
516 //
517 // Check if unit is still valid? (NETWORK!)
518 //
519 if (!unit->Removed && unit->Orders[0]->Action != UnitActionDie) {
520 if (!CanMove(unit)) {
521 // FIXME: should find a better way for pending orders.
522 order = &unit->NewOrder;
523 ReleaseOrder(order);
524 } else if (!(order = GetNextOrder(unit, flush))) {
525 return;
526 }
527 order->Init();
528
529 order->Action = UnitActionPatrol;
530 order->X = x;
531 order->Y = y;
532 Assert(!(unit->X & ~0xFFFF) && !(unit->Y & ~0xFFFF));
533 order->Arg1.Patrol.X = unit->X;
534 order->Arg1.Patrol.Y = unit->Y;
535 }
536 ClearSavedAction(unit);
537 }
538
539 /**
540 ** Board a transporter with unit.
541 **
542 ** @param unit pointer to unit.
543 ** @param dest unit to be boarded.
544 ** @param flush if true, flush command queue.
545 */
546 void CommandBoard(CUnit *unit, CUnit *dest, int flush)
547 {
548 COrder *order;
549
550 //
551 // Check if unit is still valid? (NETWORK!)
552 //
553 if (!unit->Removed && unit->Orders[0]->Action != UnitActionDie) {
554 //
555 // Destination could be killed.
556 // Should be handled in action, but is not possible!
557 // Unit::Refs is used as timeout counter.
558 //
559 if (dest->Destroyed) {
560 return;
561 }
562
563 if (unit->Type->Building) {
564 // FIXME: should find a better way for pending orders.
565 order = &unit->NewOrder;
566 ReleaseOrder(order);
567 } else if (!(order = GetNextOrder(unit, flush))) {
568 return;
569 }
570 order->Init();
571
572 order->Action = UnitActionBoard;
573 order->Goal = dest;
574 dest->RefsIncrease();
575 order->Range = 1;
576 }
577 ClearSavedAction(unit);
578 }
579
580 /**
581 ** Unload a transporter.
582 **
583 ** @param unit pointer to unit.
584 ** @param x X map position to unload.
585 ** @param y Y map position to unload.
586 ** @param what unit to be unloaded, NoUnitP all.
587 ** @param flush if true, flush command queue.
588 */
589 void CommandUnload(CUnit *unit, int x, int y, CUnit *what, int flush)
590 {
591 COrder *order;
592
593 //
594 // Check if unit is still valid? (NETWORK!)
595 //
596 if (!unit->Removed && unit->Orders[0]->Action != UnitActionDie) {
597 if (!(order = GetNextOrder(unit, flush))) {
598 return;
599 }
600 order->Init();
601
602 order->Action = UnitActionUnload;
603 order->X = x;
604 order->Y = y;
605 //
606 // Destination could be killed.
607 // Should be handled in action, but is not possible!
608 // Unit::Refs is used as timeout counter.
609 //
610 if (what && !what->Destroyed) {
611 order->Goal = what;
612 what->RefsIncrease();
613 }
614 }
615 ClearSavedAction(unit);
616 }
617
618 /**
619 ** Send a unit building
620 **
621 ** @param unit pointer to unit.
622 ** @param x X map position to build.
623 ** @param y Y map position to build.
624 ** @param what Unit type to build.
625 ** @param flush if true, flush command queue.
626 */
627 void CommandBuildBuilding(CUnit *unit, int x, int y,
628 CUnitType *what, int flush)
629 {
630 COrder *order;
631
632 //
633 // Check if unit is still valid? (NETWORK!)
634 //
635 if (!unit->Removed && unit->Orders[0]->Action != UnitActionDie) {
636 if (unit->Type->Building) {
637 // FIXME: should find a better way for pending orders.
638 order = &unit->NewOrder;
639 ReleaseOrder(order);
640 } else if (!(order = GetNextOrder(unit, flush))) {
641 return;
642 }
643 order->Init();
644
645 order->Action = UnitActionBuild;
646 order->X = x;
647 order->Y = y;
648 order->Width = what->TileWidth;
649 order->Height = what->TileHeight;
650 order->Range = unit->Type->RepairRange;
651 order->Type = what;
652 order->MinRange = 1;
653 }
654 ClearSavedAction(unit);
655 }
656
657 /**
658 ** Cancel the building construction, or kill a unit.
659 **
660 ** @param unit pointer to unit.
661 */
662 void CommandDismiss(CUnit *unit)
663 {
664 //
665 // Check if building is still under construction? (NETWORK!)
666 //
667 if (unit->Orders[0]->Action == UnitActionBuilt) {
668 unit->Data.Built.Cancel = 1;
669 } else {
670 DebugPrint("Suicide unit ... \n");
671 LetUnitDie(unit);
672 }
673 ClearSavedAction(unit);
674 }
675
676 /**
677 ** Send unit to harvest resources
678 **
679 ** @param unit pointer to unit.
680 ** @param dest destination unit.
681 ** @param flush if true, flush command queue.
682 */
683 void CommandResource(CUnit *unit, CUnit *dest, int flush)
684 {
685 COrder *order;
686
687 //
688 // Check if unit is still valid and Goal still alive? (NETWORK!)
689 //
690 if (!unit->Removed && unit->Orders[0]->Action != UnitActionDie &&
691 !dest->Destroyed) {
692 if (!unit->Type->Building && !unit->Type->Harvester) {
693 ClearSavedAction(unit);
694 return;
695 }
696
697 // FIXME: if low-level supports searching, pass NoUnitP down.
698
699 if (unit->Type->Building) {
700 // FIXME: should find a better way for pending orders.
701 order = &unit->NewOrder;
702 ReleaseOrder(order);
703 } else if (!(order = GetNextOrder(unit, flush))) {
704 return;
705 }
706 order->Init();
707 order->Action = UnitActionResource;
708 order->Goal = dest;
709 dest->RefsIncrease();
710 order->Range = 1;
711 }
712 ClearSavedAction(unit);
713 }
714
715 /**
716 ** Check whether the terrain is suitable for training a unit of a
717 ** given type. For example, you cannot train battleships at a
718 ** shipyard if there is no deep water anywhere near it.
719 **
720 ** @param trainer A unit that would train another unit.
721 ** Typically, this is a building.
722 ** @param traineeType What type of unit it would train.
723 **
724 ** @return true if there is at least one suitable map field near
725 ** the proposed trainer unit, false if not. Unexplored map fields
726 ** are never suitable.
727 **
728 ** This function only checks that training the unit would look at
729 ** least somewhat sensible. It does not check whether there are
730 ** other units blocking the way, because the situation could change
731 ** before the training would end. It also ignores the size of the
732 ** trained unit.
733 */
734 bool TerrainAllowsTraining(const CUnit *trainer, const CUnitType *traineeType)
735 {
736 // Make this nonzero if you also want to check tiles around
737 // the trainer unit.
738 const int around = 0;
739
740 const int xMin = std::max(0, trainer->X - around);
741 const int yMin = std::max(0, trainer->Y - around);
742 const int xEnd = std::min(Map.Info.MapWidth,
743 trainer->X + trainer->Type->TileWidth + around);
744 const int yEnd = std::min(Map.Info.MapHeight,
745 trainer->Y + trainer->Type->TileHeight + around);
746
747 // Ignore any units in the way. Especially ignore buildings
748 // because the trainer unit itself is probably a building.
749 const unsigned obstacles = traineeType->MovementMask & MapFieldPatchMask;
750
751 for (int y = yMin; y < yEnd; y++) {
752 for (int x = xMin; x < xEnd; x++) {
753 if (Map.IsFieldExplored(trainer->Player, x, y)
754 && !(Map.Field(x, y)->Flags & obstacles)) {
755 return true;
756 }
757 }
758 }
759
760 return false;
761 }
762
763 /**
764 ** Building starts training a unit.
765 **
766 ** @param unit pointer to unit.
767 ** @param type unit type to train.
768 ** @param flush if true, flush command queue.
769 */
770 void CommandTrainUnit(CUnit *unit, CUnitType *type, int flush)
771 {
772 COrder *order;
773
774 //
775 // Check if unit is still valid? (NETWORK!)
776 //
777 if (!unit->Removed && unit->Orders[0]->Action != UnitActionDie) {
778 //
779 // Check if enough resources remains? (NETWORK!)
780 // FIXME: wrong if append to message queue!!!
781 //
782 if (unit->Player->CheckLimits(type) < 0 ) {
783 return;
784 }
785
786 if (!(order = GetNextOrder(unit, 0))) {
787 return;
788 }
789 order->Init();
790
791 order->Action = UnitActionTrain;
792 order->Type = type;
793 }
794 ClearSavedAction(unit);
795 }
796
797 /**
798 ** Cancel the training of a unit.
799 **
800 ** @param unit pointer to unit.
801 ** @param slot slot number to cancel.
802 ** @param type Unit-type to cancel.
803 */
804 void CommandCancelTraining(CUnit *unit, int slot, const CUnitType *type)
805 {
806 DebugPrint("Cancel %d type: %s\n" _C_ slot _C_
807 type ? type->Ident.c_str() : "-any-");
808
809 ClearSavedAction(unit);
810
811 //
812 // Check if unit is still training 'slot'? (NETWORK!)
813 //
814
815 if (slot == -1) {
816 if (unit->Orders[0]->Action == UnitActionTrain) {
817 unit->Player->RemoveFromUnitsConsumingResources(unit);
818 }
819
820 // Cancel All training
821 while (unit->Orders[0]->Action == UnitActionTrain) {
822 RemoveOrder(unit, 0);
823 }
824 unit->Data.Train.Ticks = 0;
825 if (unit->Player == ThisPlayer && unit->Selected) {
826 SelectedUnitChanged();
827 }
828 } else if (unit->OrderCount <= slot) {
829 // Order has moved
830 return;
831 } else if (unit->Orders[slot]->Action != UnitActionTrain) {
832 // Order has moved, we are not training
833 return;
834 } else if (unit->Orders[slot]->Action == UnitActionTrain) {
835 // Still training this order, same unit?
836 if (type && unit->Orders[slot]->Type != type) {
837 // Different unit being trained
838 return;
839 }
840
841 DebugPrint("Cancel training\n");
842
843 if (!slot) { // Canceled in work slot
844 unit->Data.Train.Ticks = 0;
845 unit->Player->RemoveFromUnitsConsumingResources(unit);
846 }
847 RemoveOrder(unit, slot);
848
849 //
850 // Update interface.
851 //
852 if (unit->Player == ThisPlayer && unit->Selected) {
853 SelectedUnitChanged();
854 }
855 }
856 }
857
858 /**
859 ** Cast a spell at position or unit.
860 **
861 ** @param unit Pointer to unit.
862 ** @param x X map position to spell cast on.
863 ** @param y Y map position to spell cast on.
864 ** @param dest Spell cast on unit (if exist).
865 ** @param spell Spell type pointer.
866 ** @param flush If true, flush command queue.
867 */
868 void CommandSpellCast(CUnit *unit, int x, int y, CUnit *dest,
869 SpellType *spell, int flush)
870 {
871 COrder *order;
872
873 Assert(x >= 0 && y >= 0 && x < Map.Info.MapWidth && y < Map.Info.MapHeight);
874
875 DebugPrint(": %d casts %s at %d %d on %d\n" _C_
876 UnitNumber(unit) _C_ spell->Ident.c_str() _C_ x _C_ y _C_ dest ? UnitNumber(dest) : 0);
877 Assert(unit->Type->CanCastSpell[spell->Slot]);
878
879 //
880 // Check if unit is still valid? (NETWORK!)
881 //
882 if (!unit->Removed && unit->Orders[0]->Action != UnitActionDie) {
883 // FIXME: should I check here, if there is still enough charge ?
884
885 if (!(order = GetNextOrder(unit, flush))) {
886 return;
887 }
888 order->Init();
889
890 order->Action = UnitActionSpellCast;
891 order->Range = spell->Range;
892 if (dest) {
893 //
894 // Destination could be killed.
895 // Should be handled in action, but is not possible!
896 // Unit::Refs is used as timeout counter.
897 //
898 if (dest->Destroyed) {
899 // FIXME: where check if spell needs a unit as destination?
900 // FIXME: dest->Type is now set to 0. maybe we shouldn't bother.
901 order->X = dest->X /*+ dest->Type->TileWidth / 2*/ - order->Range;
902 order->Y = dest->Y /*+ dest->Type->TileHeight / 2*/ - order->Range;
903 order->Range <<= 1;
904 } else {
905 order->Goal = dest;
906 dest->RefsIncrease();
907 }
908 } else {
909 order->Range = 1;
910 order->X = x;
911 order->Y = y;
912 }
913 order->Arg1.Spell = spell;
914 }
915 ClearSavedAction(unit);
916 }
917
918 /**
919 ** Auto spell cast.
920 **
921 ** @param unit pointer to unit.
922 ** @param spellid Spell id.
923 ** @param on 1 for auto cast on, 0 for off.
924 */
925 void CommandAutoSpellCast(CUnit *unit, int spellid, int on)
926 {
927 //
928 // Check if unit is still valid? (NETWORK!)
929 //
930 if (!unit->Removed && unit->Orders[0]->Action != UnitActionDie) {
931 unit->AutoCastSpell[spellid] = on;
932 }
933 }
934
935 /**
936 ** Diplomacy changed.
937 **
938 ** @param player Player which changes his state.
939 ** @param state New diplomacy state.
940 ** @param opponent Opponent.
941 */
942 void CommandDiplomacy(int player, int state, int opponent)
943 {
944 switch (state) {
945 case DiplomacyNeutral:
946 Players[player].Enemy &= ~(1 << opponent);
947 Players[player].Allied &= ~(1 << opponent);
948 break;
949 case DiplomacyAllied:
950 Players[player].Enemy &= ~(1 << opponent);
951 Players[player].Allied |= 1 << opponent;
952 break;
953 case DiplomacyEnemy:
954 Players[player].Enemy |= 1 << opponent;
955 Players[player].Allied &= ~(1 << opponent);
956 break;
957 case DiplomacyCrazy:
958 Players[player].Enemy |= 1 << opponent;
959 Players[player].Allied |= 1 << opponent;
960 break;
961 }
962 // FIXME: Should we display a message?
963 }
964
965 /**
966 ** Shared vision changed.
967 **
968 ** @param player Player which changes his state.
969 ** @param state New shared vision state.
970 ** @param opponent Opponent.
971 */
972 void CommandSharedVision(int player, bool state, int opponent)
973 {
974 int before;
975 int after;
976 int x;
977 int y;
978 int i;
979 CMapField *mf;
980
981 //
982 // Do a real hardcore seen recount. First we unmark EVERYTHING.
983 //
984 for (i = 0; i < NumUnits; ++i) {
985 if (!Units[i]->Destroyed) {
986 MapUnmarkUnitSight(Units[i]);
987 }
988 }
989
990 //
991 // Compute Before and after.
992 //
993 before = Players[player].IsBothSharedVision(&Players[opponent]);
994 if (state == false) {
995 Players[player].SharedVision &= ~(1 << opponent);
996 } else {
997 Players[player].SharedVision |= (1 << opponent);
998 }
999 after = Players[player].IsBothSharedVision(&Players[opponent]);
1000
1001 if (before && !after) {
1002 //
1003 // Don't share vision anymore. Give each other explored terrain for good-bye.
1004 //
1005 for (x = 0; x < Map.Info.MapWidth; ++x) {
1006 for (y = 0; y < Map.Info.MapHeight; ++y) {
1007 mf = Map.Field(x, y);
1008 if (mf->Visible[player] && !mf->Visible[opponent]) {
1009 mf->Visible[opponent] = 1;
1010 }
1011 if (mf->Visible[opponent] && !mf->Visible[player]) {
1012 mf->Visible[player] = 1;
1013 }
1014 }
1015 }
1016 }
1017
1018 //
1019 // Do a real hardcore seen recount. Now we remark EVERYTHING
1020 //
1021 for (i = 0; i < NumUnits; ++i) {
1022 if (!Units[i]->Destroyed) {
1023 MapMarkUnitSight(Units[i]);
1024 }
1025 }
1026 }
1027
1028 /**
1029 ** Player quit.
1030 **
1031 ** @param player Player number that quit.
1032 */
1033 void CommandQuit(int player)
1034 {
1035 int i;
1036
1037 // Set player to neutral, remove allied/enemy/shared vision status
1038 // If the player doesn't have any units then this is pointless?
1039 Players[player].Type = PlayerNeutral;
1040 for (i = 0; i < NumPlayers; ++i) {
1041 if (i != player && Players[i].Team != Players[player].Team) {
1042 Players[i].Allied &= ~(1 << player);
1043 Players[i].Enemy &= ~(1 << player);
1044 Players[player].Enemy &= ~(1 << i);
1045 Players[player].Allied &= ~(1 << i);
1046 // We clear Shared vision by sending fake shared vision commands.
1047 // We do this because Shared vision is a bit complex.
1048 CommandSharedVision(i, 0, player);
1049 CommandSharedVision(player, 0, i);
1050 // Remove Selection from Quit Player
1051 ChangeTeamSelectedUnits(&Players[player], NULL, 0, 0);
1052 }
1053 }
1054
1055 if (Players[player].TotalNumUnits != 0) {
1056 SetMessage(_("Player \"%s\" has left the game"), Players[player].Name.c_str());
1057 } else {
1058 SetMessage(_("Player \"%s\" has been killed"), Players[player].Name.c_str());
1059 }
1060 }
1061
1062 //@}