'use strict';

// servers:
// "sumo server"
// interior office dm
// team DM

console.log(version());

var spawn_points = [
	
	{name: 'restourant',		coords: [56.31,  83.65, 2.0], heading: 233.0},
	{name: 'office building',	coords: [31.31,  91.65, 2.0], heading: 51.0},
	{name: 'trash house',		coords: [72.3,   81.22, 2.0], heading: 223.0},
	{name: 'work zone',			coords: [112.28, 82.11, 2.0], heading: 90.0} // actual z = 1.5
	
	// test areas:
	
	//{name: 'work zone',			coords: [136.5, 86.5, 4.5], heading: 90.0} // TEST AI UNDER BRIDGE
	//{name: 'work zone',			coords: [106.28, 100.5, 2.0], heading: 90.0} // CONVEYOR TEST
	
	//{name: 'test location',			coords: [162.1, 87.37, 3], heading: 90.0}, // actual z = 1.5
	//{name: 'test location',			coords: [55.1, 91.37, 2], heading: 90.0}, // actual z = 1.5
	
	// TEST AREA:
	//{name: 'work zone',			coords: [114.28 + 5 + 2 + 5, 92.11 + 5 + 2 + 0.1 + 1, 2.0], heading: 90.0}, // actual z = 1.5
	
	//{name: 'test zone',			coords: [127.5, 74.5, 2.0], heading: -90.0} // actual z = 1.5
	
	//{name: 'Test area',			coords: [128.28, 76.11, 2.5], heading: 90.0}
	
];

var game_interface = NewGameInstance();

game_interface.OnCreatePlayer = function(Player) {
	
	console.log('OnCreatePlayer: name: ' + Player.playername);
	
	Player.DamageType = null;
	// todo..maybe this should have been Player.last_damage = null;
	Player.spawn(true); // sets stuff
	
	if (Player.IsLocal == 1) { // if local player, create a car to play with.
		
		var somecar = new Vehicle('four', 54.77, 91.37, 2, 90 + 45); //
		
		/*
		var info = somecar.Get_Controlling_Actor();
		
		console.log('Get_Controlling_Actor');
		console.log(info);
		
		Player.EnterThisVehicle(somecar, false, true, -1);
		
		var info = somecar.Get_Controlling_Actor();
		
		console.log('Get_Controlling_Actor');
		console.log(info);
		console.log(info.playername);
		*/
		
		var staticobject = SysGame.CreateStaticMapObject('wodc', Player.x + 0.5, Player.y, Player.z, 45.0, 'mchg', 50, function() { console.log('crate broken') }, function() { console.log('WEPON PICKED UP') });
		console.log('static object is ' + staticobject);
		
		var newt = Game.NewTrigger('phone', 80.5, 87.5, 2.0, 20, 0.0);
		
		newt.OnPickup = function() {
			
			console.log('You picked up the phone! a mission can start.');
			
			// this.SetParameters(false, 10000); // respawn after 10 seconds
			
			//Game.Explosion(this.x, this.y, this.z, 2.0);
			Game.DeleteTrigger(this);
			
		}
		
		
		/*
		var newt = Game.NewTrigger('mchg', Player.x + 0.5, Player.y, Player.z, 20, 0.0);
		
		newt.OnPickup = function() {
			
			console.log('picked up the pickup.');
			
			this.SetParameters(false, 10000); // respawn after 10 seconds
			
			//Game.Explosion(this.x, this.y, this.z, 2.0);
			
			//Game.DeleteTrigger(this);
			
		}
		*/
		
		// ApplyDamage (bywho: Actor,FromWorld: x,FromWorld: y,FromWorld: z,amout: single,damagetype: tdamagetype)
		
	}
	
	// Player.health = 1.0; // default test
	
	Player.EchoMessage('{color:FFC518}Top down city test! ' + '{color:00FFFF}Enjoy!', constants.fontSize.font_b);
	
}

game_interface.OnDestroyPlayer = function(Player, Reason) {
	
	//console.log('OnDestroyPlayer');
	//console.log(Player.playername);
	
	if (Player.IsPlayer == false)
		return;
	
	if (Player.IsLocal == 1) {
		
		console.log('game shutting down.');
		
	} else {
		
		console.log('Player disconnected: ' + constants.disconnectReason.toString(Reason));
		
	}
	
}

