/************************************************************************\
|*	Copyright © 1997 Artificial Horizons, Inc..                    	*|
|*	Printed in the United States of America.                       	*|
|*	44 Montgomery. Suite 1480, San Francisco, CA, 94104 U.S.A.     	*|
|*	All rights reserved.                                           	*|
|*	                                                               	*|
|*	This product or document is protected by copyright and         	*|
|*	distributed under licenses restricting its use,                	*|
|*	copyright and distribution under licenses restricting          	*|
|*	its use, copying, distribution and decompilation.              	*|
|*	                                                               	*|
|*	No part of this product or document may be reproduced in       	*|
|*	any form by any means without prior written authorization of   	*|
|*	Artificial Horizons, Inc. and its licensors.                   	*|
|*	                                                               	*|
|*	Artificial Horizons, Aviator, AVDS, FlightCam, ChaseCam,       	*|
|*	and TowerCam are trademarks or registered trademarks of        	*|
|*	of Artificial Horizons, Inc.                                   	*|
\************************************************************************/
/***************************************************************************\
|*                                                                           *|
|*                            Produced under:                                *|
|*        Cooperative Reasearch and Development Agreement 94-173-EL-02       *|
|*                               between                                     *|
|*                        Artificial Horizons®, Inc.                         *|
|*                                 and                                       *|
|*                    Wright Laboratory, WL/FIGS, USAF                       *|
|*                                                                           *|
|*                                  by                                       *|
|*           Robert R. Smith Jr. and Capt Steven J. Rasmussen                *|
|*                                1994-95                                    *|
|*                                                                           *|
\***************************************************************************/
/********************************************************************\
|*	Portons Copyright © 1998 RasSimTech Ltd.                       	*|
|*	Printed in the United States of America.                       	*|
|*	All rights reserved.                                           	*|
|*	                                                               	*|
|*	This product or document is protected by copyright and         	*|
|*	distributed under licenses restricting its use,                	*|
|*	copyrightand distribution under licenses restricting           	*|
|*	its use, copying, distribution and decompilation.              	*|
|*	                                                               	*|
|*	No part of this product or document may be reproduced in       	*|
|*	any form by any means without prior written authorization of   	*|
|*	RasSimTech Ltd.							                     	*|
|*	                                                               	*|
\********************************************************************/

#include "windows.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include <UserNetwork.h>

#define PRINT_TIME_MS 1000
#define RUN_TIME_MS 20
#define SEND_TIME_MS 100
#define TOTAL_TIME_SEC 100.0  /*total run time inseconds*/

Aircraft ac;
float deg_to_rad = 3.14159/180.0;
int	new_frame = 0;
void h707_send(),b707_run();
float turn_rate,airspeed,altitude;



void
main(argc, argv)
int argc;
char *argv[];
{

	float dwTotalTimeSec=0;
	DWORD dwCurrentTimeMs,dwLastTimeMs;/*MILISECONDS*/
	DWORD dwElapsedTimeMs,dwRunTimeMs,dwSendTimeMs,dwPrintTimeMs;
	float ftime = 0.0;
	float fLon,fLat,fAlt;

	int iCount = 1;
	char* strAddress[1];
	strAddress[0] = strdup("224.0.5.20:2267");/* this is the default AVDS multicast address*/
	OpenAVDSNetwork(iCount,strAddress);

	fLon=-122.366;fLat=37.613;fAlt=15000.0;
	Init_Aircraft(&ac,"707_Drone",0,AIRCRAFT_TYPE_00,&fLon,&fLat,&fAlt);
	ac.rot_X = 0.0;
	ac.rot_Y = 8.0;
	ac.rot_Z = 0.0;
	airspeed = 500.0;
	turn_rate = 90.0/60.0;
	altitude = 15000;

	ac.packet.craftid = 1234;	// must be unique for each aircraft

	dwLastTimeMs = GetTickCount();
	dwRunTimeMs = 0;
	dwSendTimeMs=0;

	while (TRUE){
		dwCurrentTimeMs = GetTickCount();
		dwElapsedTimeMs = dwCurrentTimeMs-dwLastTimeMs;
		dwTotalTimeSec+=(float)dwElapsedTimeMs/1000.0;
		dwRunTimeMs+=dwElapsedTimeMs;
		dwSendTimeMs+=dwElapsedTimeMs;

		if (dwRunTimeMs>RUN_TIME_MS){
				b707_run( (float)dwElapsedTimeMs/1000.0 );
				dwRunTimeMs = 0;
		}
		if (dwSendTimeMs>SEND_TIME_MS){
			h707_send();
			dwSendTimeMs=0;
		}
		if (dwTotalTimeSec>TOTAL_TIME_SEC){
			break;
		}
		dwLastTimeMs = dwCurrentTimeMs;
		dwPrintTimeMs+=dwElapsedTimeMs;
		if (dwPrintTimeMs>PRINT_TIME_MS){
			fprintf(stderr,"Total Time = %5.2f \n",dwTotalTimeSec);
			dwPrintTimeMs=0;
		}
	}
	CloseAVDSNetwork();
	fprintf(stderr,"Finished:Total Time = %f \n",dwTotalTimeSec);
}

void
b707_run( dt )
float dt;
{
	float u;
	double sphi,cphi,spsi,cpsi,stheta,ctheta;
	float dN,dE,dh;

	ac.rot_Z += turn_rate*dt;
	u = airspeed;
	ac.pos_Z = altitude;

	sphi = sin(ac.rot_Y*deg_to_rad);
	cphi = cos(ac.rot_Y*deg_to_rad);
	stheta = sin(ac.rot_X*deg_to_rad);
	ctheta = cos(ac.rot_X*deg_to_rad);
	spsi = sin(ac.rot_Z*deg_to_rad);
	cpsi = cos(ac.rot_Z*deg_to_rad);

	dN = 	u*ctheta*cpsi;
	dE =	u*ctheta*spsi;
	dh	=	u*stheta;

	ac.pos_X += dE * dt;
	ac.pos_Y += dN * dt;
	ac.pos_Z += dh * dt;
	ac.engine = 100;

	ac.dt += dt;
	if ( new_frame == 1 ) {
		Put_Aircraft(&ac);
		new_frame = 0;
		ac.dt = 0.0;
	}
}


void
h707_send()
{
	new_frame = 1;
}




