/*
 * Copyright (c) 2016-2021 Marco Cawthorne <marco@icculus.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

void
Nightvision_Toggle(void)
{
	player pl = (player)pSeat->m_ePlayer;

	if (!(pl.g_items & ITEM_NIGHTVISION)) {
		pSeatLocal->m_iNightvision = 0;
		return;
	}

	pSeatLocal->m_iNightvision = 1 - pSeatLocal->m_iNightvision;

	if (pSeatLocal->m_iNightvision)
		Sound_Play(self, CHAN_AUTO, "nvg.on");
	else
		Sound_Play(self, CHAN_AUTO, "nvg.off");
}

void
Nightvision_PreFrame(void)
{
	player pl = (player)pSeat->m_ePlayer;

	if (!pSeatLocal->m_iNightvision)
		return;

	if (!(pl.g_items & ITEM_NIGHTVISION)) {
		pSeatLocal->m_iNightvision = 0;
		return;
	}

	dynamiclight_add(pSeat->m_vecPredictedOrigin, 768, [1,1,1]);
}

void
Nightvision_PostFrame(void)
{
	player pl = (player)pSeat->m_ePlayer;

	if (!pSeatLocal->m_iNightvision)
		return;

	if (!(pl.g_items & ITEM_NIGHTVISION)) {
		pSeatLocal->m_iNightvision = 0;
		return;
	}

	drawpic(video_mins, "fade_modulate", g_hudres, [0,1,0], 1.0f, 0);
}