game_interface.OnModeChange = function() {
	
	console.log('engine mode has changed to: ' + constants.engineState.toString(game_interface.EngineMode) + ' (' + game_interface.EngineMode + ')');
	
}

game_interface.OnDamagePlayer = function(Actor_Victim, Actor_Attacker, HitOrigin, OldHealth, NewHealth, DamageType) {
	
	//Actor_Victim.last_damage = DamageType;
	
	if (NewHealth == 0) {
		
		// OK:
		// console.log('KILL ' + (Actor_Victim == null ? "Unknown victim" : Actor_Victim.playername) + ' was damaged by ' + (Actor_Attacker == null ? "AI" : Actor_Attacker.playername) /*+ ' (' + OldHealth + ' -> ' + NewHealth + ') ' + constants.damageType.toString(DamageType)*/);
		
		if (Actor_Victim != null) {
			if (Actor_Victim.playername == 'Asshole') {
				
				/*
				var listk = SysGame.ActorList();
				//console.log(listk);
				
				for (var i in listk) {
					
					var ecto = listk[i];
					
					//console.log('returned actor: ' + ecto.playername);
					
				}
				*/
				
				Game.DeleteActor(Actor_Victim);
				
			}
		}
		
	} else {
		
		// OK:
		// console.log('Damage ' + (Actor_Victim == null ? "Unknown victim" : Actor_Victim.playername) + ' was damaged by ' + (Actor_Attacker == null ? "AI" : Actor_Attacker.playername) + ' (' + OldHealth + ' -> ' + NewHealth + ') ' + constants.damageType.toString(DamageType));
		
	}
	
}

game_interface.OnTick = function() {
	
	//console.log("OnTick.");
	
	for (var i in actor_stack) {
		
		var Player = actor_stack[i];
		
		if (Player.IsPlayer == false) continue;
		
		if (Player.health <= 0) {
			
			if (Player.DeadTime < 3000) // 3 seconds delay
				continue;
			
			if (game_interface.TimeDifference(Player.respawntime, game_interface.Time) < 1000) // prevent double calls - give it a chance to sync with main script.
				continue;
			
			var playerpos = Player.GetLocation();
			var playerpos_block = Player.GetBlockLocation();
			
			console.log("Player " + i + " has died at " + playerpos.x + ", " + playerpos.y + ", " + playerpos.z + " block " + playerpos_block.x + ", " + playerpos_block.y + ", " + playerpos_block.z + " and has been respawned.");
			
			// (' + constants.damageType.toString(Player.DamageType) + ')
			
			Player.EchoMessage('{color:FF0000}Died & {color:FFFFFF}Respawned!', constants.fontSize.font_b);
			
			Player.spawn(true);
			
		}
		
	}
	
}

// todo: should be global.console and prototyped from that.

Actor.spawn = function(position_too) {
	
	this.health = 100;
	
	//console.log(this.WeaponData());
	
	// todo: pick a random place to spawn.
	
	this.StripWeapons();
	this.GiveWeapon('gun1', 5000);
	this.GiveWeapon('sgun', 5000);
	this.GiveWeapon('rpgl', 15);
	this.GiveWeapon('mchg', 5000);
	
	//this.GiveWeapon('molt', 5000);
	
	//this.GiveWeapon('rpgl', 5000);
	
	//this.GiveWeapon('flam', 5000);
	
	this.GiveWeapon('gren', 5000);
	//this.GiveWeapon('mine', 5000);
	//this.GiveWeapon('oils', 5000);
	
	//this.GiveWeapon('banana', 5000);
	//this.GiveWeapon('hhg', 5000);
	
	
	
	//this.GiveWeapon('frce', 5000); // force fan
	
	
	//this.GiveWeapon('flam', 5000);
	
	
	
	var spawn_point = getRandomInt(spawn_points.length);
	
	// spawn_point = 3;
	
	if (position_too == true) {
		
		this.move(spawn_points[spawn_point].coords[0], spawn_points[spawn_point].coords[1], spawn_points[spawn_point].coords[2], true);
		this.heading = spawn_points[spawn_point].heading;
		
	}
	
	this.WantedLevel = 0;
	
	this.State = constants.actorState.AsNormal; // this could be a problem causing clients not to respawn properly.
	this.ExtinguishFires();
	this.SetActorAnimation(constants.animation.anm_stand, false, false, true);
	this.NetworkSync(); // Synchronize player over network, if remote player.
	
	this.ResetCamera(spawn_points[spawn_point].coords[0], spawn_points[spawn_point].coords[1], spawn_points[spawn_point].coords[2]); // reset the camera.
	
	this.respawntime = game_interface.Time;
	
	// create test character
	
	/*
	var tempactor = Game.NewActor(2, constants.aiClass.AI_ZOMBIE, spawn_points[spawn_point].coords[0], spawn_points[spawn_point].coords[1] + 1.0, spawn_points[spawn_point].coords[2], 45, 4);
	
	tempactor.playername = 'Asshole';
	
	// tempactor.GiveWeapon('gun1', 5000);
	tempactor.Attack(this, constants.aiClass.AI_IDIOTKILLER);
	*/
	
}

