comparison engine/core/pathfinder/linearpather/linearpather.cpp @ 272:b04a2faf7d86

* Reverted the latest pathfinding module changes as they broke building trunk * Please compile the engine and run all unittests via trunk/test_fife.py before commiting any changes to trunk!
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Tue, 16 Jun 2009 11:28:35 +0000
parents
children
comparison
equal deleted inserted replaced
271:987a4ea829c1 272:b04a2faf7d86
1 /***************************************************************************
2 * Copyright (C) 2005-2008 by the FIFE team *
3 * http://www.fifengine.de *
4 * This file is part of FIFE. *
5 * *
6 * FIFE is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Lesser General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2.1 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * Lesser General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with this library; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 // Standard C++ library includes
23
24 // 3rd party library includes
25
26 // FIFE includes
27 // These includes are split up in two parts, separated by one empty line
28 // First block: files included from the FIFE root src directory
29 // Second block: files included from the same folder
30 #include "util/log/logger.h"
31 #include "model/metamodel/grids/cellgrid.h"
32 #include "model/structures/instance.h"
33 #include "model/structures/layer.h"
34
35 #include "linearpather.h"
36
37 namespace FIFE {
38 static Logger _log(LM_PATHFINDER);
39
40 int LinearPather::getNextLocation(const Instance* instance, const Location& target,
41 double distance_to_travel, Location& nextLocation,
42 Location& facingLocation, int session_id, int priority) {
43 Location curloc = instance->getLocation();
44 assert(curloc.getMap() == target.getMap());
45 Layer* layer = curloc.getLayer();
46 assert(layer == target.getLayer());
47 CellGrid* cg = layer->getCellGrid();
48 assert(layer == target.getLayer());
49
50 assert(curloc.getLayer() == target.getLayer());
51 m_map = curloc.getMap();
52
53 int cur_session_id = session_id;
54 if (cur_session_id < 0) {
55 cur_session_id = m_session_counter++;
56
57 // extrapolate facing location for this session
58 ExactModelCoordinate cur_pos = curloc.getMapCoordinates();
59 ExactModelCoordinate fac_pos = target.getMapCoordinates();
60 fac_pos.x = fac_pos.x + (fac_pos.x - cur_pos.x);
61 fac_pos.y = fac_pos.y + (fac_pos.y - cur_pos.y);
62 facingLocation = target;
63 facingLocation.setMapCoordinates(fac_pos);
64 m_session2face[cur_session_id] = facingLocation;
65 FL_DBG(_log, LMsg("storing new facing loc ") << facingLocation);
66 } else {
67 FL_DBG(_log, LMsg("getting old facing loc ") << facingLocation);
68 facingLocation = m_session2face[cur_session_id];
69 }
70 FL_DBG(_log, LMsg("curloc ") << curloc << ", target " << target << ", dist2travel " << distance_to_travel);
71 ExactModelCoordinate cur_pos = curloc.getMapCoordinates();
72 ExactModelCoordinate target_pos = target.getMapCoordinates();
73 double dx = (target_pos.x - cur_pos.x) * cg->getXScale();
74 double dy = (target_pos.y - cur_pos.y) * cg->getYScale();
75 double dist = sqrt(dx*dx + dy*dy);
76 FL_DBG(_log, LMsg("distance from cur to target = ") << dist);
77
78 // calculate where current position evolves with movement
79 if (distance_to_travel > dist) {
80 distance_to_travel = dist;
81 }
82 cur_pos.x += dx * (distance_to_travel / dist);
83 cur_pos.y += dy * (distance_to_travel / dist);
84 nextLocation.setMapCoordinates(cur_pos);
85 FL_DBG(_log, LMsg("in case not blocking, could move to ") << nextLocation);
86
87 // check if we have collisions and if we do, keep instance on current location
88 ModelCoordinate nextCellcoord = nextLocation.getLayerCoordinates();
89 const std::vector<Instance*>& instances = target.getLayer()->getInstances();
90 std::vector<Instance*>::const_iterator instance_it = instances.begin();
91 for (;instance_it != instances.end(); ++instance_it) {
92 Instance* i = (*instance_it);
93 if ((i == instance) || (!i->getObject()->isBlocking())) {
94 continue;
95 }
96 ModelCoordinate c = i->getLocationRef().getLayerCoordinates();
97 if ((c.x == nextCellcoord.x) && (c.y == nextCellcoord.y)) {
98 FL_DBG(_log, LMsg("Found blocking instance from planned cell ") << nextLocation);
99 nextLocation = curloc;
100 break;
101 }
102 }
103 //We're there
104 if((ABS(cur_pos.x - target_pos.x) < 0.1) && (ABS(cur_pos.y - target_pos.y) < 0.1)) {
105 return -1;
106 }
107 return cur_session_id;
108 }
109 }