Moving CBotVarFloat class in its own header and source files.

dev-time-step
Grunaka 2015-11-15 21:37:56 +01:00
parent 1b3b2ea5a1
commit 44021e91f7
9 changed files with 444 additions and 233 deletions

View File

@ -276,46 +276,6 @@ public:
};
// Class for managing real numbers (float)
class CBotVarFloat : public CBotVar
{
private:
float m_val; // the value
public:
CBotVarFloat( const CBotToken* name );
// ~CBotVarFloat();
void SetValInt(int val, const char* s = nullptr) override;
void SetValFloat(float val) override;
int GetValInt() override;
float GetValFloat() override;
CBotString GetValString() override;
void Copy(CBotVar* pSrc, bool bName=true) override;
void Add(CBotVar* left, CBotVar* right) override; // addition
void Sub(CBotVar* left, CBotVar* right) override; // substraction
void Mul(CBotVar* left, CBotVar* right) override; // multiplication
int Div(CBotVar* left, CBotVar* right) override; // division
int Modulo(CBotVar* left, CBotVar* right) override; // remainder of division
void Power(CBotVar* left, CBotVar* right) override; // power
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;
};
extern CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars);
extern bool TypeCompatible( CBotTypResult& type1, CBotTypResult& type2, int op = 0 );

View File

@ -392,16 +392,6 @@ bool ReadWord(FILE* pf, unsigned short& w)
return (lg == 1);
}
////////////////////////////////////////////////////////////////////////////////
bool WriteFloat(FILE* pf, float w)
{
size_t lg;
lg = fwrite(&w, sizeof( float ), 1, pf );
return (lg == 1);
}
////////////////////////////////////////////////////////////////////////////////
bool ReadFloat(FILE* pf, float& w)
{

View File

@ -990,12 +990,6 @@ bool CBotVarInt::Save1State(FILE* pf)
return WriteWord(pf, m_val); // the value of the variable
}
////////////////////////////////////////////////////////////////////////////////
bool CBotVarFloat::Save1State(FILE* pf)
{
return WriteFloat(pf, m_val); // the value of the variable
}
////////////////////////////////////////////////////////////////////////////////
bool CBotVarClass::Save1State(FILE* pf)
{

View File

@ -127,3 +127,13 @@ bool WriteString(FILE* pf, CBotString s)
lg2 = fwrite(s, 1, lg1, pf );
return (lg1 == lg2);
}
////////////////////////////////////////////////////////////////////////////////
bool WriteFloat(FILE* pf, float w)
{
size_t lg;
lg = fwrite(&w, sizeof( float ), 1, pf );
return (lg == 1);
}

View File

@ -67,3 +67,11 @@ bool WriteWord(FILE* pf, unsigned short w);
* \return
*/
bool WriteString(FILE* pf, CBotString s);
/*!
* \brief WriteFloat
* \param pf
* \param w
* \return
*/
bool WriteFloat(FILE* pf, float w);

View File

@ -32,6 +32,7 @@
#include "CBotVar/CBotVarClass.h"
#include "CBotVar/CBotVarBoolean.h"
#include "CBotVar/CBotVarString.h"
#include "CBotVar/CBotVarFloat.h"
#include "CBotDefines.h"
#include "CBotClass.h"
@ -72,22 +73,6 @@ CBotVarInt::CBotVarInt( const CBotToken* name )
m_val = 0;
}
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 = 0;
m_val = 0;
}
CBotVar::~CBotVar( )
{
delete m_token;
@ -944,167 +929,6 @@ bool CBotVarInt::Ne(CBotVar* left, CBotVar* right)
return left->GetValInt() != right->GetValInt();
}
//////////////////////////////////////////////////////////////////////////////////////
// copy a variable into another
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 char* 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;
}
CBotString CBotVarFloat::GetValString()
{
CBotString 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;
}
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;
}
int 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 ? TX_DIVZERO : 0 );
}
int 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 ? TX_DIVZERO : 0 );
}
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();
}
////////////////////////////////////////////////////////////////
// copy a variable into another

View File

