2009-04-19 18:07:22 +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 System.Collections.Generic ;
using CodeImp.DoomBuilder.Geometry ;
using CodeImp.DoomBuilder.Rendering ;
using CodeImp.DoomBuilder.Config ;
using CodeImp.DoomBuilder.IO ;
using CodeImp.DoomBuilder.VisualModes ;
2013-07-29 08:50:50 +00:00
using CodeImp.DoomBuilder.GZBuilder.Data ;
2009-04-19 18:07:22 +00:00
#endregion
namespace CodeImp.DoomBuilder.Map
{
public sealed class Thing : SelectableElement
{
#region = = = = = = = = = = = = = = = = = = Constants
public const int NUM_ARGS = 5 ;
#endregion
#region = = = = = = = = = = = = = = = = = = Variables
// Map
private MapSet map ;
// Sector
2014-02-21 14:42:12 +00:00
private Sector sector ;
2009-04-19 18:07:22 +00:00
// List items
private LinkedListNode < Thing > selecteditem ;
// Properties
private int type ;
private Vector3D pos ;
2010-10-05 08:31:27 +00:00
private int angledoom ; // Angle as entered / stored in file
private float anglerad ; // Angle in radians
2009-04-19 18:07:22 +00:00
private Dictionary < string , bool > flags ;
private int tag ;
private int action ;
private int [ ] args ;
2013-07-29 08:50:50 +00:00
private float scale ; //mxd. Used in model rendering
private bool isModel ; //mxd
2009-04-19 18:07:22 +00:00
// Configuration
private float size ;
private PixelColor color ;
private bool fixedsize ;
2013-11-27 12:45:28 +00:00
private bool directional ; //mxd. If true, we need to render an arrow
2009-04-19 18:07:22 +00:00
#endregion
#region = = = = = = = = = = = = = = = = = = Properties
public MapSet Map { get { return map ; } }
2013-07-29 08:50:50 +00:00
public int Type { get { return type ; } set { BeforePropsChange ( ) ; type = value ; UpdateModelStatus ( ) ; } } //mxd
2009-04-19 18:07:22 +00:00
public Vector3D Position { get { return pos ; } }
2013-06-14 11:40:18 +00:00
public float Scale { get { return scale ; } } //mxd
2010-10-05 08:31:27 +00:00
public float Angle { get { return anglerad ; } }
public int AngleDoom { get { return angledoom ; } }
2009-06-11 21:21:20 +00:00
internal Dictionary < string , bool > Flags { get { return flags ; } }
public int Action { get { return action ; } set { BeforePropsChange ( ) ; action = value ; } }
2009-04-19 18:07:22 +00:00
public int [ ] Args { get { return args ; } }
public float Size { get { return size ; } }
public PixelColor Color { get { return color ; } }
public bool FixedSize { get { return fixedsize ; } }
2009-06-11 21:21:20 +00:00
public int Tag { get { return tag ; } set { BeforePropsChange ( ) ; tag = value ; if ( ( tag < General . Map . FormatInterface . MinTag ) | | ( tag > General . Map . FormatInterface . MaxTag ) ) throw new ArgumentOutOfRangeException ( "Tag" , "Invalid tag number" ) ; } }
2009-04-19 18:07:22 +00:00
public Sector Sector { get { return sector ; } }
2013-07-29 08:50:50 +00:00
public bool IsModel { get { return isModel ; } } //mxd
2013-11-27 12:45:28 +00:00
public bool IsDirectional { get { return directional ; } } //mxd
2009-04-19 18:07:22 +00:00
2013-09-11 09:47:53 +00:00
#endregion
2009-04-19 18:07:22 +00:00
#region = = = = = = = = = = = = = = = = = = Constructor / Disposer
// Constructor
2009-06-05 19:03:56 +00:00
internal Thing ( MapSet map , int listindex )
2009-04-19 18:07:22 +00:00
{
// Initialize
this . map = map ;
2009-06-05 19:03:56 +00:00
this . listindex = listindex ;
2009-04-19 18:07:22 +00:00
this . flags = new Dictionary < string , bool > ( ) ;
this . args = new int [ NUM_ARGS ] ;
2013-06-14 11:40:18 +00:00
this . scale = 1.0f ; //mxd
2009-04-19 18:07:22 +00:00
2009-06-11 21:21:20 +00:00
if ( map = = General . Map . Map )
General . Map . UndoRedo . RecAddThing ( this ) ;
2009-04-19 18:07:22 +00:00
// We have no destructor
GC . SuppressFinalize ( this ) ;
}
// Disposer
public override void Dispose ( )
{
// Not already disposed?
if ( ! isdisposed )
{
// Already set isdisposed so that changes can be prohibited
isdisposed = true ;
2009-06-11 21:21:20 +00:00
if ( map = = General . Map . Map )
General . Map . UndoRedo . RecRemThing ( this ) ;
2009-04-19 18:07:22 +00:00
// Remove from main list
2009-06-05 19:03:56 +00:00
map . RemoveThing ( listindex ) ;
2009-04-19 18:07:22 +00:00
// Remove from sector
//if(sector != null) sector.DetachThing(sectorlistitem);
// Clean up
map = null ;
sector = null ;
// Dispose base
base . Dispose ( ) ;
}
}
#endregion
#region = = = = = = = = = = = = = = = = = = Management
2009-06-11 21:21:20 +00:00
// Call this before changing properties
protected override void BeforePropsChange ( )
{
if ( map = = General . Map . Map )
General . Map . UndoRedo . RecPrpThing ( this ) ;
}
2009-04-19 18:07:22 +00:00
// Serialize / deserialize
2013-03-18 13:52:27 +00:00
new internal void ReadWrite ( IReadWriteStream s )
2009-04-19 18:07:22 +00:00
{
2009-06-11 21:21:20 +00:00
if ( ! s . IsWriting ) BeforePropsChange ( ) ;
2009-04-19 18:07:22 +00:00
base . ReadWrite ( s ) ;
if ( s . IsWriting )
{
s . wInt ( flags . Count ) ;
foreach ( KeyValuePair < string , bool > f in flags )
{
s . wString ( f . Key ) ;
s . wBool ( f . Value ) ;
}
}
else
{
int c ; s . rInt ( out c ) ;
flags = new Dictionary < string , bool > ( c ) ;
for ( int i = 0 ; i < c ; i + + )
{
string t ; s . rString ( out t ) ;
bool b ; s . rBool ( out b ) ;
flags . Add ( t , b ) ;
}
}
2010-10-05 08:31:27 +00:00
2009-04-19 18:07:22 +00:00
s . rwInt ( ref type ) ;
s . rwVector3D ( ref pos ) ;
2010-10-05 08:31:27 +00:00
s . rwInt ( ref angledoom ) ;
2009-04-19 18:07:22 +00:00
s . rwInt ( ref tag ) ;
s . rwInt ( ref action ) ;
for ( int i = 0 ; i < NUM_ARGS ; i + + ) s . rwInt ( ref args [ i ] ) ;
2013-07-29 08:50:50 +00:00
if ( ! s . IsWriting ) {
2010-10-05 08:31:27 +00:00
anglerad = Angle2D . DoomToReal ( angledoom ) ;
2013-07-29 08:50:50 +00:00
UpdateModelStatus ( ) ; //mxd
}
2009-04-19 18:07:22 +00:00
}
// This copies all properties to another thing
public void CopyPropertiesTo ( Thing t )
{
2009-06-11 21:21:20 +00:00
t . BeforePropsChange ( ) ;
2009-04-19 18:07:22 +00:00
// Copy properties
t . type = type ;
2013-07-29 08:50:50 +00:00
t . UpdateModelStatus ( ) ;
2010-10-05 08:31:27 +00:00
t . anglerad = anglerad ;
t . angledoom = angledoom ;
2009-04-19 18:07:22 +00:00
t . pos = pos ;
t . flags = new Dictionary < string , bool > ( flags ) ;
t . tag = tag ;
t . action = action ;
t . args = ( int [ ] ) args . Clone ( ) ;
t . size = size ;
t . color = color ;
2013-11-27 12:45:28 +00:00
t . directional = directional ;
2009-04-19 18:07:22 +00:00
t . fixedsize = fixedsize ;
2012-04-17 19:13:47 +00:00
2009-04-19 18:07:22 +00:00
base . CopyPropertiesTo ( t ) ;
}
// This determines which sector the thing is in and links it
public void DetermineSector ( )
{
2012-06-07 01:06:37 +00:00
//mxd
2013-09-11 09:47:53 +00:00
sector = map . GetSectorByCoordinates ( pos ) ;
2009-04-19 18:07:22 +00:00
}
// This determines which sector the thing is in and links it
public void DetermineSector ( VisualBlockMap blockmap )
{
// Find nearest sectors using the blockmap
List < Sector > possiblesectors = blockmap . GetBlock ( blockmap . GetBlockCoordinates ( pos ) ) . Sectors ;
// Check in which sector we are
sector = null ;
foreach ( Sector s in possiblesectors )
{
if ( s . Intersect ( pos ) )
{
sector = s ;
break ;
}
}
}
2013-07-29 08:50:50 +00:00
//mxd. This checks if the thing has model override
internal void UpdateModelStatus ( ) {
if ( General . Map . Data = = null ) {
isModel = false ;
return ;
}
isModel = General . Map . Data . ModeldefEntries . ContainsKey ( type ) ;
if ( ! isModel ) return ;
if ( General . Map . Data . ModeldefEntries [ type ] . LoadState = = ModelLoadState . None )
2013-07-30 09:25:27 +00:00
isModel = General . Map . Data . ProcessModel ( type ) ;
2013-07-29 08:50:50 +00:00
}
2009-04-19 18:07:22 +00:00
// This translates the flags into UDMF fields
internal void TranslateToUDMF ( )
{
// First make a single integer with all flags
int bits = 0 ;
2014-02-21 14:42:12 +00:00
int flagbit ;
2009-04-19 18:07:22 +00:00
foreach ( KeyValuePair < string , bool > f in flags )
if ( int . TryParse ( f . Key , out flagbit ) & & f . Value ) bits | = flagbit ;
// Now make the new flags
flags . Clear ( ) ;
foreach ( FlagTranslation f in General . Map . Config . ThingFlagsTranslation )
{
// Flag found in bits?
if ( ( bits & f . Flag ) = = f . Flag )
{
// Add fields and remove bits
bits & = ~ f . Flag ;
for ( int i = 0 ; i < f . Fields . Count ; i + + )
flags [ f . Fields [ i ] ] = f . FieldValues [ i ] ;
}
else
{
// Add fields with inverted value
for ( int i = 0 ; i < f . Fields . Count ; i + + )
flags [ f . Fields [ i ] ] = ! f . FieldValues [ i ] ;
}
}
}
// This translates UDMF fields back into the normal flags
internal void TranslateFromUDMF ( )
{
// Make copy of the flags
Dictionary < string , bool > oldfields = new Dictionary < string , bool > ( flags ) ;
// Make the flags
flags . Clear ( ) ;
foreach ( KeyValuePair < string , string > f in General . Map . Config . ThingFlags )
{
// Flag must be numeric
2014-02-21 14:42:12 +00:00
int flagbit ;
2009-04-19 18:07:22 +00:00
if ( int . TryParse ( f . Key , out flagbit ) )
{
foreach ( FlagTranslation ft in General . Map . Config . ThingFlagsTranslation )
{
if ( ft . Flag = = flagbit )
{
// Only set this flag when the fields match
bool fieldsmatch = true ;
for ( int i = 0 ; i < ft . Fields . Count ; i + + )
{
if ( ! oldfields . ContainsKey ( ft . Fields [ i ] ) | | ( oldfields [ ft . Fields [ i ] ] ! = ft . FieldValues [ i ] ) )
{
fieldsmatch = false ;
break ;
}
}
// Field match? Then add the flag.
if ( fieldsmatch )
{
flags . Add ( f . Key , true ) ;
break ;
}
}
}
}
}
}
// Selected
protected override void DoSelect ( )
{
base . DoSelect ( ) ;
selecteditem = map . SelectedThings . AddLast ( this ) ;
}
// Deselect
protected override void DoUnselect ( )
{
base . DoUnselect ( ) ;
if ( selecteditem . List ! = null ) selecteditem . List . Remove ( selecteditem ) ;
selecteditem = null ;
}
#endregion
#region = = = = = = = = = = = = = = = = = = Changes
// This moves the thing
// NOTE: This does not update sector! (call DetermineSector)
public void Move ( Vector3D newpos )
{
2009-06-11 21:21:20 +00:00
BeforePropsChange ( ) ;
2009-04-19 18:07:22 +00:00
// Change position
this . pos = newpos ;
2010-09-09 15:12:31 +00:00
if ( type ! = General . Map . Config . Start3DModeThingType )
General . Map . IsChanged = true ;
2009-04-19 18:07:22 +00:00
}
// This moves the thing
// NOTE: This does not update sector! (call DetermineSector)
public void Move ( Vector2D newpos )
{
2009-06-11 21:21:20 +00:00
BeforePropsChange ( ) ;
2009-04-19 18:07:22 +00:00
// Change position
this . pos = new Vector3D ( newpos . x , newpos . y , pos . z ) ;
2010-09-09 15:12:31 +00:00
if ( type ! = General . Map . Config . Start3DModeThingType )
General . Map . IsChanged = true ;
2009-04-19 18:07:22 +00:00
}
// This moves the thing
// NOTE: This does not update sector! (call DetermineSector)
public void Move ( float x , float y , float zoffset )
{
2009-06-11 21:21:20 +00:00
BeforePropsChange ( ) ;
2009-04-19 18:07:22 +00:00
// Change position
this . pos = new Vector3D ( x , y , zoffset ) ;
2010-09-09 15:12:31 +00:00
if ( type ! = General . Map . Config . Start3DModeThingType )
General . Map . IsChanged = true ;
2009-04-19 18:07:22 +00:00
}
// This rotates the thing
public void Rotate ( float newangle )
{
2009-06-11 21:21:20 +00:00
BeforePropsChange ( ) ;
2009-04-19 18:07:22 +00:00
// Change angle
2010-10-05 08:31:27 +00:00
this . anglerad = newangle ;
this . angledoom = Angle2D . RealToDoom ( newangle ) ;
if ( type ! = General . Map . Config . Start3DModeThingType )
General . Map . IsChanged = true ;
}
// This rotates the thing
public void Rotate ( int newangle )
{
BeforePropsChange ( ) ;
// Change angle
this . anglerad = Angle2D . DoomToReal ( newangle ) ;
this . angledoom = newangle ;
2010-09-09 15:12:31 +00:00
if ( type ! = General . Map . Config . Start3DModeThingType )
General . Map . IsChanged = true ;
2009-04-19 18:07:22 +00:00
}
// This updates all properties
// NOTE: This does not update sector! (call DetermineSector)
2010-10-05 08:31:27 +00:00
public void Update ( int type , float x , float y , float zoffset , int angle ,
2009-04-19 18:07:22 +00:00
Dictionary < string , bool > flags , int tag , int action , int [ ] args )
{
// Apply changes
this . type = type ;
2010-10-05 08:31:27 +00:00
this . anglerad = Angle2D . DoomToReal ( angle ) ;
this . angledoom = angle ;
2009-04-19 18:07:22 +00:00
this . flags = new Dictionary < string , bool > ( flags ) ;
this . tag = tag ;
this . action = action ;
this . args = new int [ NUM_ARGS ] ;
args . CopyTo ( this . args , 0 ) ;
this . Move ( x , y , zoffset ) ;
2013-07-29 08:50:50 +00:00
UpdateModelStatus ( ) ; //mxd
2009-04-19 18:07:22 +00:00
}
// This updates the settings from configuration
public void UpdateConfiguration ( )
{
ThingTypeInfo ti ;
// Lookup settings
ti = General . Map . Data . GetThingInfo ( type ) ;
2010-10-05 08:31:27 +00:00
2009-04-19 18:07:22 +00:00
// Apply size
size = ti . Radius ;
fixedsize = ti . FixedSize ;
2013-06-14 11:40:18 +00:00
scale = ti . SpriteScale . Width ; //mxd
2009-04-19 18:07:22 +00:00
// Color valid?
if ( ( ti . Color > = 0 ) & & ( ti . Color < ColorCollection . NUM_THING_COLORS ) )
{
// Apply color
color = General . Colors . Colors [ ti . Color + ColorCollection . THING_COLORS_OFFSET ] ;
}
else
{
// Unknown thing color
color = General . Colors . Colors [ ColorCollection . THING_COLORS_OFFSET ] ;
}
// Apply icon offset (arrow or dot)
2013-11-27 12:45:28 +00:00
//if(ti.Arrow) iconoffset = 0f; else iconoffset = 0.25f;
directional = ti . Arrow ; //mxd
2009-04-19 18:07:22 +00:00
}
#endregion
#region = = = = = = = = = = = = = = = = = = Methods
2009-06-11 21:21:20 +00:00
// This checks and returns a flag without creating it
public bool IsFlagSet ( string flagname )
{
if ( flags . ContainsKey ( flagname ) )
return flags [ flagname ] ;
else
return false ;
}
// This sets a flag
public void SetFlag ( string flagname , bool value )
{
if ( ! flags . ContainsKey ( flagname ) | | ( IsFlagSet ( flagname ) ! = value ) )
{
BeforePropsChange ( ) ;
2009-04-19 18:07:22 +00:00
2009-06-11 21:21:20 +00:00
flags [ flagname ] = value ;
}
}
// This returns a copy of the flags dictionary
public Dictionary < string , bool > GetFlags ( )
{
return new Dictionary < string , bool > ( flags ) ;
}
// This clears all flags
public void ClearFlags ( )
{
BeforePropsChange ( ) ;
flags . Clear ( ) ;
}
2009-04-19 18:07:22 +00:00
// This snaps the vertex to the grid
public void SnapToGrid ( )
{
// Calculate nearest grid coordinates
2014-02-21 14:42:12 +00:00
this . Move ( General . Map . Grid . SnappedToGrid ( pos ) ) ;
2009-04-19 18:07:22 +00:00
}
// This snaps the vertex to the map format accuracy
public void SnapToAccuracy ( )
{
// Round the coordinates
Vector3D newpos = new Vector3D ( ( float ) Math . Round ( pos . x , General . Map . FormatInterface . VertexDecimals ) ,
( float ) Math . Round ( pos . y , General . Map . FormatInterface . VertexDecimals ) ,
( float ) Math . Round ( pos . z , General . Map . FormatInterface . VertexDecimals ) ) ;
this . Move ( newpos ) ;
}
// This returns the distance from given coordinates
public float DistanceToSq ( Vector2D p )
{
return Vector2D . DistanceSq ( p , pos ) ;
}
// This returns the distance from given coordinates
public float DistanceTo ( Vector2D p )
{
return Vector2D . Distance ( p , pos ) ;
}
#endregion
}
}