summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source
diff options
context:
space:
mode:
authorEnne Walker <ennewalker@users.sourceforge.net>2009-10-31 10:59:52 -0400
committerEnne Walker <ennewalker@users.sourceforge.net>2009-10-31 11:09:28 -0400
commit95746496df9bc09763421a35bb5c3cbc1ff4ad54 (patch)
tree941adb4b2e042d6f63a2c467d96e431464cbc145 /crawl-ref/source
parent3877db329ce13f8d527b2954c3f9d5857a2b6b35 (diff)
downloadcrawl-ref-95746496df9bc09763421a35bb5c3cbc1ff4ad54.tar.gz
crawl-ref-95746496df9bc09763421a35bb5c3cbc1ff4ad54.zip
Adding you.in_branch(string) as a lua function.
This also removes you.where_are_you() so as not to require lua scripts to know the value of C++ enums. This change also fixes stricmp incorrectly falling through to strcmp on non-MSVC platforms.
Diffstat (limited to 'crawl-ref/source')
-rw-r--r--crawl-ref/source/dat/volcano.des6
-rw-r--r--crawl-ref/source/l_you.cc37
-rw-r--r--crawl-ref/source/libgui.cc5
-rw-r--r--crawl-ref/source/libgui.h1
-rw-r--r--crawl-ref/source/libunix.cc7
-rw-r--r--crawl-ref/source/libunix.h1
-rw-r--r--crawl-ref/source/libutil.cc26
-rw-r--r--crawl-ref/source/libutil.h4
8 files changed, 68 insertions, 19 deletions
diff --git a/crawl-ref/source/dat/volcano.des b/crawl-ref/source/dat/volcano.des
index 7258cdb393..fdfd592fe9 100644
--- a/crawl-ref/source/dat/volcano.des
+++ b/crawl-ref/source/dat/volcano.des
@@ -282,7 +282,7 @@ local hellknight = "hell knight w:1 ; hand axe ego:flaming . ring mail ego:fire_
function fiery_humans (e)
-- This is for the village! Wargs and orcs if you came from orc
- if you.where_are_you() == 2 then
+ if you.in_branch("orc") then
local orc = "orc warrior ; ring mail ego:fire_resistance race:orcish | leather armour ego:fire_resistance \
race:orcish"
e.mons("warg / " .. orc)
@@ -299,7 +299,7 @@ function fiery_guardians (e)
local guard_type = ""
local main_guard = ""
-- If we came from orc, we expect to only see orcs.
- if you.where_are_you() == 2 then
+ if you.in_branch("orc") then
-- We did, and maybe we'll place a unique.
if crawl.one_chance_in(9) then
e.mons("orc warrior ; spear race:orcish ego:flaming . leather armour ego:fire_resistance")
@@ -325,7 +325,7 @@ function fiery_guardians (e)
end
-- No unique, orcs!
- if you.where_are_you() == 2 then
+ if you.in_branch("orc") then
e.mons("orc warrior ; spear race:orcish ego:flaming . leather armour ego:fire_resistance")
e.mons("orc warrior ; trident race:orcish ego:flaming . ring mail ego:fire_resistance / \
orc knight ; scythe race:orcish ego:flaming . banded mail ego:fire_resistance")
diff --git a/crawl-ref/source/l_you.cc b/crawl-ref/source/l_you.cc
index aa58e0e5f8..a316cc768d 100644
--- a/crawl-ref/source/l_you.cc
+++ b/crawl-ref/source/l_you.cc
@@ -4,6 +4,7 @@
#include "l_libs.h"
#include "abl-show.h"
+#include "branch.h"
#include "chardump.h"
#include "coord.h"
#include "delay.h"
@@ -275,7 +276,39 @@ static int _you_gold(lua_State *ls)
LUAWRAP(_you_die,ouch(INSTANT_DEATH, NON_MONSTER, KILLED_BY_SOMETHING))
-LUARET1(you_where_are_you, number, you.where_are_you)
+LUAFN(you_in_branch)
+{
+ const char* name = luaL_checkstring(ls, 1);
+
+ int br = NUM_BRANCHES;
+
+ for (int i = 0; i < NUM_BRANCHES; i++)
+ {
+ if (stricmp(name, branches[i].shortname) == 0
+ || stricmp(name, branches[i].longname) == 0
+ || stricmp(name, branches[i].abbrevname) == 0)
+ {
+ if (br != NUM_BRANCHES)
+ {
+ std::string err = make_stringf(
+ "'%s' matches both branch '%s' and '%s'",
+ name, branches[br].abbrevname,
+ branches[i].abbrevname);
+ return (luaL_argerror(ls, 1, err.c_str()));
+ }
+ br = i;
+ }
+ }
+
+ if (br == NUM_BRANCHES)
+ {
+ std::string err = make_stringf("'%s' matches no branches.", name);
+ return (luaL_argerror(ls, 1, err.c_str()));
+ }
+
+ bool in_branch = (br == you.where_are_you);
+ PLUARET(boolean, in_branch);
+}
static const struct luaL_reg you_dlib[] =
{
@@ -291,7 +324,7 @@ static const struct luaL_reg you_dlib[] =
{ "gold", _you_gold },
{ "uniques", _you_uniques },
{ "die", _you_die },
-{ "where_are_you", you_where_are_you },
+{ "in_branch", you_in_branch },
{ NULL, NULL }
};
diff --git a/crawl-ref/source/libgui.cc b/crawl-ref/source/libgui.cc
index 185428e109..721572327c 100644
--- a/crawl-ref/source/libgui.cc
+++ b/crawl-ref/source/libgui.cc
@@ -405,10 +405,5 @@ char *strlwr(char *str)
return (str);
}
-int stricmp(const char *str1, const char *str2)
-{
- return (strcmp(str1, str2));
-}
-
#endif // #ifdef UNIX
#endif // #ifdef USE_TILE
diff --git a/crawl-ref/source/libgui.h b/crawl-ref/source/libgui.h
index 4735061fd6..9932520a83 100644
--- a/crawl-ref/source/libgui.h
+++ b/crawl-ref/source/libgui.h
@@ -67,7 +67,6 @@ int kbhit();
#ifdef UNIX
extern "C" char *strlwr(char *str);
int itoa(int value, char *strptr, int radix);
-int stricmp(const char *str1, const char *str2);
#endif
#endif // USE_TILE
diff --git a/crawl-ref/source/libunix.cc b/crawl-ref/source/libunix.cc
index ebd2340d32..95d5ce0b91 100644
--- a/crawl-ref/source/libunix.cc
+++ b/crawl-ref/source/libunix.cc
@@ -1129,13 +1129,6 @@ int wherey()
return (y + 1);
}
-
-extern "C" int stricmp( const char *str1, const char *str2 )
-{
- return (strcmp(str1, str2));
-}
-
-
void delay( unsigned long time )
{
usleep( time * 1000 );
diff --git a/crawl-ref/source/libunix.h b/crawl-ref/source/libunix.h
index 11d5c8a019..8da80d559c 100644
--- a/crawl-ref/source/libunix.h
+++ b/crawl-ref/source/libunix.h
@@ -40,7 +40,6 @@ int kbhit(void);
int putch(unsigned char chr);
int putwch(unsigned chr);
void put_colour_ch(int colour, unsigned ch);
-extern "C" int stricmp(const char *str1, const char *str2);
int translate_keypad(int keyin);
int wherex(void);
int wherey(void);
diff --git a/crawl-ref/source/libutil.cc b/crawl-ref/source/libutil.cc
index a73bc7276d..5b37cd5fd6 100644
--- a/crawl-ref/source/libutil.cc
+++ b/crawl-ref/source/libutil.cc
@@ -221,6 +221,32 @@ int ends_with(const std::string &s, const char *suffixes[])
return (0);
}
+#ifdef UNIX
+extern "C" int stricmp(const char *str1, const char *str2)
+{
+ int ret = 0;
+
+ // No need to check for *str1. If str1 ends, then tolower(*str1) will be
+ // 0, ret will be -1, and the loop will break.
+ while (!ret && *str2)
+ {
+ unsigned char c1 = tolower(*str1);
+ unsigned char c2 = tolower(*str2);
+
+ ret = c1 - c2;
+ str1++;
+ str2++;
+ }
+
+ if (ret < 0)
+ ret = -1;
+ else if (ret > 0)
+ ret = 1;
+
+ return (ret);
+}
+#endif
+
// Returns true if s contains tag 'tag', and strips out tag from s.
bool strip_tag(std::string &s, const std::string &tag, bool skip_padding)
{
diff --git a/crawl-ref/source/libutil.h b/crawl-ref/source/libutil.h
index ca1b754fce..c76ca9e68f 100644
--- a/crawl-ref/source/libutil.h
+++ b/crawl-ref/source/libutil.h
@@ -35,6 +35,10 @@ std::string upcase_first(std::string);
bool ends_with(const std::string &s, const std::string &suffix);
+#ifdef UNIX
+extern "C" int stricmp(const char *str1, const char *str2);
+#endif
+
// String "tags"
#define TAG_UNFOUND -20404
bool strip_tag(std::string &s, const std::string &tag, bool nopad = false);