| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name upgrade.cpp - The upgrade/allow functions. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1999-2008 by Vladi Belperchinov-Shabanski 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 "stratagus.h" |
| 36 |
|
| 37 |
#include <stdio.h> |
| 38 |
#include <stdlib.h> |
| 39 |
#include <string.h> |
| 40 |
#include <string> |
| 41 |
#include <vector> |
| 42 |
#include <map> |
| 43 |
|
| 44 |
#include "upgrade.h" |
| 45 |
#include "player.h" |
| 46 |
#include "interface.h" |
| 47 |
#include "map.h" |
| 48 |
#include "script.h" |
| 49 |
#include "spells.h" |
| 50 |
#include "unit.h" |
| 51 |
#include "unittype.h" |
| 52 |
#include "actions.h" |
| 53 |
#include "iolib.h" |
| 54 |
|
| 55 |
/*---------------------------------------------------------------------------- |
| 56 |
-- Declarations |
| 57 |
----------------------------------------------------------------------------*/ |
| 58 |
|
| 59 |
static void AllowUnitId(CPlayer *player, int id, int units); |
| 60 |
|
| 61 |
/*---------------------------------------------------------------------------- |
| 62 |
-- Functions |
| 63 |
----------------------------------------------------------------------------*/ |
| 64 |
|
| 65 |
/** |
| 66 |
** Save state of the dependencies to file. |
| 67 |
** |
| 68 |
** @param file Output file. |
| 69 |
*/ |
| 70 |
void SaveUpgrades(CFile *file) |
| 71 |
{ |
| 72 |
file->printf("\n-- -----------------------------------------\n"); |
| 73 |
file->printf("-- MODULE: upgrades\n\n"); |
| 74 |
|
| 75 |
// |
| 76 |
// Save the allow |
| 77 |
// |
| 78 |
for (size_t i = 0; i < UnitTypes.size(); ++i) { |
| 79 |
file->printf("DefineUnitAllow(\"%s\", ", UnitTypes[i]->Ident.c_str()); |
| 80 |
for (int p = 0; p < PlayerMax; ++p) { |
| 81 |
if (p) { |
| 82 |
file->printf(", "); |
| 83 |
} |
| 84 |
file->printf("%d", Players[p].Allow.Units[i]); |
| 85 |
} |
| 86 |
file->printf(")\n"); |
| 87 |
} |
| 88 |
file->printf("\n"); |
| 89 |
} |
| 90 |
|
| 91 |
/*---------------------------------------------------------------------------- |
| 92 |
-- Ccl part of upgrades |
| 93 |
----------------------------------------------------------------------------*/ |
| 94 |
|
| 95 |
/** |
| 96 |
** Define which units are allowed and how much. |
| 97 |
*/ |
| 98 |
static int CclDefineUnitAllow(lua_State *l) |
| 99 |
{ |
| 100 |
const char *ident; |
| 101 |
int i; |
| 102 |
int args; |
| 103 |
int j; |
| 104 |
int id; |
| 105 |
|
| 106 |
args = lua_gettop(l); |
| 107 |
j = 0; |
| 108 |
ident = LuaToString(l, j + 1); |
| 109 |
++j; |
| 110 |
|
| 111 |
if (strncmp(ident, "unit-", 5)) { |
| 112 |
DebugPrint(" wrong ident %s\n" _C_ ident); |
| 113 |
return 0; |
| 114 |
} |
| 115 |
id = UnitTypeIdByIdent(ident); |
| 116 |
|
| 117 |
i = 0; |
| 118 |
for (; j < args && i < PlayerMax; ++j) { |
| 119 |
AllowUnitId(&Players[i], id, LuaToNumber(l, j + 1)); |
| 120 |
++i; |
| 121 |
} |
| 122 |
|
| 123 |
return 0; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
** Define which units/upgrades are allowed. |
| 128 |
*/ |
| 129 |
static int CclDefineAllow(lua_State *l) |
| 130 |
{ |
| 131 |
const char *ident; |
| 132 |
const char *ids; |
| 133 |
int n; |
| 134 |
int args; |
| 135 |
int id; |
| 136 |
|
| 137 |
args = lua_gettop(l); |
| 138 |
for (int j = 0; j < args; ++j) { |
| 139 |
ident = LuaToString(l, j + 1); |
| 140 |
++j; |
| 141 |
ids = LuaToString(l, j + 1); |
| 142 |
|
| 143 |
n = strlen(ids); |
| 144 |
if (n > PlayerMax) { |
| 145 |
fprintf(stderr, "%s: Allow string too long %d\n", ident, n); |
| 146 |
n = PlayerMax; |
| 147 |
} |
| 148 |
|
| 149 |
if (!strncmp(ident, "unit-", 5)) { |
| 150 |
id = UnitTypeIdByIdent(ident); |
| 151 |
for (int i = 0; i < n; ++i) { |
| 152 |
if (ids[i] == 'A') { |
| 153 |
AllowUnitId(&Players[i], id, UnitMax); |
| 154 |
} else if (ids[i] == 'F') { |
| 155 |
AllowUnitId(&Players[i], id, 0); |
| 156 |
} |
| 157 |
} |
| 158 |
} else { |
| 159 |
DebugPrint(" wrong ident %s\n" _C_ ident); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
return 0; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
** Register CCL features for upgrades. |
| 168 |
*/ |
| 169 |
void UpgradesCclRegister(void) |
| 170 |
{ |
| 171 |
lua_register(Lua, "DefineAllow", CclDefineAllow); |
| 172 |
lua_register(Lua, "DefineUnitAllow", CclDefineUnitAllow); |
| 173 |
} |
| 174 |
|
| 175 |
/*---------------------------------------------------------------------------- |
| 176 |
-- General/Map functions |
| 177 |
----------------------------------------------------------------------------*/ |
| 178 |
|
| 179 |
// AllowStruct will be static in the player so will be |
| 180 |
// load/saved with the player struct |
| 181 |
|
| 182 |
/** |
| 183 |
** UnitType ID by identifier. |
| 184 |
** |
| 185 |
** @param ident The unit-type identifier. |
| 186 |
** @return Unit-type ID (int) or -1 if not found. |
| 187 |
*/ |
| 188 |
int UnitTypeIdByIdent(const std::string &ident) |
| 189 |
{ |
| 190 |
const CUnitType *type; |
| 191 |
|
| 192 |
if ((type = UnitTypeByIdent(ident))) { |
| 193 |
return type->Slot; |
| 194 |
} |
| 195 |
DebugPrint(" fix this %s\n" _C_ ident.c_str()); |
| 196 |
Assert(0); |
| 197 |
return -1; |
| 198 |
} |
| 199 |
|
| 200 |
/*---------------------------------------------------------------------------- |
| 201 |
-- Allow(s) |
| 202 |
----------------------------------------------------------------------------*/ |
| 203 |
|
| 204 |
// all the following functions are just map handlers, no specific notes |
| 205 |
|
| 206 |
/** |
| 207 |
** Change allow for a unit-type. |
| 208 |
** |
| 209 |
** @param player Player to change |
| 210 |
** @param id unit type id |
| 211 |
** @param units maximum amount of units allowed |
| 212 |
*/ |
| 213 |
static void AllowUnitId(CPlayer *player, int id, int units) |
| 214 |
{ |
| 215 |
player->Allow.Units[id] = units; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
** Return the allow state of the unit. |
| 220 |
** |
| 221 |
** @param player Check state of this player. |
| 222 |
** @param id Unit identifier. |
| 223 |
** |
| 224 |
** @return the allow state of the unit. |
| 225 |
*/ |
| 226 |
int UnitIdAllowed(const CPlayer *player, int id) |
| 227 |
{ |
| 228 |
Assert(id >= 0 && id < UnitTypeMax); |
| 229 |
return player->Allow.Units[id]; |
| 230 |
} |
| 231 |
|
| 232 |
//@} |