mail[Wesnoth-commits] r43877 - in /trunk/src: ./ whiteboard/


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by gabrielmorin on July 04, 2010 - 09:58:
Author: gabba
Date: Sun Jul  4 09:58:42 2010
New Revision: 43877

URL: http://svn.gna.org/viewcvs/wesnoth?rev=43877&view=rev
Log:
Whiteboard: Modify the system for applying temp modifiers on the unit map, to 
prepare for movement modifications.

Modified:
    trunk/src/unit.cpp
    trunk/src/unit.hpp
    trunk/src/whiteboard/action.hpp
    trunk/src/whiteboard/manager.cpp
    trunk/src/whiteboard/mapbuilder_visitor.cpp
    trunk/src/whiteboard/mapbuilder_visitor.hpp
    trunk/src/whiteboard/move.cpp
    trunk/src/whiteboard/move.hpp
    trunk/src/whiteboard/side_actions.cpp
    trunk/src/whiteboard/side_actions.hpp
    trunk/src/whiteboard/validate_visitor.cpp
    trunk/src/whiteboard/validate_visitor.hpp

Modified: trunk/src/unit.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/unit.cpp?rev=43877&r1=43876&r2=43877&view=diff
==============================================================================
--- trunk/src/unit.cpp (original)
+++ trunk/src/unit.cpp Sun Jul  4 09:58:42 2010
@@ -2805,10 +2805,6 @@
        return res;
 }
 
