#include #include #include using namespace std; struct place_contents { float x = 1000000, y = 1000000, z = 1000000; char contents[16] = ""; }; #define SIZE 100 // Have to say "struct place_conents" in C place_contents entity_table[SIZE]; size_t coord_hash(float x, float y, float z){ size_t hash_value = 13 * round(x) + 23 * round(y) + 29 * round(z); return hash_value % SIZE; } bool equal(place_contents *loc, float x, float y, float z){ return round(x) == loc->x && round(y) == loc->y && round(z) == loc->z; } const char* get_contents(float x, float y, float z){ place_contents *found_thing = &entity_table[coord_hash(x, y, z)]; while(!equal(found_thing, x, y, z) && found_thing->contents[0] != 0) { found_thing++; if(found_thing >= entity_table + SIZE) found_thing = entity_table; } if(found_thing->contents[0]) return found_thing->contents; else return "Not found"; } void set_contents(float x, float y, float z, const char* new_contents){ place_contents *found_thing = &entity_table[coord_hash(x, y, z)]; while(!equal(found_thing, x, y, z) && found_thing->contents[0] != 0) { found_thing++; // What if we hit the end of the table? if(found_thing >= entity_table + SIZE) found_thing = entity_table; } strncpy(found_thing->contents, new_contents, 16); found_thing->x = round(x); found_thing->y = round(y); found_thing->z = round(z); } int main(){ set_contents(2.3, 4.1, 3.2, "A Goblin"); set_contents(-2.0, 4.1, 3.2, "A Ogre"); set_contents(2.3, 4.1, 5.2, "A good NPC"); set_contents(2.3, 4.1, 10.1, "A bad NPC"); cout << get_contents(2.2, 4.0, 5.1) << endl; cout << get_contents(2.2, 4.0, 10.1) << endl; cout << get_contents(1.0, 4.0, 5.1) << endl; cout << get_contents(2.1, 3.8, 2.9) << endl; }