mirror of
https://github.com/devZoGok/PlanetFleet.git
synced 2026-08-26 19:43:29 +00:00
Units move using the new pathfinding system
Minor refactoring
This commit is contained in:
@@ -1,15 +1,26 @@
|
||||
numUnits = 11
|
||||
|
||||
UnitType = {VESSEL = 0, DESTROYER = 1, CRUISER = 2, AIRCRAFT_CARRIER = 3, SUBMARINE = 4, MISSILE_JET = 5, DEMO_JET = 6};
|
||||
UnitClass = {VESSEL = 0, DESTROYER = 1, CRUISER = 2, AIRCRAFT_CARRIER = 3, SUBMARINE = 4, MISSILE_JET = 5, DEMO_JET = 6};
|
||||
|
||||
UnitType = {UNDERWATER = 0, SEA_LEVEL = 1, LAND = 2, AIR = 3};
|
||||
|
||||
unitClass = {
|
||||
UnitClass.VESSEL, UnitClass.VESSEL,
|
||||
UnitClass.DESTROYER, UnitClass.DESTROYER,
|
||||
UnitClass.CRUISER, UnitClass.CRUISER,
|
||||
UnitClass.AIRCRAFT_CARRIER, UnitClass.AIRCRAFT_CARRIER,
|
||||
UnitClass.SUBMARINE,
|
||||
UnitClass.MISSILE_JET, UnitClass.DEMO_JET
|
||||
};
|
||||
|
||||
unitType = {
|
||||
UnitType.VESSEL, UnitType.VESSEL,
|
||||
UnitType.DESTROYER, UnitType.DESTROYER,
|
||||
UnitType.CRUISER, UnitType.CRUISER,
|
||||
UnitType.AIRCRAFT_CARRIER, UnitType.AIRCRAFT_CARRIER,
|
||||
UnitType.SUBMARINE,
|
||||
UnitType.MISSILE_JET, UnitType.DEMO_JET
|
||||
};
|
||||
UnitType.SEA_LEVEL, UnitType.SEA_LEVEL,
|
||||
UnitType.SEA_LEVEL, UnitType.SEA_LEVEL,
|
||||
UnitType.SEA_LEVEL, UnitType.SEA_LEVEL,
|
||||
UnitType.SEA_LEVEL, UnitType.SEA_LEVEL,
|
||||
UnitType.UNDERWATER,
|
||||
UnitType.AIR, UnitType.AIR
|
||||
}
|
||||
|
||||
health = {500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500}
|
||||
speed = {.025, .025, .05, .05, .1, .1, .1, .1, .1, .1, .15}
|
||||
@@ -17,7 +28,7 @@ destinationOffset = {.75, .75, 1, 1, .1, .1, .1, .1, .1, .5, .5}
|
||||
anglePrecision = {.1, .1, .1, .1, .1, .1, .1, .1, .1, 3, 3}
|
||||
cost = {500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500}
|
||||
maxTurnAngle = {1, 1, .5, .5, 1, 1, .5, .5, 1, 2, 2}
|
||||
range = {15, 15, 14, 14, 13, 13, 12, 12,15,25}
|
||||
range = {15, 15, 14, 14, 13, 13, 12, 12, 15, 25}
|
||||
|
||||
unitCornerPoints = {
|
||||
{
|
||||
|
||||
+1
-1
@@ -300,7 +300,7 @@ namespace battleship{
|
||||
case Bind::TOGGLE_SUB:
|
||||
if (isPressed) {
|
||||
for (Unit *u : selectedUnits) {
|
||||
if (u->getPlayer() == mainPlayer && u->getType() == UnitType::SUBMARINE) {
|
||||
if (u->getPlayer() == mainPlayer && u->getUnitClass() == UnitClass::SUBMARINE) {
|
||||
Submarine *s = (Submarine*) u;
|
||||
|
||||
if (s->isSubmerged())
|
||||
|
||||
+1
-1
@@ -36,7 +36,7 @@ namespace battleship{
|
||||
|
||||
for(Player *p : inGameState->getPlayers())
|
||||
for(Unit *u : p->getUnits())
|
||||
if(u->getType() == UnitType::SUBMARINE && order.targets[0].pos == u->getPos())
|
||||
if(u->getUnitClass() == UnitClass::SUBMARINE && order.targets[0].pos == u->getPos())
|
||||
sub = true;
|
||||
|
||||
if(sub)
|
||||
|
||||
+34
-35
@@ -241,75 +241,74 @@ namespace battleship{
|
||||
if (p) {
|
||||
p->update();
|
||||
|
||||
for (int i=0;i<p->getNumberOfUnits();i++){
|
||||
Unit *u=p->getUnit(i);
|
||||
for (int i = 0; i < p->getNumberOfUnits(); i++){
|
||||
Unit *u = p->getUnit(i);
|
||||
|
||||
if(u->isWorking())
|
||||
u->update();
|
||||
else{
|
||||
int selectedId=-1;
|
||||
std::vector<Unit*> &units=p->getUnits(),&selectedUnits=activeState->getSelectedUnits();
|
||||
units.erase(units.begin()+i);
|
||||
int selectedId = -1;
|
||||
std::vector<Unit*> &units = p->getUnits(), &selectedUnits = activeState->getSelectedUnits();
|
||||
units.erase(units.begin() + i);
|
||||
|
||||
if(u->getType() == UnitType::MISSILE_JET || u->getType() == UnitType::DEMO_JET){
|
||||
AircraftCarrier *carrier=nullptr;
|
||||
if(u->getUnitClass() == UnitClass::MISSILE_JET || u->getUnitClass() == UnitClass::DEMO_JET){
|
||||
AircraftCarrier *carrier = nullptr;
|
||||
|
||||
for(int i2=0;i2<p->getUnits().size()&&!carrier;i2++){
|
||||
AircraftCarrier *a=((Jet*)u)->getAircraftCarrier();
|
||||
for(int i2 = 0; i2 < p->getUnits().size() && !carrier; i2++){
|
||||
AircraftCarrier *a = ((Jet*)u)->getAircraftCarrier();
|
||||
|
||||
if(a)
|
||||
carrier=a;
|
||||
carrier = a;
|
||||
}
|
||||
|
||||
if(carrier){
|
||||
Jet** jets=carrier->getJets();
|
||||
jets[((Jet*)u)->getJetId()]=nullptr;
|
||||
Jet** jets = carrier->getJets();
|
||||
jets[((Jet*)u)->getJetId()] = nullptr;
|
||||
}
|
||||
}
|
||||
else if(u->getType() == UnitType::AIRCRAFT_CARRIER){
|
||||
AircraftCarrier *carrier=(AircraftCarrier*)u;
|
||||
else if(u->getUnitClass() == UnitClass::AIRCRAFT_CARRIER){
|
||||
AircraftCarrier *carrier = (AircraftCarrier*)u;
|
||||
|
||||
for(int i2=0;i2<carrier->getMaxNumJets();i2++){
|
||||
Jet *j=carrier->getJet(i2);
|
||||
for(int i2 = 0; i2 < carrier->getMaxNumJets(); i2++){
|
||||
Jet *j = carrier->getJet(i2);
|
||||
|
||||
if(j&&j->isOnBoard())
|
||||
if(j && j->isOnBoard())
|
||||
j->blowUp();
|
||||
}
|
||||
}
|
||||
|
||||
for(int i2=0;i2<10;i2++){
|
||||
int id=-1;
|
||||
std::vector<Unit*> &group=activeState->getUnitGroup(i2);
|
||||
for(int i2 = 0; i2 < 10; i2++){
|
||||
int id = -1;
|
||||
std::vector<Unit*> &group = activeState->getUnitGroup(i2);
|
||||
|
||||
for(int i3=0;i3<group.size()&&id==-1;i3++)
|
||||
if(group[i3]==u)
|
||||
id=i3;
|
||||
for(int i3 = 0; i3 < group.size() && id == -1; i3++)
|
||||
if(group[i3] == u)
|
||||
id = i3;
|
||||
|
||||
if(id!=-1)
|
||||
group.erase(group.begin()+id);
|
||||
if(id != -1)
|
||||
group.erase(group.begin() + id);
|
||||
}
|
||||
|
||||
for(int i2=0;i2<selectedUnits.size()&&selectedId==-1;i2++)
|
||||
if(selectedUnits[i2]==u)
|
||||
selectedId=i2;
|
||||
for(int i2 = 0; i2 < selectedUnits.size() && selectedId == -1; i2++)
|
||||
if(selectedUnits[i2] == u)
|
||||
selectedId = i2;
|
||||
|
||||
if(selectedId!=-1)
|
||||
selectedUnits.erase(selectedUnits.begin()+selectedId);
|
||||
if(selectedId != -1)
|
||||
selectedUnits.erase(selectedUnits.begin() + selectedId);
|
||||
|
||||
delete u;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(int i=0;i<projectiles.size();i++){
|
||||
Projectile *p=projectiles[i];
|
||||
for(int i = 0; i < projectiles.size(); i++){
|
||||
Projectile *p = projectiles[i];
|
||||
|
||||
if(!p->isExploded()){
|
||||
if(!p->isExploded())
|
||||
p->update();
|
||||
}
|
||||
else{
|
||||
delete p;
|
||||
projectiles.erase(projectiles.begin()+i);
|
||||
projectiles.erase(projectiles.begin() + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <luaManager.h>
|
||||
|
||||
#include "map.h"
|
||||
#include "pathfinder.h"
|
||||
#include "gameManager.h"
|
||||
#include "defConfigs.h"
|
||||
|
||||
@@ -114,6 +115,8 @@ namespace battleship{
|
||||
LuaManager *luaManager = LuaManager::getSingleton();
|
||||
luaManager->buildScript(vector<string>{GameManager::getSingleton()->getPath() + "Models/Maps/" + mapName + "/" + mapName + ".lua"});
|
||||
|
||||
Pathfinder::getSingleton()->setImpassibleNodeVal(u16(0 - 1));
|
||||
|
||||
nodeParent = new Node();
|
||||
Root *root = Root::getSingleton();
|
||||
root->getRootNode()->attachChild(nodeParent);
|
||||
@@ -131,7 +134,6 @@ namespace battleship{
|
||||
|
||||
Vector3 Map::getCellPos(int id, Vector3 cellSize){
|
||||
int numCellsX = size.x / cellSize.x;
|
||||
int numCellsY = size.y / cellSize.y;
|
||||
int numCellsZ = size.z / cellSize.z;
|
||||
|
||||
Vector3 initPos = Vector3(size.x, 0, size.z) * -0.5;
|
||||
@@ -144,12 +146,11 @@ namespace battleship{
|
||||
|
||||
int Map::getCellId(Vector3 pos, Vector3 cellSize){
|
||||
int numCellsX = size.x / cellSize.x;
|
||||
int numCellsY = size.y / cellSize.y;
|
||||
int numCellsZ = size.z / cellSize.z;
|
||||
|
||||
Vector3 initPos = Vector3(size.x, 0, size.z) * -0.5;
|
||||
int x = fabs(pos.x - initPos.x) / cellSize.x;
|
||||
int y = fabs(pos.y - initPos.y) / cellSize.y;
|
||||
int y = (cellSize.y > 0 ? (fabs(pos.y - initPos.y) / cellSize.y) : 0);
|
||||
int z = fabs(pos.z - initPos.z) / cellSize.z;
|
||||
|
||||
return (numCellsX * numCellsZ * y + (numCellsX * z + x));
|
||||
|
||||
@@ -43,7 +43,7 @@ namespace battleship{
|
||||
std::string mapName;
|
||||
std::vector<WaterBody> waterBodies;
|
||||
//TODO replace hardcoded values
|
||||
vb01::Vector3 size = vb01::Vector3(20, 6, 20);
|
||||
vb01::Vector3 size = vb01::Vector3(100, 6, 100);
|
||||
|
||||
Map(){}
|
||||
void loadSkybox(gameBase::LuaManager*);
|
||||
|
||||
+1
-1
@@ -24,7 +24,7 @@ namespace battleship{
|
||||
|
||||
for(Player *p : inGameState->getPlayers())
|
||||
for(Unit *u : p->getUnits()){
|
||||
bool jet = (u->getType() == UnitType::MISSILE_JET || u->getType() == UnitType::DEMO_JET);
|
||||
bool jet = (u->getUnitClass() == UnitClass::MISSILE_JET || u->getUnitClass() == UnitClass::DEMO_JET);
|
||||
|
||||
if((jet && type == AAM) && (!jet && type == AWM) && u->getPos() == order.targets[0].pos)
|
||||
targetPtr = u->getPos();
|
||||
|
||||
+10
-2
@@ -1,4 +1,5 @@
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
#include "pathfinder.h"
|
||||
#include "map.h"
|
||||
@@ -24,8 +25,15 @@ namespace battleship{
|
||||
vector<int> paths[size];
|
||||
paths[source].push_back(source);
|
||||
|
||||
for(int i = 0; i < size; i++)
|
||||
distances[i] = (i == source ? 0 : u16(0 - 1));
|
||||
for(int i = 0; i < size; i++){
|
||||
distances[i] = (i == source ? 0 : impassibleNodeVal);
|
||||
|
||||
for(int j = 0; j < size; j++)
|
||||
if(weights[i][j] > impassibleNodeVal){
|
||||
cout << "Node val higher than allowed max value\n";
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
vector<int> checkedNodes;
|
||||
|
||||
|
||||
@@ -10,8 +10,12 @@ namespace battleship{
|
||||
public:
|
||||
static Pathfinder* getSingleton();
|
||||
std::vector<int> findPath(vb01::u32**, int, int, int);
|
||||
inline vb01::u32 getImpassibleNodeVal(){return impassibleNodeVal;}
|
||||
inline void setImpassibleNodeVal(vb01::u32 val){this->impassibleNodeVal = val;}
|
||||
private:
|
||||
Pathfinder();
|
||||
|
||||
vb01::u32 impassibleNodeVal;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -11,7 +11,8 @@ namespace battleship{
|
||||
|
||||
void PathfinderTest::testFindPath(){
|
||||
int size = 7;
|
||||
const u32 INF = u16(0 - 1);
|
||||
const u16 INF = u16(0 - 1);
|
||||
//pathfinder->setImpassibleNodeVal(INF);
|
||||
u32 w[size][size] = {
|
||||
{0, 2, 4, INF, INF, INF, INF},
|
||||
{INF, 0, 1, 9, 13, INF, INF},
|
||||
|
||||
@@ -28,6 +28,7 @@ namespace battleship{
|
||||
range = udm->getRange()[id];
|
||||
lineOfSight = udm->getLineOfSight()[id];
|
||||
speed = udm->getSpeed()[id];
|
||||
unitClass = udm->getUnitClass()[id];
|
||||
type = udm->getUnitType()[id];
|
||||
width = udm->getUnitCornerPoints()[id][0].x - udm->getUnitCornerPoints()[id][1].x;
|
||||
height = udm->getUnitCornerPoints()[id][4].y - udm->getUnitCornerPoints()[id][0].y;
|
||||
@@ -277,24 +278,61 @@ namespace battleship{
|
||||
}
|
||||
|
||||
void Unit::generateWeights(u32 **weights, int size){
|
||||
Map *map = Map::getSingleton();
|
||||
int waterbodyId = -1;
|
||||
|
||||
if(type == UnitType::UNDERWATER || type == UnitType::SEA_LEVEL)
|
||||
for(int i = 0; i < map->getNumWaterBodies(); i++){
|
||||
WaterBody waterBody = map->getWaterBody(i);
|
||||
Vector3 p = waterBody.pos;
|
||||
Vector2 s = waterBody.size;
|
||||
|
||||
if((waterBody.rect && fabs(pos.x - p.x) < s.x && fabs(pos.z - p.z) < s.y) ||
|
||||
(!waterBody.rect && Vector2(pos.x, pos.z).getDistanceFrom(Vector2(p.x, p.z)) < s.x)){
|
||||
waterbodyId = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
u32 impassibleNodeVal = Pathfinder::getSingleton()->getImpassibleNodeVal();
|
||||
|
||||
for(int i = 0; i < size; i++){
|
||||
for(int j = 0; j < size; j++){
|
||||
int weight = (abs(i - j) == 1 ? 1 : impassibleNodeVal);
|
||||
weights[i][j] = weight;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Unit::addOrder(Order order){
|
||||
float eps = getCircleRadius();
|
||||
Map *map = Map::getSingleton();
|
||||
|
||||
Vector3 mapSize = map->getSize();
|
||||
Vector3 cellSize = Vector3(int(mapSize.x / eps), 0, int(mapSize.z / eps));
|
||||
int numCells = cellSize.x * (cellSize.y > 0 ? cellSize.y : 1) * cellSize.z;
|
||||
Vector3 cellSize = Vector3(eps, (type == UnitType::UNDERWATER ? int(mapSize.y / height) : 0), eps);
|
||||
|
||||
int numCells =
|
||||
int(mapSize.x / cellSize.x) *
|
||||
(cellSize.y == 0 ? 1 : int(mapSize.y / cellSize.y)) *
|
||||
int(mapSize.z / cellSize.z);
|
||||
u32 **weights = new u32*[numCells];
|
||||
|
||||
for(int i = 0; i < numCells; i++)
|
||||
weights[i] = new u32[numCells];
|
||||
|
||||
generateWeights(weights, numCells);
|
||||
|
||||
Pathfinder *pathfinder = Pathfinder::getSingleton();
|
||||
vector<int> path = pathfinder->findPath(weights,
|
||||
numCells,
|
||||
map->getCellId(pos, cellSize),
|
||||
map->getCellId(order.targets[0].pos, cellSize));
|
||||
|
||||
for(int i = 0; i < numCells; i++)
|
||||
delete[] weights[i];
|
||||
|
||||
delete[] weights;
|
||||
|
||||
pathPoints.clear();
|
||||
|
||||
for(int p : path)
|
||||
|
||||
@@ -77,6 +77,7 @@ namespace battleship{
|
||||
inline vb01::Model* getNode() {return model;}
|
||||
inline Player* getPlayer(){return player;}
|
||||
inline UnitType getType() {return type;}
|
||||
inline UnitClass getUnitClass() {return unitClass;}
|
||||
inline void toggleDebugging(bool d){this->debugging=d;}
|
||||
inline void takeDamage(int damage) {health -= damage;}
|
||||
inline int getId() {return id;}
|
||||
@@ -101,6 +102,7 @@ namespace battleship{
|
||||
protected:
|
||||
Player *player;
|
||||
MoveDir moveDir = MoveDir::FORWARD;
|
||||
UnitClass unitClass;
|
||||
UnitType type;
|
||||
vb01::Vector2 screenPos;
|
||||
std::vector<Order> orders;
|
||||
|
||||
+2
-1
@@ -31,6 +31,7 @@ namespace battleship{
|
||||
speed = new float[numUnits];
|
||||
destinationOffset = new float[numUnits];
|
||||
unitType = new UnitType[numUnits];
|
||||
unitClass = new UnitClass[numUnits];
|
||||
anglePrecision = new float[numUnits];
|
||||
cost = new int[numUnits];
|
||||
maxTurnAngle = new float[numUnits];
|
||||
@@ -46,7 +47,7 @@ namespace battleship{
|
||||
for(int i = 0; i < numUnits; i++){
|
||||
health[i] = luaManager->getIntFromTable("health", vector<Index>{Index(to_string(i + 1), false)});
|
||||
speed[i] = luaManager->getFloatFromTable("speed", vector<Index>{Index(to_string(i + 1), false)});
|
||||
unitType[i] = (UnitType)luaManager->getIntFromTable("unitType", vector<Index>{Index(to_string(i + 1), false)});
|
||||
unitClass[i] = (UnitClass)luaManager->getIntFromTable("unitClass", vector<Index>{Index(to_string(i + 1), false)});
|
||||
destinationOffset[i] = luaManager->getFloatFromTable("destinationOffset", vector<Index>{Index(to_string(i + 1), false)});
|
||||
anglePrecision[i] = luaManager->getFloatFromTable("anglePrecision", vector<Index>{Index(to_string(i + 1), false)});
|
||||
cost[i] = luaManager->getIntFromTable("cost", vector<Index>{Index(to_string(i + 1), false)});
|
||||
|
||||
+5
-1
@@ -6,7 +6,9 @@
|
||||
#include <vector.h>
|
||||
|
||||
namespace battleship{
|
||||
enum class UnitType {VESSEL, DESTROYER, CRUISER, AIRCRAFT_CARRIER, SUBMARINE, MISSILE_JET, DEMO_JET};
|
||||
enum class UnitClass {VESSEL, DESTROYER, CRUISER, AIRCRAFT_CARRIER, SUBMARINE, MISSILE_JET, DEMO_JET};
|
||||
|
||||
enum class UnitType {UNDERWATER, SEA_LEVEL, LAND, AIR};
|
||||
|
||||
class UnitDataManager{
|
||||
public:
|
||||
@@ -14,6 +16,7 @@ namespace battleship{
|
||||
inline int getNumUnits(){return numUnits;}
|
||||
inline int* getHealth(){return health;}
|
||||
inline UnitType* getUnitType(){return unitType;}
|
||||
inline UnitClass* getUnitClass(){return unitClass;}
|
||||
inline float* getMaxTurnAngle(){return maxTurnAngle;}
|
||||
inline float* getRange(){return range;}
|
||||
inline float* getLineOfSight(){return lineOfSight;}
|
||||
@@ -29,6 +32,7 @@ namespace battleship{
|
||||
|
||||
int numUnits;
|
||||
UnitType *unitType;
|
||||
UnitClass *unitClass;
|
||||
int *health;
|
||||
float *speed;
|
||||
float *destinationOffset;
|
||||
|
||||
Reference in New Issue
Block a user