1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2024-11-20 03:54:47 +00:00

Replace IF_KEY_EXISTS with a plain old function, not a macro.

This commit is contained in:
Arthur O'Dwyer 2021-07-18 17:02:52 -04:00
parent 8c6aba5f6f
commit 74052ef2bf
3 changed files with 15 additions and 13 deletions

View File

@ -45,7 +45,7 @@ EX bool hide_player() {
;
}
#define ADC(V,c) IF_KEY_EXISTS(it, current_display->all_drawn_copies, c) for(const shiftmatrix& V: it->second)
#define ADC(V,c) if (auto *it = hr::find_or_null(current_display->all_drawn_copies, c)) for(const shiftmatrix& V: it->second)
EX hookset<bool(int sym, int uni)> hooks_handleKey;
EX hookset<bool(cell *c, const shiftmatrix& V)> hooks_drawcell;

12
hyper.h
View File

@ -575,11 +575,6 @@ typedef function<int(struct cell*)> cellfunction;
#define forCellCM(ct, cf) forCellIdCM(ct,forCellCM ## __LINE__,cf)
#define forCellAll(ct, cf) forCellIdCM(ct,forCellAll ## __LINE__,cf)
/* conditions */
/** `IF_KEY_EXISTS(it, map, key) statement` checks whether the map 'map' contain key 'key', and if so, executes statement with it set to the relevant iterator */
#define IF_KEY_EXISTS(it, map, key) for(auto it: {map.find(key)}) if(it != map.end())
// canAttack/moveval flags
#define AF_NORMAL 0 // nothing special about this attack
@ -851,6 +846,13 @@ template<class T> array<T, 4> make_array(T a, T b, T c, T d) { array<T,4> x; x[0
template<class T> array<T, 3> make_array(T a, T b, T c) { array<T,3> x; x[0] = a; x[1] = b; x[2] = c; return x; }
template<class T> array<T, 2> make_array(T a, T b) { array<T,2> x; x[0] = a; x[1] = b; return x; }
// Find in a std::map or std::unordered_map, or return null.
template<class Map, class Key>
const typename Map::value_type *find_or_null(const Map& map, const Key& key) {
auto it = map.find(key);
return (it == map.end()) ? nullptr : &*it;
}
namespace daily {
extern bool on;
extern int daily_id;

View File

@ -207,7 +207,7 @@ cld exp_parser::parse(int prio) {
cld c = rparse(0);
force_eat(")");
IF_KEY_EXISTS(it, extra_params, "angleunit") {
if (auto *it = hr::find_or_null(extra_params, "angleunit")) {
a *= it->second;
b *= it->second;
c *= it->second;
@ -237,13 +237,13 @@ cld exp_parser::parse(int prio) {
test.compute_sum();
test.compute_geometry();
res = test.edgelength;
IF_KEY_EXISTS(it, extra_params, "distunit")
if (auto *it = hr::find_or_null(extra_params, "distunit"))
res /= it->second;
}
#endif
else if(eat("regangle(")) {
cld edgelen = parse(0);
IF_KEY_EXISTS(it, extra_params, "distunit") {
if (auto *it = hr::find_or_null(extra_params, "distunit")) {
edgelen = edgelen * it->second;
}
@ -260,13 +260,13 @@ cld exp_parser::parse(int prio) {
if(arb::legacy) {
res = M_PI - result;
IF_KEY_EXISTS(it, extra_params, "angleofs")
if (auto *it = hr::find_or_null(extra_params, "angleofs"))
res -= it->second;
}
else
res = result;
IF_KEY_EXISTS(it, extra_params, "angleunit")
if (auto *it = hr::find_or_null(extra_params, "angleunit"))
res /= it->second;
}
else if(eat("test(")) {
@ -318,8 +318,8 @@ cld exp_parser::parse(int prio) {
else if(next() == '(') at++, res = parsepar();
else {
string number = next_token();
IF_KEY_EXISTS(it, extra_params, number) res = it->second;
else IF_KEY_EXISTS(it, params, number) res = it->second->get_cld();
if (auto *it = hr::find_or_null(extra_params, number)) res = it->second;
else if (auto *it = hr::find_or_null(params, number)) res = it->second->get_cld();
else if(number == "e") res = exp(1);
else if(number == "i") res = cld(0, 1);
else if(number == "p" || number == "pi") res = M_PI;