From: TimePath Date: Fri, 14 Aug 2015 04:40:42 +0000 (+1000) Subject: Update iterators X-Git-Tag: xonotic-v0.8.2~2072 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=4a1190b6273d8909b799314abb4f7b665b220176;p=xonotic%2Fxonotic-data.pk3dir.git Update iterators --- diff --git a/qcsrc/client/hud.qc b/qcsrc/client/hud.qc index bd687c538..8ea4155ae 100644 --- a/qcsrc/client/hud.qc +++ b/qcsrc/client/hud.qc @@ -1403,12 +1403,9 @@ void HUD_Powerups(void) if(superTime) addPowerupItem("Superweapons", "superweapons", autocvar_hud_progressbar_superweapons_color, superTime, 30); - FOREACH(BUFFS, 0, BUFFS_COUNT, - it.m_itemid & allBuffs, - LAMBDA( - addPowerupItem(it.m_prettyName, strcat("buff_", it.m_name), it.m_color, bound(0, getstatf(STAT_BUFF_TIME) - time, 99), 60); - ) - ); + FOREACH(BUFFS, it.m_itemid & allBuffs, LAMBDA( + addPowerupItem(it.m_prettyName, strcat("buff_", it.m_name), it.m_color, bound(0, getstatf(STAT_BUFF_TIME) - time, 99), 60); + )); if(!powerupItemsCount) return; diff --git a/qcsrc/client/waypointsprites.qc b/qcsrc/client/waypointsprites.qc index de4e84d9e..4a1ba2a1f 100644 --- a/qcsrc/client/waypointsprites.qc +++ b/qcsrc/client/waypointsprites.qc @@ -207,13 +207,10 @@ string spritelookuptext(string s) if (substring(s, 0, 5) == "buff-") { entity buff = BUFF_NULL; - FOREACH(BUFFS, 0, BUFFS_COUNT, - it.m_sprite == s, - LAMBDA({ - buff = it; - break; - }) - ); + FOREACH(BUFFS, it.m_sprite == s, LAMBDA( + buff = it; + break; + )); return buff.m_prettyName; } diff --git a/qcsrc/common/items/all.qc b/qcsrc/common/items/all.qc index f221ab2d3..fc225cdb7 100644 --- a/qcsrc/common/items/all.qc +++ b/qcsrc/common/items/all.qc @@ -6,9 +6,9 @@ void Dump_Items() { - FOREACH(ITEMS, 0, ITEM_COUNT, true, LAMBDA({ + FOREACH(ITEMS, true, LAMBDA( ITEM_HANDLE(Show, it); - })); + )); } #endif diff --git a/qcsrc/common/items/inventory.qh b/qcsrc/common/items/inventory.qh index 25590ea43..8b0d43305 100644 --- a/qcsrc/common/items/inventory.qh +++ b/qcsrc/common/items/inventory.qh @@ -15,12 +15,12 @@ class(Inventory) .int inv_items[MAX_ITEMS]; void Inventory_Read(Inventory data) { const int bits = ReadInt24_t(); - FOREACH(ITEMS, 0, ITEM_COUNT, bits & BIT(i), LAMBDA({ + FOREACH(ITEMS, bits & BIT(i), LAMBDA( .int fld = inv_items[i]; int prev = data.(fld); int next = data.(fld) = ReadByte(); dprintf("%s: %.0f -> %.0f\n", ITEMS[i].m_name, prev, next); - })); + )); } #endif @@ -28,14 +28,14 @@ void Inventory_Read(Inventory data) void Inventory_Write(Inventory data) { int bits = 0; - FOREACH(ITEMS, 0, ITEM_COUNT, true, LAMBDA({ + FOREACH(ITEMS, true, LAMBDA( .int fld = inv_items[i]; bits = BITSET(bits, BIT(i), data.inventory.(fld) != (data.inventory.(fld) = data.(fld))); - })); + )); WriteInt24_t(MSG_ENTITY, bits); - FOREACH(ITEMS, 0, ITEM_COUNT, bits & BIT(i), LAMBDA({ + FOREACH(ITEMS, bits & BIT(i), LAMBDA( WriteByte(MSG_ENTITY, data.inv_items[i]); - })); + )); } #endif diff --git a/qcsrc/common/mapinfo.qc b/qcsrc/common/mapinfo.qc index 33af1b390..0b851d53b 100644 --- a/qcsrc/common/mapinfo.qc +++ b/qcsrc/common/mapinfo.qc @@ -602,19 +602,13 @@ void _MapInfo_Map_ApplyGametype(string s, int pWantedType, int pThisType, int lo string _MapInfo_GetDefaultEx(float t) { - entity e; - for(e = MAPINFO_TYPES_first; e; e = e.enemy) - if(t == e.items) - return e.model2; + FOREACH(MAPINFO_TYPES, it.items == t, LAMBDA(return it.model2)); return ""; } float _MapInfo_GetTeamPlayBool(float t) { - entity e; - for(e = MAPINFO_TYPES_first; e; e = e.enemy) - if(t == e.items) - return e.team; + FOREACH(MAPINFO_TYPES, it.items == t, LAMBDA(return it.team)); return false; } @@ -715,7 +709,6 @@ void _MapInfo_Map_ApplyGametypeEx(string s, int pWantedType, int pThisType) float MapInfo_Type_FromString(string t) { - entity e; if(t == "nexball") { print("MapInfo_Type_FromString (probably ", MapInfo_Map_bspname, "): using deprecated name '", t); @@ -754,38 +747,27 @@ float MapInfo_Type_FromString(string t) } if(t == "all") return MAPINFO_TYPE_ALL; - for(e = MAPINFO_TYPES_first; e; e = e.enemy) - if(t == e.mdl) - return e.items; + FOREACH(MAPINFO_TYPES, it.mdl == t, LAMBDA(return it.items)); return 0; } string MapInfo_Type_Description(float t) { - entity e; - for(e = MAPINFO_TYPES_first; e; e = e.enemy) - if(t == e.items) - return e.gametype_description; + FOREACH(MAPINFO_TYPES, it.items == t, LAMBDA(return it.gametype_description)); return ""; } string MapInfo_Type_ToString(float t) { - entity e; if(t == MAPINFO_TYPE_ALL) return "all"; - for(e = MAPINFO_TYPES_first; e; e = e.enemy) - if(t == e.items) - return e.mdl; + FOREACH(MAPINFO_TYPES, it.items == t, LAMBDA(return it.mdl)); return ""; } string MapInfo_Type_ToText(float t) { - entity e; - for(e = MAPINFO_TYPES_first; e; e = e.enemy) - if(t == e.items) - return e.message; + FOREACH(MAPINFO_TYPES, it.items == t, LAMBDA(return it.message)); /* xgettext:no-c-format */ return _("@!#%'n Tuba Throwing"); } @@ -1289,14 +1271,9 @@ int MapInfo_CurrentFeatures() int MapInfo_CurrentGametype() { - entity e; int prev = cvar("gamecfg"); - for(e = MAPINFO_TYPES_first; e; e = e.enemy) - if(cvar(e.netname)) - if(prev != e.items) - return e.items; - if(prev) - return prev; + FOREACH(MAPINFO_TYPES, cvar(it.netname) && it.items != prev, LAMBDA(return it.items)); + if (prev) return prev; return MAPINFO_TYPE_DEATHMATCH; } @@ -1321,9 +1298,9 @@ float MapInfo_CheckMap(string s) // returns 0 if the map can't be played with th void MapInfo_SwitchGameType(int t) { - for (entity e = MAPINFO_TYPES_first; e; e = e.enemy) { - cvar_set(e.netname, (t == e.items) ? "1" : "0"); - } + FOREACH(MAPINFO_TYPES, true, LAMBDA( + cvar_set(it.netname, (it.items == t) ? "1" : "0") + )); } void MapInfo_LoadMap(string s, float reinit) diff --git a/qcsrc/common/nades.qh b/qcsrc/common/nades.qh index d029da43e..01eb7fff2 100644 --- a/qcsrc/common/nades.qh +++ b/qcsrc/common/nades.qh @@ -95,32 +95,28 @@ REGISTER_NADE(MONSTER) { entity Nade_FromProjectile(float proj) { - for (int i = 0; i < NADES_COUNT; i++) - { - entity nade = NADES[i]; + FOREACH(NADES, true, LAMBDA( for (int j = 0; j < 2; j++) { - if (nade.m_projectile[j] == proj) return nade; + if (it.m_projectile[j] == proj) return it; } - } + )); return NADE_TYPE_NULL; } string Nade_TrailEffect(float proj, float nade_team) { - for (int i = 0; i < NADES_COUNT; i++) - { - entity nade = NADES[i]; + FOREACH(NADES, true, LAMBDA( for (int j = 0; j < 2; j++) { - if (nade.m_projectile[j] == proj) + if (it.m_projectile[j] == proj) { - string trail = nade.m_trail[j]; + string trail = it.m_trail[j]; if (trail) return trail; break; } } - } + )); switch (proj) { case PROJECTILE_NADE: return strcat("nade_", Static_Team_ColorName_Lower(nade_team)); diff --git a/qcsrc/common/util-pre.qh b/qcsrc/common/util-pre.qh index 18b46c8d3..b96e78d51 100644 --- a/qcsrc/common/util-pre.qh +++ b/qcsrc/common/util-pre.qh @@ -41,13 +41,22 @@ [[deprecated("use true")]] [[alias("true")]] const bool TRUE; [[deprecated("use false")]] [[alias("false")]] const bool FALSE; -#define FOREACH(arr, start, end, cond, body) do { \ - for (int i = start; i < end; ++i) { \ - const noref entity it = arr[i]; \ - if (cond) { body } \ - } \ +#define FOREACH_ARRAY(arr, start, end, cond, body) do { \ + for (int i = start; i < end; ++i) { \ + const noref entity it = arr[i]; \ + if (cond) { body } \ + } \ } while(0) +#define FOREACH_LIST(list, next, cond, body) do { \ + noref int i = 0; \ + for (entity it = list##_first; it; (it = it.next, ++i)) { \ + if (cond) { body } \ + } \ +} while(0) + +#define FOREACH(list, cond, body) FOREACH_LIST(list, enemy, cond, body) + #ifdef GMQCC #define OVERLOAD(F, ...) F##_##__VA_COUNT__(__VA_ARGS__) #else diff --git a/qcsrc/server/mutators/mutator_buffs.qc b/qcsrc/server/mutators/mutator_buffs.qc index 877bca9f6..170f59874 100644 --- a/qcsrc/server/mutators/mutator_buffs.qc +++ b/qcsrc/server/mutators/mutator_buffs.qc @@ -10,12 +10,7 @@ entity buff_FirstFromFlags(int _buffs) { if (flags) { - FOREACH(BUFFS, 0, BUFFS_COUNT, - it.m_itemid & _buffs, - LAMBDA({ - return it; - }) - ); + FOREACH(BUFFS, it.m_itemid & _buffs, LAMBDA(return it)); } return BUFF_NULL; } @@ -207,14 +202,11 @@ float buff_Available(entity buff) void buff_NewType(entity ent, float cb) { RandomSelection_Init(); - FOREACH(BUFFS, 0, BUFFS_COUNT, - buff_Available(it), - LAMBDA({ - it.buff_seencount += 1; - // if it's already been chosen, give it a lower priority - RandomSelection_Add(world, it.m_itemid, string_null, 1, max(0.2, 1 / it.buff_seencount)); - }) - ); + FOREACH(BUFFS, buff_Available(it), LAMBDA( + it.buff_seencount += 1; + // if it's already been chosen, give it a lower priority + RandomSelection_Add(world, it.m_itemid, string_null, 1, max(0.2, 1 / it.buff_seencount)); + )); ent.buffs = RandomSelection_chosen_float; }