/*
  simpletestcar.c: simple test program for Car class
*/


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

#include "vector2.h"
#include "vv.h"
#include "vehicle.h"
#include "car.h"


Vector2 startingpoint;


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

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

	// where is the Car 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 = a.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 );
	Car c1        = new_Car( "c1",             // name
			         new_VV(0,0),      // pos: origin
			   	 new_Vector2(0,0), // dir: standstill
			   	 3,                // speed
				 1,                // people_cur
			   	 6,                // people_max
				 -1,               // default wheels
				 100,              // fuel_cur
				 200               // fuel_max
	);

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

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

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

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

	// now addfuel
	OM1( c1, addfuel, 50 );
	printf( "\n%s after refuelling: ", c1->name );
	OM0( c1, print );
	printf( "\n" );

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

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