/*
  simpletestvehicle.c: simple test program for vehicle class
*/


#include <stdio.h>
#include <stdlib.h>

#include "vector2.h"
#include "vehicle.h"


Vector2 startingpoint;


//
// d = movefor( v, dx, dy, dirname, speed, t );
//	move vehicle v in current direction for t seconds, describing
//	where it ends up; return distance from starting point
//
static double movefor( Vehicle v, double dx, double dy, char *dirname, double speed, double t )
{
	v->dir->x = dx;
	v->dir->y = dy;
	v->speed  = speed;

	// let v move for t seconds in direction (dx,dy) at the given speed
	OM1( v, move, t );

	// where is the vehicle now?
	printf( "\nafter %.3f seconds moving %s at %g m/s:\n  ",
		t, dirname, speed );
	OM0(v,print);
	printf( "\n" );

	// how far has it travelled?
	// d = v.pos.distance_to(startingpoint)
	double d = OM1(v->pos,distance_to,startingpoint);

	printf( "%s is now %.3f metres away from startingpoint\n", v->name, d );
	return d;
}


int main( void )
{
	startingpoint = new_Vector2( 0, 0 );
	Vehicle v1    = new_Vehicle( "v1",             // name
			             new_Vector2(0,0), // pos: origin
			   	     new_Vector2(0,0), // dir: standstill
			   	     3,                // speed
				     1,                // people_cur (default)
			   	     6                 // people_max
	);

	printf( "%s initially: ", v1->name );
	OM0( v1, print );
	printf( "\n" );

	// try moving when you're stationary
	double d = movefor( v1, 0, 0, "stationary", 3, 2 );

	// now make v1 move north for 3 seconds
	d = movefor( v1, 0, 1, "north", 3, 2 );

	// now make v1 move east at 2m/s for 5 seconds
	d = movefor( v1, 1, 0, "east", 2, 5 );

	// move back to starting point at 1m/s
	// - compute unit vector back to starting point
	Vector2 dirback = new_Vector2( -v1->pos->x, -v1->pos->y );
	OM0( dirback, normalize );
	// - move in that direction at 1m/s for d seconds
	movefor( v1, dirback->x, dirback->y, "back", 1, d );

	OM0( dirback, dispose );	    // free dirback
	OM0( v1, dispose );		    // disposes dir and pos
	OM0( startingpoint, dispose );      // but startingpoint needs freeing
	exit(0);
	return 0;
}
