/*
* [U]NITED [G]AMING [C]HEATS Client Hook
*
* Copyright (c) LIQUID     2001-2002
* Copyright (c) ZOOLABUS   2001-2002
* 
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
* 
* GNU license doesn't cover Engine directory.
* Content of Engine directory is copyrighted (c) 1999, 2000, by Valve LLC
* and it is licensed under Valve's proprietary license (see original HL SDK).
*/

#include "stringfinder.h"
#include <vector>

typedef unsigned long DWORD;
#include "color.h"

ColorManager colorList;

//========================================================================================
void ColorManager::init()
{
	indexes.clear();
	list.clear();
	insertpos = 0;

	//   identifer     R   G   B   A      Access Index
	add("cross"      ,255,255,255,200  ); // index: 0
	add("team1"      ,255, 40, 40      ); // index: 1
	add("team2"      , 40, 80,255      ); // index: 2
	add("special"    ,155,155,155      ); // index: 3
	add("target"     ,102,204, 51      ); // index: 4
	add("hud"        ,120,120,255      ); // index: 5
	add("sound"      , 68, 68,255      ); // index: 6
	add("bombtimer"  ,120,120,255      ); // index: 7
	add("systime"    ,120,120,255      ); // index: 8
	add("esp"        ,120,120,255      ); // index: 9
	add("back"       ,  0,  0,  0, 128 ); // index:10
	add("radar1"     , 30, 50,150, 150 ); // index:11
	add("radar2"     , 10, 10, 50, 100 ); // index:12
	add("text"       ,120,120,255      ); // index:13
	add("wire"       ,255,255,  0      ); // index:14
	add("lambert"    ,255,255,255      ); // index:15
	add("death"      ,255,  0,  0      ); // index:16
	add("con_back"   , 10, 10, 30, 128 ); // index:17
	add("con_text"   ,255,255, 30      ); // index:18
	add("con_text2"  ,160,160,222, 128 ); // index:19
	add("con_edit"   ,255,255,255      ); // index:20
	add("menu_back"  , 10, 10, 30, 128 ); // index:21
	add("menu_select",  0, 80,255, 128 ); // index:22
	add("menu_text"  ,255,255,255      ); // index:23
	add("text_back" ,  0,  0,  0,  128 ); // index:24
	add("snake_back" ,  0,  0,  0, 128 ); // index:25
	add("snake_body" ,  0,200, 66, 200 ); // index:26
	add("snake_item" ,  0, 55,222, 200 ); // index:27
	add("snake_wall" ,222, 66, 22, 200 ); // index:28
	add("tlight"     ,222,111,111, 255 ); // index:29
	add("snd_step"   ,255,255,255, 255 ); // index:30
	add("snd_weapon" ,144,155,255, 255 ); // index:31
	add("snd_hit"    ,222,100, 61, 254 ); // index:32
	add("snd_special",222,111,111, 254 ); // index:33
	add("snd_default",222,255,222, 255 ); // index:34
	add("team3"      ,200,200,  0      ); // index:35
	add("team4"      , 20,222,100      ); // index:36
	add("nade"       ,120,120,255      ); // index:37
	add("treload"    ,255,128,  0      ); // index:38
	add("ctreload"   , 64,144,255      ); // index:39
	add("lamer"      ,165,  0,165      ); // index:40
	add("lamer-lock" ,222,  0,222      ); // index:41
	add("tsound"     ,255,144,144      ); // index:42
	add("ctsound"    ,144,144,255      ); // index:43
	add("meterone"   ,255,  0,  0      ); // index:44
	add("metertwo"   ,255,128,  0      ); // index:45
	add("meterthree" ,255,255,  0      ); // index:46
	add("meterfour"  ,128,255,  0      ); // index:47
	add("meterfive"  ,  0,255,  0      ); // index:48
	add("ugc"        ,120,120,255, 255 ); // index:49
}

//========================================================================================
void ColorEntry::fill_missing()
{
	compiled = (r<<24) + (g<<16) + (b<<8) + a;
	fr= float(r)/255.0f/30.0f;
	fg= float(g)/255.0f/30.0f;
	fb= float(b)/255.0f/30.0f;

	onebased_r=float(r)/255.0f;
	onebased_g=float(g)/255.0f;
	onebased_b=float(b)/255.0f;
	onebased_a=float(a)/255.0f;
}

//========================================================================================
void ColorManager::add(const char* name,int r, int g, int b, int a)
{
	indexes.add(name,insertpos);

	ColorEntry tmp;
	tmp.r=r;
	tmp.g=g;
	tmp.b=b;
	tmp.a=a;
	tmp.fill_missing();

	list.push_back(tmp);

	insertpos++;
}
#define BOUND_INTEGER(var,min,max) if((var)>(max)){(var)=(max);};if((var)<(min)){(var)=(min);}

//========================================================================================
void ColorManager::set(char* name, char* red, char* green, char* blue, char* alpha)
{
	if( !indexes.find(name) ){ return; }

	int index = indexes.num;
	char format[]="%d";

	ColorEntry tmp;
	sscanf(red   ,format,&tmp.r);
	sscanf(green ,format,&tmp.g);
	sscanf(blue  ,format,&tmp.b);
	sscanf(alpha ,format,&tmp.a);
	
	tmp.fill_missing();

	BOUND_INTEGER(index,0,int(list.size()-1));
	list[index] = tmp;
}
