Initial commit
This commit is contained in:
43
engines/pink/objects/walk/walk_location.cpp
Normal file
43
engines/pink/objects/walk/walk_location.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "common/debug.h"
|
||||
|
||||
#include "pink/archive.h"
|
||||
#include "pink/pink.h"
|
||||
#include "pink/objects/walk/walk_location.h"
|
||||
|
||||
namespace Pink {
|
||||
|
||||
void WalkLocation::deserialize(Pink::Archive &archive) {
|
||||
NamedObject::deserialize(archive);
|
||||
_neighbors.deserialize(archive);
|
||||
}
|
||||
|
||||
void WalkLocation::toConsole() const {
|
||||
debugC(6, kPinkDebugLoadingObjects, "\tWalkLocation: _name =%s", _name.c_str());
|
||||
debugC(6, kPinkDebugLoadingObjects, "\tNeighbors:");
|
||||
for (uint i = 0; i < _neighbors.size(); ++i) {
|
||||
debugC(6, kPinkDebugLoadingObjects, "\t\t%s", _neighbors[i].c_str());
|
||||
}
|
||||
}
|
||||
|
||||
} // End of namespace Pink
|
||||
40
engines/pink/objects/walk/walk_location.h
Normal file
40
engines/pink/objects/walk/walk_location.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
#ifndef PINK_WALK_LOCATION_H
|
||||
#define PINK_WALK_LOCATION_H
|
||||
|
||||
#include "pink/utils.h"
|
||||
|
||||
namespace Pink {
|
||||
|
||||
class WalkLocation : public NamedObject {
|
||||
public:
|
||||
void deserialize(Archive &archive) override;
|
||||
void toConsole() const override;
|
||||
Common::StringArray &getNeigbors() { return _neighbors;}
|
||||
|
||||
private:
|
||||
StringArray _neighbors;
|
||||
};
|
||||
|
||||
} // End of namespace Pink
|
||||
|
||||
#endif
|
||||
175
engines/pink/objects/walk/walk_mgr.cpp
Normal file
175
engines/pink/objects/walk/walk_mgr.cpp
Normal file
@@ -0,0 +1,175 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "math/utils.h"
|
||||
|
||||
#include "pink/archive.h"
|
||||
#include "pink/cel_decoder.h"
|
||||
#include "pink/pink.h"
|
||||
#include "pink/objects/actions/walk_action.h"
|
||||
#include "pink/objects/actors/lead_actor.h"
|
||||
#include "pink/objects/walk/walk_location.h"
|
||||
|
||||
namespace Pink {
|
||||
|
||||
WalkMgr::WalkMgr()
|
||||
: _isWalking(false), _leadActor(nullptr),
|
||||
_destination(nullptr) {}
|
||||
|
||||
WalkMgr::~WalkMgr() {
|
||||
for (uint i = 0; i < _locations.size(); ++i) {
|
||||
delete _locations[i];
|
||||
}
|
||||
}
|
||||
|
||||
void WalkMgr::deserialize(Pink::Archive &archive) {
|
||||
_leadActor = static_cast<LeadActor *>(archive.readObject());
|
||||
_locations.deserialize(archive);
|
||||
}
|
||||
|
||||
WalkLocation *WalkMgr::findLocation(const Common::String &name) {
|
||||
for (uint i = 0; i < _locations.size(); ++i) {
|
||||
if (_locations[i]->getName() == name)
|
||||
return _locations[i];
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void WalkMgr::toConsole() const {
|
||||
debugC(6, kPinkDebugLoadingObjects, "WalkMgr:");
|
||||
for (uint i = 0; i < _locations.size(); ++i) {
|
||||
_locations[i]->toConsole();
|
||||
}
|
||||
}
|
||||
|
||||
void WalkMgr::start(WalkLocation *destination) {
|
||||
if (_current.name.empty()) {
|
||||
_current.name = _locations[0]->getName();
|
||||
_current.coords = getLocationCoordinates(_locations[0]->getName());
|
||||
}
|
||||
|
||||
_destination = destination;
|
||||
|
||||
if (_isWalking)
|
||||
return;
|
||||
|
||||
if (_current.name == _destination->getName()) {
|
||||
end();
|
||||
} else {
|
||||
_isWalking = true;
|
||||
WalkLocation *currentLocation = findLocation(_current.name);
|
||||
WalkShortestPath path(this);
|
||||
WalkLocation *nextLocation = path.next(currentLocation, _destination);
|
||||
initNextWayPoint(nextLocation);
|
||||
_leadActor->setAction(getWalkAction());
|
||||
}
|
||||
}
|
||||
|
||||
void WalkMgr::initNextWayPoint(WalkLocation *location) {
|
||||
_next.name = location->getName();
|
||||
_next.coords = getLocationCoordinates(location->getName());
|
||||
}
|
||||
|
||||
WalkAction *WalkMgr::getWalkAction() {
|
||||
Common::String walkActionName;
|
||||
bool horizontal = false;
|
||||
if (_current.coords.z == _next.coords.z) {
|
||||
if (_next.coords.point.x > _current.coords.point.x) {
|
||||
walkActionName = Common::String::format("%dRight", _current.coords.z);
|
||||
} else
|
||||
walkActionName = Common::String::format("%dLeft", _next.coords.z);
|
||||
horizontal = true;
|
||||
} else
|
||||
walkActionName = Common::String::format("%dTo%d", _current.coords.z, _next.coords.z);
|
||||
|
||||
WalkAction *action = (WalkAction *)_leadActor->findAction(walkActionName);
|
||||
if (action) {
|
||||
action->setWalkMgr(this);
|
||||
action->setType(horizontal);
|
||||
}
|
||||
return action;
|
||||
}
|
||||
|
||||
double WalkMgr::getLengthBetweenLocations(WalkLocation *first, WalkLocation *second) {
|
||||
Coordinates firstCoord = getLocationCoordinates(first->getName());
|
||||
Coordinates secondCoord = getLocationCoordinates(second->getName());
|
||||
return Math::hypotenuse(secondCoord.point.x - firstCoord.point.x, secondCoord.point.y - firstCoord.point.y);
|
||||
}
|
||||
|
||||
Coordinates WalkMgr::getLocationCoordinates(const Common::String &locationName) {
|
||||
Action *action = _leadActor->findAction(locationName);
|
||||
return action->getCoordinates();
|
||||
}
|
||||
|
||||
void WalkMgr::setCurrentWayPoint(WalkLocation *location) {
|
||||
_current.name = location->getName();
|
||||
_current.coords = getLocationCoordinates(_current.name);
|
||||
}
|
||||
|
||||
void WalkMgr::update() {
|
||||
if (_leadActor->isPlaying())
|
||||
return;
|
||||
|
||||
WalkShortestPath path(this);
|
||||
_current = _next;
|
||||
WalkLocation *next = path.next(findLocation(_current.name), _destination);
|
||||
if (next) {
|
||||
initNextWayPoint(next);
|
||||
_leadActor->setAction(getWalkAction());
|
||||
} else
|
||||
end();
|
||||
|
||||
}
|
||||
|
||||
void WalkMgr::end() {
|
||||
_isWalking = false;
|
||||
_leadActor->onWalkEnd(_destination->getName());
|
||||
}
|
||||
|
||||
void WalkMgr::loadState(Archive &archive) {
|
||||
_isWalking = archive.readByte();
|
||||
_current.name = archive.readString();
|
||||
if (!_current.name.empty()) {
|
||||
_current.coords = getLocationCoordinates(_current.name);
|
||||
}
|
||||
if (_isWalking) {
|
||||
_next.name = archive.readString();
|
||||
_destination = findLocation(archive.readString());
|
||||
_next.coords = getLocationCoordinates(_next.name);
|
||||
}
|
||||
}
|
||||
|
||||
void WalkMgr::saveState(Archive &archive) {
|
||||
archive.writeByte(_isWalking);
|
||||
archive.writeString(_current.name);
|
||||
if (_isWalking) {
|
||||
archive.writeString(_next.name);
|
||||
archive.writeString(_destination->getName());
|
||||
}
|
||||
}
|
||||
|
||||
void WalkMgr::skip() {
|
||||
initNextWayPoint(_destination);
|
||||
_current = _next;
|
||||
end();
|
||||
}
|
||||
|
||||
} // End of namespace Pink
|
||||
85
engines/pink/objects/walk/walk_mgr.h
Normal file
85
engines/pink/objects/walk/walk_mgr.h
Normal file
@@ -0,0 +1,85 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef PINK_WALK_MGR_H
|
||||
#define PINK_WALK_MGR_H
|
||||
|
||||
#include "common/rect.h"
|
||||
|
||||
#include "pink/objects/object.h"
|
||||
#include "pink/objects/walk/walk_shortest_path.h"
|
||||
#include "pink/utils.h"
|
||||
|
||||
namespace Pink {
|
||||
|
||||
class WalkLocation;
|
||||
class LeadActor;
|
||||
class WalkAction;
|
||||
|
||||
struct Coordinates {
|
||||
Common::Point point;
|
||||
int z;
|
||||
};
|
||||
|
||||
class WalkMgr : public Object {
|
||||
public:
|
||||
WalkMgr();
|
||||
~WalkMgr() override;
|
||||
void deserialize(Archive &archive) override;
|
||||
void toConsole() const override;
|
||||
|
||||
WalkLocation *findLocation(const Common::String &name);
|
||||
void start(WalkLocation *destination);
|
||||
void update();
|
||||
|
||||
double getLengthBetweenLocations(WalkLocation *first, WalkLocation *second);
|
||||
void setCurrentWayPoint(WalkLocation *location);
|
||||
|
||||
void loadState(Archive &archive);
|
||||
void saveState(Archive &archive);
|
||||
|
||||
void skip();
|
||||
|
||||
const Coordinates &getStartCoords() { return _current.coords; }
|
||||
const Coordinates &getEndCoords() { return _next.coords; }
|
||||
|
||||
private:
|
||||
struct WayPoint {
|
||||
Common::String name;
|
||||
Coordinates coords;
|
||||
};
|
||||
|
||||
Coordinates getLocationCoordinates(const Common::String &locationName);
|
||||
void end();
|
||||
void initNextWayPoint(WalkLocation *location);
|
||||
WalkAction *getWalkAction();
|
||||
|
||||
LeadActor *_leadActor;
|
||||
WalkLocation *_destination;
|
||||
Array<WalkLocation *> _locations;
|
||||
WayPoint _current;
|
||||
WayPoint _next;
|
||||
bool _isWalking;
|
||||
};
|
||||
|
||||
} // End of namespace Pink
|
||||
|
||||
#endif
|
||||
157
engines/pink/objects/walk/walk_shortest_path.cpp
Normal file
157
engines/pink/objects/walk/walk_shortest_path.cpp
Normal file
@@ -0,0 +1,157 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "pink/objects/walk/walk_location.h"
|
||||
#include "pink/objects/walk/walk_mgr.h"
|
||||
|
||||
namespace Pink {
|
||||
|
||||
WalkShortestPath::WalkShortestPath(WalkMgr *manager)
|
||||
: _manager(manager)
|
||||
{}
|
||||
|
||||
WalkLocation *WalkShortestPath::next(WalkLocation *start, WalkLocation *destination) {
|
||||
if (start == destination)
|
||||
return nullptr;
|
||||
add(start, 0.0, nullptr);
|
||||
while (build() != destination) {}
|
||||
return getNearestNeighbor(destination);
|
||||
}
|
||||
|
||||
void WalkShortestPath::add(WalkLocation *wl, double val, WalkLocation *nearest) {
|
||||
_locations.push_back(wl);
|
||||
_visited.push_back(wl);
|
||||
_weight.push_back(val);
|
||||
_nearestNeigbor.push_back(nearest);
|
||||
}
|
||||
|
||||
WalkLocation *WalkShortestPath::build() {
|
||||
WalkLocation *nearest = nullptr;
|
||||
WalkLocation *location = nullptr;
|
||||
double len = -1.0;
|
||||
addLocationsToVisit();
|
||||
for (uint i = 0; i < _toVisit.size(); ++i) {
|
||||
double curLen = getLengthToNearestNeigbor(_toVisit[i]);
|
||||
if (curLen < 0) {
|
||||
remove(_toVisit[i]);
|
||||
continue;
|
||||
}
|
||||
curLen += getWeight(_toVisit[i]);
|
||||
if (len < 0.0 || len > curLen) {
|
||||
len = curLen;
|
||||
location = _toVisit[i];
|
||||
nearest = getNearestNeighbor(_toVisit[i]);
|
||||
if (!nearest)
|
||||
nearest = findNearestNeighbor(_toVisit[i]);
|
||||
}
|
||||
}
|
||||
|
||||
WalkLocation *neighbor = findNearestNeighbor(location);
|
||||
if (neighbor)
|
||||
add(neighbor, len, nearest);
|
||||
|
||||
return neighbor;
|
||||
}
|
||||
|
||||
WalkLocation *WalkShortestPath::getNearestNeighbor(WalkLocation *location) {
|
||||
for(uint i = 0; i < _visited.size(); ++i) {
|
||||
if (_visited[i] == location)
|
||||
return _nearestNeigbor[i];
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void WalkShortestPath::addLocationsToVisit() {
|
||||
_toVisit.resize(_locations.size());
|
||||
for (uint i = 0; i < _locations.size(); ++i) {
|
||||
_toVisit[i] = _locations[i];
|
||||
}
|
||||
}
|
||||
|
||||
double WalkShortestPath::getLengthToNearestNeigbor(WalkLocation *location) {
|
||||
double minLength = -1.0;
|
||||
Common::StringArray &neighbors = location->getNeigbors();
|
||||
for (uint i = 0; i < neighbors.size(); ++i) {
|
||||
WalkLocation *neighbor = _manager->findLocation(neighbors[i]);
|
||||
if (!isLocationVisited(neighbor)) {
|
||||
double length = _manager->getLengthBetweenLocations(location, neighbor);
|
||||
if (minLength >= 0.0) {
|
||||
if (length < minLength)
|
||||
minLength = length;
|
||||
} else
|
||||
minLength = length;
|
||||
}
|
||||
}
|
||||
|
||||
return minLength;
|
||||
}
|
||||
|
||||
WalkLocation *WalkShortestPath::findNearestNeighbor(WalkLocation *location) {
|
||||
double minLength = -1.0;
|
||||
WalkLocation *nearest = nullptr;
|
||||
Common::StringArray &neighbors = location->getNeigbors();
|
||||
for (uint i = 0; i < neighbors.size(); ++i) {
|
||||
WalkLocation *neighbor = _manager->findLocation(neighbors[i]);
|
||||
if (!isLocationVisited(neighbor)) {
|
||||
double length = _manager->getLengthBetweenLocations(location, neighbor);
|
||||
if (minLength >= 0.0) {
|
||||
if (length < minLength) {
|
||||
nearest = neighbor;
|
||||
minLength = length;
|
||||
}
|
||||
} else {
|
||||
nearest = neighbor;
|
||||
minLength = length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nearest;
|
||||
}
|
||||
|
||||
double WalkShortestPath::getWeight(WalkLocation *location) {
|
||||
for (uint i = 0; i < _locations.size(); ++i) {
|
||||
if (_locations[i] == location)
|
||||
return _weight[i];
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
bool WalkShortestPath::isLocationVisited(WalkLocation *location) {
|
||||
for (uint i = 0; i < _visited.size(); ++i) {
|
||||
if (_visited[i] == location)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void WalkShortestPath::remove(WalkLocation *location) {
|
||||
for (uint i = 0; i < _locations.size(); ++i) {
|
||||
if (_locations[i] == location) {
|
||||
_locations.remove_at(i);
|
||||
_weight.remove_at(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // End of namespace Pink
|
||||
59
engines/pink/objects/walk/walk_shortest_path.h
Normal file
59
engines/pink/objects/walk/walk_shortest_path.h
Normal file
@@ -0,0 +1,59 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef PINK_WALK_SHORTEST_PATH_H
|
||||
#define PINK_WALK_SHORTEST_PATH_H
|
||||
|
||||
#include "common/array.h"
|
||||
|
||||
namespace Pink {
|
||||
|
||||
class WalkLocation;
|
||||
class WalkMgr;
|
||||
|
||||
class WalkShortestPath {
|
||||
public:
|
||||
WalkShortestPath(WalkMgr *manager);
|
||||
WalkLocation *next(WalkLocation *start, WalkLocation *destination);
|
||||
|
||||
private:
|
||||
void add(WalkLocation *wl, double val, WalkLocation *nearest);
|
||||
void remove(WalkLocation *location);
|
||||
WalkLocation *build();
|
||||
WalkLocation *getNearestNeighbor(WalkLocation *location);
|
||||
WalkLocation *findNearestNeighbor(WalkLocation *location);
|
||||
double getLengthToNearestNeigbor(WalkLocation *location);
|
||||
double getWeight(WalkLocation *location);
|
||||
void addLocationsToVisit();
|
||||
bool isLocationVisited(WalkLocation *location);
|
||||
|
||||
|
||||
WalkMgr *_manager;
|
||||
Common::Array<WalkLocation *> _locations;
|
||||
Common::Array<WalkLocation *> _toVisit;
|
||||
Common::Array<double> _weight;
|
||||
Common::Array<WalkLocation *> _visited;
|
||||
Common::Array<WalkLocation *> _nearestNeigbor;
|
||||
};
|
||||
|
||||
} // End of namespace Pink
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user