From 1a806b6387382abe5857b990d44b0bb4502f5387 Mon Sep 17 00:00:00 2001 From: BuddyFriendGuy Date: Tue, 7 Apr 2015 21:25:41 -0400 Subject: [PATCH] refactor the filter for cleaner code --- qcsrc/common/mapinfo.qc | 42 +++++++++++++---------------------- qcsrc/common/mapinfo.qh | 1 + qcsrc/menu/xonotic/maplist.qc | 11 ++++----- 3 files changed, 20 insertions(+), 34 deletions(-) diff --git a/qcsrc/common/mapinfo.qc b/qcsrc/common/mapinfo.qc index 316847069..b522d8987 100644 --- a/qcsrc/common/mapinfo.qc +++ b/qcsrc/common/mapinfo.qc @@ -187,37 +187,25 @@ float MapInfo_FilterGametype(int pGametype, int pFeatures, int pFlagsRequired, i return 1; } -float MapInfo_FilterGametypeAndString(int pGametype, int pFeatures, int pFlagsRequired, int pFlagsForbidden, bool pAbortOnGenerate, string sf) +float MapInfo_FilterString(string sf) { + // this function further filters _MapInfo_filtered, which is prepared by MapInfo_FilterGametype by string float i, j; string title; - if (!_MapInfo_filtered_allocated) - { - _MapInfo_filtered_allocated = 1; - _MapInfo_filtered = buf_create(); - } - MapInfo_count = 0; - for(i = 0, j = -1; i < _MapInfo_globcount; ++i) + + for(i = 0, j = -1; i < MapInfo_count; ++i) { - if(MapInfo_Get_ByName(_MapInfo_GlobItem(i), 1, 0) == 2) // if we generated one... BAIL OUT and let the caller continue in the next frame. - if(pAbortOnGenerate) - { - dprint("Autogenerated a .mapinfo, doing the rest later.\n"); - MapInfo_progress = i / _MapInfo_globcount; - return 0; - } - // prepare for keyword filter - if (MapInfo_Map_title && strstrofs(MapInfo_Map_title, "", 0) == -1) - title = MapInfo_Map_title; - else - title = MapInfo_Map_bspname; - // keyword filter - if((strstrofs(strtolower(title), strtolower(sf), 0)) >= 0) - if((MapInfo_Map_supportedGametypes & pGametype) != 0) - if((MapInfo_Map_supportedFeatures & pFeatures) == pFeatures) - if((MapInfo_Map_flags & pFlagsForbidden) == 0) - if((MapInfo_Map_flags & pFlagsRequired) == pFlagsRequired) - bufstr_set(_MapInfo_filtered, ++j, ftos(i)); + if (MapInfo_Get_ByID(i)) + { + // prepare for keyword filter + if (MapInfo_Map_title && strstrofs(MapInfo_Map_title, "<TITLE>", 0) == -1) + title = MapInfo_Map_title; + else + title = MapInfo_Map_bspname; + // keyword filter + if((strstrofs(strtolower(title), strtolower(sf), 0)) >= 0) + bufstr_set(_MapInfo_filtered, ++j, bufstr_get(_MapInfo_filtered, i)); + } } MapInfo_count = j + 1; MapInfo_ClearTemps(); diff --git a/qcsrc/common/mapinfo.qh b/qcsrc/common/mapinfo.qh index dc69a5568..1278b16a3 100644 --- a/qcsrc/common/mapinfo.qh +++ b/qcsrc/common/mapinfo.qh @@ -118,6 +118,7 @@ void MapInfo_Enumerate(); // filter the info by game type mask (updates MapInfo_count) float MapInfo_progress; float MapInfo_FilterGametype(float gametype, float features, float pFlagsRequired, float pFlagsForbidden, float pAbortOnGenerate); // 1 on success, 0 on temporary failure (call it again next frame then; use MapInfo_progress as progress indicator) +float MapInfo_FilterString(string sf); // this one runs after MapInfo_FilterGametype to futhur filter the list by a string float MapInfo_FilterGametypeAndString(float gametype, float features, float pFlagsRequired, float pFlagsForbidden, float pAbortOnGenerate, string filterString); // 1 on success, 0 on temporary failure (call it again next frame then; use MapInfo_progress as progress indicator) int MapInfo_CurrentFeatures(); // retrieves currently required features from cvars int MapInfo_CurrentGametype(); // retrieves current gametype from cvars diff --git a/qcsrc/menu/xonotic/maplist.qc b/qcsrc/menu/xonotic/maplist.qc index a525b31d9..27baa35e8 100644 --- a/qcsrc/menu/xonotic/maplist.qc +++ b/qcsrc/menu/xonotic/maplist.qc @@ -219,11 +219,9 @@ void XonoticMapList_refilter(entity me) float gt, f; gt = MapInfo_CurrentGametype(); f = MapInfo_CurrentFeatures(); - // TODO consider consolidating the two functions + MapInfo_FilterGametype(gt, f, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0); if (me.stringFilter) - MapInfo_FilterGametypeAndString(gt, f, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0, me.stringFilter); - else - MapInfo_FilterGametype(gt, f, MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0); + MapInfo_FilterString(me.stringFilter); me.nItems = MapInfo_count; for(i = 0; i < MapInfo_count; ++i) @@ -273,10 +271,9 @@ void MapList_All(entity btn, entity me) { float i; string s; + MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, 0, MapInfo_ForbiddenFlags(), 0); // all if (me.stringFilter) - MapInfo_FilterGametypeAndString(MAPINFO_TYPE_ALL, 0, 0, MapInfo_ForbiddenFlags(), 0, me.stringFilter); - else - MapInfo_FilterGametype(MAPINFO_TYPE_ALL, 0, 0, MapInfo_ForbiddenFlags(), 0); // all + MapInfo_FilterString(me.stringFilter); s = ""; for(i = 0; i < MapInfo_count; ++i) s = strcat(s, " ", MapInfo_BSPName_ByID(i)); -- 2.39.2