Skip to content

Commit

Permalink
-Added map-like interface to OrderSet.
Browse files Browse the repository at this point in the history
-Exposed OrderSet and Order to Python API.
  • Loading branch information
geoffthemedio committed Jun 17, 2018
1 parent 84bc1d7 commit b3e58c0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
9 changes: 8 additions & 1 deletion python/AI/AIWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "../../util/MultiplayerCommon.h"
#include "../../util/OptionsDB.h"
#include "../../util/OrderSet.h"
#include "../../util/Order.h"
#include "../../Empire/Empire.h"
#include "../../Empire/EmpireManager.h"
#include "../../Empire/Diplomacy.h"
Expand Down Expand Up @@ -173,9 +174,15 @@ namespace FreeOrionPython {
def("issueCreateShipDesignOrder", IssueCreateShipDesignOrderWrapper, "Orders the creation of a new ship design with the name (string), description (string), hull (string), parts vector partsVec (StringVec), graphic (string) and model (string). model should be left as an empty string as of this writing. There is currently no easy way to find the id of the new design, though the client's empire should have the new design after this order is issued successfully. Returns 1 (int) on success or 0 (int) on failure if any of the name, description, hull or graphic are empty strings, if the design is invalid (due to not following number and type of slot requirements for the hull) or if creating the design fails for some reason.");

class_<OrderSet, noncopyable>("OrderSet", no_init)
.def(map_indexing_suite<OrderSet>())
.add_property("size", &OrderSet::size)
.add_property("empty", &OrderSet::empty)
;

class_<Order, boost::noncopyable>("Order", no_init)
.add_property("empireID", &Order::EmpireID)
.add_property("executed", &Order::Executed)
;

def("getOrders", &AIInterface::IssuedOrders, return_value_policy<reference_existing_object>(), "Returns the orders the client empire has issued (OrderSet).");

def("sendChatMessage", AIInterface::SendPlayerChatMessage, "Sends the indicated message (string) to the player with the indicated recipientID (int) or to all players if recipientID is -1.");
Expand Down
1 change: 0 additions & 1 deletion util/Order.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ class FO_COMMON_API Order {

private:
virtual void ExecuteImpl() const = 0;

virtual bool UndoImpl() const;

int m_empire = ALL_EMPIRES;
Expand Down
13 changes: 13 additions & 0 deletions util/OrderSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
#include "Order.h"


namespace {
OrderPtr EMPTY_ORDER_PTR;
}

OrderPtr& OrderSet::operator[](std::size_t i) {
auto it = m_orders.find(i);
if (it == m_orders.end())
return EMPTY_ORDER_PTR;
return it->second;
}

int OrderSet::IssueOrder(const OrderPtr& order)
{ return IssueOrder(OrderPtr(order)); }

Expand All @@ -14,6 +25,8 @@ int OrderSet::IssueOrder(OrderPtr&& order) {
auto inserted = m_orders.insert({retval, std::forward<OrderPtr>(order)});
inserted.first->second->Execute();

TraceLogger() << "OrderSetIssueOrder m_orders size: " << m_orders.size();

return retval;
}

Expand Down
22 changes: 17 additions & 5 deletions util/OrderSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,25 @@ class FO_COMMON_API OrderSet {
typedef std::map<int, OrderPtr> OrderMap;

public:
typedef OrderMap::const_iterator const_iterator; ///< defines a public const_iterator type for OrderSet
typedef OrderMap::const_iterator const_iterator;
typedef OrderMap::iterator iterator;
typedef OrderMap::value_type value_type;
typedef OrderMap::size_type size_type;
typedef OrderMap::key_type key_type;
typedef OrderMap::difference_type difference_type;
typedef OrderMap::key_compare key_compare;

/** \name Accessors */ //@{
const_iterator begin() const { return m_orders.begin(); }///< returns the begin const_iterator for the OrderSet
const_iterator end() const { return m_orders.end(); } ///< returns the end const_iterator for the OrderSet
std::size_t size() const { return m_orders.size(); }
bool empty() const { return m_orders.empty(); }
const_iterator begin() const { return m_orders.begin(); }///< returns the begin const_iterator for the OrderSet
const_iterator end() const { return m_orders.end(); } ///< returns the end const_iterator for the OrderSet
iterator begin() { return m_orders.begin(); }///< returns the begin const_iterator for the OrderSet
iterator end() { return m_orders.end(); } ///< returns the end const_iterator for the OrderSet
std::size_t size() const { return m_orders.size(); }
bool empty() const { return m_orders.empty(); }
iterator find(const key_type& k) { return m_orders.find(k); }
void erase(const key_type& k){ m_orders.erase(k); }
OrderPtr& operator[](std::size_t i);
key_compare key_comp() const { return m_orders.key_comp(); }
//@}

/** \name Mutators */ //@{
Expand Down

0 comments on commit b3e58c0

Please sign in to comment.