-temporary_unit_map_modifier::~temporary_unit_map_modifier()
-{
-}
-
 temporary_unit_placer::temporary_unit_placer(unit_map& m, const 
map_location& loc, unit& u)
        : m_(m), loc_(loc), temp_(m.extract(loc))
 {

Modified: trunk/src/unit.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/unit.hpp?rev=43877&r1=43876&r2=43877&view=diff
==============================================================================
--- trunk/src/unit.hpp (original)
+++ trunk/src/unit.hpp Sun Jul  4 09:58:42 2010
@@ -502,20 +502,11 @@
 team_data calculate_team_data(const class team& tm, int side);
 
 /**
- * Superclass for temporary unit_map modifiers. Allows putting them together
- * in stl containers without too much fuss.
- */
-struct temporary_unit_map_modifier
-{
-       virtual ~temporary_unit_map_modifier() = 0;
-};
-
-/**
  * This object is used to temporary place a unit in the unit map, swapping 
out
  * any unit that is already there.  On destruction, it restores the unit map 
to
  * its original.
  */
-struct temporary_unit_placer : public temporary_unit_map_modifier
+struct temporary_unit_placer
 {
        temporary_unit_placer(unit_map& m, const map_location& loc, unit& u);
        virtual  ~temporary_unit_placer();
@@ -531,7 +522,7 @@
  * any unit that is already there.  On destruction, it restores the unit map 
to
  * its original.
  */
-struct temporary_unit_mover : public temporary_unit_map_modifier
+struct temporary_unit_mover
 {
        temporary_unit_mover(unit_map& m, const map_location& src,  const 
map_location& dst);
        virtual  ~temporary_unit_mover();

Modified: trunk/src/whiteboard/action.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/whiteboard/action.hpp?rev=43877&r1=43876&r2=43877&view=diff
==============================================================================
--- trunk/src/whiteboard/action.hpp (original)
+++ trunk/src/whiteboard/action.hpp Sun Jul  4 09:58:42 2010
@@ -58,12 +58,10 @@
        /** Returns true if the action has been completely executed and can 
be deleted */
        virtual bool execute() = 0;
 
-       /**
-        * Applies temporarily the result of this action to the unit map, and 
returns
-        * the corresponding modifier. Dropping the returned reference will 
revert
-        * the changes.
-        */
-       virtual modifier_ptr apply_temp_modifier(unit_map& unit_map) = 0;
+       /** Applies temporarily the result of this action to the specified 
unit map. */
+       virtual void apply_temp_modifier(unit_map& unit_map) = 0;
+       /** Removes the result of this action from the specified unit map. */
+       virtual void remove_temp_modifier(unit_map& unit_map) = 0;
 
        /**
         * Indicates whether this action is related to the specified hex.

Modified: trunk/src/whiteboard/manager.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/whiteboard/manager.cpp?rev=43877&r1=43876&r2=43877&view=diff
==============================================================================
--- trunk/src/whiteboard/manager.cpp (original)
+++ trunk/src/whiteboard/manager.cpp Sun Jul  4 09:58:42 2010
@@ -33,8 +33,6 @@
 #include "unit_display.hpp"
 
 namespace wb {
-
-typedef boost::shared_ptr<side_actions> side_actions_ptr;
 
 manager::manager():
                active_(false),
@@ -72,15 +70,9 @@
        {
                if (!planned_unit_map_active_)
                {
-                       //wb_scoped_lock lock(actions_modification_mutex_);
-                       mapbuilder_.reset(new 
mapbuilder_visitor(*resources::units));
-                       const action_set& actions = 
current_actions()->actions();
                        DBG_WB << "Building planned unit map.\n";
-                       foreach (const action_ptr &action, actions)
-                       {
-                               assert(action);
-                               action->accept(*mapbuilder_);
-                       }
+                       mapbuilder_.reset(new 
mapbuilder_visitor(*resources::units, current_actions()));
+                       mapbuilder_->build_map();
                        planned_unit_map_active_ = true;
                }
                else
@@ -96,7 +88,6 @@
        {
                if (planned_unit_map_active_)
                {
-                       //wb_scoped_lock lock(actions_modification_mutex_);
                        DBG_WB << "Restoring regular unit map.\n";
                        mapbuilder_.reset();
                        planned_unit_map_active_ = false;

Modified: trunk/src/whiteboard/mapbuilder_visitor.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/whiteboard/mapbuilder_visitor.cpp?rev=43877&r1=43876&r2=43877&view=diff
==============================================================================
--- trunk/src/whiteboard/mapbuilder_visitor.cpp (original)
+++ trunk/src/whiteboard/mapbuilder_visitor.cpp Sun Jul  4 09:58:42 2010
@@ -17,27 +17,44 @@
  */
 
 #include "mapbuilder_visitor.hpp"
+#include "action.hpp"
 #include "move.hpp"
+#include "side_actions.hpp"
 
+#include "foreach.hpp"
 #include "unit.hpp"
 #include "unit_map.hpp"
 
 namespace wb
 {
 
-mapbuilder_visitor::mapbuilder_visitor(unit_map& unit_map)
+mapbuilder_visitor::mapbuilder_visitor(unit_map& unit_map, side_actions_ptr 
side_actions)
        : unit_map_(unit_map)
     , excluded_units_()
-       , modifiers_()
+       , side_actions_(side_actions)
+       , mode_(BUILD_PLANNED_MAP)
 {
 }
 
 mapbuilder_visitor::~mapbuilder_visitor()
 {
-       size_t stack_size = modifiers_.size();
-       for (size_t i = 0; i < stack_size; ++i )
+       mode_ = RESTORE_NORMAL_MAP;
+       const action_set& actions = side_actions_->actions();
+       action_set::const_reverse_iterator rit;
+       for (rit = actions.rbegin(); rit != actions.rend(); ++rit)
        {
-               modifiers_.pop();
+               (*rit)->accept(*this);
+       }
+
+}
+
+void mapbuilder_visitor::build_map()
+{
+       mode_ = BUILD_PLANNED_MAP;
+       const action_set& actions = side_actions_->actions();
+       foreach(action_ptr action, actions)
+       {
+               action->accept(*this);
        }
 }
 
@@ -45,7 +62,14 @@
 {
        if (excluded_units_.find(&move->get_unit()) == excluded_units_.end())
        {
-               modifiers_.push(move->apply_temp_modifier(unit_map_));
+               if(mode_ == BUILD_PLANNED_MAP)
+               {
+                       move->apply_temp_modifier(unit_map_);
+               }
+               else if (mode_ == RESTORE_NORMAL_MAP)
+               {
+                       move->remove_temp_modifier(unit_map_);
+               }
        }
 }
 

Modified: trunk/src/whiteboard/mapbuilder_visitor.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/whiteboard/mapbuilder_visitor.hpp?rev=43877&r1=43876&r2=43877&view=diff
==============================================================================
--- trunk/src/whiteboard/mapbuilder_visitor.hpp (original)
+++ trunk/src/whiteboard/mapbuilder_visitor.hpp Sun Jul  4 09:58:42 2010
@@ -33,6 +33,10 @@
 namespace wb
 {
 
+class side_actions;
+
+typedef boost::shared_ptr<side_actions> side_actions_ptr;
+
 /**
  * Visitor that collects and applies unit_map modifications from the actions 
it visits
  * and reverts all changes on destruction.
@@ -41,21 +45,37 @@
 {
 
 public:
-       mapbuilder_visitor(unit_map& unit_map);
+       mapbuilder_visitor(unit_map& unit_map, side_actions_ptr side_actions);
        virtual ~mapbuilder_visitor();
 
+       enum mapbuilder_mode {
+               BUILD_PLANNED_MAP,
+               RESTORE_NORMAL_MAP
+       };
+
+       /**
+        * Visits all the actions contained in the side_actions object passed 
to the constructor,
+        * and calls the appropriate visit_* method on each of them.
+        */
+       virtual void build_map();
+
+       /// Any actions associated with this unit will be ignored when 
modifying the unit map
+       virtual void exclude(const unit& unit) { 
excluded_units_.insert(&unit); }
+
+       /// Visitor pattern method, no need to call this directly
        virtual void visit_move(boost::shared_ptr<move> move);
 
-       // Any actions associated with this unit will be ignored when 
modifying the unit map
-       virtual void exclude(const unit& unit) { 
excluded_units_.insert(&unit); }
+
 
 protected:
        unit_map& unit_map_;
 
        std::set<unit const*> excluded_units_;
 
-private:
-       std::stack<modifier_ptr> modifiers_;
+       side_actions_ptr side_actions_;
+
+       mapbuilder_mode mode_;
+
 };
 
 }

Modified: trunk/src/whiteboard/move.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/whiteboard/move.cpp?rev=43877&r1=43876&r2=43877&view=diff
==============================================================================
--- trunk/src/whiteboard/move.cpp (original)
+++ trunk/src/whiteboard/move.cpp Sun Jul  4 09:58:42 2010
@@ -42,7 +42,7 @@
 move::move(unit& subject, const map_location& source_hex, const 
map_location& target_hex,
                boost::shared_ptr<arrow> arrow, boost::shared_ptr<unit> 
fake_unit)
 : unit_(subject),
-  orig_hex_(source_hex),
+  source_hex_(source_hex),
   dest_hex_(target_hex),
   arrow_(arrow),
   fake_unit_(fake_unit),
@@ -115,7 +115,7 @@
                }
                if (found)
                {
-                       orig_hex_ = final_location;
+                       source_hex_ = final_location;
                        --start_new_path; //since the for loop incremented 
the iterator once after we found the right one.
                        arrow_path_t new_path(start_new_path, 
arrow_path.end());
                        LOG_WB << "Setting new path for this move from (" << 
new_path.front()
@@ -138,21 +138,28 @@
        return move_finished_completely;
 }
 
-modifier_ptr move::apply_temp_modifier(unit_map& unit_map)
+void move::apply_temp_modifier(unit_map& unit_map)
 {
        //TODO: properly handle movement points
 
-       assert(unit_.get_location() == orig_hex_);
-       DBG_WB << "Adding temp unit mover for unit " << unit_.name() << " [" 
<< unit_.underlying_id() << "] "
-                       << " from (" << orig_hex_ << ") to (" << dest_hex_ 
<<")\n";
-       modifier_ptr modifier(new temporary_unit_mover(unit_map, orig_hex_, 
dest_hex_));
-       return modifier;
+       assert(unit_.get_location() == source_hex_);
+       DBG_WB << "Temporarily moving unit " << unit_.name() << " [" << 
unit_.underlying_id() << "] "
+                       << " from (" << source_hex_ << ") to (" << dest_hex_ 
<<")\n";
+       unit_map.move(source_hex_, dest_hex_);
+       assert(unit_.get_location() == dest_hex_);
+}
+
+void move::remove_temp_modifier(unit_map& unit_map)
+{
+       assert(unit_.get_location() == dest_hex_);
+       unit_map.move(dest_hex_, source_hex_);
+       assert(unit_.get_location() == source_hex_);
 }
 
 bool move::is_related_to(const map_location& hex) const
 {
        //bool is_related = arrow_->path_contains(hex);
-       bool is_related = hex == orig_hex_ || hex == dest_hex_;
+       bool is_related = hex == source_hex_ || hex == dest_hex_;
        return is_related;
 }
 

Modified: trunk/src/whiteboard/move.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/whiteboard/move.hpp?rev=43877&r1=43876&r2=43877&view=diff
==============================================================================
--- trunk/src/whiteboard/move.hpp (original)
+++ trunk/src/whiteboard/move.hpp Sun Jul  4 09:58:42 2010
@@ -58,12 +58,10 @@
        boost::shared_ptr<arrow> get_arrow() { return arrow_; }
        boost::shared_ptr<unit> get_fake_unit() { return fake_unit_; }
 
-       /**
-        * Applies temporarily the result of this move to the unit map, and 
returns
-        * the corresponding modifier. Dropping the returned reference will 
revert
-        * the changes.
-        */
-       virtual modifier_ptr apply_temp_modifier(unit_map& unit_map);
+       /** Applies temporarily the result of this action to the specified 
unit map. */
+       virtual void apply_temp_modifier(unit_map& unit_map);
+       /** Removes the result of this action from the specified unit map. */
+       virtual void remove_temp_modifier(unit_map& unit_map);
 
        virtual bool is_related_to(const map_location& hex) const;
        virtual bool is_related_to(const unit& unit) const;
@@ -77,7 +75,7 @@
 
 private:
        unit & unit_;
-       map_location orig_hex_;
+       map_location source_hex_;
        map_location dest_hex_;
 
        boost::shared_ptr<arrow> arrow_;

Modified: trunk/src/whiteboard/side_actions.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/whiteboard/side_actions.cpp?rev=43877&r1=43876&r2=43877&view=diff
==============================================================================
--- trunk/src/whiteboard/side_actions.cpp (original)
+++ trunk/src/whiteboard/side_actions.cpp Sun Jul  4 09:58:42 2010
@@ -238,11 +238,8 @@
                ERR_WB << "Validating action queue while temp modifiers are 
applied!!!\n";
        }
 
-       validate_visitor validator(*resources::units);
-       foreach(action_ptr action, actions_)
-       {
-               action->accept(validator);
-       }
+       validate_visitor validator(*resources::units, shared_from_this());
+       validator.validate_actions();
        update_last_action_display();
 }
 

