From 7d43c4e7ad6b83a23516b26b4aebf74f398c251b Mon Sep 17 00:00:00 2001
From: Paweł Redman <pawel.redman@gmail.com>
Date: Tue, 17 Oct 2017 23:13:04 +0200
Subject: Fix a bug in entity linking.

---
 src/common.hpp |  2 +-
 src/game.cpp   |  2 +-
 src/world.cpp  | 11 +++++++++--
 3 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/common.hpp b/src/common.hpp
index 75dd17d..6798ec8 100644
--- a/src/common.hpp
+++ b/src/common.hpp
@@ -1,5 +1,5 @@
 #include <iostream>
-#include <cstdint>
+#include <cinttypes>
 #include <cmath>
 #include <cassert>
 #include <map>
diff --git a/src/game.cpp b/src/game.cpp
index 2af7736..59b8ad9 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -56,7 +56,7 @@ void state_t::tick(void)
 		}
 
 		if (human.stalin < -4.0f)
-			human.stalin = 2.0f;
+			human.stalin = (float)rand() / RAND_MAX * 3.0f;
 	}
 }
 
diff --git a/src/world.cpp b/src/world.cpp
index 06369f6..c18a7ab 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -192,7 +192,7 @@ void entity_t::link(world_t *world)
 
 	for (int64_t y = 0; y < (int64_t)ysecs; y++)
 	for (int64_t x = 0; x < (int64_t)xsecs; x++) {
-		sector_index_t index = sector_index_at(v2f_t(base[0] + x, base[1] + y));
+		sector_index_t index = base + sector_index_t(x, y);
 		sector_t *sector;
 
 		sector = world->get_sector(index);
@@ -202,8 +202,15 @@ void entity_t::link(world_t *world)
 
 void entity_t::unlink(void)
 {
-	for (sector_t *sector : parents)
+	for (sector_t *sector : parents) {
+		if (sector->ents.find(this) == sector->ents.end()) {
+			printf("entity_t::unlink: %p should belong to %p (%" PRIi64", %" PRIi64") but it doesn't\n",
+			       this, sector, sector->index[0], sector->index[1]);
+			continue;
+		}
+
 		sector->ents.erase(sector->ents.find(this));
+	}
 
 	parents.clear();
 	parent_world = nullptr;
-- 
cgit