2009-05-08 13:28:41 +00:00
|
|
|
/*
|
|
|
|
* OpenClonk, http://www.openclonk.org
|
|
|
|
*
|
2009-06-05 13:41:20 +00:00
|
|
|
* Copyright (c) 2002 Peter Wortmann
|
|
|
|
* Copyright (c) 2009 Günther Brammer
|
2009-05-08 13:28:41 +00:00
|
|
|
* Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de
|
|
|
|
*
|
|
|
|
* Portions might be copyrighted by other authors who have contributed
|
|
|
|
* to OpenClonk.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
* See isc_license.txt for full license and disclaimer.
|
|
|
|
*
|
|
|
|
* "Clonk" is a registered trademark of Matthes Bender.
|
|
|
|
* See clonk_trademark_license.txt for full license.
|
|
|
|
*/
|
|
|
|
/* string table: holds all strings used by script engine */
|
|
|
|
|
|
|
|
#ifndef C4STRINGTABLE_H
|
|
|
|
|
|
|
|
#define C4STRINGTABLE_H
|
|
|
|
|
2009-10-25 23:09:34 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2009-05-08 13:28:41 +00:00
|
|
|
class C4StringTable;
|
2009-10-25 23:09:34 +00:00
|
|
|
class C4Group;
|
2009-05-08 13:28:41 +00:00
|
|
|
|
|
|
|
class C4String
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
2010-12-08 22:27:02 +00:00
|
|
|
int RefCnt;
|
|
|
|
public:
|
|
|
|
unsigned int Hash;
|
|
|
|
private:
|
|
|
|
StdCopyStrBuf Data; // string data
|
|
|
|
|
2009-04-01 21:36:58 +00:00
|
|
|
explicit C4String(StdStrBuf strString);
|
2010-12-06 15:24:41 +00:00
|
|
|
C4String();
|
|
|
|
void operator=(const char * s);
|
2009-04-01 21:36:58 +00:00
|
|
|
|
2009-05-08 13:28:41 +00:00
|
|
|
friend class C4StringTable;
|
|
|
|
public:
|
2009-04-01 21:36:58 +00:00
|
|
|
~C4String();
|
2009-05-08 13:28:41 +00:00
|
|
|
|
2010-12-08 22:27:02 +00:00
|
|
|
// Add/Remove Reference
|
|
|
|
void IncRef() { ++RefCnt; }
|
|
|
|
void DecRef() { if (!--RefCnt) delete this; }
|
2009-05-08 13:28:41 +00:00
|
|
|
|
|
|
|
const char * GetCStr() const { return Data.getData(); }
|
|
|
|
StdStrBuf GetData() const { return Data.getRef(); }
|
|
|
|
|
2010-03-28 17:58:21 +00:00
|
|
|
};
|
2009-05-08 13:28:41 +00:00
|
|
|
|
2009-04-01 21:36:58 +00:00
|
|
|
template<typename T> class C4Set
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
2009-04-01 21:36:58 +00:00
|
|
|
unsigned int Capacity;
|
|
|
|
unsigned int Size;
|
|
|
|
T * Table;
|
2010-03-23 22:56:59 +00:00
|
|
|
T * AddInternal(T e)
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
2009-04-01 21:36:58 +00:00
|
|
|
unsigned int h = Hash(e);
|
|
|
|
T * p = &Table[h % Capacity];
|
|
|
|
while (*p && *p != e)
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
2009-04-01 21:36:58 +00:00
|
|
|
p = &Table[++h % Capacity];
|
2010-03-28 17:58:21 +00:00
|
|
|
}
|
2009-04-01 21:36:58 +00:00
|
|
|
*p = e;
|
2010-03-23 22:56:59 +00:00
|
|
|
return p;
|
2010-03-28 17:58:21 +00:00
|
|
|
}
|
2009-04-01 21:36:58 +00:00
|
|
|
public:
|
|
|
|
template<typename H> static unsigned int Hash(H);
|
|
|
|
template<typename H> static bool Equals(T, H);
|
|
|
|
static bool Equals(T a, T b) { return a == b; }
|
|
|
|
// FIXME: Profile for initial size
|
|
|
|
C4Set(): Capacity(16), Size(0), Table(new T[Capacity])
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
2009-04-01 21:36:58 +00:00
|
|
|
Clear();
|
2010-03-28 17:58:21 +00:00
|
|
|
}
|
2009-04-01 21:36:58 +00:00
|
|
|
~C4Set()
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
2009-04-01 21:36:58 +00:00
|
|
|
delete[] Table;
|
2010-03-28 17:58:21 +00:00
|
|
|
}
|
2010-03-21 23:40:03 +00:00
|
|
|
void CompileFunc(StdCompiler *pComp);
|
2009-04-01 21:36:58 +00:00
|
|
|
void Clear()
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
2009-04-01 21:36:58 +00:00
|
|
|
for (unsigned int i = 0; i < Capacity; ++i)
|
|
|
|
Table[i] = 0;
|
2010-03-28 17:58:21 +00:00
|
|
|
}
|
2010-03-23 22:56:59 +00:00
|
|
|
template<typename H> T & Get(H e) const
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
2009-04-01 21:36:58 +00:00
|
|
|
unsigned int h = Hash(e);
|
2009-05-19 22:12:11 +00:00
|
|
|
T * r = &Table[h % Capacity];
|
|
|
|
while (*r && !Equals(*r, e))
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
2009-05-19 22:12:11 +00:00
|
|
|
r = &Table[++h % Capacity];
|
|
|
|
}
|
2010-03-28 17:58:21 +00:00
|
|
|
return *r;
|
|
|
|
}
|
2010-03-23 22:56:59 +00:00
|
|
|
template<typename H> bool Has(H e) const
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
2009-05-19 22:12:11 +00:00
|
|
|
unsigned int h = Hash(e);
|
|
|
|
T * r = &Table[h % Capacity];
|
|
|
|
while (*r && !Equals(*r, e))
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
2009-05-19 22:12:11 +00:00
|
|
|
r = &Table[++h % Capacity];
|
2009-04-01 21:36:58 +00:00
|
|
|
}
|
2010-03-28 17:58:21 +00:00
|
|
|
return !!*r;
|
|
|
|
}
|
2010-09-10 21:01:30 +00:00
|
|
|
unsigned int GetSize() const { return Size; }
|
2010-03-23 22:56:59 +00:00
|
|
|
T * Add(T e)
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
2009-04-01 21:36:58 +00:00
|
|
|
// FIXME: Profile for load factor
|
|
|
|
if (Size > Capacity / 2)
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
2009-04-01 21:36:58 +00:00
|
|
|
unsigned int OCapacity = Capacity;
|
|
|
|
Capacity *= 2;
|
|
|
|
T * OTable = Table;
|
|
|
|
Table = new T[Capacity];
|
|
|
|
Clear();
|
|
|
|
for (unsigned int i = 0; i < OCapacity; ++i)
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
2009-04-01 21:36:58 +00:00
|
|
|
if (OTable[i])
|
|
|
|
AddInternal(OTable[i]);
|
|
|
|
}
|
2010-03-28 17:58:21 +00:00
|
|
|
delete [] OTable;
|
|
|
|
}
|
2010-03-23 22:56:59 +00:00
|
|
|
T * r = AddInternal(e);
|
2009-04-01 21:36:58 +00:00
|
|
|
++Size;
|
2010-03-23 22:56:59 +00:00
|
|
|
return r;
|
2010-03-28 17:58:21 +00:00
|
|
|
}
|
2009-04-01 21:36:58 +00:00
|
|
|
template<typename H> void Remove(H e)
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
2009-04-01 21:36:58 +00:00
|
|
|
unsigned int h = Hash(e);
|
|
|
|
T * r = &Table[h % Capacity];
|
|
|
|
while (*r && !Equals(*r, e))
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
2009-04-01 21:36:58 +00:00
|
|
|
r = &Table[++h % Capacity];
|
2010-03-28 17:58:21 +00:00
|
|
|
}
|
2009-04-01 21:36:58 +00:00
|
|
|
assert(*r);
|
|
|
|
*r = 0;
|
|
|
|
--Size;
|
|
|
|
// Move entries which might have collided with e
|
|
|
|
while (*(r = &Table[++h % Capacity]))
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
2009-04-01 21:36:58 +00:00
|
|
|
T m = *r;
|
|
|
|
*r = 0;
|
|
|
|
AddInternal(m);
|
|
|
|
}
|
2010-03-28 17:58:21 +00:00
|
|
|
}
|
2009-04-01 21:36:58 +00:00
|
|
|
T const * First() const { return Next(Table - 1); }
|
|
|
|
T const * Next(T const * p) const
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
2009-04-01 21:36:58 +00:00
|
|
|
while (++p != &Table[Capacity])
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
2009-04-01 21:36:58 +00:00
|
|
|
if (*p) return p;
|
|
|
|
}
|
2010-03-28 17:58:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
2009-05-08 13:28:41 +00:00
|
|
|
|
2009-04-01 21:36:58 +00:00
|
|
|
template<> template<>
|
|
|
|
inline unsigned int C4Set<C4String *>::Hash<const C4String *>(const C4String * e)
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
2009-04-01 21:36:58 +00:00
|
|
|
return e->Hash;
|
2010-03-28 17:58:21 +00:00
|
|
|
}
|
2009-04-01 21:36:58 +00:00
|
|
|
template<> template<>
|
|
|
|
inline unsigned int C4Set<C4String *>::Hash<C4String *>(C4String * e)
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
2009-04-01 21:36:58 +00:00
|
|
|
return e->Hash;
|
2010-03-28 17:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum C4PropertyName
|
|
|
|
{
|
|
|
|
P_Prototype,
|
|
|
|
P_Name,
|
|
|
|
P_Collectible,
|
|
|
|
P_ActMap,
|
|
|
|
P_Attach,
|
|
|
|
P_Visibility,
|
|
|
|
P_Parallaxity,
|
|
|
|
P_LineColors,
|
|
|
|
P_LineAttach,
|
|
|
|
P_PictureTransformation,
|
|
|
|
P_MeshTransformation,
|
|
|
|
P_Procedure,
|
2010-12-11 20:10:05 +00:00
|
|
|
P_Speed,
|
|
|
|
P_Accel,
|
|
|
|
P_Decel,
|
2010-03-28 17:58:21 +00:00
|
|
|
P_Directions,
|
|
|
|
P_FlipDir,
|
|
|
|
P_Length,
|
|
|
|
P_Delay,
|
|
|
|
P_X,
|
|
|
|
P_Y,
|
|
|
|
P_Wdt,
|
|
|
|
P_Hgt,
|
|
|
|
P_OffX,
|
|
|
|
P_OffY,
|
|
|
|
P_FacetBase,
|
|
|
|
P_FacetTopFace,
|
|
|
|
P_FacetTargetStretch,
|
|
|
|
P_NextAction,
|
|
|
|
P_Hold,
|
|
|
|
P_Idle,
|
|
|
|
P_NoOtherAction,
|
|
|
|
P_StartCall,
|
|
|
|
P_EndCall,
|
|
|
|
P_AbortCall,
|
|
|
|
P_PhaseCall,
|
|
|
|
P_Sound,
|
|
|
|
P_ObjectDisabled,
|
|
|
|
P_DigFree,
|
|
|
|
P_EnergyUsage,
|
|
|
|
P_InLiquidAction,
|
|
|
|
P_TurnAction,
|
|
|
|
P_Reverse,
|
|
|
|
P_Step,
|
|
|
|
P_MouseDragImage,
|
|
|
|
P_Animation,
|
|
|
|
P_Action,
|
2010-12-12 20:49:47 +00:00
|
|
|
P_BreatheWater,
|
|
|
|
P_CorrosionResist,
|
2010-12-12 21:38:15 +00:00
|
|
|
P_MaxEnergy,
|
2010-12-12 22:01:08 +00:00
|
|
|
P_MaxBreath,
|
2010-12-08 22:17:00 +00:00
|
|
|
// Default Action Procedures
|
|
|
|
DFA_WALK,
|
|
|
|
DFA_FLIGHT,
|
|
|
|
DFA_KNEEL,
|
|
|
|
DFA_SCALE,
|
|
|
|
DFA_HANGLE,
|
|
|
|
DFA_DIG,
|
|
|
|
DFA_SWIM,
|
|
|
|
DFA_THROW,
|
|
|
|
DFA_BRIDGE,
|
|
|
|
DFA_BUILD,
|
|
|
|
DFA_PUSH,
|
|
|
|
DFA_CHOP,
|
|
|
|
DFA_LIFT,
|
|
|
|
DFA_FLOAT,
|
|
|
|
DFA_ATTACH,
|
|
|
|
DFA_CONNECT,
|
|
|
|
DFA_PULL,
|
2010-03-28 17:58:21 +00:00
|
|
|
P_LAST
|
|
|
|
};
|
2009-05-19 22:12:11 +00:00
|
|
|
|
2010-07-06 00:10:54 +00:00
|
|
|
// There is only one Stringtable
|
2009-05-08 13:28:41 +00:00
|
|
|
class C4StringTable
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
2009-05-08 13:28:41 +00:00
|
|
|
public:
|
|
|
|
C4StringTable();
|
|
|
|
virtual ~C4StringTable();
|
|
|
|
|
|
|
|
C4String *RegString(StdStrBuf String);
|
|
|
|
C4String *RegString(const char * s) { return RegString(StdStrBuf(s)); }
|
2009-04-01 21:36:58 +00:00
|
|
|
// Find existing C4String
|
2009-05-08 13:28:41 +00:00
|
|
|
C4String *FindString(const char *strString);
|
|
|
|
|
2010-12-06 15:24:41 +00:00
|
|
|
C4String P[P_LAST];
|
|
|
|
private:
|
2009-04-01 21:36:58 +00:00
|
|
|
C4Set<C4String *> Set;
|
2010-12-06 15:24:41 +00:00
|
|
|
friend class C4String;
|
2010-03-28 17:58:21 +00:00
|
|
|
};
|
2009-05-08 13:28:41 +00:00
|
|
|
|
2009-04-03 21:53:48 +00:00
|
|
|
extern C4StringTable Strings;
|
|
|
|
|
2009-05-08 13:28:41 +00:00
|
|
|
#endif
|