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
|
// ____ _ __
// / __ )____ _____ | | / /___ ___________
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
// /_____/\____/____/ |__/|__/\__,_/_/ /____/
//
// A futuristic real-time strategy game.
// This file is part of Bos Wars.
//
/**@name groups.cpp - The units' groups handling. */
//
// (c) Copyright 1999-2007 by Patrice Fortier, 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.
//
//@{
/*----------------------------------------------------------------------------
-- Includes
----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stratagus.h"
#include "unit.h"
#include "unit_manager.h"
#include "unittype.h"
#include "script.h"
#include "player.h"
#include "iolib.h"
/*----------------------------------------------------------------------------
-- Variables
----------------------------------------------------------------------------*/
/**
** Defines a group of units.
*/
struct CUnitGroup {
CUnit **Units; /// Units in the group
int NumUnits; /// How many units in the group
}; /// group of units
static CUnitGroup Groups[NUM_GROUPS]; /// Number of groups predefined
/*----------------------------------------------------------------------------
-- Functions
----------------------------------------------------------------------------*/
/**
** Initialize group part.
**
** @todo Not needed with the new unit code!
*/
void InitGroups(void)
{
for (int i = 0; i < NUM_GROUPS; ++i) {
if (!Groups[i].Units) {
Groups[i].Units = new CUnit *[MaxSelectable];
}
}
}
/**
** Save groups.
**
** @param file Output file.
*/
void SaveGroups(CFile *file)
{
file->printf("\n--- -----------------------------------------\n");
file->printf("--- MODULE: groups\n\n");
for (int g = 0; g < NUM_GROUPS; ++g) {
file->printf("Group(%d, %d, {", g, Groups[g].NumUnits);
for (int i = 0; i < Groups[g].NumUnits; ++i) {
file->printf("\"%s\", ", UnitReference(Groups[g].Units[i]).c_str());
}
file->printf("})\n");
}
}
/**
** Clean up group part.
*/
void CleanGroups(void)
{
for (int i = 0; i < NUM_GROUPS; ++i) {
delete[] Groups[i].Units;
Groups[i].Units = NULL;
Groups[i].NumUnits = 0;
}
}
/**
** Return the number of units of group num
**
** @param num Group number.
**
** @return Returns the number of units in the group.
*/
int GetNumberUnitsOfGroup(int num)
{
return Groups[num].NumUnits;
}
/**
** Return the units of group num
**
** @param num Group number.
**
** @return Returns an array of all units in the group.
*/
CUnit **GetUnitsOfGroup(int num)
{
return Groups[num].Units;
}
/**
** Clear contents of group num
**
** @param num Group number.
*/
void ClearGroup(int num)
{
CUnitGroup *group;
int i;
group = &Groups[num];
for (i = 0; i < group->NumUnits; ++i) {
group->Units[i]->GroupId &= ~(1 << num);
Assert(!group->Units[i]->Destroyed);
}
group->NumUnits = 0;
}
/**
** Add units to group num contents from unit array "units"
**
** @param units Array of units to place into group.
** @param nunits Number of units in array.
** @param num Group number for storage.
*/
void AddToGroup(CUnit **units, int nunits, int num)
{
CUnitGroup *group;
int i;
Assert(num <= NUM_GROUPS);
group = &Groups[num];
// Check to make sure we don't have a building as the only group member
// If so, we are unable to add these units to the group.
if (group->NumUnits == 1 && !group->Units[0]->Type->SelectableByRectangle) {
return;
}
for (i = 0; group->NumUnits < MaxSelectable && i < nunits; ++i) {
// Add to group only if they are on our team, and are rectangle
// selectable. Otherwise buildings and units can be in a group.
// or enemy units and my units. Exceptions is when there is only
// one unit in the group, then we can group a buildings.
if (ThisPlayer->IsTeamed(units[i]) &&
(units[i]->Type->SelectableByRectangle ||
(nunits == 1 && group->NumUnits == 0))) {
group->Units[group->NumUnits++] = units[i];
units[i]->GroupId |= (1 << num);
}
}
}
/**
** Set group num contents to unit array "units"
**
** @param units Array of units to place into group.
** @param nunits Number of units in array.
** @param num Group number for storage.
*/
void SetGroup(CUnit **units, int nunits, int num)
{
Assert(num <= NUM_GROUPS && nunits <= MaxSelectable);
ClearGroup(num);
AddToGroup(units, nunits, num);
}
/**
** Remove unit from its groups
**
** @param unit Unit to remove from group.
*/
void RemoveUnitFromGroups(CUnit *unit)
{
CUnitGroup *group;
int num;
int i;
Assert(unit->GroupId != 0); // unit doesn't belong to a group
for (num = 0; unit->GroupId; ++num, unit->GroupId >>= 1) {
if ((unit->GroupId & 1) != 1) {
continue;
}
group = &Groups[num];
for (i = 0; group->Units[i] != unit; ++i) {
;
}
Assert(i < group->NumUnits); // oops not found
// This is a clean way that will allow us to add a unit
// to a group easily, or make an easy array walk...
if (i < --group->NumUnits) {
group->Units[i] = group->Units[group->NumUnits];
}
}
}
// ----------------------------------------------------------------------------
/**
** Define the group.
**
** @param l Lua state.
*/
static int CclGroup(lua_State *l)
{
int i;
CUnitGroup *grp;
int args;
int j;
LuaCheckArgs(l, 3);
InitGroups();
grp = &Groups[(int)LuaToNumber(l, 1)];
grp->NumUnits = LuaToNumber(l, 2);
i = 0;
args = lua_objlen(l, 3);
for (j = 0; j < args; ++j) {
const char *str;
str = LuaToString(l, 3, j + 1);
grp->Units[i++] = UnitSlots[strtol(str + 1, NULL, 16)];
}
return 0;
}
/**
** Register CCL features for groups.
*/
void GroupCclRegister(void)
{
lua_register(Lua, "Group", CclGroup);
}
//@}
|