var cw = 0.0;

var allweaps = [ 'gun1', 'mchg', 'rpgl', 'flam', 'riotg', 'gren', 'sgun', 'molt', 'phas', /* 'watr', 'frce',*/ 'banana', 'hhg', 'mine', 'oils' ];

for ( var weap in allweaps) {
	
	var newt = Game.NewTrigger(allweaps[weap], 58.0 + 0.5 + cw, 80.5, 3.0, 2000, 0.0);
	
	cw += 0.3;
	
	newt.OnPickup = function() {
		
		this.SetParameters(false, 10000); // respawn after 10 seconds
		
	}
	
}

SysGame.ClearVehicleLists();

SysGame.AddToEmergencyList('ftr1');
SysGame.AddToEmergencyList('ftr2');
SysGame.AddToEmergencyList('medc');

SysGame.AddToPoliceList('pmar');
SysGame.AddToPoliceList('acop');

SysGame.AddToTrafficList('buss'); // long bus
SysGame.AddToTrafficList('ftr1'); // sort of a school bus

SysGame.AddToTrafficList('ftr2'); // fire truck

SysGame.AddToTrafficList('acop'); // cop
SysGame.AddToTrafficList('pmar'); // cop van

SysGame.AddToTrafficList('sbik');
SysGame.AddToTrafficList('acur');
SysGame.AddToTrafficList('audi');
SysGame.AddToTrafficList('bmw1');

SysGame.AddToTrafficList('chev');
SysGame.AddToTrafficList('chry');
SysGame.AddToTrafficList('fiat');
SysGame.AddToTrafficList('ford');
SysGame.AddToTrafficList('four');
SysGame.AddToTrafficList('hond');
SysGame.AddToTrafficList('lanc');
SysGame.AddToTrafficList('maz1');
SysGame.AddToTrafficList('maz2');
SysGame.AddToTrafficList('medc');
SysGame.AddToTrafficList('merc');
SysGame.AddToTrafficList('mtru');
SysGame.AddToTrafficList('nis2');
SysGame.AddToTrafficList('niss');
SysGame.AddToTrafficList('opel');
SysGame.AddToTrafficList('peug');
SysGame.AddToTrafficList('pont');
SysGame.AddToTrafficList('ren1');
SysGame.AddToTrafficList('ren2');
SysGame.AddToTrafficList('rtan');
SysGame.AddToTrafficList('seat');
SysGame.AddToTrafficList('suzu');
SysGame.AddToTrafficList('truk');
SysGame.AddToTrafficList('vana');
SysGame.AddToTrafficList('vanb');
SysGame.AddToTrafficList('vlad');
SysGame.AddToTrafficList('vw');
SysGame.AddToTrafficList('wspa');


var somecar = new Vehicle('tank', 53.5, 89.5, 2, 45);
//var info = somecar.Get_Controlling_Actor();

//somecar.ShootFromTurret(actor_stack[0]);

//somecar.free();
/**somecar = null; // free?

/*

//console.log(somecar.health);
somecar.health = 10;
//console.log(somecar.health);

somecar.move(53.5, 89.5, 2, 45);

*/

/*var myseats = somecar.seats();
console.log(typeof myseats);
console.log(myseats);*/

//somecar.free();
//console.log('valid: ' + somecar.valid());

/*
var loop = function() {
	
}
*/
