diff --git a/Participant.cpp b/Participant.cpp index 9924df1..6637209 100644 --- a/Participant.cpp +++ b/Participant.cpp @@ -341,12 +341,15 @@ void Participant::Process(Participant *sender, NetworkIdMsg *msg) { << " " << (int)msg->networkId << "\n"; if (this->networkId != msg->networkId) { this->networkId = msg->networkId; - Thing **allThings = Thing::GetAllThings(); - for (uint16_t ix = 0; ix < THING_STORE_SIZE; ix++) { - Thing *thing = allThings[ix]; - if (thing == nullptr) - continue; + // Thing **allThings = Thing::GetAllThings(); + // for (uint16_t ix = 0; ix < THING_STORE_SIZE; ix++) { + // Thing *thing = allThings[ix]; + // if (thing == nullptr) + // continue; + // sender->SendThingInfo(thing); + // } + for (Thing* thing : allThings) { sender->SendThingInfo(thing); } } diff --git a/Thing.cpp b/Thing.cpp index f765603..9c45426 100644 --- a/Thing.cpp +++ b/Thing.cpp @@ -5,8 +5,8 @@ #include Thing::Thing(unsigned char networkId, unsigned char thingType) { - this->position = Spherical16::zero; - this->orientation = SwingTwist16::identity; + // this->position = Spherical16::zero; + // this->orientation = SwingTwist16::identity; this->type = thingType; this->networkId = networkId; @@ -152,44 +152,60 @@ Spherical16 Thing::GetLinearVelocity() { return this->linearVelocity; } Spherical16 Thing::GetAngularVelocity() { return this->angularVelocity; } // All things -Thing *Thing::allThings[THING_STORE_SIZE] = {nullptr}; +// Thing *Thing::allThings[THING_STORE_SIZE] = {nullptr}; -Thing **Passer::Control::Thing::GetAllThings() { return allThings; } +// Thing **Passer::Control::Thing::GetAllThings() { return allThings; } Thing *Thing::Get(unsigned char networkId, unsigned char thingId) { - for (uint16_t ix = 0; ix < THING_STORE_SIZE; ix++) { - Thing *thing = allThings[ix]; - if (thing == nullptr) - continue; - if (thing->networkId == networkId && thing->id == thingId) - return thing; - } - return nullptr; + // for (uint16_t ix = 0; ix < THING_STORE_SIZE; ix++) { + // Thing *thing = allThings[ix]; + // if (thing == nullptr) + // continue; + // if (thing->networkId == networkId && thing->id == thingId) + // return thing; + // } + // return nullptr; + std::find_if(allThings.begin(), allThings.end(), [networkId, thingId](Thing* thing){ + return thing->networkId == networkId && thing->id == thingId; + }); } int Thing::Add(Thing *newThing) { // Exclude 0 because that value is reserved for 'no thing' - for (uint16_t ix = 1; ix < THING_STORE_SIZE; ix++) { - Thing *thing = allThings[ix]; - if (thing == nullptr) { - allThings[ix] = newThing; + // for (uint16_t ix = 1; ix < THING_STORE_SIZE; ix++) { + // Thing *thing = allThings[ix]; + // if (thing == nullptr) { + // allThings[ix] = newThing; - // std::cout << " Add new thing " << (int)ix << "\n"; - return ix; - } + // std::cout << " Add new thing " << (int)ix << "\n"; + // return ix; + // } + // } + for (Thing* thing : allThings) { + if (thing == newThing) + return thing->id; } - return -1; + allThings.push_back(newThing); } void Thing::Remove(Thing *thing) { // std::cout << " remove " << (int)thing->id << "\n"; - allThings[thing->id] = nullptr; + // allThings[thing->id] = nullptr; + allThings.remove_if([thing](Thing* obj) { return obj == thing; }); } void Thing::UpdateAll(unsigned long currentTimeMs) { // Not very efficient, but it works for now. - for (uint16_t ix = 0; ix < THING_STORE_SIZE; ix++) { - Thing *thing = allThings[ix]; + // for (uint16_t ix = 0; ix < THING_STORE_SIZE; ix++) { + // Thing *thing = allThings[ix]; + // if (thing != nullptr && + // thing->parent == nullptr) { // update all root things + // // std::cout << " update " << (int)ix << " thingid " << (int)thing->id + // // << "\n"; + // thing->Update(currentTimeMs); + // } + // } + for (Thing* thing : allThings) { if (thing != nullptr && thing->parent == nullptr) { // update all root things // std::cout << " update " << (int)ix << " thingid " << (int)thing->id diff --git a/Thing.h b/Thing.h index 1a5e10e..61d7fa7 100644 --- a/Thing.h +++ b/Thing.h @@ -2,6 +2,7 @@ #include "LinearAlgebra/Spherical.h" #include "LinearAlgebra/SwingTwist.h" #include +#include namespace Passer { namespace Control { @@ -123,16 +124,18 @@ protected: //------------ All things public: - static Thing **GetAllThings(); + // static Thing **GetAllThings(); static Thing *Get(unsigned char networkId, unsigned char thingId); static int Add(Thing *thing); static void Remove(Thing *thing); static void UpdateAll(unsigned long currentTimeMs); -private: - static Thing *allThings[]; +//private: + // static Thing *allThings[]; }; +static std::list allThings; + } // namespace Control } // namespace Passer using namespace Passer::Control; \ No newline at end of file diff --git a/test/thing_test.cc b/test/thing_test.cc index 9e5e009..e3d20de 100644 --- a/test/thing_test.cc +++ b/test/thing_test.cc @@ -77,5 +77,23 @@ TEST_F(ControlCoreSuite, SiteParticipant) { ASSERT_EQ(1, 1); } +TEST_F(ControlCoreSuite, Thing) { + SiteServer site = SiteServer(7681); + Participant participant = Participant("127.0.0.1", 7681); + Thing thing = Thing(); + + unsigned long milliseconds = get_time_ms(); + unsigned long startTime = milliseconds; + while (milliseconds < startTime + 1000) { + site.Update(milliseconds); + participant.Update(milliseconds); + + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + milliseconds = get_time_ms(); + } + ASSERT_EQ(1, 1); +} + + } // namespace Passer #endif