Refactored CBotVar to templates
parent
0e101debe0
commit
df111dbf98
|
@ -61,6 +61,11 @@ CBotVar::CBotVar( )
|
||||||
m_mPrivate = ProtectionLevel::Public;
|
m_mPrivate = ProtectionLevel::Public;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CBotVar::CBotVar(const CBotToken &name) : CBotVar()
|
||||||
|
{
|
||||||
|
m_token = new CBotToken(name);
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
CBotVar::~CBotVar( )
|
CBotVar::~CBotVar( )
|
||||||
{
|
{
|
||||||
|
@ -698,7 +703,16 @@ void CBotVar::Dec()
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
void CBotVar::Copy(CBotVar* pSrc, bool bName)
|
void CBotVar::Copy(CBotVar* pSrc, bool bName)
|
||||||
{
|
{
|
||||||
assert(0);
|
if (bName) *m_token = *pSrc->m_token;
|
||||||
|
m_type = pSrc->m_type;
|
||||||
|
m_binit = pSrc->m_binit;
|
||||||
|
//- m_bStatic = pSrc->m_bStatic;
|
||||||
|
m_next = nullptr;
|
||||||
|
m_pMyThis = nullptr;//p->m_pMyThis;
|
||||||
|
m_pUserPtr = pSrc->m_pUserPtr;
|
||||||
|
|
||||||
|
// keeps indentificator the same (by default)
|
||||||
|
if (m_ident == 0) m_ident = pSrc->m_ident;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -727,4 +741,5 @@ CBotClass* CBotVar::GetClass()
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace CBot
|
} // namespace CBot
|
||||||
|
|
|
@ -50,6 +50,11 @@ public:
|
||||||
*/
|
*/
|
||||||
CBotVar();
|
CBotVar();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Constructor. Do not call directly, use CBotVar::Create()
|
||||||
|
*/
|
||||||
|
CBotVar(const CBotToken& name);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Destructor. Do not call directly, use CBotVar::Destroy()
|
* \brief Destructor. Do not call directly, use CBotVar::Destroy()
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -19,137 +19,27 @@
|
||||||
|
|
||||||
#include "CBot/CBotVar/CBotVarBoolean.h"
|
#include "CBot/CBotVar/CBotVarBoolean.h"
|
||||||
|
|
||||||
#include "CBot/CBotEnums.h"
|
|
||||||
#include "CBot/CBotUtils.h"
|
|
||||||
|
|
||||||
#include "CBot/CBotToken.h"
|
|
||||||
|
|
||||||
|
|
||||||
namespace CBot
|
namespace CBot
|
||||||
{
|
{
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
CBotVarBoolean::CBotVarBoolean(const CBotToken& name)
|
|
||||||
{
|
|
||||||
m_token = new CBotToken(name);
|
|
||||||
m_next = nullptr;
|
|
||||||
m_pMyThis = nullptr;
|
|
||||||
m_pUserPtr = nullptr;
|
|
||||||
m_InitExpr = nullptr;
|
|
||||||
m_LimExpr = nullptr;
|
|
||||||
m_type = CBotTypBoolean;
|
|
||||||
m_binit = InitType::UNDEF;
|
|
||||||
m_bStatic = false;
|
|
||||||
m_mPrivate = ProtectionLevel::Public;
|
|
||||||
m_val = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarBoolean::Copy(CBotVar* pSrc, bool bName)
|
|
||||||
{
|
|
||||||
CBotVarBoolean* p = static_cast<CBotVarBoolean*>(pSrc);
|
|
||||||
|
|
||||||
if (bName) *m_token = *p->m_token;
|
|
||||||
m_type = p->m_type;
|
|
||||||
m_val = p->m_val;
|
|
||||||
m_binit = p->m_binit;
|
|
||||||
//- m_bStatic = p->m_bStatic;
|
|
||||||
m_next = nullptr;
|
|
||||||
m_pMyThis = nullptr;//p->m_pMyThis;
|
|
||||||
m_pUserPtr = p->m_pUserPtr;
|
|
||||||
|
|
||||||
// keeps indentificator the same (by default)
|
|
||||||
if (m_ident == 0 ) m_ident = p->m_ident;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarBoolean::SetValInt(int val, const std::string& s)
|
|
||||||
{
|
|
||||||
m_val = static_cast<bool>(val);
|
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarBoolean::SetValFloat(float val)
|
|
||||||
{
|
|
||||||
m_val = static_cast<bool>(val);
|
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
int CBotVarBoolean::GetValInt()
|
|
||||||
{
|
|
||||||
return m_val;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
float CBotVarBoolean::GetValFloat()
|
|
||||||
{
|
|
||||||
return static_cast<float>(m_val);
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
std::string CBotVarBoolean::GetValString()
|
|
||||||
{
|
|
||||||
std::string ret;
|
|
||||||
|
|
||||||
std::string res;
|
|
||||||
|
|
||||||
if ( m_binit == CBotVar::InitType::UNDEF )
|
|
||||||
{
|
|
||||||
res = LoadString(TX_UNDEF);
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
if ( m_binit == CBotVar::InitType::IS_NAN )
|
|
||||||
{
|
|
||||||
res = LoadString(TX_NAN);
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = LoadString( m_val > 0 ? ID_TRUE : ID_FALSE );
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarBoolean::And(CBotVar* left, CBotVar* right)
|
void CBotVarBoolean::And(CBotVar* left, CBotVar* right)
|
||||||
{
|
{
|
||||||
m_val = left->GetValInt() && right->GetValInt();
|
SetValInt(left->GetValInt() && right->GetValInt());
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarBoolean::Or(CBotVar* left, CBotVar* right)
|
void CBotVarBoolean::Or(CBotVar* left, CBotVar* right)
|
||||||
{
|
{
|
||||||
m_val = left->GetValInt() || right->GetValInt();
|
SetValInt(left->GetValInt() || right->GetValInt());
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarBoolean::XOr(CBotVar* left, CBotVar* right)
|
void CBotVarBoolean::XOr(CBotVar* left, CBotVar* right)
|
||||||
{
|
{
|
||||||
m_val = left->GetValInt() ^ right->GetValInt();
|
SetValInt(left->GetValInt() ^ right->GetValInt());
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarBoolean::Not()
|
void CBotVarBoolean::Not()
|
||||||
{
|
{
|
||||||
m_val = m_val ? false : true ;
|
SetValInt(!GetValInt());
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
bool CBotVarBoolean::Eq(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
return left->GetValInt() == right->GetValInt();
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
bool CBotVarBoolean::Ne(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
return left->GetValInt() != right->GetValInt();
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
bool CBotVarBoolean::Save1State(FILE* pf)
|
bool CBotVarBoolean::Save1State(FILE* pf)
|
||||||
{
|
{
|
||||||
return WriteWord(pf, m_val); // the value of the variable
|
return WriteWord(pf, m_val); // the value of the variable
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "CBot/CBotVar/CBotVar.h"
|
#include "CBot/CBotVar/CBotVarValue.h"
|
||||||
|
|
||||||
namespace CBot
|
namespace CBot
|
||||||
{
|
{
|
||||||
|
@ -27,35 +27,17 @@ namespace CBot
|
||||||
/**
|
/**
|
||||||
* \brief CBotVar subclass for managing boolean values (::CBotTypBoolean)
|
* \brief CBotVar subclass for managing boolean values (::CBotTypBoolean)
|
||||||
*/
|
*/
|
||||||
class CBotVarBoolean : public CBotVar
|
class CBotVarBoolean : public CBotVarNumberBase<bool, CBotTypBoolean>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/**
|
CBotVarBoolean(const CBotToken &name) : CBotVarNumberBase(name) {}
|
||||||
* \brief Constructor. Do not call directly, use CBotVar::Create()
|
|
||||||
*/
|
|
||||||
CBotVarBoolean(const CBotToken& name);
|
|
||||||
|
|
||||||
void SetValInt(int val, const std::string& s = nullptr) override;
|
|
||||||
void SetValFloat(float val) override;
|
|
||||||
int GetValInt() override;
|
|
||||||
float GetValFloat() override;
|
|
||||||
std::string GetValString() override;
|
|
||||||
|
|
||||||
void Copy(CBotVar* pSrc, bool bName = true) override;
|
|
||||||
|
|
||||||
void And(CBotVar* left, CBotVar* right) override;
|
void And(CBotVar* left, CBotVar* right) override;
|
||||||
void Or(CBotVar* left, CBotVar* right) override;
|
void Or(CBotVar* left, CBotVar* right) override;
|
||||||
void XOr(CBotVar* left, CBotVar* right) override;
|
void XOr(CBotVar* left, CBotVar* right) override;
|
||||||
void Not() override;
|
void Not() override;
|
||||||
|
|
||||||
bool Eq(CBotVar* left, CBotVar* right) override;
|
|
||||||
bool Ne(CBotVar* left, CBotVar* right) override;
|
|
||||||
|
|
||||||
bool Save1State(FILE* pf) override;
|
bool Save1State(FILE* pf) override;
|
||||||
|
|
||||||
private:
|
|
||||||
//! The value.
|
|
||||||
bool m_val;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace CBot
|
} // namespace CBot
|
||||||
|
|
|
@ -19,205 +19,9 @@
|
||||||
|
|
||||||
#include "CBot/CBotVar/CBotVarFloat.h"
|
#include "CBot/CBotVar/CBotVarFloat.h"
|
||||||
|
|
||||||
#include "CBot/CBotEnums.h"
|
|
||||||
#include "CBot/CBotToken.h"
|
|
||||||
|
|
||||||
#include "CBot/CBotUtils.h"
|
|
||||||
|
|
||||||
#include <cmath>
|
|
||||||
|
|
||||||
namespace CBot
|
namespace CBot
|
||||||
{
|
{
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
CBotVarFloat::CBotVarFloat(const CBotToken& name)
|
|
||||||
{
|
|
||||||
m_token = new CBotToken(name);
|
|
||||||
m_next = nullptr;
|
|
||||||
m_pMyThis = nullptr;
|
|
||||||
m_pUserPtr = nullptr;
|
|
||||||
m_InitExpr = nullptr;
|
|
||||||
m_LimExpr = nullptr;
|
|
||||||
m_type = CBotTypFloat;
|
|
||||||
m_binit = InitType::UNDEF;
|
|
||||||
m_bStatic = false;
|
|
||||||
m_mPrivate = ProtectionLevel::Public;
|
|
||||||
|
|
||||||
m_val = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarFloat::Copy(CBotVar* pSrc, bool bName)
|
|
||||||
{
|
|
||||||
CBotVarFloat* p = static_cast<CBotVarFloat*>(pSrc);
|
|
||||||
|
|
||||||
if (bName) *m_token = *p->m_token;
|
|
||||||
m_type = p->m_type;
|
|
||||||
m_val = p->m_val;
|
|
||||||
m_binit = p->m_binit;
|
|
||||||
//- m_bStatic = p->m_bStatic;
|
|
||||||
m_next = nullptr;
|
|
||||||
m_pMyThis = nullptr;//p->m_pMyThis;
|
|
||||||
m_pUserPtr = p->m_pUserPtr;
|
|
||||||
|
|
||||||
// keeps indentificator the same (by default)
|
|
||||||
if (m_ident == 0 ) m_ident = p->m_ident;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarFloat::SetValInt(int val, const std::string& s)
|
|
||||||
{
|
|
||||||
m_val = static_cast<float>(val);
|
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarFloat::SetValFloat(float val)
|
|
||||||
{
|
|
||||||
m_val = val;
|
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
int CBotVarFloat::GetValInt()
|
|
||||||
{
|
|
||||||
return static_cast<int>(m_val);
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
float CBotVarFloat::GetValFloat()
|
|
||||||
{
|
|
||||||
return m_val;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
std::string CBotVarFloat::GetValString()
|
|
||||||
{
|
|
||||||
std::string res;
|
|
||||||
|
|
||||||
if ( m_binit == CBotVar::InitType::UNDEF )
|
|
||||||
{
|
|
||||||
return LoadString(TX_UNDEF);
|
|
||||||
}
|
|
||||||
if ( m_binit == CBotVar::InitType::IS_NAN )
|
|
||||||
{
|
|
||||||
return LoadString(TX_NAN);
|
|
||||||
}
|
|
||||||
|
|
||||||
char buffer[300];
|
|
||||||
sprintf(buffer, "%.2f", m_val);
|
|
||||||
res = buffer;
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarFloat::Mul(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
m_val = left->GetValFloat() * right->GetValFloat();
|
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarFloat::Power(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
m_val = static_cast<float>(pow( left->GetValFloat() , right->GetValFloat() ));
|
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
CBotError CBotVarFloat::Div(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
float r = right->GetValFloat();
|
|
||||||
if ( r != 0 )
|
|
||||||
{
|
|
||||||
m_val = left->GetValFloat() / r;
|
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
|
||||||
return ( r == 0 ? CBotErrZeroDiv : CBotNoErr );
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
CBotError CBotVarFloat::Modulo(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
float r = right->GetValFloat();
|
|
||||||
if ( r != 0 )
|
|
||||||
{
|
|
||||||
m_val = static_cast<float>(fmod( left->GetValFloat() , r ));
|
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
|
||||||
return ( r == 0 ? CBotErrZeroDiv : CBotNoErr );
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarFloat::Add(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
m_val = left->GetValFloat() + right->GetValFloat();
|
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarFloat::Sub(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
m_val = left->GetValFloat() - right->GetValFloat();
|
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarFloat::Neg()
|
|
||||||
{
|
|
||||||
m_val = -m_val;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarFloat::Inc()
|
|
||||||
{
|
|
||||||
m_val++;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarFloat::Dec()
|
|
||||||
{
|
|
||||||
m_val--;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
bool CBotVarFloat::Lo(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
return left->GetValFloat() < right->GetValFloat();
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
bool CBotVarFloat::Hi(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
return left->GetValFloat() > right->GetValFloat();
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
bool CBotVarFloat::Ls(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
return left->GetValFloat() <= right->GetValFloat();
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
bool CBotVarFloat::Hs(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
return left->GetValFloat() >= right->GetValFloat();
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
bool CBotVarFloat::Eq(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
return left->GetValFloat() == right->GetValFloat();
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
bool CBotVarFloat::Ne(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
return left->GetValFloat() != right->GetValFloat();
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
bool CBotVarFloat::Save1State(FILE* pf)
|
bool CBotVarFloat::Save1State(FILE* pf)
|
||||||
{
|
{
|
||||||
return WriteFloat(pf, m_val); // the value of the variable
|
return WriteFloat(pf, m_val); // the value of the variable
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "CBot/CBotVar/CBotVar.h"
|
#include "CBot/CBotVar/CBotVarValue.h"
|
||||||
|
|
||||||
namespace CBot
|
namespace CBot
|
||||||
{
|
{
|
||||||
|
@ -27,45 +27,12 @@ namespace CBot
|
||||||
/**
|
/**
|
||||||
* \brief CBotVar subclass for managing float values (::CBotTypFloat)
|
* \brief CBotVar subclass for managing float values (::CBotTypFloat)
|
||||||
*/
|
*/
|
||||||
class CBotVarFloat : public CBotVar
|
class CBotVarFloat : public CBotVarNumber<float, CBotTypFloat>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/**
|
CBotVarFloat(const CBotToken &name) : CBotVarNumber(name) {}
|
||||||
* \brief Constructor. Do not call directly, use CBotVar::Create()
|
|
||||||
*/
|
|
||||||
CBotVarFloat(const CBotToken& name);
|
|
||||||
|
|
||||||
void SetValInt(int val, const std::string& s = nullptr) override;
|
|
||||||
void SetValFloat(float val) override;
|
|
||||||
int GetValInt() override;
|
|
||||||
float GetValFloat() override;
|
|
||||||
std::string GetValString() override;
|
|
||||||
|
|
||||||
void Copy(CBotVar* pSrc, bool bName = true) override;
|
|
||||||
|
|
||||||
void Add(CBotVar* left, CBotVar* right) override;
|
|
||||||
void Sub(CBotVar* left, CBotVar* right) override;
|
|
||||||
void Mul(CBotVar* left, CBotVar* right) override;
|
|
||||||
CBotError Div(CBotVar* left, CBotVar* right) override;
|
|
||||||
CBotError Modulo(CBotVar* left, CBotVar* right) override;
|
|
||||||
void Power(CBotVar* left, CBotVar* right) override;
|
|
||||||
|
|
||||||
bool Lo(CBotVar* left, CBotVar* right) override;
|
|
||||||
bool Hi(CBotVar* left, CBotVar* right) override;
|
|
||||||
bool Ls(CBotVar* left, CBotVar* right) override;
|
|
||||||
bool Hs(CBotVar* left, CBotVar* right) override;
|
|
||||||
bool Eq(CBotVar* left, CBotVar* right) override;
|
|
||||||
bool Ne(CBotVar* left, CBotVar* right) override;
|
|
||||||
|
|
||||||
void Neg() override;
|
|
||||||
void Inc() override;
|
|
||||||
void Dec() override;
|
|
||||||
|
|
||||||
bool Save1State(FILE* pf) override;
|
bool Save1State(FILE* pf) override;
|
||||||
|
|
||||||
private:
|
|
||||||
//! The value.
|
|
||||||
float m_val;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace CBot
|
} // namespace CBot
|
||||||
|
|
|
@ -19,276 +19,93 @@
|
||||||
|
|
||||||
#include "CBot/CBotVar/CBotVarInt.h"
|
#include "CBot/CBotVar/CBotVarInt.h"
|
||||||
|
|
||||||
#include "CBot/CBotEnums.h"
|
|
||||||
#include "CBot/CBotToken.h"
|
|
||||||
#include "CBot/CBotUtils.h"
|
|
||||||
|
|
||||||
#include <cmath>
|
|
||||||
|
|
||||||
namespace CBot
|
namespace CBot
|
||||||
{
|
{
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
CBotVarInt::CBotVarInt(const CBotToken& name)
|
|
||||||
{
|
|
||||||
m_token = new CBotToken(name);
|
|
||||||
m_next = nullptr;
|
|
||||||
m_pMyThis = nullptr;
|
|
||||||
m_pUserPtr = nullptr;
|
|
||||||
m_InitExpr = nullptr;
|
|
||||||
m_LimExpr = nullptr;
|
|
||||||
m_type = CBotTypInt;
|
|
||||||
m_binit = InitType::UNDEF;
|
|
||||||
m_bStatic = false;
|
|
||||||
m_mPrivate = ProtectionLevel::Public;
|
|
||||||
|
|
||||||
m_val = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarInt::Copy(CBotVar* pSrc, bool bName)
|
void CBotVarInt::Copy(CBotVar* pSrc, bool bName)
|
||||||
{
|
{
|
||||||
CBotVarInt* p = static_cast<CBotVarInt*>(pSrc);
|
CBotVarNumber::Copy(pSrc, bName);
|
||||||
|
CBotVarInt* p = static_cast<CBotVarInt*>(pSrc);
|
||||||
if ( bName) *m_token = *p->m_token;
|
m_defnum = p->m_defnum;
|
||||||
m_type = p->m_type;
|
|
||||||
m_val = p->m_val;
|
|
||||||
m_binit = p->m_binit;
|
|
||||||
m_pMyThis = nullptr;
|
|
||||||
m_pUserPtr = p->m_pUserPtr;
|
|
||||||
|
|
||||||
// identificator is the same (by défaut)
|
|
||||||
if (m_ident == 0 ) m_ident = p->m_ident;
|
|
||||||
|
|
||||||
m_defnum = p->m_defnum;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarInt::SetValInt(int val, const std::string& defnum)
|
void CBotVarInt::SetValInt(int val, const std::string& defnum)
|
||||||
{
|
{
|
||||||
m_val = val;
|
CBotVarNumber::SetValInt(val, defnum);
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
m_defnum = defnum;
|
m_defnum = defnum;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarInt::SetValFloat(float val)
|
|
||||||
{
|
|
||||||
m_val = static_cast<int>(val);
|
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
int CBotVarInt::GetValInt()
|
|
||||||
{
|
|
||||||
return m_val;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
float CBotVarInt::GetValFloat()
|
|
||||||
{
|
|
||||||
return static_cast<float>(m_val);
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
std::string CBotVarInt::GetValString()
|
std::string CBotVarInt::GetValString()
|
||||||
{
|
{
|
||||||
if ( !m_defnum.empty() ) return m_defnum;
|
if (!m_defnum.empty()) return m_defnum;
|
||||||
|
return CBotVarValue::GetValString();
|
||||||
std::string res;
|
|
||||||
|
|
||||||
if ( m_binit == CBotVar::InitType::UNDEF )
|
|
||||||
{
|
|
||||||
return LoadString(TX_UNDEF);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( m_binit == CBotVar::InitType::IS_NAN )
|
|
||||||
{
|
|
||||||
return LoadString(TX_NAN);
|
|
||||||
}
|
|
||||||
|
|
||||||
char buffer[300];
|
|
||||||
sprintf(buffer, "%d", m_val);
|
|
||||||
res = buffer;
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarInt::Mul(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
m_val = left->GetValInt() * right->GetValInt();
|
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarInt::Power(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
m_val = static_cast<int>( pow( static_cast<double>(left->GetValInt()) , static_cast<double>(right->GetValInt()) ));
|
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
CBotError CBotVarInt::Div(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
int r = right->GetValInt();
|
|
||||||
if ( r != 0 )
|
|
||||||
{
|
|
||||||
m_val = left->GetValInt() / r;
|
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
|
||||||
return ( r == 0 ? CBotErrZeroDiv : CBotNoErr );
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
CBotError CBotVarInt::Modulo(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
int r = right->GetValInt();
|
|
||||||
if ( r != 0 )
|
|
||||||
{
|
|
||||||
m_val = left->GetValInt() % r;
|
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
|
||||||
return ( r == 0 ? CBotErrZeroDiv : CBotNoErr );
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarInt::Add(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
m_val = left->GetValInt() + right->GetValInt();
|
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarInt::Sub(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
m_val = left->GetValInt() - right->GetValInt();
|
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarInt::XOr(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
m_val = left->GetValInt() ^ right->GetValInt();
|
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarInt::And(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
m_val = left->GetValInt() & right->GetValInt();
|
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarInt::Or(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
m_val = left->GetValInt() | right->GetValInt();
|
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarInt::SL(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
m_val = left->GetValInt() << right->GetValInt();
|
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarInt::ASR(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
m_val = left->GetValInt() >> right->GetValInt();
|
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarInt::SR(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
int source = left->GetValInt();
|
|
||||||
int shift = right->GetValInt();
|
|
||||||
if (shift>=1) source &= 0x7fffffff;
|
|
||||||
m_val = source >> shift;
|
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarInt::Neg()
|
void CBotVarInt::Neg()
|
||||||
{
|
{
|
||||||
m_val = -m_val;
|
CBotVarNumber::Neg();
|
||||||
|
m_defnum.empty();
|
||||||
|
}
|
||||||
|
void CBotVarInt::Inc()
|
||||||
|
{
|
||||||
|
CBotVarNumber::Inc();
|
||||||
|
m_defnum.empty();
|
||||||
|
}
|
||||||
|
void CBotVarInt::Dec()
|
||||||
|
{
|
||||||
|
CBotVarNumber::Dec();
|
||||||
|
m_defnum.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
void CBotVarInt::XOr(CBotVar* left, CBotVar* right)
|
||||||
|
{
|
||||||
|
SetValInt(left->GetValInt() ^ right->GetValInt());
|
||||||
|
}
|
||||||
|
void CBotVarInt::And(CBotVar* left, CBotVar* right)
|
||||||
|
{
|
||||||
|
SetValInt(left->GetValInt() & right->GetValInt());
|
||||||
|
}
|
||||||
|
void CBotVarInt::Or(CBotVar* left, CBotVar* right)
|
||||||
|
{
|
||||||
|
SetValInt(left->GetValInt() | right->GetValInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
void CBotVarInt::SL(CBotVar* left, CBotVar* right)
|
||||||
|
{
|
||||||
|
SetValInt(left->GetValInt() << right->GetValInt());
|
||||||
|
}
|
||||||
|
void CBotVarInt::ASR(CBotVar* left, CBotVar* right)
|
||||||
|
{
|
||||||
|
SetValInt(left->GetValInt() >> right->GetValInt());
|
||||||
|
}
|
||||||
|
void CBotVarInt::SR(CBotVar* left, CBotVar* right)
|
||||||
|
{
|
||||||
|
int source = left->GetValInt();
|
||||||
|
int shift = right->GetValInt();
|
||||||
|
if (shift >= 1) source &= 0x7fffffff;
|
||||||
|
SetValInt(source >> shift);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarInt::Not()
|
void CBotVarInt::Not()
|
||||||
{
|
{
|
||||||
m_val = ~m_val;
|
m_val = ~m_val;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarInt::Inc()
|
|
||||||
{
|
|
||||||
m_val++;
|
|
||||||
m_defnum.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarInt::Dec()
|
|
||||||
{
|
|
||||||
m_val--;
|
|
||||||
m_defnum.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
bool CBotVarInt::Lo(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
return left->GetValInt() < right->GetValInt();
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
bool CBotVarInt::Hi(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
return left->GetValInt() > right->GetValInt();
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
bool CBotVarInt::Ls(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
return left->GetValInt() <= right->GetValInt();
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
bool CBotVarInt::Hs(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
return left->GetValInt() >= right->GetValInt();
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
bool CBotVarInt::Eq(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
return left->GetValInt() == right->GetValInt();
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
bool CBotVarInt::Ne(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
return left->GetValInt() != right->GetValInt();
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
bool CBotVarInt::Save0State(FILE* pf)
|
bool CBotVarInt::Save0State(FILE* pf)
|
||||||
{
|
{
|
||||||
if ( !m_defnum.empty() )
|
if (!m_defnum.empty())
|
||||||
{
|
{
|
||||||
if(!WriteWord(pf, 200 )) return false; // special marker
|
if(!WriteWord(pf, 200)) return false; // special marker
|
||||||
if(!WriteString(pf, m_defnum)) return false; // name of the value
|
if(!WriteString(pf, m_defnum)) return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return CBotVar::Save0State(pf);
|
return CBotVar::Save0State(pf);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
bool CBotVarInt::Save1State(FILE* pf)
|
bool CBotVarInt::Save1State(FILE* pf)
|
||||||
{
|
{
|
||||||
return WriteWord(pf, m_val); // the value of the variable
|
return WriteWord(pf, m_val);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace CBot
|
} // namespace CBot
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "CBot/CBotVar/CBotVar.h"
|
#include "CBot/CBotVar/CBotVarValue.h"
|
||||||
|
|
||||||
namespace CBot
|
namespace CBot
|
||||||
{
|
{
|
||||||
|
@ -27,35 +27,19 @@ namespace CBot
|
||||||
/**
|
/**
|
||||||
* \brief CBotVar subclass for managing integer values (::CBotTypInt)
|
* \brief CBotVar subclass for managing integer values (::CBotTypInt)
|
||||||
*/
|
*/
|
||||||
class CBotVarInt : public CBotVar
|
class CBotVarInt : public CBotVarNumber<int, CBotTypInt>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/**
|
CBotVarInt(const CBotToken &name) : CBotVarNumber(name) {}
|
||||||
* \brief Constructor. Do not call directly, use CBotVar::Create()
|
|
||||||
*/
|
|
||||||
CBotVarInt(const CBotToken& name);
|
|
||||||
|
|
||||||
void SetValInt(int val, const std::string& s = "") override;
|
void SetValInt(int val, const std::string& s = "") override;
|
||||||
void SetValFloat(float val) override;
|
|
||||||
int GetValInt() override;
|
|
||||||
float GetValFloat() override;
|
|
||||||
std::string GetValString() override;
|
std::string GetValString() override;
|
||||||
|
|
||||||
void Copy(CBotVar* pSrc, bool bName = true) override;
|
void Copy(CBotVar* pSrc, bool bName = true) override;
|
||||||
|
|
||||||
void Add(CBotVar* left, CBotVar* right) override;
|
void Neg() override;
|
||||||
void Sub(CBotVar* left, CBotVar* right) override;
|
void Inc() override;
|
||||||
void Mul(CBotVar* left, CBotVar* right) override;
|
void Dec() override;
|
||||||
CBotError Div(CBotVar* left, CBotVar* right) override;
|
|
||||||
CBotError Modulo(CBotVar* left, CBotVar* right) override;
|
|
||||||
void Power(CBotVar* left, CBotVar* right) override;
|
|
||||||
|
|
||||||
bool Lo(CBotVar* left, CBotVar* right) override;
|
|
||||||
bool Hi(CBotVar* left, CBotVar* right) override;
|
|
||||||
bool Ls(CBotVar* left, CBotVar* right) override;
|
|
||||||
bool Hs(CBotVar* left, CBotVar* right) override;
|
|
||||||
bool Eq(CBotVar* left, CBotVar* right) override;
|
|
||||||
bool Ne(CBotVar* left, CBotVar* right) override;
|
|
||||||
|
|
||||||
void XOr(CBotVar* left, CBotVar* right) override;
|
void XOr(CBotVar* left, CBotVar* right) override;
|
||||||
void Or(CBotVar* left, CBotVar* right) override;
|
void Or(CBotVar* left, CBotVar* right) override;
|
||||||
|
@ -66,16 +50,10 @@ public:
|
||||||
void SR(CBotVar* left, CBotVar* right) override;
|
void SR(CBotVar* left, CBotVar* right) override;
|
||||||
void ASR(CBotVar* left, CBotVar* right) override;
|
void ASR(CBotVar* left, CBotVar* right) override;
|
||||||
|
|
||||||
void Neg() override;
|
|
||||||
void Inc() override;
|
|
||||||
void Dec() override;
|
|
||||||
|
|
||||||
bool Save0State(FILE* pf) override;
|
bool Save0State(FILE* pf) override;
|
||||||
bool Save1State(FILE* pf) override;
|
bool Save1State(FILE* pf) override;
|
||||||
|
|
||||||
private:
|
protected:
|
||||||
//! The value.
|
|
||||||
int m_val;
|
|
||||||
//! The name if given by DefineNum.
|
//! The name if given by DefineNum.
|
||||||
std::string m_defnum;
|
std::string m_defnum;
|
||||||
friend class CBotVar;
|
friend class CBotVar;
|
||||||
|
|
|
@ -19,117 +19,27 @@
|
||||||
|
|
||||||
#include "CBot/CBotVar/CBotVarString.h"
|
#include "CBot/CBotVar/CBotVarString.h"
|
||||||
|
|
||||||
#include "CBot/CBotEnums.h"
|
|
||||||
#include "CBot/CBotToken.h"
|
|
||||||
#include "CBot/CBotUtils.h"
|
|
||||||
|
|
||||||
namespace CBot
|
namespace CBot
|
||||||
{
|
{
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
CBotVarString::CBotVarString(const CBotToken& name)
|
|
||||||
{
|
|
||||||
m_token = new CBotToken(name);
|
|
||||||
m_next = nullptr;
|
|
||||||
m_pMyThis = nullptr;
|
|
||||||
m_pUserPtr = nullptr;
|
|
||||||
m_InitExpr = nullptr;
|
|
||||||
m_LimExpr = nullptr;
|
|
||||||
m_type = CBotTypString;
|
|
||||||
m_binit = InitType::UNDEF;
|
|
||||||
m_bStatic = false;
|
|
||||||
m_mPrivate = ProtectionLevel::Public;
|
|
||||||
|
|
||||||
m_val.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarString::Copy(CBotVar* pSrc, bool bName)
|
|
||||||
{
|
|
||||||
CBotVarString* p = static_cast<CBotVarString*>(pSrc);
|
|
||||||
|
|
||||||
if (bName) *m_token = *p->m_token;
|
|
||||||
m_type = p->m_type;
|
|
||||||
m_val = p->m_val;
|
|
||||||
m_binit = p->m_binit;
|
|
||||||
//- m_bStatic = p->m_bStatic;
|
|
||||||
m_next = nullptr;
|
|
||||||
m_pMyThis = nullptr;//p->m_pMyThis;
|
|
||||||
m_pUserPtr = p->m_pUserPtr;
|
|
||||||
|
|
||||||
// keeps indentificator the same (by default)
|
|
||||||
if (m_ident == 0 ) m_ident = p->m_ident;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarString::SetValString(const std::string& val)
|
|
||||||
{
|
|
||||||
m_val = val;
|
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
std::string CBotVarString::GetValString()
|
|
||||||
{
|
|
||||||
if ( m_binit == CBotVar::InitType::UNDEF )
|
|
||||||
{
|
|
||||||
return LoadString(TX_UNDEF);
|
|
||||||
}
|
|
||||||
if ( m_binit == CBotVar::InitType::IS_NAN )
|
|
||||||
{
|
|
||||||
return LoadString(TX_NAN);
|
|
||||||
}
|
|
||||||
|
|
||||||
return m_val;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void CBotVarString::Add(CBotVar* left, CBotVar* right)
|
void CBotVarString::Add(CBotVar* left, CBotVar* right)
|
||||||
{
|
{
|
||||||
m_val = left->GetValString() + right->GetValString();
|
SetValString(left->GetValString() + right->GetValString());
|
||||||
m_binit = CBotVar::InitType::DEF;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
bool CBotVarString::Eq(CBotVar* left, CBotVar* right)
|
bool CBotVarString::Eq(CBotVar* left, CBotVar* right)
|
||||||
{
|
{
|
||||||
return (left->GetValString() == right->GetValString());
|
return left->GetValString() == right->GetValString();
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
bool CBotVarString::Ne(CBotVar* left, CBotVar* right)
|
bool CBotVarString::Ne(CBotVar* left, CBotVar* right)
|
||||||
{
|
{
|
||||||
return (left->GetValString() != right->GetValString());
|
return left->GetValString() != right->GetValString();
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
bool CBotVarString::Lo(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
return (left->GetValString() == right->GetValString());
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
bool CBotVarString::Hi(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
return (left->GetValString() == right->GetValString());
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
bool CBotVarString::Ls(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
return (left->GetValString() == right->GetValString());
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
bool CBotVarString::Hs(CBotVar* left, CBotVar* right)
|
|
||||||
{
|
|
||||||
return (left->GetValString() == right->GetValString());
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
bool CBotVarString::Save1State(FILE* pf)
|
bool CBotVarString::Save1State(FILE* pf)
|
||||||
{
|
{
|
||||||
return WriteString(pf, m_val); // the value of the variable
|
return WriteString(pf, m_val);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace CBot
|
} // namespace CBot
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "CBot/CBotVar/CBotVar.h"
|
#include "CBot/CBotVar/CBotVarValue.h"
|
||||||
|
|
||||||
namespace CBot
|
namespace CBot
|
||||||
{
|
{
|
||||||
|
@ -27,33 +27,55 @@ namespace CBot
|
||||||
/**
|
/**
|
||||||
* \brief CBotVar subclass for managing string values (::CBotTypString)
|
* \brief CBotVar subclass for managing string values (::CBotTypString)
|
||||||
*/
|
*/
|
||||||
class CBotVarString : public CBotVar
|
class CBotVarString : public CBotVarValue<std::string, CBotTypString>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/**
|
CBotVarString(const CBotToken &name) : CBotVarValue(name) {}
|
||||||
* \brief Constructor. Do not call directly, use CBotVar::Create()
|
|
||||||
*/
|
|
||||||
CBotVarString(const CBotToken& name);
|
|
||||||
|
|
||||||
void SetValString(const std::string& val) override;
|
void SetValInt(int val, const std::string& s = "") override
|
||||||
std::string GetValString() override;
|
{
|
||||||
|
SetValString(ToString(val));
|
||||||
|
}
|
||||||
|
|
||||||
void Copy(CBotVar* pSrc, bool bName = true) override;
|
void SetValFloat(float val) override
|
||||||
|
{
|
||||||
|
SetValString(ToString(val));
|
||||||
|
}
|
||||||
|
|
||||||
|
int GetValInt()
|
||||||
|
{
|
||||||
|
return FromString<int>(GetValString());
|
||||||
|
}
|
||||||
|
|
||||||
|
float GetValFloat()
|
||||||
|
{
|
||||||
|
return FromString<float>(GetValString());
|
||||||
|
}
|
||||||
|
|
||||||
void Add(CBotVar* left, CBotVar* right) override;
|
void Add(CBotVar* left, CBotVar* right) override;
|
||||||
|
|
||||||
bool Lo(CBotVar* left, CBotVar* right) override;
|
|
||||||
bool Hi(CBotVar* left, CBotVar* right) override;
|
|
||||||
bool Ls(CBotVar* left, CBotVar* right) override;
|
|
||||||
bool Hs(CBotVar* left, CBotVar* right) override;
|
|
||||||
bool Eq(CBotVar* left, CBotVar* right) override;
|
bool Eq(CBotVar* left, CBotVar* right) override;
|
||||||
bool Ne(CBotVar* left, CBotVar* right) override;
|
bool Ne(CBotVar* left, CBotVar* right) override;
|
||||||
|
|
||||||
bool Save1State(FILE* pf) override;
|
bool Save1State(FILE* pf) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//! The value.
|
template<typename T>
|
||||||
std::string m_val;
|
static std::string ToString(T val)
|
||||||
|
{
|
||||||
|
std::ostringstream ss;
|
||||||
|
ss << val;
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static T FromString(std::string val)
|
||||||
|
{
|
||||||
|
std::istringstream ss(val);
|
||||||
|
T v;
|
||||||
|
ss >> v;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace CBot
|
} // namespace CBot
|
||||||
|
|
|
@ -0,0 +1,177 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "CBot/CBotVar/CBotVar.h"
|
||||||
|
|
||||||
|
#include "CBot/CBotEnums.h"
|
||||||
|
#include "CBot/CBotToken.h"
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
|
||||||
|
namespace CBot
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief A variable holding a simple value (bool, int, float, string)
|
||||||
|
*/
|
||||||
|
template <typename T, CBotType type>
|
||||||
|
class CBotVarValue : public CBotVar
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* \brief Constructor. Do not call directly, use CBotVar::Create()
|
||||||
|
*/
|
||||||
|
CBotVarValue(const CBotToken& name) : CBotVar(name)
|
||||||
|
{
|
||||||
|
m_type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Copy(CBotVar* pSrc, bool bName = true) override
|
||||||
|
{
|
||||||
|
CBotVar::Copy(pSrc, bName);
|
||||||
|
|
||||||
|
CBotVarValue* p = static_cast<CBotVarValue*>(pSrc);
|
||||||
|
m_val = p->m_val;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void SetValString(const std::string& val) override
|
||||||
|
{
|
||||||
|
std::istringstream s(val);
|
||||||
|
s >> m_val;
|
||||||
|
m_binit = CBotVar::InitType::DEF;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string GetValString() override
|
||||||
|
{
|
||||||
|
if (m_binit == CBotVar::InitType::UNDEF)
|
||||||
|
return LoadString(TX_UNDEF);
|
||||||
|
if (m_binit == CBotVar::InitType::IS_NAN)
|
||||||
|
return LoadString(TX_NAN);
|
||||||
|
|
||||||
|
std::ostringstream s;
|
||||||
|
s << m_val;
|
||||||
|
return s.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
//! The value
|
||||||
|
T m_val;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief A number based variable (bool, int, float)
|
||||||
|
*/
|
||||||
|
template <typename T, CBotType type>
|
||||||
|
class CBotVarNumberBase : public CBotVarValue<T, type>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
CBotVarNumberBase(const CBotToken &name) : CBotVarValue<T, type>(name) {}
|
||||||
|
|
||||||
|
void SetValInt(int val, const std::string &s = "") override
|
||||||
|
{
|
||||||
|
this->m_val = static_cast<T>(val);
|
||||||
|
this->m_binit = CBotVar::InitType::DEF;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetValFloat(float val) override
|
||||||
|
{
|
||||||
|
this->m_val = static_cast<T>(val);
|
||||||
|
this->m_binit = CBotVar::InitType::DEF;
|
||||||
|
}
|
||||||
|
|
||||||
|
int GetValInt() override
|
||||||
|
{
|
||||||
|
return static_cast<int>(this->m_val);
|
||||||
|
}
|
||||||
|
|
||||||
|
float GetValFloat() override
|
||||||
|
{
|
||||||
|
return static_cast<float>(this->m_val);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool Eq(CBotVar* left, CBotVar* right) override
|
||||||
|
{
|
||||||
|
return left->GetValFloat() == right->GetValFloat();
|
||||||
|
}
|
||||||
|
bool Ne(CBotVar* left, CBotVar* right) override
|
||||||
|
{
|
||||||
|
return left->GetValFloat() != right->GetValFloat();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief A number variable (int, float)
|
||||||
|
*/
|
||||||
|
template <typename T, CBotType type>
|
||||||
|
class CBotVarNumber : public CBotVarNumberBase<T, type>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
CBotVarNumber(const CBotToken &name) : CBotVarNumberBase<T, type>(name) {}
|
||||||
|
|
||||||
|
void Mul(CBotVar* left, CBotVar* right) override
|
||||||
|
{
|
||||||
|
this->SetValFloat(left->GetValFloat() * right->GetValFloat());
|
||||||
|
}
|
||||||
|
void Power(CBotVar* left, CBotVar* right) override
|
||||||
|
{
|
||||||
|
this->SetValFloat(pow(left->GetValFloat(), right->GetValFloat()));
|
||||||
|
}
|
||||||
|
CBotError Div(CBotVar* left, CBotVar* right) override
|
||||||
|
{
|
||||||
|
float r = right->GetValFloat();
|
||||||
|
if (r == 0) return CBotErrZeroDiv;
|
||||||
|
this->SetValFloat(left->GetValFloat() / r);
|
||||||
|
return CBotNoErr;
|
||||||
|
}
|
||||||
|
CBotError Modulo(CBotVar* left, CBotVar* right) override
|
||||||
|
{
|
||||||
|
float r = right->GetValFloat();
|
||||||
|
if (r == 0) return CBotErrZeroDiv;
|
||||||
|
this->SetValFloat(fmod(left->GetValFloat(), r));
|
||||||
|
return CBotNoErr;
|
||||||
|
}
|
||||||
|
void Add(CBotVar* left, CBotVar* right) override
|
||||||
|
{
|
||||||
|
this->SetValFloat(left->GetValFloat() + right->GetValFloat());
|
||||||
|
}
|
||||||
|
void Sub(CBotVar* left, CBotVar* right) override
|
||||||
|
{
|
||||||
|
this->SetValFloat(left->GetValFloat() - right->GetValFloat());
|
||||||
|
}
|
||||||
|
|
||||||
|
void Neg() override
|
||||||
|
{
|
||||||
|
this->m_val = - this->m_val;
|
||||||
|
}
|
||||||
|
void Inc() override
|
||||||
|
{
|
||||||
|
this->m_val++;
|
||||||
|
}
|
||||||
|
void Dec() override
|
||||||
|
{
|
||||||
|
this->m_val--;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Lo(CBotVar* left, CBotVar* right) override
|
||||||
|
{
|
||||||
|
return left->GetValFloat() < right->GetValFloat();
|
||||||
|
}
|
||||||
|
bool Hi(CBotVar* left, CBotVar* right) override
|
||||||
|
{
|
||||||
|
return left->GetValFloat() > right->GetValFloat();
|
||||||
|
}
|
||||||
|
bool Ls(CBotVar* left, CBotVar* right) override
|
||||||
|
{
|
||||||
|
return left->GetValFloat() <= right->GetValFloat();
|
||||||
|
}
|
||||||
|
bool Hs(CBotVar* left, CBotVar* right) override
|
||||||
|
{
|
||||||
|
return left->GetValFloat() >= right->GetValFloat();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -122,6 +122,7 @@ set(SOURCES
|
||||||
CBotUtils.h
|
CBotUtils.h
|
||||||
CBotVar/CBotVar.cpp
|
CBotVar/CBotVar.cpp
|
||||||
CBotVar/CBotVar.h
|
CBotVar/CBotVar.h
|
||||||
|
CBotVar/CBotVarValue.h
|
||||||
CBotVar/CBotVarArray.cpp
|
CBotVar/CBotVarArray.cpp
|
||||||
CBotVar/CBotVarArray.h
|
CBotVar/CBotVarArray.h
|
||||||
CBotVar/CBotVarBoolean.cpp
|
CBotVar/CBotVarBoolean.cpp
|
||||||
|
|
Loading…
Reference in New Issue