/*
 * Copyright (c) 2024 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.
 */

var string g_defaultArsenal = "";
var string g_mapArsenal = "";

void
SHArsenal_Init(void)
{
	int c = 0i;
	string line;
	string lineFeed;
	filestream arsHandle;
	string standardArsenal = "arsenals/starteqp.txt";
	string mapArsenal = cvars.GetString("sh_weapfileloc"); //strcat("arsenal/", game.GetMap(), ".txt");

	g_defaultArsenal = "";
	g_mapArsenal = "";

	if (exists.InVFS(standardArsenal) == true) {
		arsHandle = fopen(standardArsenal, FILE_READ);

		if (arsHandle >= 0) {
			while ((lineFeed = fgets(arsHandle))) {
				c = (int)tokenize_console(lineFeed);
				line = argv(0);

				if (STRING_SET(line)) {
					g_defaultArsenal = strcat(g_defaultArsenal, line, " ");
				}
			}

			fclose(arsHandle);
		}

	}

	if (exists.InVFS(mapArsenal) == true) {
		arsHandle = fopen(mapArsenal, FILE_READ);

		if (arsHandle >= 0) {
			while ((lineFeed = fgets(arsHandle))) {
				c = (int)tokenize_console(lineFeed);
				line = argv(0);

				if (STRING_SET(line)) {
					g_mapArsenal = strcat(g_mapArsenal, line, " ");
				}
			}

			fclose(arsHandle);
		}
	}

// 	NSWarning("Default equipment: %s\nMap equipment: %s", g_defaultArsenal, g_mapArsenal);
}

void
SHArsenal_GiveItemsToPlayer(entity targetPlayer)
{
	for (int i = 0; i < (int)tokenize_console(g_defaultArsenal); i++) {
		ents.Input(targetPlayer, "GiveItem", argv(i), targetPlayer);
	}
	for (int i = 0; i < (int)tokenize_console(g_mapArsenal); i++) {
		ents.Input(targetPlayer, "GiveItem", argv(i), targetPlayer);
	}
}