@ -0,0 +1,226 @@
/*
* This file is part of the Colobot: Gold Edition source code
* Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam
* http://epsitec.ch; http://colobot.info; http://github.com/colobot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://gnu.org/licenses
*/
// Modules inlcude
#include "CBotVarFloat.h"
#include "CBotToken.h"
#include "CBotUtils.h"
// Local include
// Global include
#include <cmath>
////////////////////////////////////////////////////////////////////////////////
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 = 0;
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 char* 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;
}
////////////////////////////////////////////////////////////////////////////////
CBotString CBotVarFloat::GetValString()
{
CBotString 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;
}
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;
}
////////////////////////////////////////////////////////////////////////////////
int 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 ? TX_DIVZERO : 0 );
}
////////////////////////////////////////////////////////////////////////////////
int 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 ? TX_DIVZERO : 0 );
}
////////////////////////////////////////////////////////////////////////////////
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)
{
return WriteFloat(pf, m_val); // the value of the variable
}

View File

@ -0,0 +1,198 @@
/*
* This file is part of the Colobot: Gold Edition source code
* Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam
* http://epsitec.ch; http://colobot.info; http://github.com/colobot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://gnu.org/licenses
*/
#pragma once
// Modules inlcude
#include "CBotDll.h"
// Local include
// Global include
/*!
* \brief The CBotVarFloat class Class for managing real numbers (float).
*/
class CBotVarFloat : public CBotVar
{
public:
/*!
* \brief CBotVarFloat
* \param name
*/
CBotVarFloat( const CBotToken* name );
/*!
* \brief SetValInt
* \param val
* \param s
*/
void SetValInt(int val, const char* s = nullptr) override;
/*!
* \brief SetValFloat
* \param val
*/
void SetValFloat(float val) override;
/*!
* \brief GetValInt
* \return
*/
int GetValInt() override;
/*!
* \brief GetValFloat
* \return
*/
float GetValFloat() override;
/*!
* \brief GetValString
* \return
*/
CBotString GetValString() override;
/*!
* \brief Copy Copy a variable into another.
* \param pSrc
* \param bName
*/
void Copy(CBotVar* pSrc, bool bName=true) override;
/*!
* \brief Add Addition.
* \param left
* \param right
*/
void Add(CBotVar* left, CBotVar* right) override;
/*!
* \brief Sub Substraction.
* \param left
* \param right
*/
void Sub(CBotVar* left, CBotVar* right) override;
/*!
* \brief Mul Multiplication.
* \param left
* \param right
*/
void Mul(CBotVar* left, CBotVar* right) override;
/*!
* \brief Div Division.
* \param left
* \param right
* \return
*/
int Div(CBotVar* left, CBotVar* right) override;
/*!
* \brief Modulo Remainder of division.
* \param left
* \param right
* \return
*/
int Modulo(CBotVar* left, CBotVar* right) override;
/*!
* \brief Power
* \param left
* \param right
*/
void Power(CBotVar* left, CBotVar* right) override;
/*!
* \brief Lo
* \param left
* \param right
* \return
*/
bool Lo(CBotVar* left, CBotVar* right) override;
/*!
* \brief Hi
* \param left
* \param right
* \return
*/
bool Hi(CBotVar* left, CBotVar* right) override;
/*!
* \brief Ls
* \param left
* \param right
* \return
*/
bool Ls(CBotVar* left, CBotVar* right) override;
/*!
* \brief Hs
* \param left
* \param right
* \return
*/
bool Hs(CBotVar* left, CBotVar* right) override;
/*!
* \brief Eq
* \param left
* \param right
* \return
*/
bool Eq(CBotVar* left, CBotVar* right) override;
/*!
* \brief Ne
* \param left
* \param right
* \return
*/
bool Ne(CBotVar* left, CBotVar* right) override;
/*!
* \brief Neg
*/
void Neg() override;
/*!
* \brief Inc
*/
void Inc() override;
/*!
* \brief Dec
*/
void Dec() override;
/*!
* \brief Save1State
* \param pf
* \return
*/
bool Save1State(FILE* pf) override;
private:
//! The value.
float m_val;
};

View File

@ -60,6 +60,7 @@ set(SOURCES
CBotVar/CBotVarClass.cpp
CBotVar/CBotVarBoolean.cpp
CBotVar/CBotVarString.cpp
CBotVar/CBotVarFloat.cpp
)
# Includes