From 44021e91f7ae3a5f710c4f28840a390d59c35277 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 15 Nov 2015 21:37:56 +0100 Subject: [PATCH] Moving CBotVarFloat class in its own header and source files. --- src/CBot/CBot.h | 40 ------ src/CBot/CBotProgram.cpp | 10 -- src/CBot/CBotStack.cpp | 6 - src/CBot/CBotUtils.cpp | 10 ++ src/CBot/CBotUtils.h | 8 ++ src/CBot/CBotVar.cpp | 178 +---------------------- src/CBot/CBotVar/CBotVarFloat.cpp | 226 ++++++++++++++++++++++++++++++ src/CBot/CBotVar/CBotVarFloat.h | 198 ++++++++++++++++++++++++++ src/CBot/CMakeLists.txt | 1 + 9 files changed, 444 insertions(+), 233 deletions(-) create mode 100644 src/CBot/CBotVar/CBotVarFloat.cpp create mode 100644 src/CBot/CBotVar/CBotVarFloat.h diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 2c23fa77..a742d47a 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -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 ); diff --git a/src/CBot/CBotProgram.cpp b/src/CBot/CBotProgram.cpp index 9ce35958..44eaca50 100644 --- a/src/CBot/CBotProgram.cpp +++ b/src/CBot/CBotProgram.cpp @@ -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) { diff --git a/src/CBot/CBotStack.cpp b/src/CBot/CBotStack.cpp index 574c81cf..e8310081 100644 --- a/src/CBot/CBotStack.cpp +++ b/src/CBot/CBotStack.cpp @@ -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) { diff --git a/src/CBot/CBotUtils.cpp b/src/CBot/CBotUtils.cpp index 4596b6f2..eee71e44 100644 --- a/src/CBot/CBotUtils.cpp +++ b/src/CBot/CBotUtils.cpp @@ -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); +} diff --git a/src/CBot/CBotUtils.h b/src/CBot/CBotUtils.h index 000c90af..354595b8 100644 --- a/src/CBot/CBotUtils.h +++ b/src/CBot/CBotUtils.h @@ -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); diff --git a/src/CBot/CBotVar.cpp b/src/CBot/CBotVar.cpp index b87f718d..154405e1 100644 --- a/src/CBot/CBotVar.cpp +++ b/src/CBot/CBotVar.cpp @@ -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(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(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(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(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(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 diff --git a/src/CBot/CBotVar/CBotVarFloat.cpp b/src/CBot/CBotVar/CBotVarFloat.cpp new file mode 100644 index 00000000..465c2940 --- /dev/null +++ b/src/CBot/CBotVar/CBotVarFloat.cpp @@ -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 + +//////////////////////////////////////////////////////////////////////////////// +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(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(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(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(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(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 +} diff --git a/src/CBot/CBotVar/CBotVarFloat.h b/src/CBot/CBotVar/CBotVarFloat.h new file mode 100644 index 00000000..fb30cd49 --- /dev/null +++ b/src/CBot/CBotVar/CBotVarFloat.h @@ -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; +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 1dd05b8b..33474db8 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -60,6 +60,7 @@ set(SOURCES CBotVar/CBotVarClass.cpp CBotVar/CBotVarBoolean.cpp CBotVar/CBotVarString.cpp + CBotVar/CBotVarFloat.cpp ) # Includes