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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <locale>
#include <cctype>
#include "UnitDefHandler.h"
#include "UnitDef.h"
#include "UnitDefImage.h"
#include "Lua/LuaParser.h"
#include "Rendering/Textures/Bitmap.h"
#include "Sim/Misc/SideParser.h"
#include "Sim/Misc/Team.h"
#include "Sim/Projectiles/ExplosionGenerator.h"
#include "System/Exceptions.h"
#include "System/Log/ILog.h"
#include "System/Util.h"
#include "System/FileSystem/FileHandler.h"
#include "System/Sound/ISound.h"
CUnitDefHandler* unitDefHandler = NULL;
#if defined(_MSC_VER) && (_MSC_VER < 1800)
bool isblank(int c) {
return (c == ' ') || (c == '\t') || (c == '\r') || (c == '\n');
}
#endif
CUnitDefHandler::CUnitDefHandler(LuaParser* defsParser) : noCost(false)
{
const LuaTable rootTable = defsParser->GetRoot().SubTable("UnitDefs");
if (!rootTable.IsValid()) {
throw content_error("Error loading UnitDefs");
}
vector<string> unitDefNames;
rootTable.GetKeys(unitDefNames);
unitDefs.reserve(unitDefNames.size() + 1);
unitDefs.emplace_back();
for (unsigned int a = 0; a < unitDefNames.size(); ++a) {
const string& unitName = unitDefNames[a];
LuaTable udTable = rootTable.SubTable(unitName);
// parse the unitdef data (but don't load buildpics, etc...)
PushNewUnitDef(StringToLower(unitName), udTable);
}
CleanBuildOptions();
FindStartUnits();
ProcessDecoys();
AssignTechLevels();
}
CUnitDefHandler::~CUnitDefHandler()
{ }
int CUnitDefHandler::PushNewUnitDef(const std::string& unitName, const LuaTable& udTable)
{
if (std::find_if(unitName.begin(), unitName.end(), isblank) != unitName.end()) {
LOG_L(L_WARNING,
"UnitDef name \"%s\" contains white-spaces, "
"which will likely cause problems. "
"Please contact the Game/Mod developers.",
unitName.c_str());
}
int defid = unitDefs.size();
try {
unitDefs.emplace_back(udTable, unitName, defid);
UnitDef& newDef = unitDefs.back();
UnitDefLoadSounds(&newDef, udTable);
if (!newDef.decoyName.empty()) {
decoyNameMap[unitName] = StringToLower(newDef.decoyName);
}
// force-initialize the real* members
newDef.SetNoCost(true);
newDef.SetNoCost(noCost);
} catch (const content_error& err) {
LOG_L(L_ERROR, "%s", err.what());
return 0;
}
unitDefIDsByName[unitName] = defid;
return defid;
}
void CUnitDefHandler::CleanBuildOptions()
{
// remove invalid build options
for (int i = 1; i < unitDefs.size(); i++) {
UnitDef& ud = unitDefs[i];
map<int, string>& bo = ud.buildOptions;
map<int, string>::iterator it = bo.begin();
while (it != bo.end()) {
bool erase = false;
const UnitDef* bd = GetUnitDefByName(it->second);
if (bd == NULL) {
LOG_L(L_WARNING,
"removed the \"%s\" entry from the \"%s\" build menu",
it->second.c_str(), ud.name.c_str());
erase = true;
}
/*
else if (bd->maxThisUnit <= 0) {
// don't remove, just grey out the icon
erase = true; // silent removal
}
*/
if (erase) {
map<int, string>::iterator tmp = it;
++it;
bo.erase(tmp);
} else {
++it;
}
}
}
}
void CUnitDefHandler::ProcessDecoys()
{
// assign the decoy pointers, and build the decoy map
map<string, string>::const_iterator mit;
for (mit = decoyNameMap.begin(); mit != decoyNameMap.end(); ++mit) {
map<string, int>::iterator fakeIt, realIt;
fakeIt = unitDefIDsByName.find(mit->first);
realIt = unitDefIDsByName.find(mit->second);
if ((fakeIt != unitDefIDsByName.end()) && (realIt != unitDefIDsByName.end())) {
UnitDef& fake = unitDefs[fakeIt->second];
UnitDef& real = unitDefs[realIt->second];
fake.decoyDef = ℜ
decoyMap[real.id].insert(fake.id);
}
}
decoyNameMap.clear();
}
void CUnitDefHandler::FindStartUnits()
{
for (unsigned int i = 0; i < sideParser.GetCount(); i++) {
const std::string& startUnit = sideParser.GetStartUnit(i);
if (!startUnit.empty()) {
std::map<std::string, int>::iterator it = unitDefIDsByName.find(startUnit);
if (it != unitDefIDsByName.end()) {
startUnitIDs.insert(it->second);
}
}
}
}
void CUnitDefHandler::UnitDefLoadSounds(UnitDef* ud, const LuaTable& udTable)
{
LuaTable soundsTable = udTable.SubTable("sounds");
LoadSounds(soundsTable, ud->sounds.ok, "ok"); // eg. "ok1", "ok2", ...
LoadSounds(soundsTable, ud->sounds.select, "select"); // eg. "select1", "select2", ...
LoadSounds(soundsTable, ud->sounds.arrived, "arrived"); // eg. "arrived1", "arrived2", ...
LoadSounds(soundsTable, ud->sounds.build, "build");
LoadSounds(soundsTable, ud->sounds.activate, "activate");
LoadSounds(soundsTable, ud->sounds.deactivate, "deactivate");
LoadSounds(soundsTable, ud->sounds.cant, "cant");
LoadSounds(soundsTable, ud->sounds.underattack, "underattack");
}
void CUnitDefHandler::LoadSounds(const LuaTable& soundsTable, GuiSoundSet& gsound, const string& soundName)
{
string fileName = soundsTable.GetString(soundName, "");
if (!fileName.empty()) {
LoadSound(gsound, fileName, 1.0f);
return;
}
LuaTable sndTable = soundsTable.SubTable(soundName);
for (int i = 1; true; i++) {
LuaTable sndFileTable = sndTable.SubTable(i);
if (sndFileTable.IsValid()) {
fileName = sndFileTable.GetString("file", "");
if (!fileName.empty()) {
const float volume = sndFileTable.GetFloat("volume", 1.0f);
if (volume > 0.0f) {
LoadSound(gsound, fileName, volume);
}
}
} else {
fileName = sndTable.GetString(i, "");
if (fileName.empty()) {
break;
}
LoadSound(gsound, fileName, 1.0f);
}
}
}
void CUnitDefHandler::LoadSound(GuiSoundSet& gsound, const string& fileName, const float volume)
{
const int id = LoadSoundFile(fileName);
if (id > 0)
{
GuiSoundSet::Data soundData(fileName, id, volume);
gsound.sounds.push_back(soundData);
}
}
const UnitDef* CUnitDefHandler::GetUnitDefByName(std::string name)
{
StringToLowerInPlace(name);
std::map<std::string, int>::iterator it = unitDefIDsByName.find(name);
if (it == unitDefIDsByName.end()) {
return NULL;
}
return &unitDefs[it->second];
}
const UnitDef* CUnitDefHandler::GetUnitDefByID(int defid)
{
if ((defid <= 0) || (defid >= unitDefs.size())) {
return NULL;
}
return &unitDefs[defid];
}
static bool LoadBuildPic(const string& filename, CBitmap& bitmap)
{
CFileHandler bfile(filename);
if (bfile.FileExists()) {
bitmap.Load(filename);
return true;
}
return false;
}
unsigned int CUnitDefHandler::GetUnitDefImage(const UnitDef* unitDef)
{
if (unitDef->buildPic != NULL) {
return (unitDef->buildPic->textureID);
}
SetUnitDefImage(unitDef, unitDef->buildPicName);
return unitDef->buildPic->textureID;
}
void CUnitDefHandler::SetUnitDefImage(const UnitDef* unitDef, const std::string& texName)
{
if (unitDef->buildPic == NULL) {
unitDef->buildPic = new UnitDefImage();
} else {
unitDef->buildPic->Free();
}
CBitmap bitmap;
if (!texName.empty()) {
bitmap.Load("unitpics/" + texName);
}
else {
if (!LoadBuildPic("unitpics/" + unitDef->name + ".dds", bitmap) &&
!LoadBuildPic("unitpics/" + unitDef->name + ".png", bitmap) &&
!LoadBuildPic("unitpics/" + unitDef->name + ".pcx", bitmap) &&
!LoadBuildPic("unitpics/" + unitDef->name + ".bmp", bitmap)) {
bitmap.AllocDummy(SColor(255, 0, 0, 255));
}
}
const unsigned int texID = bitmap.CreateTexture();
UnitDefImage* unitImage = unitDef->buildPic;
unitImage->textureID = texID;
unitImage->imageSizeX = bitmap.xsize;
unitImage->imageSizeY = bitmap.ysize;
}
void CUnitDefHandler::SetUnitDefImage(const UnitDef* unitDef,
unsigned int texID, int xsize, int ysize)
{
if (unitDef->buildPic == NULL) {
unitDef->buildPic = new UnitDefImage();
} else {
unitDef->buildPic->Free();
}
UnitDefImage* unitImage = unitDef->buildPic;
unitImage->textureID = texID;
unitImage->imageSizeX = xsize;
unitImage->imageSizeY = ysize;
}
bool CUnitDefHandler::ToggleNoCost()
{
noCost = !noCost;
for (int i = 1; i < unitDefs.size(); ++i) {
unitDefs[i].SetNoCost(noCost);
}
return noCost;
}
void CUnitDefHandler::AssignTechLevels()
{
set<int>::iterator it;
for (it = startUnitIDs.begin(); it != startUnitIDs.end(); ++it) {
AssignTechLevel(unitDefs[*it], 0);
}
}
void CUnitDefHandler::AssignTechLevel(UnitDef& ud, int level)
{
if ((ud.techLevel >= 0) && (ud.techLevel <= level)) {
return;
}
ud.techLevel = level;
level++;
map<int, std::string>::const_iterator bo_it;
for (bo_it = ud.buildOptions.begin(); bo_it != ud.buildOptions.end(); ++bo_it) {
std::map<std::string, int>::const_iterator ud_it = unitDefIDsByName.find(bo_it->second);
if (ud_it != unitDefIDsByName.end()) {
AssignTechLevel(unitDefs[ud_it->second], level);
}
}
}
|