2012-08-27 11:39:14 +00:00
#region = = = = = = = = = = = = = = = = = = Copyright ( c ) 2007 Pascal vd Heiden
/ *
* Copyright ( c ) 2007 Pascal vd Heiden , www . codeimp . com
* This program is released under GNU General Public License
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* /
#endregion
#region = = = = = = = = = = = = = = = = = = Namespaces
using System ;
using CodeImp.DoomBuilder.IO ;
using CodeImp.DoomBuilder.Map ;
#endregion
namespace CodeImp.DoomBuilder.Config
{
public class ThingFlagsCompare
{
public enum CompareMethod
{
Equal ,
And
} ;
#region = = = = = = = = = = = = = = = = = = Constants
#endregion
#region = = = = = = = = = = = = = = = = = = Variables
2014-09-29 20:49:41 +00:00
private readonly string flag ;
private readonly CompareMethod comparemethod ;
private readonly bool invert ;
private readonly string group ;
2012-08-27 11:39:14 +00:00
#endregion
#region = = = = = = = = = = = = = = = = = = Properties
public string Flag { get { return flag ; } }
public string Group { get { return group ; } }
#endregion
#region = = = = = = = = = = = = = = = = = = Constructor / Disposer
// Constructor
public ThingFlagsCompare ( Configuration cfg , string group , string flag )
{
string cfgpath = "thingflagscompare." + group + "." + flag ;
this . flag = flag ;
this . group = group ;
string cm = cfg . ReadSetting ( cfgpath + ".comparemethod" , "and" ) ;
switch ( cm )
{
default :
General . ErrorLogger . Add ( ErrorType . Warning , "Unrecognized value \"" + cm + "\" for comparemethod in " + cfgpath + " in game configuration " + cfg . ReadSetting ( "game" , "<unnamed game>" ) + ". Defaulting to \"and\"." ) ;
goto case "and" ;
case "and" :
comparemethod = CompareMethod . And ;
break ;
case "equal" :
comparemethod = CompareMethod . Equal ;
break ;
}
invert = cfg . ReadSetting ( cfgpath + ".invert" , false ) ;
// We have no destructor
GC . SuppressFinalize ( this ) ;
}
#endregion
#region = = = = = = = = = = = = = = = = = = Methods
// Compares the flag of the two things.
// Returns:
// -1 if the flag does not overlap
// 0 if the flag should be ignored
// 1 if the flag overlaps
public int Compare ( Thing t1 , Thing t2 )
{
2014-02-21 14:42:12 +00:00
bool t1flag ;
bool t2flag ;
2012-08-27 11:39:14 +00:00
// Check if the flags exist
2012-11-09 17:40:47 +00:00
if ( ! t1 . Flags . ContainsKey ( flag ) | | ! t2 . Flags . ContainsKey ( flag ) ) {
//mxd. If a map is in UDMF format - check Fields
if ( ! General . Map . UDMF | | ! t1 . Fields . ContainsKey ( flag ) | | ! t2 . Fields . ContainsKey ( flag ) )
return 0 ;
// tag flag inversion into account
t1flag = invert ? ! ( bool ) t1 . Fields [ flag ] . Value : ( bool ) t1 . Fields [ flag ] . Value ;
t2flag = invert ? ! ( bool ) t2 . Fields [ flag ] . Value : ( bool ) t2 . Fields [ flag ] . Value ;
} else {
// tag flag inversion into account
t1flag = invert ? ! t1 . Flags [ flag ] : t1 . Flags [ flag ] ;
t2flag = invert ? ! t2 . Flags [ flag ] : t2 . Flags [ flag ] ;
}
2012-08-27 11:39:14 +00:00
2014-09-29 20:49:41 +00:00
if ( comparemethod = = CompareMethod . And & & ( t1flag & & t2flag ) ) return 1 ;
if ( comparemethod = = CompareMethod . Equal & & ( t1flag = = t2flag ) ) return 1 ;
2012-08-27 11:39:14 +00:00
return 0 ;
}
#endregion
}
}