summaryrefslogtreecommitdiff
path: root/src/lua_bind.c
diff options
context:
space:
mode:
authorBardur Arantsson <bardur@scientician.net>2012-03-29 21:10:46 +0200
committerBardur Arantsson <bardur@scientician.net>2012-03-29 21:22:23 +0200
commit2d82b906749e7249b98b32a81c66a92e46c1cf10 (patch)
tree770ae89b97f9274b0f1068703c99224987f5b10c /src/lua_bind.c
parentaef0a9a6670ec219c3fc00e9cd9bc30458a5cab5 (diff)
Refactor: Move compass() and approximate_distance() from Lua to C
Diffstat (limited to 'src/lua_bind.c')
-rw-r--r--src/lua_bind.c82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/lua_bind.c b/src/lua_bind.c
index 116147e9..7894b7d4 100644
--- a/src/lua_bind.c
+++ b/src/lua_bind.c
@@ -548,3 +548,85 @@ void desc_god(int g_idx, int d, char *desc)
if (d >= 0 && d < 10)
strncpy(deity_info[g_idx].desc[d], desc, 79);
}
+
+/*
+ * Returns the direction of the compass that y2, x2 is from y, x
+ * the return value will be one of the following: north, south,
+ * east, west, north-east, south-east, south-west, north-west,
+ * or "close" if it is within 2 tiles.
+ */
+cptr compass(int y, int x, int y2, int x2)
+{
+ static char compass_dir[64];
+
+ // is it close to the north/south meridian?
+ int y_diff = y2 - y;
+
+ // determine if y2, x2 is to the north or south of y, x
+ const char *y_axis;
+ if ((y_diff > -3) && (y_diff < 3))
+ {
+ y_axis = 0;
+ }
+ else if (y2 > y)
+ {
+ y_axis = "south";
+ }
+ else
+ {
+ y_axis = "north";
+ }
+
+ // is it close to the east/west meridian?
+ int x_diff = x2 - x;
+
+ // determine if y2, x2 is to the east or west of y, x
+ const char *x_axis;
+ if ((x_diff > -3) && (x_diff < 3))
+ {
+ x_axis = 0;
+ }
+ else if (x2 > x)
+ {
+ x_axis = "east";
+ }
+ else
+ {
+ x_axis = "west";
+ }
+
+ // Maybe it is very close
+ if ((!x_axis) && (!y_axis)) { strcpy(compass_dir, "close"); }
+ // Maybe it is (almost) due N/S
+ else if (!x_axis) { strcpy(compass_dir, y_axis); }
+ // Maybe it is (almost) due E/W
+ else if (!y_axis) { strcpy(compass_dir, x_axis); }
+ // or if it is neither
+ else { sprintf(compass_dir, "%s-%s", y_axis, x_axis); }
+ return compass_dir;
+}
+
+/* Returns a relative approximation of the 'distance' of y2, x2 from y, x. */
+cptr approximate_distance(int y, int x, int y2, int x2)
+{
+ // how far to away to the north/south?
+ int y_diff = abs(y2 - y);
+ // how far to away to the east/west?
+ int x_diff = abs(x2 - x);
+ // find which one is the larger distance
+ int most_dist = x_diff;
+ if (y_diff > most_dist) {
+ most_dist = y_diff;
+ }
+
+ // how far away then?
+ if (most_dist >= 41) {
+ return "a very long way";
+ } else if (most_dist >= 25) {
+ return "a long way";
+ } else if (most_dist >= 8) {
+ return "quite some way";
+ } else {
+ return "not very far";
+ }
+}