
#include "ligh.h"

#ifndef PandaWave_RESOURCES
#include "RevengeResources.h"
#endif

#include "RevengeApp.h"
#include <XMessages.h>
#include "tagpick.h"
#include <assert.h>
#include <XPopup.h>
#include <XWindow.h>


// ==============================  BYTE SWAPPING DATA  ========================================
static byte_swap_code lightning_byte_swapping_data[]=
{
	_begin_bs_array, 1,

//	unsigned long flags;
	_4byte,

//	file_tag collection_reference_tag;
	_4byte,

//	long shock_duration; // in ticks
//	long fade_duration;
	_4byte,
	_4byte,

//	world_distance bolt_length;
	_4byte,

// short sequence_indexes[MAXIMUM_NUMBER_OF_LIGHTNING_DEFINITION_SEQUENCES];
	
	_begin_bs_array, MAXIMUM_NUMBER_OF_LIGHTNING_DEFINITION_SEQUENCES,
		
		_2byte,
		
	_end_bs_array,
	
// 	short_world_distance velocity;
//	short_fixed scale;
	_2byte,
	_2byte,	

// 	short fork_segment_minimum;
//	short fork_segment_delta;
	_2byte,
	_2byte,	

//	short fork_overlap_amount;
//	short main_bolt_overlap_amount;
	_2byte,
	_2byte,	

//	angle angular_limit;
	_2byte,	

// struct damage_definition damage;

	// short type;
	// word flags;
	_2byte,
	_2byte,
	
	// short damage_lower_bound, damage_delta;
	_2byte, _2byte,

	// short_world_distance radius_lower_bound, radius_delta;
	// short_world_distance rate_of_expansion; // world units per tick
	_2byte, _2byte,
	_2byte,

	// short_fixed damage_to_velocity;
	_2byte,

	//	short apparent_number_of_bolts;
	_2byte,

	//------- postprocessed stuff
//	short collection_reference_index;
//	short collection_index;
//	short color_table_index;
	3*sizeof(short),
	
	_end_bs_array
};

lightning_definition default_ligh =
{
	0,
	-1,
	0,
	0,
	0,
	0, 0, 0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	// damage
	0,
	0,
	0, 0,
	0, 0,
	0,
	0,
	// end damage
	0,
	0,
	0,
	0
};

// ==============================  EDIT WINDOW  ========================================
void edit_ligh( tag_entry* e )
{
	TlighEditForm* form = new TlighEditForm( e );
	form->AttachWindow( 1007 );
}

void defaultnew_ligh( tag_entry *e )
{
	lightning_definition def;
	byte_swap_move_be( "ligh", &def, &default_ligh, sizeof(lightning_definition), 1, lightning_byte_swapping_data );
	set_entry_data( e, &def, sizeof(def), false );
}

void dependency_ligh( tag_entry *e )
{
	lightning_definition def;
	read_lightning_definition( &def, e );
	add_tag_dependency( 'txst', def.collection_reference_tag );
}

// ==============================  READ TAG DATA  ========================================
bool read_lightning_definition( lightning_definition* def, tag_entry* e )
{
	return load_tag_data_into_struct( "ligh", e, def, SIZEOF_STRUCT_LIGHTNING_DEFINITION, 1, lightning_byte_swapping_data);
}


// ==============================  SAVE TAG DATA  ========================================
bool save_lightning_definition( lightning_definition* def, tag_entry* e )
{
	return save_tag_data_from_struct( "ligh", e, def, SIZEOF_STRUCT_LIGHTNING_DEFINITION, 1, lightning_byte_swapping_data);
}


// ==============================  READ TAG DATA (WITH ID) ========================================
bool read_lightning_definition( lightning_definition* def, tag id )
{
	tag_entry* e;
	
	assert( def );
	
	e = get_entry( 'ligh', id );
	if( 0 == e ) {
		return FALSE;
	}
	
	return read_lightning_definition( def, e );	
}


// ==============================  SAVE TAG DATA (WITH ID) ========================================
bool save_lightning_definition( lightning_definition* def, tag id )
{
	tag_entry* e;
	
	assert( def );
	
	e = get_entry( 'ligh', id );
	if( 0 == e ) {
		return FALSE;
	}

	return save_lightning_definition( def, e );
}


// ==============================  DISPATCH  ========================================
BEGINDISPATCH(TlighEditForm,TTagEditForm)
	DISPATCHTABLE(KXGControlClick,TlighEditForm::DoControlClick)
ENDDISPATCH


TlighEditForm::TlighEditForm( struct tag_entry *e ) : TTagEditForm( e )
{
}

// ==============================  INITIALIZE FORM =======================================
void TlighEditForm::DoFormInitialize()
{
	TTagEditForm::DoFormInitialize();

}


