____ _ __
/ __ )____ _____ | | / /___ ___________
/ __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
/ /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
/_____/\____/____/ |__/|__/\__,_/_/ /____/
A futuristic real-time strategy game.
This file is part of Bos Wars.
(C) Copyright 2001-2007 by the Bos Wars and Stratagus Project.
Distributed under the "GNU General Public License"00001 // ____ _ __ 00002 // / __ )____ _____ | | / /___ ___________ 00003 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ 00004 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) 00005 // /_____/\____/____/ |__/|__/\__,_/_/ /____/ 00006 // 00007 // A futuristic real-time strategy game. 00008 // This file is part of Bos Wars. 00009 // 00011 // 00012 // (c) Copyright 1998-2007 by Lutz Sammer and Jimmy Salmon 00013 // 00014 // This program is free software; you can redistribute it and/or modify 00015 // it under the terms of the GNU General Public License as published by 00016 // the Free Software Foundation; only version 2 of the License. 00017 // 00018 // This program is distributed in the hope that it will be useful, 00019 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00021 // GNU General Public License for more details. 00022 // 00023 // You should have received a copy of the GNU General Public License 00024 // along with this program; if not, write to the Free Software 00025 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 00026 // 02111-1307, USA. 00027 // 00028 00030 00031 /*---------------------------------------------------------------------------- 00032 -- Includes 00033 ----------------------------------------------------------------------------*/ 00034 00035 #include <stdio.h> 00036 #include <stdlib.h> 00037 00038 #include "stratagus.h" 00039 #include "unit.h" 00040 #include "unittype.h" 00041 #include "actions.h" 00042 #include "pathfinder.h" 00043 #include "map.h" 00044 00045 /*---------------------------------------------------------------------------- 00046 -- Functions 00047 ----------------------------------------------------------------------------*/ 00048 00049 extern bool AutoRepair(CUnit *unit); 00050 00051 00055 static void SwapPatrolPoints(CUnit *unit) 00056 { 00057 int tmp; 00058 00059 tmp = unit->Orders[0]->Arg1.Patrol.X; 00060 unit->Orders[0]->Arg1.Patrol.X = unit->Orders[0]->X; 00061 unit->Orders[0]->X = tmp; 00062 tmp = unit->Orders[0]->Arg1.Patrol.Y; 00063 unit->Orders[0]->Arg1.Patrol.Y = unit->Orders[0]->Y; 00064 unit->Orders[0]->Y = tmp; 00065 00066 NewResetPath(unit); 00067 } 00068 00080 void HandleActionPatrol(CUnit *unit) 00081 { 00082 if (unit->Wait) { 00083 unit->Wait--; 00084 return; 00085 } 00086 00087 if (!unit->SubAction) { // first entry. 00088 NewResetPath(unit); 00089 unit->SubAction = 1; 00090 } 00091 00092 switch (DoActionMove(unit)) { 00093 case PF_FAILED: 00094 unit->SubAction = 1; 00095 break; 00096 case PF_UNREACHABLE: 00097 // Increase range and try again 00098 unit->SubAction = 1; 00099 if (unit->Orders[0]->Range <= Map.Info.MapWidth || 00100 unit->Orders[0]->Range <= Map.Info.MapHeight) { 00101 unit->Orders[0]->Range++; 00102 break; 00103 } 00104 // FALL THROUGH 00105 case PF_REACHED: 00106 unit->SubAction = 1; 00107 unit->Orders[0]->Range = 0; 00108 SwapPatrolPoints(unit); 00109 break; 00110 case PF_WAIT: 00111 // Wait for a while then give up 00112 unit->SubAction++; 00113 if (unit->SubAction == 5) { 00114 unit->SubAction = 1; 00115 unit->Orders[0]->Range = 0; 00116 SwapPatrolPoints(unit); 00117 } 00118 break; 00119 default: // moving 00120 unit->SubAction = 1; 00121 break; 00122 } 00123 00124 if (!unit->Anim.Unbreakable) { 00125 // 00126 // Attack any enemy in reaction range. 00127 // If don't set the goal, the unit can then choose a 00128 // better goal if moving nearer to enemy. 00129 // 00130 if (unit->Type->CanAttack) { 00131 const CUnit *goal = AttackUnitsInReactRange(unit); 00132 if (goal) { 00133 DebugPrint("Patrol attack %d\n" _C_ UnitNumber(goal)); 00134 CommandAttack(unit, goal->X, goal->Y, NULL, FlushCommands); 00135 // Save current command to come back. 00136 unit->SavedOrder = *unit->Orders[0]; 00137 unit->ClearAction(); 00138 unit->Orders[0]->Goal = NoUnitP; 00139 return; 00140 } 00141 } 00142 00143 // Look for something to auto repair 00144 if (AutoRepair(unit)) { 00145 return; 00146 } 00147 } 00148 } 00149
1.5.6