//       _________ __                 __
//      /   _____//  |_____________ _/  |______     ____  __ __  ______
//      \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
//      /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ |
//     /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
//             \/                  \/          \//_____/            \/
//  ______________________                           ______________________
//                        T H E   W A R   B E G I N S
//         Stratagus - A free fantasy real time strategy game engine
//
/**@name missile_pointtopointbounce.cpp - The missile PointToPointBounce. */
//
//      (c) Copyright 2012 by Joris Dauphin
//
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; only version 2 of the License.
//
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
//      02111-1307, USA.

#include "stratagus.h"

#include "missile.h"

#include "database/defines.h"
#include "map/map.h"

/**
**  Missile flies from x,y to x1,y1 than bounces NumBounces times
*/
void MissilePointToPointBounce::Action()
{
	this->Wait = this->Type->get_sleep();
	if (PointToPointMissile(*this)) {
		if (this->State < 2 * this->Type->NumBounces - 1 && this->TotalStep) {
			const PixelPos step = (this->destination - this->source);

			this->destination += step * ((wyrmgus::defines::get()->get_tile_width() + wyrmgus::defines::get()->get_tile_height()) * 3) / 4 / this->TotalStep;
			this->State++; // !(State & 1) to initialise
			this->source = this->position;
			PointToPointMissile(*this);
			//this->State++;
			this->MissileHit();
			// FIXME: hits to left and right
			// FIXME: reduce damage effects on later impacts
		} else {
			this->MissileHit();
			this->TTL = 0;
		}
	} else {
		this->NextMissileFrame(1, 0);
	}
}
