From 212734b31102b11f4819c6676270baa1c99ea27b Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 13 Oct 2024 21:51:08 +0200 Subject: use std::list instead of std::vector for storing artists + identify some instability causes --- Canvas.cpp | 9 ++++----- Museum.cpp | 1 + People.cpp | 22 +++++++--------------- People.h | 9 ++++----- Tile.cpp | 2 +- Tile.h | 2 +- ViewController.cpp | 9 ++++----- readme.md | 8 ++++++++ 8 files changed, 30 insertions(+), 32 deletions(-) diff --git a/Canvas.cpp b/Canvas.cpp index 821be0c..4b326c7 100644 --- a/Canvas.cpp +++ b/Canvas.cpp @@ -75,12 +75,11 @@ string Canvas::to_string(bool truecolor) { } void Canvas::update_steps() { - for (size_t i = 0; i < this->museum.people.artists_size(); i++) { - Artist & artist = this->museum.people.get_artist(i); - if (artist.step == false) continue; - artist.step = false; + for (Artist * artist : this->museum.people.get_artists()) { + if (artist->step == false) continue; + artist->step = false; - this->get_tile(artist.data.x, artist.data.y).step(artist); + this->get_tile(artist->data.x, artist->data.y).step(*artist); } } diff --git a/Museum.cpp b/Museum.cpp index 361afaf..c34bb5b 100644 --- a/Museum.cpp +++ b/Museum.cpp @@ -26,6 +26,7 @@ void Museum::work() { auto next = chrono::steady_clock::now() + this->tick_interval; this->update(); + printf("%lu artists\n", this->people.get_artists().size()); this_thread::sleep_until(next); } } diff --git a/People.cpp b/People.cpp index c60e5a1..dbd2a6c 100644 --- a/People.cpp +++ b/People.cpp @@ -1,7 +1,6 @@ #include #include "People.h" -#include "Exception.h" #include "util.h" using namespace std; @@ -16,8 +15,8 @@ People::~People() { this->artists.clear(); } -void People::add_artist(ArtistData data) { - if (this->artists.size() > 30e3) return; +void People::add_artist(const ArtistData & data) { + if (this->artists.size() >= 30e3) return; this->artists.push_back(new Artist(this->museum, data)); } @@ -27,23 +26,16 @@ void People::remove_artist(Artist & target) { this->artists.erase(it); } -size_t People::artists_size() { - return this->artists.size(); -} - -Artist & People::get_artist(size_t index) { - if (index >= this->artists_size()) - throw Exception("No artist with index %lu", index); - return *this->artists[index]; +list People::get_artists() { + return this->artists; } string People::to_string() { string out = ""; - out += stringf("%d artists\n", this->artists_size()); - for (size_t i = 0; i < this->artists_size(); i++) { - Artist & artist = this->get_artist(i); - out += stringf("[%d] at (%.2f,%.2f)\n", i, artist.data.x, artist.data.y); + out += stringf("%d artists\n", this->artists.size()); + for (Artist * artist : this->artists) { + out += stringf("- at (%.2f,%.2f)\n", artist->data.x, artist->data.y); } return out; diff --git a/People.h b/People.h index 4dcae96..1f9f869 100644 --- a/People.h +++ b/People.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include "Artist.h" @@ -15,16 +15,15 @@ public: std::string to_string(); public: - void add_artist(ArtistData data); + void add_artist(const ArtistData & data); void remove_artist(Artist & artist); - Artist & get_artist(size_t index); - size_t artists_size(); + std::list get_artists(); void update(); private: - std::vector artists; + std::list artists; Museum & museum; }; diff --git a/Tile.cpp b/Tile.cpp index 89524d0..1f49d96 100644 --- a/Tile.cpp +++ b/Tile.cpp @@ -9,7 +9,7 @@ Tile::Tile(Museum & museum, TileData data) : museum(museum) { this->set_data(data); } -void Tile::set_data(TileData data) { +void Tile::set_data(TileData & data) { this->data = data; this->color = TileAppearance::get_color(this->data.type); this->behavior = TileBehavior::get_strategy(this->data.type).clone(this->museum); diff --git a/Tile.h b/Tile.h index 3f56313..9575c99 100644 --- a/Tile.h +++ b/Tile.h @@ -21,7 +21,7 @@ public: unsigned int y = 0; public: - void set_data(TileData data); + void set_data(TileData & data); void update(); void step(Artist &); Tile * get_neighbor(int dx, int dy); diff --git a/ViewController.cpp b/ViewController.cpp index ea84bb8..cafccae 100644 --- a/ViewController.cpp +++ b/ViewController.cpp @@ -34,15 +34,14 @@ void ViewController::update_tiles(View & view) { void ViewController::update_artists(View & view) { People & people = this->museum.people; - for (size_t i = 0; i < people.artists_size(); i++) { - Artist & artist = people.get_artist(i); + for (Artist * artist : people.get_artists()) { Rectangle rect = { - .x = static_cast(artist.data.x * scale), - .y = static_cast(artist.data.y * scale), + .x = static_cast(artist->data.x * scale), + .y = static_cast(artist->data.y * scale), .width = artist_size, .height = artist_size, }; - view.draw_rect(rect, artist.color); + view.draw_rect(rect, artist->color); } } diff --git a/readme.md b/readme.md index 6254800..aca57ca 100644 --- a/readme.md +++ b/readme.md @@ -5,3 +5,11 @@ - artist-artist collision behavior - artist steps on tile behavior +stability issues: + +- `BlueTileBehavior::update_neighbor` causes segfault pretty consistently when + >15k artists exist +- The view very rarely reads artists that were just deleted by the museum + worker thread (set to nullptr first (atomic), delete later) + + -- cgit v1.2.3