diff options
-rw-r--r-- | lib/core/util.lua | 86 | ||||
-rw-r--r-- | lib/mods/theme/core/util.lua | 86 | ||||
-rw-r--r-- | src/externs.h | 5 | ||||
-rw-r--r-- | src/lua_bind.c | 82 | ||||
-rw-r--r-- | src/util.pkg | 6 |
5 files changed, 93 insertions, 172 deletions
diff --git a/lib/core/util.lua b/lib/core/util.lua index 5128dfbe..eea13014 100644 --- a/lib/core/util.lua +++ b/lib/core/util.lua @@ -71,92 +71,6 @@ function msg_print(c, m) end end --- 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. -function compass(y, x, y2, x2) - local y_axis, x_axis, y_diff, x_diff, compass_dir - - -- is it close to the north/south meridian? - y_diff = y2 - y - - -- determine if y2, x2 is to the north or south of y, x - if (y_diff > -3) and (y_diff < 3) then - y_axis = nil - elseif y2 > y then - y_axis = "south" - else - y_axis = "north" - end - - -- is it close to the east/west meridian? - x_diff = x2 - x - - -- determine if y2, x2 is to the east or west of y, x - if (x_diff > -3) and (x_diff < 3) then - x_axis = nil - elseif x2 > x then - x_axis = "east" - else - x_axis = "west" - end - - -- Maybe it is very close - if ((not x_axis) and (not y_axis)) then compass_dir = "close" - -- Maybe it is (almost) due N/S - elseif not x_axis then compass_dir = y_axis - -- Maybe it is (almost) due E/W - elseif not y_axis then compass_dir = x_axis - -- or if it is neither - else compass_dir = y_axis.."-"..x_axis - end - - return compass_dir -end - --- Returns a relative approximation of the 'distance' of y2, x2 from y, x. -function approximate_distance(y, x, y2, x2) - local y_diff, x_diff, most_dist - - -- how far to away to the north/south - y_diff = y2 - y - - -- make sure it's a positive integer - if y_diff < 0 then - y_diff = 0 - y_diff - end - - -- how far to away to the east/west - x_diff = x2 - x - - -- make sure it's a positive integer - if x_diff < 0 then - x_diff = 0 - x_diff - end - - -- find which one is the larger distance - if x_diff > y_diff then - most_dist = x_diff - else - most_dist = y_diff - end - - -- how far away then? - if most_dist >= 41 then - how_far = "a very long way" - elseif most_dist >= 25 then - how_far = "a long way" - elseif most_dist >= 8 then - how_far = "quite some way" - else - how_far = "not very far" - end - - return how_far - -end - -- better timer add function __timers_callback_max = 0 diff --git a/lib/mods/theme/core/util.lua b/lib/mods/theme/core/util.lua index 5128dfbe..eea13014 100644 --- a/lib/mods/theme/core/util.lua +++ b/lib/mods/theme/core/util.lua @@ -71,92 +71,6 @@ function msg_print(c, m) end end --- 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. -function compass(y, x, y2, x2) - local y_axis, x_axis, y_diff, x_diff, compass_dir - - -- is it close to the north/south meridian? - y_diff = y2 - y - - -- determine if y2, x2 is to the north or south of y, x - if (y_diff > -3) and (y_diff < 3) then - y_axis = nil - elseif y2 > y then - y_axis = "south" - else - y_axis = "north" - end - - -- is it close to the east/west meridian? - x_diff = x2 - x - - -- determine if y2, x2 is to the east or west of y, x - if (x_diff > -3) and (x_diff < 3) then - x_axis = nil - elseif x2 > x then - x_axis = "east" - else - x_axis = "west" - end - - -- Maybe it is very close - if ((not x_axis) and (not y_axis)) then compass_dir = "close" - -- Maybe it is (almost) due N/S - elseif not x_axis then compass_dir = y_axis - -- Maybe it is (almost) due E/W - elseif not y_axis then compass_dir = x_axis - -- or if it is neither - else compass_dir = y_axis.."-"..x_axis - end - - return compass_dir -end - --- Returns a relative approximation of the 'distance' of y2, x2 from y, x. -function approximate_distance(y, x, y2, x2) - local y_diff, x_diff, most_dist - - -- how far to away to the north/south - y_diff = y2 - y - - -- make sure it's a positive integer - if y_diff < 0 then - y_diff = 0 - y_diff - end - - -- how far to away to the east/west - x_diff = x2 - x - - -- make sure it's a positive integer - if x_diff < 0 then - x_diff = 0 - x_diff - end - - -- find which one is the larger distance - if x_diff > y_diff then - most_dist = x_diff - else - most_dist = y_diff - end - - -- how far away then? - if most_dist >= 41 then - how_far = "a very long way" - elseif most_dist >= 25 then - how_far = "a long way" - elseif most_dist >= 8 then - how_far = "quite some way" - else - how_far = "not very far" - end - - return how_far - -end - -- better timer add function __timers_callback_max = 0 diff --git a/src/externs.h b/src/externs.h index f5bb610c..dc01f361 100644 --- a/src/externs.h +++ b/src/externs.h @@ -1824,6 +1824,11 @@ extern void lua_delete_list(list_type *, int size); extern void lua_add_to_list(list_type *, int idx, cptr str); extern void lua_display_list(int y, int x, int h, int w, cptr title, list_type *list, int max, int begin, int sel, byte sel_color); +extern cptr compass(int y, int x, int y2, int x2); +extern cptr approximate_distance(int y, int x, int y2, int x2); + +extern cptr compass(int y, int x, int y2, int x2); +extern cptr approximate_distance(int y, int x, int y2, int x2); /* skills.c */ extern void dump_skills(FILE *fff); 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"; + } +} diff --git a/src/util.pkg b/src/util.pkg index b118f2b0..c6f97ebf 100644 --- a/src/util.pkg +++ b/src/util.pkg @@ -1514,6 +1514,12 @@ extern void quit(cptr str); */ extern s32b value_scale(int value, int vmax, int max, int min); +/* + * compass, approximate_distance + */ +extern cptr compass(int y, int x, int y2, int x2); +extern cptr approximate_distance(int y, int x, int y2, int x2); + /** @fn text_out_c(byte a, cptr str) * @brief Output text to the screen (in color) or to a file depending on the * selected hook.\n |