| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name groups.cpp - The units' groups handling. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1999-2007 by Patrice Fortier, Lutz Sammer, |
| 13 |
// and Jimmy Salmon |
| 14 |
// |
| 15 |
// This program is free software; you can redistribute it and/or modify |
| 16 |
// it under the terms of the GNU General Public License as published by |
| 17 |
// the Free Software Foundation; only version 2 of the License. |
| 18 |
// |
| 19 |
// This program is distributed in the hope that it will be useful, |
| 20 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
// GNU General Public License for more details. |
| 23 |
// |
| 24 |
// You should have received a copy of the GNU General Public License |
| 25 |
// along with this program; if not, write to the Free Software |
| 26 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 27 |
// 02111-1307, USA. |
| 28 |
// |
| 29 |
|
| 30 |
//@{ |
| 31 |
|
| 32 |
/*---------------------------------------------------------------------------- |
| 33 |
-- Includes |
| 34 |
----------------------------------------------------------------------------*/ |
| 35 |
|
| 36 |
#include <stdio.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#include <string.h> |
| 39 |
|
| 40 |
#include "stratagus.h" |
| 41 |
#include "unit.h" |
| 42 |
#include "unit_manager.h" |
| 43 |
#include "unittype.h" |
| 44 |
#include "script.h" |
| 45 |
#include "player.h" |
| 46 |
#include "iolib.h" |
| 47 |
|
| 48 |
/*---------------------------------------------------------------------------- |
| 49 |
-- Variables |
| 50 |
----------------------------------------------------------------------------*/ |
| 51 |
|
| 52 |
/** |
| 53 |
** Defines a group of units. |
| 54 |
*/ |
| 55 |
struct CUnitGroup { |
| 56 |
CUnit **Units; /// Units in the group |
| 57 |
int NumUnits; /// How many units in the group |
| 58 |
}; /// group of units |
| 59 |
|
| 60 |
static CUnitGroup Groups[NUM_GROUPS]; /// Number of groups predefined |
| 61 |
|
| 62 |
/*---------------------------------------------------------------------------- |
| 63 |
-- Functions |
| 64 |
----------------------------------------------------------------------------*/ |
| 65 |
|
| 66 |
/** |
| 67 |
** Initialize group part. |
| 68 |
** |
| 69 |
** @todo Not needed with the new unit code! |
| 70 |
*/ |
| 71 |
void InitGroups(void) |
| 72 |
{ |
| 73 |
for (int i = 0; i < NUM_GROUPS; ++i) { |
| 74 |
if (!Groups[i].Units) { |
| 75 |
Groups[i].Units = new CUnit *[MaxSelectable]; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
** Save groups. |
| 82 |
** |
| 83 |
** @param file Output file. |
| 84 |
*/ |
| 85 |
void SaveGroups(CFile *file) |
| 86 |
{ |
| 87 |
file->printf("\n--- -----------------------------------------\n"); |
| 88 |
file->printf("--- MODULE: groups\n\n"); |
| 89 |
|
| 90 |
for (int g = 0; g < NUM_GROUPS; ++g) { |
| 91 |
file->printf("Group(%d, %d, {", g, Groups[g].NumUnits); |
| 92 |
for (int i = 0; i < Groups[g].NumUnits; ++i) { |
| 93 |
file->printf("\"%s\", ", UnitReference(Groups[g].Units[i]).c_str()); |
| 94 |
} |
| 95 |
file->printf("})\n"); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
** Clean up group part. |
| 101 |
*/ |
| 102 |
void CleanGroups(void) |
| 103 |
{ |
| 104 |
for (int i = 0; i < NUM_GROUPS; ++i) { |
| 105 |
delete[] Groups[i].Units; |
| 106 |
Groups[i].Units = NULL; |
| 107 |
Groups[i].NumUnits = 0; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
** Return the number of units of group num |
| 113 |
** |
| 114 |
** @param num Group number. |
| 115 |
** |
| 116 |
** @return Returns the number of units in the group. |
| 117 |
*/ |
| 118 |
int GetNumberUnitsOfGroup(int num) |
| 119 |
{ |
| 120 |
return Groups[num].NumUnits; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
** Return the units of group num |
| 125 |
** |
| 126 |
** @param num Group number. |
| 127 |
** |
| 128 |
** @return Returns an array of all units in the group. |
| 129 |
*/ |
| 130 |
CUnit **GetUnitsOfGroup(int num) |
| 131 |
{ |
| 132 |
return Groups[num].Units; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
** Clear contents of group num |
| 137 |
** |
| 138 |
** @param num Group number. |
| 139 |
*/ |
| 140 |
void ClearGroup(int num) |
| 141 |
{ |
| 142 |
CUnitGroup *group; |
| 143 |
int i; |
| 144 |
|
| 145 |
group = &Groups[num]; |
| 146 |
for (i = 0; i < group->NumUnits; ++i) { |
| 147 |
group->Units[i]->GroupId &= ~(1 << num); |
| 148 |
Assert(!group->Units[i]->Destroyed); |
| 149 |
} |
| 150 |
group->NumUnits = 0; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
** Add units to group num contents from unit array "units" |
| 155 |
** |
| 156 |
** @param units Array of units to place into group. |
| 157 |
** @param nunits Number of units in array. |
| 158 |
** @param num Group number for storage. |
| 159 |
*/ |
| 160 |
void AddToGroup(CUnit **units, int nunits, int num) |
| 161 |
{ |
| 162 |
CUnitGroup *group; |
| 163 |
int i; |
| 164 |
|
| 165 |
Assert(num <= NUM_GROUPS); |
| 166 |
|
| 167 |
group = &Groups[num]; |
| 168 |
// Check to make sure we don't have a building as the only group member |
| 169 |
// If so, we are unable to add these units to the group. |
| 170 |
if (group->NumUnits == 1 && !group->Units[0]->Type->SelectableByRectangle) { |
| 171 |
return; |
| 172 |
} |
| 173 |
for (i = 0; group->NumUnits < MaxSelectable && i < nunits; ++i) { |
| 174 |
// Add to group only if they are on our team, and are rectangle |
| 175 |
// selectable. Otherwise buildings and units can be in a group. |
| 176 |
// or enemy units and my units. Exceptions is when there is only |
| 177 |
// one unit in the group, then we can group a buildings. |
| 178 |
if (ThisPlayer->IsTeamed(units[i]) && |
| 179 |
(units[i]->Type->SelectableByRectangle || |
| 180 |
(nunits == 1 && group->NumUnits == 0))) { |
| 181 |
group->Units[group->NumUnits++] = units[i]; |
| 182 |
units[i]->GroupId |= (1 << num); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
** Set group num contents to unit array "units" |
| 189 |
** |
| 190 |
** @param units Array of units to place into group. |
| 191 |
** @param nunits Number of units in array. |
| 192 |
** @param num Group number for storage. |
| 193 |
*/ |
| 194 |
void SetGroup(CUnit **units, int nunits, int num) |
| 195 |
{ |
| 196 |
Assert(num <= NUM_GROUPS && nunits <= MaxSelectable); |
| 197 |
|
| 198 |
ClearGroup(num); |
| 199 |
AddToGroup(units, nunits, num); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
** Remove unit from its groups |
| 204 |
** |
| 205 |
** @param unit Unit to remove from group. |
| 206 |
*/ |
| 207 |
void RemoveUnitFromGroups(CUnit *unit) |
| 208 |
{ |
| 209 |
CUnitGroup *group; |
| 210 |
int num; |
| 211 |
int i; |
| 212 |
|
| 213 |
Assert(unit->GroupId != 0); // unit doesn't belong to a group |
| 214 |
|
| 215 |
for (num = 0; unit->GroupId; ++num, unit->GroupId >>= 1) { |
| 216 |
if ((unit->GroupId & 1) != 1) { |
| 217 |
continue; |
| 218 |
} |
| 219 |
|
| 220 |
group = &Groups[num]; |
| 221 |
for (i = 0; group->Units[i] != unit; ++i) { |
| 222 |
; |
| 223 |
} |
| 224 |
|
| 225 |
Assert(i < group->NumUnits); // oops not found |
| 226 |
|
| 227 |
// This is a clean way that will allow us to add a unit |
| 228 |
// to a group easily, or make an easy array walk... |
| 229 |
if (i < --group->NumUnits) { |
| 230 |
group->Units[i] = group->Units[group->NumUnits]; |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
// ---------------------------------------------------------------------------- |
| 236 |
|
| 237 |
/** |
| 238 |
** Define the group. |
| 239 |
** |
| 240 |
** @param l Lua state. |
| 241 |
*/ |
| 242 |
static int CclGroup(lua_State *l) |
| 243 |
{ |
| 244 |
int i; |
| 245 |
CUnitGroup *grp; |
| 246 |
int args; |
| 247 |
int j; |
| 248 |
|
| 249 |
LuaCheckArgs(l, 3); |
| 250 |
|
| 251 |
InitGroups(); |
| 252 |
grp = &Groups[(int)LuaToNumber(l, 1)]; |
| 253 |
grp->NumUnits = LuaToNumber(l, 2); |
| 254 |
i = 0; |
| 255 |
args = lua_objlen(l, 3); |
| 256 |
for (j = 0; j < args; ++j) { |
| 257 |
const char *str; |
| 258 |
|
| 259 |
str = LuaToString(l, 3, j + 1); |
| 260 |
grp->Units[i++] = UnitSlots[strtol(str + 1, NULL, 16)]; |
| 261 |
} |
| 262 |
|
| 263 |
return 0; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
** Register CCL features for groups. |
| 268 |
*/ |
| 269 |
void GroupCclRegister(void) |
| 270 |
{ |
| 271 |
lua_register(Lua, "Group", CclGroup); |
| 272 |
} |
| 273 |
|
| 274 |
//@} |