From: <sv...@ww...> - 2005-05-14 20:19:54
|
Author: mkrose Date: 2005-05-14 13:19:42 -0700 (Sat, 14 May 2005) New Revision: 1544 Modified: trunk/CSP/CSPSim/CHANGES.current trunk/CSP/SimCore/Battlefield/Battlefield.cpp trunk/CSP/SimCore/Battlefield/Battlefield.h trunk/CSP/SimCore/Battlefield/LocalBattlefield.cpp trunk/CSP/SimCore/Battlefield/LocalBattlefield.h trunk/CSP/SimCore/Battlefield/SimObject.cpp trunk/CSP/SimCore/Battlefield/SimObject.h Log: Add a list of contacts to SimObject of nearby vehicles that is maintained by the Battlefield. The battlefield was already compiling this list, it just wasn't easily accessible from within a SimObject class. Currently the list is updated every few seconds, and there is a hash of the ids in the list to make it easy to detect when the list has changed. Browse at: https://www.zerobar.net/viewcvs/viewcvs.cgi?view=rev&rev=1544 Modified: trunk/CSP/CSPSim/CHANGES.current =================================================================== --- trunk/CSP/CSPSim/CHANGES.current 2005-05-14 19:54:53 UTC (rev 1543) +++ trunk/CSP/CSPSim/CHANGES.current 2005-05-14 20:19:42 UTC (rev 1544) @@ -1,10 +1,18 @@ Version 0.4.0 (in progress) =========================== +2005-05-14: onsight + * Add a list of contacts to SimObject of nearby vehicles that is + maintained by the Battlefield. The battlefield was already + compiling this list, it just wasn't easily accessible from + within a SimObject class. Currently the list is updated every + few seconds, and there is a hash of the ids in the list to make + it easy to detect when the list has changed. + 2005-05-04: delta * Replace 'remainder' by 'fmod'. - - * Protect hud binding when no bus is defined (in + + * Protect hud binding when no bus is defined (in SceneModel::bindAnimationChannels). 2005-05-03: onsight Modified: trunk/CSP/SimCore/Battlefield/Battlefield.cpp =================================================================== --- trunk/CSP/SimCore/Battlefield/Battlefield.cpp 2005-05-14 19:54:53 UTC (rev 1543) +++ trunk/CSP/SimCore/Battlefield/Battlefield.cpp 2005-05-14 20:19:42 UTC (rev 1544) @@ -291,6 +291,11 @@ } } +Battlefield::Unit Battlefield::getUnitById(ObjectId id) { + UnitWrapper *wrapper = findUnitWrapper(id); + return wrapper ? wrapper->unit() : 0; +} + Battlefield::UnitWrapper *Battlefield::findUnitWrapper(ObjectId id) { UnitMap::iterator iter = m_UnitMap.find(id); if (iter == m_UnitMap.end()) return 0; Modified: trunk/CSP/SimCore/Battlefield/Battlefield.h =================================================================== --- trunk/CSP/SimCore/Battlefield/Battlefield.h 2005-05-14 19:54:53 UTC (rev 1543) +++ trunk/CSP/SimCore/Battlefield/Battlefield.h 2005-05-14 20:19:42 UTC (rev 1544) @@ -119,6 +119,9 @@ */ Unit getNextUnit(Unit const &unit, int human, int local, int category); + /** Lookup a unit by its id. + */ + Unit getUnitById(ObjectId id); protected: Modified: trunk/CSP/SimCore/Battlefield/LocalBattlefield.cpp =================================================================== --- trunk/CSP/SimCore/Battlefield/LocalBattlefield.cpp 2005-05-14 19:54:53 UTC (rev 1543) +++ trunk/CSP/SimCore/Battlefield/LocalBattlefield.cpp 2005-05-14 20:19:42 UTC (rev 1544) @@ -237,15 +237,20 @@ dynamicIndex()->query(region, contacts); const unsigned n_contacts = contacts.size(); std::vector<PeerContact> peer_contacts; + unit->m_ContactList.resize(0); peer_contacts.reserve(n_contacts); simdata::Vector3 unit_position = unit->getGlobalPosition(); + simdata::uint32 signature = 0; for (unsigned i=0; i < n_contacts; ++i) { LocalUnitWrapper *contact = static_cast<LocalUnitWrapper*>(contacts[i]); if (contact->id() == wrapper->id()) continue; if (!contact->unit()) continue; float distance = static_cast<float>((contact->unit()->getGlobalPosition() - unit_position).length2()); peer_contacts.push_back(PeerContact(contact->owner(), distance)); + unit->m_ContactList.push_back(contact->id()); + signature = simdata::make_unordered_fingerprint(signature, simdata::hash_uint32(static_cast<uint32>(contact->id()))); } + unit->m_ContactSignature = signature; CSP_LOG(BATTLEFIELD, INFO, "found " << peer_contacts.size() << " nearby units"); if (!peer_contacts.empty()) { std::sort(peer_contacts.begin(), peer_contacts.end(), PeerContactSorter()); Modified: trunk/CSP/SimCore/Battlefield/LocalBattlefield.h =================================================================== --- trunk/CSP/SimCore/Battlefield/LocalBattlefield.h 2005-05-14 19:54:53 UTC (rev 1543) +++ trunk/CSP/SimCore/Battlefield/LocalBattlefield.h 2005-05-14 20:19:42 UTC (rev 1544) @@ -185,7 +185,13 @@ int m_ScanSignature; std::vector<Unit> m_ScanUnits; + // Perform a spatial query for objects near the specified unit. Updates + // the unit's contact list and the rate at which position messages are + // sent to peers. Called by continueUnitScan. void scanUnit(LocalUnitWrapper *wrapper); + + // Continue a slow iteration through all units in the battlefield, calling + // scanUnit on each. This method should be called once per time step. void continueUnitScan(double dt); simdata::ScopedPointer<simcore::Signal2<int, const std::string&> > m_PlayerJoinSignal; Modified: trunk/CSP/SimCore/Battlefield/SimObject.cpp =================================================================== --- trunk/CSP/SimCore/Battlefield/SimObject.cpp 2005-05-14 19:54:53 UTC (rev 1543) +++ trunk/CSP/SimCore/Battlefield/SimObject.cpp 2005-05-14 20:19:42 UTC (rev 1544) @@ -35,7 +35,8 @@ m_Flags(0), // remote agent m_Name("?"), m_AirBubble(0), - m_GroundBubble(0) + m_GroundBubble(0), + m_ContactSignature(0) { CSP_LOG(BATTLEFIELD, DEBUG, "SimObject::SimObject(): " << _debugId()); // XXX battlefield should set this! Modified: trunk/CSP/SimCore/Battlefield/SimObject.h =================================================================== --- trunk/CSP/SimCore/Battlefield/SimObject.h 2005-05-14 19:54:53 UTC (rev 1543) +++ trunk/CSP/SimCore/Battlefield/SimObject.h 2005-05-14 20:19:42 UTC (rev 1544) @@ -73,6 +73,9 @@ int m_AirBubble; int m_GroundBubble; + std::vector<ObjectId> m_ContactList; + simdata::uint32 m_ContactSignature; + inline void setFlags(int flag, bool on) { if (on) m_Flags |= flag; else m_Flags &= ~flag; } @@ -310,6 +313,19 @@ // get the object state to be sent to remote mirrors of the object virtual simdata::Ref<simnet::NetworkMessage> getState(simcore::TimeStamp /*current_time*/, simdata::SimTime /*interval*/, int /*detail*/) const { return 0; } + // retrieve a list of ids of nearby objects. this list is periodically + // updated by the battlefield. the battlefield updates are synchronous, + // so iterators are safe to use for the duration of an object update + // method, but should not be may not remain valid across multiple updates. + // see also contactSignature(). + std::vector<ObjectId> const &getContacts() const { return m_ContactList; } + + // return a signature of the ids in the list returned by getContacts. this + // signature changes whenever objects are added or removed from the contact + // list. contact lists with the same ids have identical signatures, although + // the order of the ids may differ. + int contactSignature() const { return m_ContactSignature; } + protected: virtual void onNewContact(simdata::Ref<SimObject> const &/*contact*/) { } |