// ==============================  INPUT HANDLING  ========================================
long TlighEditForm::DoControlClick( long arg, void *ptr )
{
#ifdef __MWERKS__
	#pragma unused(ptr)
#endif

	tag_entry* e;
	tag new_id;
	
	switch( arg )
	{
		case 10701:	// Choose a tag
			new_id = pick_tag_of_type('txst', GetTag( 10701 ), fEntry );
			if(new_id != 0) SetTag( 10701, 'txst', new_id );
		break; 
		
		case 10702:	// Open tag window
			e = get_entry('txst', GetTag( 10701 ) );
			if(e) TRevengeApp::OpenItem( e );
		break;

		case 10710:	// Open damage window
//			e = get_entry('core', GetTag( 10710 ) );
//			if(e) TRevengeApp::OpenItem( e );
		break;

		default:
			return -1;
	}

	return 0;
}

		
// ========================================  SAVE TO TAG  ==================================================
void TlighEditForm::CommitToTag()
{
	GetValues();
	save_lightning_definition( &def, fEntry );
}


// ========================================  RELOAD TAG  ==================================================
void TlighEditForm::ReloadTag()
{
	read_lightning_definition( &def, fEntry );
	SetValues();
}


// ========================================  TAG NEEDS UPDATING  ==================================================
bool TlighEditForm::NeedCommit()
{
	GetValues();
	return tag_data_needs_update( "ligh", fEntry, &def, SIZEOF_STRUCT_LIGHTNING_DEFINITION, 1, lightning_byte_swapping_data);
}

		
// ========================================  SET INTERFACE VALUES  ==================================================
void TlighEditForm::SetValues()
{
	int i;

	SetTag( 10701, 'txst', def.collection_reference_tag );
	
	SetInt( 10703, def.sequence_indexes[_lightning_sequence_fork] );
	SetInt( 10704, def.sequence_indexes[_lightning_sequence_main_bolt] );
	SetInt( 10705, def.sequence_indexes[_lightning_sequence_stain] );
	
	if(def.flags&_lightning_duration_based_on_length_flag ) {
		SetFlag( 10706, TRUE );
	} else {
		SetFlag( 10706, FALSE );
	}
	if(def.flags&_lightning_scars_ground_flag ) {
		SetFlag( 10707, TRUE );
	} else {
		SetFlag( 10707, FALSE );
	}
	if(def.flags&_lightning_moves_objects_flag ) {
		SetFlag( 10708, TRUE );
	} else {
		SetFlag( 10708, FALSE );
	}
	if(def.flags&_lightning_ignores_collisions_with_models_flag ) {
		SetFlag( 10709, TRUE );
	} else {
		SetFlag( 10709, FALSE );
	}

	SetFloat( 10711, def.shock_duration / TICKS_PER_SECOND);
	SetFloat( 10712, def.fade_duration );
	SetWorldDistance( 10713, def.bolt_length );
	SetShortWorldDistance( 10714, def.velocity );
	SetShortFixed( 10715, def.scale );
 	SetInt( 10716, def.fork_segment_minimum );
	SetInt( 10717, def.fork_segment_minimum + def.fork_segment_delta );
	SetInt( 10718, def.fork_overlap_amount );
	SetInt( 10719, def.main_bolt_overlap_amount );
	SetAngle( 10720, def.angular_limit );
	SetInt( 10721, def.apparent_number_of_bolts );

}


// ========================================  GET INTERFACE VALUES  ==================================================
void TlighEditForm::GetValues()
{

	def.collection_reference_tag = GetTag( 10701 );
	
	def.sequence_indexes[_lightning_sequence_fork] = GetInt( 10703 );
	def.sequence_indexes[_lightning_sequence_main_bolt] = GetInt( 10704 );
	def.sequence_indexes[_lightning_sequence_stain] = GetInt( 10705 );
	
	def.flags = 0;
	if(GetFlag( 10706 )) def.flags |= _lightning_duration_based_on_length_flag;
	if(GetFlag( 10707 )) def.flags |= _lightning_scars_ground_flag;
	if(GetFlag( 10708 )) def.flags |= _lightning_moves_objects_flag;
	if(GetFlag( 10709 )) def.flags |= _lightning_ignores_collisions_with_models_flag;
	
	def.shock_duration = GetFloat( 10711 * TICKS_PER_SECOND);
	def.fade_duration = GetFloat( 10712 );
	def.bolt_length = GetWorldDistance( 10713 );
	def.velocity = GetShortWorldDistance( 10714 );
	def.scale = GetShortFixed( 10715 );
 	def.fork_segment_minimum = GetInt( 10716 );
	def.fork_segment_delta = GetInt( 10717 ) - def.fork_segment_minimum;
	def.fork_overlap_amount = GetInt( 10718 );
	def.main_bolt_overlap_amount = GetInt( 10719 );
	def.angular_limit = GetAngle( 10720 );
	def.apparent_number_of_bolts = GetInt( 10721 );
}

