Skip to content

Commit

Permalink
fix sim
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya246 committed Jun 26, 2022
1 parent 4334b75 commit 78819bc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 36 deletions.
1 change: 0 additions & 1 deletion include/entities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ struct Entity {

struct Triangle: public Entity {
Triangle();
Triangle(bool ghost);

void control(movement& cont) override;
void draw() override;
Expand Down
18 changes: 7 additions & 11 deletions src/entities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Entity::Entity() {
updateGroup.push_back(this);
if (!headless) {
trajectory = std::make_unique<std::vector<Point>>();
ghost = simulating;
}
}

Expand Down Expand Up @@ -150,7 +151,7 @@ void Entity::update() {
lastCollideScan = globalTime;
}
for (Entity* e : near) {
if (dst2(x - e->x, y - e->y) <= (radius + e->radius) * (radius + e->radius)) [[unlikely]] {
if (dst2(x - e->x, y - e->y) <= (radius + e->radius) * (radius + e->radius) && !(ghost && e->ghost)) [[unlikely]] {
collide(e, true);
if (type() == Entities::Attractor) {
if (((Attractor*)this)->star && e->type() == Entities::Triangle) [[unlikely]] {
Expand Down Expand Up @@ -262,7 +263,7 @@ void Entity::simReset() {
Triangle::Triangle() : Entity() {
mass = 5000.0;
radius = 16.0;
if (!headless) {
if (!headless && !simulating) {
shape = std::make_unique<sf::CircleShape>(radius, 3);
shape->setOrigin(radius, radius);
forwards = std::make_unique<sf::CircleShape>(2.f, 6);
Expand All @@ -272,11 +273,6 @@ Triangle::Triangle() : Entity() {
icon->setFillColor(sf::Color(255, 255, 255));
}
}
Triangle::Triangle(bool ghost) : Entity() {
mass = 5000.0;
radius = 16.0;
this->ghost = true;
}

void Triangle::loadCreatePacket(sf::Packet& packet) {
packet << type() << id << x << y << velX << velY << rotation;
Expand Down Expand Up @@ -568,7 +564,7 @@ Projectile::Projectile() : Entity() {
this->color[0] = 180;
this->color[1] = 0;
this->color[2] = 0;
if (!headless) {
if (!headless && !simulating) {
shape = std::make_unique<sf::CircleShape>(radius, 10);
shape->setOrigin(radius, radius);
icon = std::make_unique<sf::CircleShape>(2.f, 4);
Expand Down Expand Up @@ -618,13 +614,13 @@ void Projectile::collide(Entity* with, bool collideOther) {
chatPacket << Packets::Chat << sendMessage;
for (Player* p : playerGroup) {
p->tcpSocket.send(chatPacket);
if (owner) {
owner->kills++;
}
}
setupShip(p->entity);
}
}
if (owner) {
owner->kills++;
}
}
if (headless || simulating) {
entityDeleteBuffer.push_back(this);
Expand Down
31 changes: 7 additions & 24 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,25 +595,26 @@ int main(int argc, char** argv) {
if (!headless && globalTime - lastPredict > predictSpacing && trajectoryRef) [[unlikely]] {
double resdelta = delta;
double resTime = globalTime;
std::vector<Entity*> retUpdateGroup(updateGroup);
delta = predictDelta;
simulating = true;
ghostTrajectories.clear();
ghostTrajectoryColors.clear();
bool controlsActive = *(unsigned char*) &controls != 0;
Triangle* ghost = nullptr;
if (ownEntity && controlsActive) {
ghost = new Triangle(true);
ghost = new Triangle();
ghost->x = ownEntity->x;
ghost->y = ownEntity->y;
ghost->velX = ownEntity->velX;
ghost->velY = ownEntity->velY;
ghostTrajectoryColors.push_back(sf::Color(ownEntity->color[0] * 0.7, ownEntity->color[1] * 0.7, ownEntity->color[2] * 0.7));
std::copy(std::begin(ownEntity->color), std::end(ownEntity->color), std::begin(ghost->color));
simCleanupBuffer.push_back(ghost);
}
for (Entity* e : updateGroup) {
e->simSetup();
e->trajectory->clear();
}
std::vector<Entity*> retUpdateGroup(updateGroup);
for (int i = 0; i < predictSteps; i++) {
predictingFor = predictDelta * predictSteps;
globalTime += predictDelta / 60.0;
Expand Down Expand Up @@ -656,30 +657,12 @@ int main(int argc, char** argv) {
entityDeleteBuffer.clear();
}
for (Entity* en : simCleanupBuffer) {
for (size_t i = 0; i < updateGroup.size(); i++) {
Entity* e = updateGroup[i];
if (e == en) [[unlikely]] {
ghostTrajectories.push_back(*en->trajectory);
ghostTrajectoryColors.push_back(sf::Color(en->color[0], en->color[1], en->color[2]));
updateGroup[i] = updateGroup[updateGroup.size() - 1];
updateGroup.pop_back();
} else {
for (size_t i = 0; i < e->near.size(); i++){
if (e->near[i] == en) [[unlikely]] {
e->near[i] = e->near[e->near.size() - 1];
e->near.pop_back();
break;
}
}
}
}
ghostTrajectories.push_back(*en->trajectory);
ghostTrajectoryColors.push_back(sf::Color(en->color[0] * 0.7, en->color[1] * 0.7, en->color[2] * 0.7));
entityDeleteBuffer.push_back(en);
}
simCleanupBuffer.clear();
updateGroup = retUpdateGroup;
if (ghost) {
ghostTrajectories.push_back(*ghost->trajectory);
delete ghost;
}
for (Entity* e : updateGroup) {
e->simReset();
}
Expand Down

0 comments on commit 78819bc

Please sign in to comment.