Modified: trunk/src/whiteboard/side_actions.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/whiteboard/side_actions.hpp?rev=43877&r1=43876&r2=43877&view=diff
==============================================================================
--- trunk/src/whiteboard/side_actions.hpp (original)
+++ trunk/src/whiteboard/side_actions.hpp Sun Jul  4 09:58:42 2010
@@ -25,6 +25,7 @@
 
 #include <deque>
 
+#include <boost/enable_shared_from_this.hpp>
 #include <boost/shared_ptr.hpp>
 
 class unit;
@@ -34,10 +35,12 @@
 {
 
 class move;
+class side_actions;
 
+typedef boost::shared_ptr<side_actions> side_actions_ptr;
 typedef std::deque<action_ptr> action_set;
 
-class side_actions
+class side_actions: public boost::enable_shared_from_this<side_actions>
 {
 public:
 

Modified: trunk/src/whiteboard/validate_visitor.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/whiteboard/validate_visitor.cpp?rev=43877&r1=43876&r2=43877&view=diff
==============================================================================
--- trunk/src/whiteboard/validate_visitor.cpp (original)
+++ trunk/src/whiteboard/validate_visitor.cpp Sun Jul  4 09:58:42 2010
@@ -17,9 +17,11 @@
  */
 
 #include "validate_visitor.hpp"
+#include "move.hpp"
+#include "side_actions.hpp"
+
 #include "arrow.hpp"
-#include "move.hpp"
-
+#include "foreach.hpp"
 #include "pathfind/pathfind.hpp"
 #include "play_controller.hpp"
 #include "resources.hpp"
@@ -28,8 +30,8 @@
 namespace wb
 {
 
-validate_visitor::validate_visitor(unit_map& unit_map)
-       : mapbuilder_visitor(unit_map)
+validate_visitor::validate_visitor(unit_map& unit_map, side_actions_ptr 
side_actions)
+       : mapbuilder_visitor(unit_map, side_actions)
 {
 }
 
@@ -45,14 +47,22 @@
        return current_team;
 }
 
+void validate_visitor::validate_actions()
+{
+       foreach(action_ptr action, side_actions_->actions())
+       {
+               action->accept(*this);
+       }
+}
+
 void validate_visitor::visit_move(boost::shared_ptr<move> move)
 {
        bool valid = true;
 
-       if (!(move->orig_hex_.valid() && move->dest_hex_.valid()))
+       if (!(move->source_hex_.valid() && move->dest_hex_.valid()))
                valid = false;
 
-       if (valid && resources::units->find(move->orig_hex_) == 
resources::units->end())
+       if (valid && resources::units->find(move->source_hex_) == 
resources::units->end())
                valid = false;
 
        pathfind::plain_route route;
@@ -60,7 +70,7 @@
        {
                pathfind::shortest_path_calculator path_calc(move->unit_, 
get_current_team(), *resources::units,
                                *resources::teams, *resources::game_map);
-               route = pathfind::a_star_search(move->orig_hex_,
+               route = pathfind::a_star_search(move->source_hex_,
                                move->dest_hex_, 10000, &path_calc, 
resources::game_map->w(), resources::game_map->h());
                if (route.move_cost >= path_calc.getNoPathValue())
                {

Modified: trunk/src/whiteboard/validate_visitor.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/whiteboard/validate_visitor.hpp?rev=43877&r1=43876&r2=43877&view=diff
==============================================================================
--- trunk/src/whiteboard/validate_visitor.hpp (original)
+++ trunk/src/whiteboard/validate_visitor.hpp Sun Jul  4 09:58:42 2010
@@ -27,8 +27,10 @@
 class validate_visitor: public mapbuilder_visitor
 {
 public:
-       validate_visitor(unit_map& unit_map);
+       validate_visitor(unit_map& unit_map, side_actions_ptr side_actions);
        virtual ~validate_visitor();
+
+       void validate_actions();
 
        virtual void visit_move(boost::shared_ptr<move> move);
 };




Related Messages


Powered by MHonArc, Updated Sun Jul 04 10:00:06 2010