From ec425c768b59806d4cae9333b934351d185d2432 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kapu=C5=9Bci=C5=84ski?= Date: Sat, 9 Jul 2022 16:47:59 +0200 Subject: [PATCH 1/6] Use the Y floor coordinate from topo() so it doesn't ignore spaceship's modified floor height --- src/level/robotmain.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/level/robotmain.cpp b/src/level/robotmain.cpp index e4693680..27d09873 100644 --- a/src/level/robotmain.cpp +++ b/src/level/robotmain.cpp @@ -4142,7 +4142,7 @@ bool CRobotMain::FreeSpace(Math::Vector ¢er, float minRadius, float maxRadiu pos.x = p.x; pos.z = p.y; pos.y = 0.0f; - m_terrain->AdjustToFloor(pos, true); + pos.y = m_terrain->GetFloorLevel(pos); if (!BlockedByObject(m_objMan.get(), pos, space, exclu)) { float flat = m_terrain->GetFlatZoneRadius(pos, space); From b5e27c9f1945b6ea5610e4706a426a4187cdf9dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kapu=C5=9Bci=C5=84ski?= Date: Sat, 9 Jul 2022 16:48:59 +0200 Subject: [PATCH 2/6] space() now returns a point with NaNs instead of the original position when it can't find free space --- src/level/robotmain.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/level/robotmain.cpp b/src/level/robotmain.cpp index 27d09873..2c348e7a 100644 --- a/src/level/robotmain.cpp +++ b/src/level/robotmain.cpp @@ -4154,6 +4154,11 @@ bool CRobotMain::FreeSpace(Math::Vector ¢er, float minRadius, float maxRadiu } } } + + float nan = std::nanf(""); + + center = Math::Vector{ nan, nan, nan }; + return false; } From 3c98af04b5ba53b32644db7efc755f581519e63f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kapu=C5=9Bci=C5=84ski?= Date: Sat, 13 Aug 2022 17:18:14 +0200 Subject: [PATCH 3/6] Fixed passing bool to message() --- src/CBot/CBotVar/CBotVarValue.h | 2 +- src/script/scriptfunc.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/CBot/CBotVar/CBotVarValue.h b/src/CBot/CBotVar/CBotVarValue.h index c305aa2f..0cc83104 100644 --- a/src/CBot/CBotVar/CBotVarValue.h +++ b/src/CBot/CBotVar/CBotVarValue.h @@ -70,7 +70,7 @@ public: return LoadString(TX_NAN); std::ostringstream s; - s << m_val; + s << std::boolalpha << m_val; return s.str(); } diff --git a/src/script/scriptfunc.cpp b/src/script/scriptfunc.cpp index f36121f7..323bea85 100644 --- a/src/script/scriptfunc.cpp +++ b/src/script/scriptfunc.cpp @@ -2859,6 +2859,7 @@ CBotTypResult CScriptFunctions::cMessage(CBotVar* &var, void* user) { if ( var == nullptr ) return CBotTypResult(CBotErrLowParam); if ( var->GetType() != CBotTypString && + var->GetType() != CBotTypBoolean && var->GetType() > CBotTypDouble ) return CBotTypResult(CBotErrBadNum); var = var->GetNext(); From 2ea94244e9b1da632b024efeb4a9bec7af1e8604 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kapu=C5=9Bci=C5=84ski?= Date: Sun, 14 Aug 2022 23:47:47 +0200 Subject: [PATCH 4/6] Fixed const-correctness of getters --- src/CBot/CBotVar/CBotVar.cpp | 18 +++++++++--------- src/CBot/CBotVar/CBotVar.h | 18 +++++++++--------- src/CBot/CBotVar/CBotVarArray.cpp | 2 +- src/CBot/CBotVar/CBotVarArray.h | 2 +- src/CBot/CBotVar/CBotVarChar.h | 2 +- src/CBot/CBotVar/CBotVarClass.cpp | 2 +- src/CBot/CBotVar/CBotVarClass.h | 2 +- src/CBot/CBotVar/CBotVarInt.cpp | 2 +- src/CBot/CBotVar/CBotVarInt.h | 2 +- src/CBot/CBotVar/CBotVarPointer.cpp | 2 +- src/CBot/CBotVar/CBotVarPointer.h | 2 +- src/CBot/CBotVar/CBotVarString.h | 4 ++-- src/CBot/CBotVar/CBotVarValue.h | 16 ++++++++-------- 13 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/CBot/CBotVar/CBotVar.cpp b/src/CBot/CBotVar/CBotVar.cpp index c975ed7b..4c268cb4 100644 --- a/src/CBot/CBotVar/CBotVar.cpp +++ b/src/CBot/CBotVar/CBotVar.cpp @@ -355,7 +355,7 @@ CBotTypResult CBotVar::GetTypResult(GetTypeMode mode) } //////////////////////////////////////////////////////////////////////////////// -CBotType CBotVar::GetType(GetTypeMode mode) +CBotType CBotVar::GetType(GetTypeMode mode) const { if ( mode == GetTypeMode::CLASS_AS_POINTER && m_type.Eq(CBotTypClass) ) return CBotTypPointer; @@ -584,43 +584,43 @@ CBotVarClass* CBotVar::GetPointer() // All these functions must be defined in the subclasses // derived from class CBotVar -signed char CBotVar::GetValByte() +signed char CBotVar::GetValByte() const { assert(0); return 0; } -short CBotVar::GetValShort() +short CBotVar::GetValShort() const { assert(0); return 0; } -uint32_t CBotVar::GetValChar() +uint32_t CBotVar::GetValChar() const { assert(0); return 0; } -int CBotVar::GetValInt() +int CBotVar::GetValInt() const { assert(0); return 0; } -long CBotVar::GetValLong() +long CBotVar::GetValLong() const { assert(0); return 0; } -float CBotVar::GetValFloat() +float CBotVar::GetValFloat() const { assert(0); return 0; } -double CBotVar::GetValDouble() +double CBotVar::GetValDouble() const { assert(0); return 0; @@ -822,7 +822,7 @@ void CBotVar::SetValString(const std::string& val) } //////////////////////////////////////////////////////////////////////////////// -std::string CBotVar::GetValString() +std::string CBotVar::GetValString() const { assert(0); return std::string(); diff --git a/src/CBot/CBotVar/CBotVar.h b/src/CBot/CBotVar/CBotVar.h index e351398d..d1d336cf 100644 --- a/src/CBot/CBotVar/CBotVar.h +++ b/src/CBot/CBotVar/CBotVar.h @@ -199,7 +199,7 @@ public: * \brief GetType Returns the base type of the variable (::CBotType) * \param mode Mode, see GetTypeMode enum */ - CBotType GetType(GetTypeMode mode = GetTypeMode::NORMAL); + CBotType GetType(GetTypeMode mode = GetTypeMode::NORMAL) const; /** * \brief Returns the complete type of the variable (CBotTypResult) @@ -508,27 +508,27 @@ public: */ virtual void SetValString(const std::string& val); - virtual signed char GetValByte(); + virtual signed char GetValByte() const; - virtual short GetValShort(); + virtual short GetValShort() const; - virtual uint32_t GetValChar(); + virtual uint32_t GetValChar() const; /** * \brief Get value as integer * \return Current value */ - virtual int GetValInt(); + virtual int GetValInt() const; - virtual long GetValLong(); + virtual long GetValLong() const; /** * \brief Get value as float * \return Current value */ - virtual float GetValFloat(); + virtual float GetValFloat() const; - virtual double GetValDouble(); + virtual double GetValDouble() const; /** * \brief Get value as string @@ -539,7 +539,7 @@ public: * * \return Current value */ - virtual std::string GetValString(); + virtual std::string GetValString() const; /** * \brief Set value for pointer types diff --git a/src/CBot/CBotVar/CBotVarArray.cpp b/src/CBot/CBotVar/CBotVarArray.cpp index b0895448..be31ddd2 100644 --- a/src/CBot/CBotVar/CBotVarArray.cpp +++ b/src/CBot/CBotVar/CBotVarArray.cpp @@ -129,7 +129,7 @@ CBotVar* CBotVarArray::GetItemList() } //////////////////////////////////////////////////////////////////////////////// -std::string CBotVarArray::GetValString() +std::string CBotVarArray::GetValString() const { if ( m_pInstance == nullptr ) return ( std::string( "Null pointer" ) ) ; return m_pInstance->GetValString(); diff --git a/src/CBot/CBotVar/CBotVarArray.h b/src/CBot/CBotVar/CBotVarArray.h index 46986900..144a5050 100644 --- a/src/CBot/CBotVar/CBotVarArray.h +++ b/src/CBot/CBotVar/CBotVarArray.h @@ -49,7 +49,7 @@ public: CBotVar* GetItem(int n, bool grow = false) override; CBotVar* GetItemList() override; - std::string GetValString() override; + std::string GetValString() const override; bool Save1State(std::ostream &ostr) override; diff --git a/src/CBot/CBotVar/CBotVarChar.h b/src/CBot/CBotVar/CBotVarChar.h index 7b6031d5..6bbfb9c1 100644 --- a/src/CBot/CBotVar/CBotVarChar.h +++ b/src/CBot/CBotVar/CBotVarChar.h @@ -32,7 +32,7 @@ class CBotVarChar : public CBotVarInteger public: CBotVarChar(const CBotToken &name) : CBotVarInteger(name) {} - std::string GetValString() override + std::string GetValString() const override { if (m_binit == CBotVar::InitType::UNDEF) return LoadString(TX_UNDEF); diff --git a/src/CBot/CBotVar/CBotVarClass.cpp b/src/CBot/CBotVar/CBotVarClass.cpp index c7d81cb5..5f7743e9 100644 --- a/src/CBot/CBotVar/CBotVarClass.cpp +++ b/src/CBot/CBotVar/CBotVarClass.cpp @@ -291,7 +291,7 @@ CBotVar* CBotVarClass::GetItemList() } //////////////////////////////////////////////////////////////////////////////// -std::string CBotVarClass::GetValString() +std::string CBotVarClass::GetValString() const { std::string res; diff --git a/src/CBot/CBotVar/CBotVarClass.h b/src/CBot/CBotVar/CBotVarClass.h index 254ec88b..75abca72 100644 --- a/src/CBot/CBotVar/CBotVarClass.h +++ b/src/CBot/CBotVar/CBotVarClass.h @@ -52,7 +52,7 @@ public: CBotVar* GetItemRef(int nIdent) override; CBotVar* GetItem(int n, bool bExtend) override; CBotVar* GetItemList() override; - std::string GetValString() override; + std::string GetValString() const override; bool Save1State(std::ostream &ostr) override; diff --git a/src/CBot/CBotVar/CBotVarInt.cpp b/src/CBot/CBotVar/CBotVarInt.cpp index 8a69a9f4..7e4fdc24 100644 --- a/src/CBot/CBotVar/CBotVarInt.cpp +++ b/src/CBot/CBotVar/CBotVarInt.cpp @@ -35,7 +35,7 @@ void CBotVarInt::SetValInt(int val, const std::string& defnum) m_defnum = defnum; } -std::string CBotVarInt::GetValString() +std::string CBotVarInt::GetValString() const { if (!m_defnum.empty()) return m_defnum; return CBotVarValue::GetValString(); diff --git a/src/CBot/CBotVar/CBotVarInt.h b/src/CBot/CBotVar/CBotVarInt.h index 62c4adeb..c41034d9 100644 --- a/src/CBot/CBotVar/CBotVarInt.h +++ b/src/CBot/CBotVar/CBotVarInt.h @@ -33,7 +33,7 @@ public: CBotVarInt(const CBotToken &name) : CBotVarInteger(name) {} void SetValInt(int val, const std::string& s = "") override; - std::string GetValString() override; + std::string GetValString() const override; void Copy(CBotVar* pSrc, bool bName = true) override; diff --git a/src/CBot/CBotVar/CBotVarPointer.cpp b/src/CBot/CBotVar/CBotVarPointer.cpp index ed5eda00..db979c97 100644 --- a/src/CBot/CBotVar/CBotVarPointer.cpp +++ b/src/CBot/CBotVar/CBotVarPointer.cpp @@ -90,7 +90,7 @@ CBotVar* CBotVarPointer::GetItemList() } //////////////////////////////////////////////////////////////////////////////// -std::string CBotVarPointer::GetValString() +std::string CBotVarPointer::GetValString() const { std::string s = "Pointer to "; if ( m_pVarClass == nullptr ) s = "Null pointer" ; diff --git a/src/CBot/CBotVar/CBotVarPointer.h b/src/CBot/CBotVar/CBotVarPointer.h index 4fa1d0ae..0d03ae44 100644 --- a/src/CBot/CBotVar/CBotVarPointer.h +++ b/src/CBot/CBotVar/CBotVarPointer.h @@ -47,7 +47,7 @@ public: CBotVar* GetItem(const std::string& name) override; CBotVar* GetItemRef(int nIdent) override; CBotVar* GetItemList() override; - std::string GetValString() override; + std::string GetValString() const override; void SetPointer(CBotVar* p) override; CBotVarClass* GetPointer() override; diff --git a/src/CBot/CBotVar/CBotVarString.h b/src/CBot/CBotVar/CBotVarString.h index 679c7a9a..b4ed70e4 100644 --- a/src/CBot/CBotVar/CBotVarString.h +++ b/src/CBot/CBotVar/CBotVarString.h @@ -48,12 +48,12 @@ public: SetValString(ToString(val)); } - int GetValInt() override + int GetValInt() const override { return FromString(GetValString()); } - float GetValFloat() override + float GetValFloat() const override { return FromString(GetValString()); } diff --git a/src/CBot/CBotVar/CBotVarValue.h b/src/CBot/CBotVar/CBotVarValue.h index 0cc83104..9d1e6f4a 100644 --- a/src/CBot/CBotVar/CBotVarValue.h +++ b/src/CBot/CBotVar/CBotVarValue.h @@ -62,7 +62,7 @@ public: m_binit = CBotVar::InitType::DEF; } - std::string GetValString() override + std::string GetValString() const override { if (m_binit == CBotVar::InitType::UNDEF) return LoadString(TX_UNDEF); @@ -133,37 +133,37 @@ public: this->SetValue(static_cast(val)); } - signed char GetValByte() override + signed char GetValByte() const override { return static_cast(this->m_val); } - short GetValShort() override + short GetValShort() const override { return static_cast(this->m_val); } - uint32_t GetValChar() override + uint32_t GetValChar() const override { return static_cast(this->m_val); } - int GetValInt() override + int GetValInt() const override { return static_cast(this->m_val); } - long GetValLong() override + long GetValLong() const override { return static_cast(this->m_val); } - float GetValFloat() override + float GetValFloat() const override { return static_cast(this->m_val); } - double GetValDouble() override + double GetValDouble() const override { return static_cast(this->m_val); } From 8632c7404df91077ea826fe298138e1447dae696 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kapu=C5=9Bci=C5=84ski?= Date: Mon, 15 Aug 2022 00:04:54 +0200 Subject: [PATCH 5/6] Refactored NaN implementation strfind() now returns -1 instead of NaN when substring can't be found added isnan() as an alternative way of checking if a value is NaN --- src/CBot/CBotInstr/CBotExprLitNan.cpp | 4 +-- src/CBot/CBotInstr/CBotExpression.cpp | 6 ---- src/CBot/CBotInstr/CBotParExpr.cpp | 4 +-- src/CBot/CBotInstr/CBotPostIncExpr.cpp | 5 ---- src/CBot/CBotInstr/CBotPreIncExpr.cpp | 6 ---- src/CBot/CBotInstr/CBotTwoOpExpr.cpp | 12 ++++++-- src/CBot/CBotVar/CBotVar.h | 7 ----- src/CBot/CBotVar/CBotVarChar.h | 2 -- src/CBot/CBotVar/CBotVarValue.h | 2 -- src/CBot/stdlib/FileFunctions.cpp | 4 +-- src/CBot/stdlib/MathFunctions.cpp | 32 +++++++++++++++++++++ src/CBot/stdlib/StringFunctions.cpp | 2 +- src/script/cbottoken.cpp | 5 +++- src/script/scriptfunc.cpp | 40 +++++++++++++++++--------- 14 files changed, 78 insertions(+), 53 deletions(-) diff --git a/src/CBot/CBotInstr/CBotExprLitNan.cpp b/src/CBot/CBotInstr/CBotExprLitNan.cpp index 52e30d5f..60f73e8e 100644 --- a/src/CBot/CBotInstr/CBotExprLitNan.cpp +++ b/src/CBot/CBotInstr/CBotExprLitNan.cpp @@ -41,9 +41,9 @@ bool CBotExprLitNan::Execute(CBotStack* &pj) CBotStack* pile = pj->AddStack(this); if (pile->IfStep()) return false; - CBotVar* var = CBotVar::Create("", CBotTypInt); + CBotVar* var = CBotVar::Create("", CBotTypFloat); - var->SetInit(CBotVar::InitType::IS_NAN); // nan + var->SetValFloat(std::nanf("")); pile->SetVar(var); // put on the stack return pj->Return(pile); // forward below } diff --git a/src/CBot/CBotInstr/CBotExpression.cpp b/src/CBot/CBotInstr/CBotExpression.cpp index 2374a2a6..8a9e91de 100644 --- a/src/CBot/CBotInstr/CBotExpression.cpp +++ b/src/CBot/CBotInstr/CBotExpression.cpp @@ -206,12 +206,6 @@ bool CBotExpression::Execute(CBotStack* &pj) if (m_token.GetType() != ID_ASS) { pVar = pile1->GetVar(); // recovers if interrupted - initKind = pVar->GetInit(); - if (initKind == CBotVar::InitType::IS_NAN) - { - pile2->SetError(CBotErrNan, m_leftop->GetToken()); - return pj->Return(pile2); - } result = CBotVar::Create("", pVar->GetTypResult(CBotVar::GetTypeMode::CLASS_AS_INTRINSIC)); } diff --git a/src/CBot/CBotInstr/CBotParExpr.cpp b/src/CBot/CBotInstr/CBotParExpr.cpp index 46f72818..fe33c82d 100644 --- a/src/CBot/CBotInstr/CBotParExpr.cpp +++ b/src/CBot/CBotInstr/CBotParExpr.cpp @@ -217,8 +217,8 @@ CBotInstr* CBotParExpr::CompileLitExpr(CBotToken* &p, CBotCStack* pStack) { CBotInstr* inst = new CBotExprLitNan(); inst->SetToken(pp); - CBotVar* var = CBotVar::Create("", CBotTypInt); - var->SetInit(CBotVar::InitType::IS_NAN); + CBotVar* var = CBotVar::Create("", CBotTypFloat); + var->SetValFloat(std::nanf("")); pStk->SetVar(var); return pStack->Return(inst, pStk); } diff --git a/src/CBot/CBotInstr/CBotPostIncExpr.cpp b/src/CBot/CBotInstr/CBotPostIncExpr.cpp index fac31e2c..5ce6284d 100644 --- a/src/CBot/CBotInstr/CBotPostIncExpr.cpp +++ b/src/CBot/CBotInstr/CBotPostIncExpr.cpp @@ -56,11 +56,6 @@ bool CBotPostIncExpr::Execute(CBotStack* &pj) CBotStack* pile3 = pile2->AddStack(this); if (pile3->IfStep()) return false; - if (var1->IsNAN()) - { - pile1->SetError(CBotErrNan, &m_token); - } - if (!var1->IsDefined()) { pile1->SetError(CBotErrNotInit, &m_token); diff --git a/src/CBot/CBotInstr/CBotPreIncExpr.cpp b/src/CBot/CBotInstr/CBotPreIncExpr.cpp index afb2b65c..6416215b 100644 --- a/src/CBot/CBotInstr/CBotPreIncExpr.cpp +++ b/src/CBot/CBotInstr/CBotPreIncExpr.cpp @@ -55,12 +55,6 @@ bool CBotPreIncExpr::Execute(CBotStack* &pj) // pile2 is modified on return if (!(static_cast(m_instr))->ExecuteVar(var1, pile2, nullptr, true)) return false; - if (var1->IsNAN()) - { - pile->SetError(CBotErrNan, &m_token); - return pj->Return(pile); // operation performed - } - if (!var1->IsDefined()) { pile->SetError(CBotErrNotInit, &m_token); diff --git a/src/CBot/CBotInstr/CBotTwoOpExpr.cpp b/src/CBot/CBotInstr/CBotTwoOpExpr.cpp index ad09f221..52b78372 100644 --- a/src/CBot/CBotInstr/CBotTwoOpExpr.cpp +++ b/src/CBot/CBotInstr/CBotTwoOpExpr.cpp @@ -305,7 +305,13 @@ CBotInstr* CBotTwoOpExpr::Compile(CBotToken* &p, CBotCStack* pStack, int* pOpera static bool VarIsNAN(const CBotVar* var) { - return var->GetInit() > CBotVar::InitType::DEF; + if (var->GetType() == CBotTypFloat) + return std::isnan(var->GetValFloat()); + + if (var->GetType() == CBotTypDouble) + return std::isnan(var->GetValDouble()); + + return false; } static bool IsNan(CBotVar* left, CBotVar* right, CBotError* err = nullptr) @@ -475,13 +481,13 @@ bool CBotTwoOpExpr::Execute(CBotStack* &pStack) break; case ID_EQ: if ( IsNan(left, right) ) - result->SetValInt(left->GetInit() == right->GetInit()) ; + result->SetValInt(VarIsNAN(left) == VarIsNAN(right)); else result->SetValInt(temp->Eq(left , right)); // equal break; case ID_NE: if ( IsNan(left, right) ) - result->SetValInt(left ->GetInit() != right->GetInit()) ; + result->SetValInt(VarIsNAN(left) != VarIsNAN(right)); else result->SetValInt(temp->Ne(left , right)); // different break; diff --git a/src/CBot/CBotVar/CBotVar.h b/src/CBot/CBotVar/CBotVar.h index d1d336cf..c9739e3f 100644 --- a/src/CBot/CBotVar/CBotVar.h +++ b/src/CBot/CBotVar/CBotVar.h @@ -243,7 +243,6 @@ public: UNDEF = 0, //!< the variable value is currently not defined DEF = 1, //!< the variable value is defined IS_POINTER = 2, //!< the variable value is as a pointer - IS_NAN = 999 //!< the variable value is NAN }; /** @@ -269,12 +268,6 @@ public: */ bool IsDefined() const { return GetInit() == InitType::DEF; } - /** - * \brief Checks if the variable is currently NAN - * \return InitType::NAN - */ - bool IsNAN() const { return GetInit() == InitType::IS_NAN; } - //@} //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/CBot/CBotVar/CBotVarChar.h b/src/CBot/CBotVar/CBotVarChar.h index 6bbfb9c1..168604f6 100644 --- a/src/CBot/CBotVar/CBotVarChar.h +++ b/src/CBot/CBotVar/CBotVarChar.h @@ -36,8 +36,6 @@ public: { if (m_binit == CBotVar::InitType::UNDEF) return LoadString(TX_UNDEF); - if (m_binit == CBotVar::InitType::IS_NAN) - return LoadString(TX_NAN); if (0x10FFFF < m_val || (0xD7FF < m_val && m_val < 0xE000)) return "\xEF\xBF\xBD"; // replacement character U+FFFD diff --git a/src/CBot/CBotVar/CBotVarValue.h b/src/CBot/CBotVar/CBotVarValue.h index 9d1e6f4a..fddb6bfe 100644 --- a/src/CBot/CBotVar/CBotVarValue.h +++ b/src/CBot/CBotVar/CBotVarValue.h @@ -66,8 +66,6 @@ public: { 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 << std::boolalpha << m_val; diff --git a/src/CBot/stdlib/FileFunctions.cpp b/src/CBot/stdlib/FileFunctions.cpp index 04e54ac1..bb48268d 100644 --- a/src/CBot/stdlib/FileFunctions.cpp +++ b/src/CBot/stdlib/FileFunctions.cpp @@ -133,7 +133,7 @@ bool rfdestruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception if (!pVar->IsDefined()) return true; // file not opened g_files.erase(pVar->GetValInt()); - pVar->SetInit(CBotVar::InitType::IS_NAN); + pVar->SetInit(CBotVar::InitType::UNDEF); return true; } @@ -203,7 +203,7 @@ bool rfclose (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, v g_files.erase(handleIter); - pVar->SetInit(CBotVar::InitType::IS_NAN); + pVar->SetInit(CBotVar::InitType::UNDEF); return true; } diff --git a/src/CBot/stdlib/MathFunctions.cpp b/src/CBot/stdlib/MathFunctions.cpp index 5353f80d..105f805a 100644 --- a/src/CBot/stdlib/MathFunctions.cpp +++ b/src/CBot/stdlib/MathFunctions.cpp @@ -218,6 +218,37 @@ bool rTrunc(CBotVar* var, CBotVar* result, int& exception, void* user) return true; } +// Instruction "isnan()" + +CBotTypResult cIsNAN(CBotVar*& var, void* user) +{ + if (var == nullptr) return CBotTypResult(CBotErrLowParam); + + if (var->GetType() > CBotTypDouble) return CBotTypResult(CBotErrBadNum); + + var = var->GetNext(); + if (var != nullptr) return CBotTypResult(CBotErrOverParam); + + return CBotTypResult(CBotTypBoolean); +} + +bool rIsNAN(CBotVar* var, CBotVar* result, int& exception, void* user) +{ + bool isnan = false; + + if (var->GetType() == CBotTypFloat) + { + if (std::isnan(var->GetValFloat())) isnan = true; + } + else if (var->GetType() == CBotTypDouble) + { + if (std::isnan(var->GetValDouble())) isnan = true; + } + + result->SetValInt(isnan); + return true; +} + } // namespace void InitMathFunctions() @@ -237,6 +268,7 @@ void InitMathFunctions() CBotProgram::AddFunction("ceil", rCeil, cOneFloat); CBotProgram::AddFunction("round", rRound, cOneFloat); CBotProgram::AddFunction("trunc", rTrunc, cOneFloat); + CBotProgram::AddFunction("isnan", rIsNAN, cIsNAN); } } // namespace CBot diff --git a/src/CBot/stdlib/StringFunctions.cpp b/src/CBot/stdlib/StringFunctions.cpp index c543df7f..a9d61b81 100644 --- a/src/CBot/stdlib/StringFunctions.cpp +++ b/src/CBot/stdlib/StringFunctions.cpp @@ -233,7 +233,7 @@ bool rStrFind( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) } else { - pResult->SetInit(CBotVar::InitType::IS_NAN); + pResult->SetValInt(-1); } return true; } diff --git a/src/script/cbottoken.cpp b/src/script/cbottoken.cpp index 911b985d..d14a7068 100644 --- a/src/script/cbottoken.cpp +++ b/src/script/cbottoken.cpp @@ -301,6 +301,7 @@ std::string GetHelpFilename(const char *token) if ( strcmp(token, "ceil" ) == 0 ) helpfile = "cbot/ceil"; if ( strcmp(token, "round" ) == 0 ) helpfile = "cbot/round"; if ( strcmp(token, "trunc" ) == 0 ) helpfile = "cbot/trunc"; + if ( strcmp(token, "isnan" ) == 0 ) helpfile = "cbot/isnan"; if ( strcmp(token, "retobject" ) == 0 ) helpfile = "cbot/retobj"; if ( strcmp(token, "errmode" ) == 0 ) helpfile = "cbot/errmode"; if ( strcmp(token, "isbusy" ) == 0 ) helpfile = "cbot/isbusy"; @@ -463,9 +464,10 @@ bool IsFunction(const char *token) if ( strcmp(token, "ceil" ) == 0 ) return true; if ( strcmp(token, "round" ) == 0 ) return true; if ( strcmp(token, "trunc" ) == 0 ) return true; + if ( strcmp(token, "isnan" ) == 0 ) return true; if ( strcmp(token, "retobjectbyid") == 0 ) return true; if ( strcmp(token, "retobject" ) == 0 ) return true; - if ( strcmp(token, "isbusy" ) == 0 ) return true; + if ( strcmp(token, "isbusy" ) == 0 ) return true; if ( strcmp(token, "factory" ) == 0 ) return true; if ( strcmp(token, "research" ) == 0 ) return true; if ( strcmp(token, "takeoff" ) == 0 ) return true; @@ -571,6 +573,7 @@ const char* GetHelpText(const char *token) if ( strcmp(token, "ceil" ) == 0 ) return "ceil ( value );"; if ( strcmp(token, "round" ) == 0 ) return "round ( value );"; if ( strcmp(token, "trunc" ) == 0 ) return "trunc ( value );"; + if ( strcmp(token, "isnan" ) == 0 ) return "isnan ( value );"; if ( strcmp(token, "retobject" ) == 0 ) return "retobject ( rank );"; if ( strcmp(token, "retobjectbyid") == 0 ) return "retobjectbyid ( rank );"; if ( strcmp(token, "progfunc" ) == 0 ) return "progfunc ( funcname );"; diff --git a/src/script/scriptfunc.cpp b/src/script/scriptfunc.cpp index 323bea85..bd782618 100644 --- a/src/script/scriptfunc.cpp +++ b/src/script/scriptfunc.cpp @@ -1836,18 +1836,30 @@ bool CScriptFunctions::rSpace(CBotVar* var, CBotVar* result, int& exception, voi } } } - script->m_main->FreeSpace(center, rMin, rMax, dist, pThis); + + bool success = script->m_main->FreeSpace(center, rMin, rMax, dist, pThis); if ( result != nullptr ) { pSub = result->GetItemList(); if ( pSub != nullptr ) { - pSub->SetValFloat(center.x/g_unit); - pSub = pSub->GetNext(); // "y" - pSub->SetValFloat(center.z/g_unit); - pSub = pSub->GetNext(); // "z" - pSub->SetValFloat(center.y/g_unit); + if (success) + { + pSub->SetValFloat(center.x / g_unit); + pSub = pSub->GetNext(); // "y" + pSub->SetValFloat(center.z / g_unit); + pSub = pSub->GetNext(); // "z" + pSub->SetValFloat(center.y / g_unit); + } + else + { + pSub->SetValFloat(center.x); + pSub = pSub->GetNext(); // "y" + pSub->SetValFloat(center.y); + pSub = pSub->GetNext(); // "z" + pSub->SetValFloat(center.z); + } } } return true; @@ -2313,7 +2325,7 @@ bool CScriptFunctions::rReceive(CBotVar* var, CBotVar* result, int& exception, v if ( err != ERR_OK ) { script->m_taskExecutor->StopForegroundTask(); - result->SetInit(CBotVar::InitType::IS_NAN); + result->SetValFloat(std::nanf("")); return true; } @@ -2324,7 +2336,7 @@ bool CScriptFunctions::rReceive(CBotVar* var, CBotVar* result, int& exception, v if ( script->m_returnValue == boost::none ) { - result->SetInit(CBotVar::InitType::IS_NAN); + result->SetValFloat(std::nanf("")); } else { @@ -3645,11 +3657,11 @@ void CScriptFunctions::uObject(CBotVar* botThis, void* user) if (IsObjectBeingTransported(object)) { pSub = pVar->GetItemList(); // "x" - pSub->SetInit(CBotVar::InitType::IS_NAN); + pSub->SetValFloat(std::nanf("")); pSub = pSub->GetNext(); // "y" - pSub->SetInit(CBotVar::InitType::IS_NAN); + pSub->SetValFloat(std::nanf("")); pSub = pSub->GetNext(); // "z" - pSub->SetInit(CBotVar::InitType::IS_NAN); + pSub->SetValFloat(std::nanf("")); } else { @@ -3750,11 +3762,11 @@ void CScriptFunctions::uObject(CBotVar* botThis, void* user) if (IsObjectBeingTransported(object) || physics == nullptr) { pSub = pVar->GetItemList(); // "x" - pSub->SetInit(CBotVar::InitType::IS_NAN); + pSub->SetValFloat(std::nanf("")); pSub = pSub->GetNext(); // "y" - pSub->SetInit(CBotVar::InitType::IS_NAN); + pSub->SetValFloat(std::nanf("")); pSub = pSub->GetNext(); // "z" - pSub->SetInit(CBotVar::InitType::IS_NAN); + pSub->SetValFloat(std::nanf("")); } else { From 789c8b22920dc4292c463e910ebac3393992bb3d Mon Sep 17 00:00:00 2001 From: tomangelo2 Date: Thu, 15 Sep 2022 01:14:29 +0200 Subject: [PATCH 6/6] Fix compilation with gcc --- src/CBot/CBotInstr/CBotExprLitNan.cpp | 4 +++- src/CBot/CBotInstr/CBotParExpr.cpp | 4 +++- src/CBot/CBotInstr/CBotTwoOpExpr.cpp | 1 + src/level/robotmain.cpp | 3 ++- src/math/half.cpp | 2 +- src/script/scriptfunc.cpp | 18 ++++++++++-------- 6 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/CBot/CBotInstr/CBotExprLitNan.cpp b/src/CBot/CBotInstr/CBotExprLitNan.cpp index 60f73e8e..9ae23150 100644 --- a/src/CBot/CBotInstr/CBotExprLitNan.cpp +++ b/src/CBot/CBotInstr/CBotExprLitNan.cpp @@ -23,6 +23,8 @@ #include "CBot/CBotVar/CBotVar.h" +#include + namespace CBot { @@ -43,7 +45,7 @@ bool CBotExprLitNan::Execute(CBotStack* &pj) if (pile->IfStep()) return false; CBotVar* var = CBotVar::Create("", CBotTypFloat); - var->SetValFloat(std::nanf("")); + var->SetValFloat(nanf("")); pile->SetVar(var); // put on the stack return pj->Return(pile); // forward below } diff --git a/src/CBot/CBotInstr/CBotParExpr.cpp b/src/CBot/CBotInstr/CBotParExpr.cpp index fe33c82d..4986560b 100644 --- a/src/CBot/CBotInstr/CBotParExpr.cpp +++ b/src/CBot/CBotInstr/CBotParExpr.cpp @@ -38,6 +38,8 @@ #include "CBot/CBotCStack.h" +#include + namespace CBot { @@ -218,7 +220,7 @@ CBotInstr* CBotParExpr::CompileLitExpr(CBotToken* &p, CBotCStack* pStack) CBotInstr* inst = new CBotExprLitNan(); inst->SetToken(pp); CBotVar* var = CBotVar::Create("", CBotTypFloat); - var->SetValFloat(std::nanf("")); + var->SetValFloat(nanf("")); pStk->SetVar(var); return pStack->Return(inst, pStk); } diff --git a/src/CBot/CBotInstr/CBotTwoOpExpr.cpp b/src/CBot/CBotInstr/CBotTwoOpExpr.cpp index 52b78372..9072a48e 100644 --- a/src/CBot/CBotInstr/CBotTwoOpExpr.cpp +++ b/src/CBot/CBotInstr/CBotTwoOpExpr.cpp @@ -31,6 +31,7 @@ #include "CBot/CBotVar/CBotVar.h" #include +#include #include namespace CBot diff --git a/src/level/robotmain.cpp b/src/level/robotmain.cpp index 2c348e7a..e2ddc726 100644 --- a/src/level/robotmain.cpp +++ b/src/level/robotmain.cpp @@ -112,6 +112,7 @@ #include #include #include +#include #include #include @@ -4155,7 +4156,7 @@ bool CRobotMain::FreeSpace(Math::Vector ¢er, float minRadius, float maxRadiu } } - float nan = std::nanf(""); + float nan = nanf(""); center = Math::Vector{ nan, nan, nan }; diff --git a/src/math/half.cpp b/src/math/half.cpp index a205c736..be847c81 100644 --- a/src/math/half.cpp +++ b/src/math/half.cpp @@ -100,7 +100,7 @@ float HaltToFloat(uint16_t value) // NaN else if ((exponent == 31) && (mantissa != 0)) { - result = std::nanf(""); + result = nanf(""); } // Normal number else diff --git a/src/script/scriptfunc.cpp b/src/script/scriptfunc.cpp index bd782618..e9cd4d40 100644 --- a/src/script/scriptfunc.cpp +++ b/src/script/scriptfunc.cpp @@ -67,6 +67,8 @@ #include "ui/displaytext.h" +#include + using namespace CBot; CBotTypResult CScriptFunctions::cClassNull(CBotVar* thisclass, CBotVar* &var) @@ -2325,7 +2327,7 @@ bool CScriptFunctions::rReceive(CBotVar* var, CBotVar* result, int& exception, v if ( err != ERR_OK ) { script->m_taskExecutor->StopForegroundTask(); - result->SetValFloat(std::nanf("")); + result->SetValFloat(nanf("")); return true; } @@ -2336,7 +2338,7 @@ bool CScriptFunctions::rReceive(CBotVar* var, CBotVar* result, int& exception, v if ( script->m_returnValue == boost::none ) { - result->SetValFloat(std::nanf("")); + result->SetValFloat(nanf("")); } else { @@ -3657,11 +3659,11 @@ void CScriptFunctions::uObject(CBotVar* botThis, void* user) if (IsObjectBeingTransported(object)) { pSub = pVar->GetItemList(); // "x" - pSub->SetValFloat(std::nanf("")); + pSub->SetValFloat(nanf("")); pSub = pSub->GetNext(); // "y" - pSub->SetValFloat(std::nanf("")); + pSub->SetValFloat(nanf("")); pSub = pSub->GetNext(); // "z" - pSub->SetValFloat(std::nanf("")); + pSub->SetValFloat(nanf("")); } else { @@ -3762,11 +3764,11 @@ void CScriptFunctions::uObject(CBotVar* botThis, void* user) if (IsObjectBeingTransported(object) || physics == nullptr) { pSub = pVar->GetItemList(); // "x" - pSub->SetValFloat(std::nanf("")); + pSub->SetValFloat(nanf("")); pSub = pSub->GetNext(); // "y" - pSub->SetValFloat(std::nanf("")); + pSub->SetValFloat(nanf("")); pSub = pSub->GetNext(); // "z" - pSub->SetValFloat(std::nanf("")); + pSub->SetValFloat(nanf("")); } else {