openclonk/src/object/C4ObjectInfo.cpp

206 lines
4.7 KiB
C++
Raw Normal View History

2009-05-08 13:28:41 +00:00
/*
* OpenClonk, http://www.openclonk.org
*
* Copyright (c) 1998-2000, Matthes Bender
* Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
* Copyright (c) 2009-2013, The OpenClonk Team and contributors
2009-05-08 13:28:41 +00:00
*
* Distributed under the terms of the ISC license; see accompanying file
* "COPYING" for details.
2009-05-08 13:28:41 +00:00
*
* "Clonk" is a registered trademark of Matthes Bender, used with permission.
* See accompanying file "TRADEMARK" for details.
2009-05-08 13:28:41 +00:00
*
* To redistribute this file separately, substitute the full license texts
* for the above references.
2009-05-08 13:28:41 +00:00
*/
/* Holds crew member information */
#include <C4Include.h>
#include <C4ObjectInfo.h>
#include <C4DefList.h>
2009-05-08 13:28:41 +00:00
#include <C4Random.h>
#include <C4Components.h>
#include <C4Game.h>
#include <C4Config.h>
#include <C4Application.h>
#include <C4RankSystem.h>
#include <C4Log.h>
#include <C4Player.h>
#include <C4GraphicsResource.h>
2009-06-12 23:09:32 +00:00
#include <C4PlayerList.h>
2009-05-08 13:28:41 +00:00
#ifdef _MSC_VER
#define snprintf _snprintf
#endif
C4ObjectInfo::C4ObjectInfo()
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
Default();
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
C4ObjectInfo::~C4ObjectInfo()
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
Clear();
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4ObjectInfo::Default()
2010-03-28 18:58:01 +00:00
{
WasInAction=false;
InAction=false;
2009-05-08 13:28:41 +00:00
InActionTime=0;
HasDied=false;
2009-05-08 13:28:41 +00:00
ControlCount=0;
Filename[0]=0;
Next=NULL;
2009-05-08 13:28:41 +00:00
pDef = NULL;
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
bool C4ObjectInfo::Load(C4Group &hMother, const char *szEntryname)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// New version
2011-03-13 15:56:26 +00:00
if (SEqualNoCase(GetExtension(szEntryname),"oci"))
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
C4Group hChild;
if (hChild.OpenAsChild(&hMother,szEntryname))
2010-03-28 18:58:01 +00:00
{
if (!C4ObjectInfo::Load(hChild))
{ hChild.Close(); return false; }
2009-05-08 13:28:41 +00:00
// resolve definition, if possible
// only works in game, but is not needed in frontend or startup editing anyway
pDef = C4Id2Def(id);
hChild.Close();
return true;
2009-05-08 13:28:41 +00:00
}
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
return false;
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
bool C4ObjectInfo::Load(C4Group &hGroup)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// Store group file name
SCopy(GetFilename(hGroup.GetName()),Filename,_MAX_FNAME);
// Load core
if (!C4ObjectInfoCore::Load(hGroup)) return false;
2010-03-28 18:58:01 +00:00
return true;
}
2009-05-08 13:28:41 +00:00
bool C4ObjectInfo::Save(C4Group &hGroup, bool fStoreTiny, C4DefList *pDefs)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// Set group file name; rename if necessary
char szTempGroup[_MAX_PATH+1];
SCopy(Name, szTempGroup, _MAX_PATH);
MakeFilenameFromTitle(szTempGroup);
2011-03-13 15:56:26 +00:00
SAppend(".oci",szTempGroup, _MAX_PATH);
2009-05-08 13:28:41 +00:00
if (!SEqualNoCase(Filename, szTempGroup))
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
if (!Filename[0])
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// first time creation of file - make sure it's not a duplicate
SCopy(szTempGroup, Filename, _MAX_PATH);
while (hGroup.FindEntry(Filename))
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// if a crew info of that name exists already, rename!
RemoveExtension(Filename);
int32_t iFinNum = GetTrailingNumber(Filename), iLen = SLen(Filename);
while (iLen && Inside(Filename[iLen-1], '0', '9')) --iLen;
if (iLen>_MAX_PATH-22) { LogF("Error generating unique filename for %s(%s): Path overflow", Name, hGroup.GetFullName().getData()); break; }
snprintf(Filename+iLen, 22, "%d", iFinNum+1);
2011-03-13 15:56:26 +00:00
EnforceExtension(Filename, "oci");
2009-05-08 13:28:41 +00:00
}
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
else
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// Crew was renamed; file rename necessary, if the name is not blocked by another crew info
if (!hGroup.FindEntry(szTempGroup))
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
if (hGroup.Rename(Filename, szTempGroup))
SCopy(szTempGroup, Filename, _MAX_PATH);
else
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// could not rename. Not fatal; just use old file
LogF("Error adjusting crew info for %s into %s: Rename error from %s to %s!", Name, hGroup.GetFullName().getData(), Filename, szTempGroup);
2010-01-25 04:00:59 +00:00
}
2009-05-08 13:28:41 +00:00
}
}
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
// Open group
C4Group hTemp;
if (!hTemp.OpenAsChild(&hGroup, Filename, false, true))
return false;
2009-05-08 13:28:41 +00:00
// custom rank image present?
if (pDefs && !fStoreTiny)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
C4Def *pDef = pDefs->ID2Def(id);
if (pDef)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
if (pDef->pRankSymbols)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
C4FacetSurface fctRankSymbol;
if (C4RankSystem::DrawRankSymbol(&fctRankSymbol, Rank, pDef->pRankSymbols, pDef->iNumRankSymbols, true))
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
fctRankSymbol.GetFace().SavePNG(hTemp, C4CFN_ClonkRank);
}
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
else
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// definition does not have custom rank symbols: Remove any rank image from Clonk
hTemp.Delete(C4CFN_ClonkRank);
}
}
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
// Save info to temp group
if (!C4ObjectInfoCore::Save(hTemp, pDefs))
{ hTemp.Close(); return false; }
2009-05-08 13:28:41 +00:00
// Close temp group
hTemp.Close();
// Success
return true;
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4ObjectInfo::Evaluate()
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
Retire();
if (WasInAction) Rounds++;
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4ObjectInfo::Clear()
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
pDef=NULL;
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4ObjectInfo::Recruit()
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// already recruited?
if (InAction) return;
WasInAction=true;
InAction=true;
2009-05-08 13:28:41 +00:00
InActionTime=Game.Time;
// rank name overload by def?
2009-06-05 16:46:37 +00:00
C4Def *pDef=::Definitions.ID2Def(id);
2009-05-08 13:28:41 +00:00
if (pDef) if (pDef->pRankNames)
{
2010-03-28 18:58:01 +00:00
StdStrBuf sRank(pDef->pRankNames->GetRankName(Rank, true));
if (sRank) sRankName.Copy(sRank);
2009-05-08 13:28:41 +00:00
}
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4ObjectInfo::Retire()
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// not recruited?
if (!InAction) return;
// retire
InAction=false;
2009-05-08 13:28:41 +00:00
TotalPlayingTime+=(Game.Time-InActionTime);
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4ObjectInfo::SetBirthday()
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
Birthday=time(NULL);
2010-03-28 18:58:01 +00:00
}