/***********************************************************************
*Copyright (c)2012 , 天际边工作室
*All rights reserved.
**
文件名称：	Map.cpp
*摘要：		游戏主地图类
**
*修改历史：
*	版本    日期     作者     改动内容和原因
*   ----------------------------------------------------
*	1.0    2012-10-5  wstjb     基本的功能完成
***********************************************************************/

#include "GoodList.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

extern  PAINTSTRUCT ps;
extern 	HDC hdc;
extern 	HBITMAP hbmp; 
extern 	HBITMAP hbmp2; 
extern 	BITMAP bmp;
extern 	HDC hdcMem;
extern 	HDC hdcTmp;
extern 	RECT rect;
extern 	HWND hwnd;
extern  int color;
extern  int flag;

extern int KeyReturn;
extern int KeyEsc;
extern int KeyHome;
extern int KeySpace;
extern int KeyLeft0;
extern int KeyUp0;
extern int KeyRight0;
extern int KeyDown0;

extern byte resByte[2731][16384];//存储lib资源
extern int resByteLen[2731];
extern int resPos[2731][6];//存储lib地址和编号 第六位为长度
extern int resCount;

/***********************************************************************
 * 说明:     GoodList类初始化函数
 * 输入参数: int num——用于设定君主等信息，由君主选择传值过来
 * 返回值  : 无
 * 功能	   : 初始化
 * 修改历史:
 *               姓名            日期             说明
 *             ------          ----------      -------------
 *             wstjb          2012-10-5       完成基本功能
***********************************************************************/
void GoodList::Init()
{
	int ret = 0;
	for(int  i = 0; i<14;i++)
	{
		for(int j = 0;j < 256 ; j++)
		{
			//good[i][j] = new Good();
			ret = good[i][j].Init(i+1,j+1);
			//if(ret == -1)
			{
				//break;
			}
		}
	}
	for(int i =0;i <2731;i++)
	{
		goodInList[i][0] = 0;
		goodInList[i][1] = 0;
		goodInList[i][2] = 0;
		equipInList[i][0] = 0;
		equipInList[i][1] = 0;
		equipInList[i][2] = 0;
	}
	goodInListCount = 0;
	equipInListCount = 0;
}

/***********************************************************************
 * 说明:     GoodList类显示函数
 * 输入参数: WPARAM KeyCode——按键信息，用于控制地图移动与对应的显示
 * 返回值  : 无
 * 功能	   : 显示地图及相关界面
 * 修改历史:
 *               姓名            日期             说明
 *             ------          ----------      -------------
 *             wstjb          2012-10-5       完成基本功能
***********************************************************************/
void GoodList::Show(WPARAM KeyCode)
{

}

/***********************************************************************
 * 说明:     GoodList类按键函数
 * 输入参数: WPARAM KeyCode——按键信息，用于确定当前的城市
 * 返回值  : 无
 * 功能	   : 处理当前类的按键信息
 * 修改历史:
 *               姓名            日期             说明
 *             ------          ----------      -------------
 *             wstjb          2012-10-5       完成基本功能
 *			   wstjb			2012-10-20		增加功能
***********************************************************************/
int GoodList::KeyDown(WPARAM KeyCode)
{
	
	return 0;
}

int GoodList::CheckGoodExist(int type,int idx)
{
	if(type <=7)
	{
		for(int i = 0; i < equipInListCount ; i ++)
		{
			if(equipInList[i][0] == type && equipInList[i][1] == idx)
			{
				return equipInList[i][2];
			}

		}

	}
	else
	{
		for(int i = 0; i < goodInListCount ; i ++)
		{
			if(goodInList[i][0] == type && goodInList[i][1] == idx)
			{
				return goodInList[i][2];
			}

		}
	}
	return 0;
}

int GoodList::CheckGoodExistReturnPos(int type,int idx)
{
	
		for(int i = 0; i < equipInListCount ; i ++)
		{
			if(equipInList[i][0] == type && equipInList[i][1] == idx)
			{
				return i+1;
			}

		}

	
		for(int i = 0; i < goodInListCount ; i ++)
		{
			if(goodInList[i][0] == type && goodInList[i][1] == idx)
			{
				return i+1;
			}

		}
	return 0;
}

void GoodList::Add(int type,int idx,int sum)
{
	wlogfile(1, __FUNCTION__ ,"type=",type);
	wlogfile(1, __FUNCTION__ ,"idx=",idx);
	wlogfile(1, __FUNCTION__ ,"sum=",sum);
	if(type > 7)
	{
		for(int i = 0; i < goodInListCount ; i ++)
		{
			if(goodInList[i][0] == type && goodInList[i][1] == idx)
			{
				goodInList[i][2] += sum;
				if(goodInList[i][2]>99)goodInList[i][2]=99;//20160816
				return;
			}

		}
		goodInList[goodInListCount][0] = type;
		goodInList[goodInListCount][1] = idx;
		goodInList[goodInListCount][2] = sum;
		goodInListCount ++;
	}
	else
	{
		for(int i = 0; i < equipInListCount ; i ++)
		{
			if(equipInList[i][0] == type && equipInList[i][1] == idx)
			{
				equipInList[i][2] += sum;
				if(equipInList[i][2]>99)equipInList[i][2]=99;//20160816
				return;
			}

		}
		equipInList[equipInListCount][0] = type;
		equipInList[equipInListCount][1] = idx;
		equipInList[equipInListCount][2] = sum;
		equipInListCount ++;
	}
}

int GoodList::Del(int type,int idx,int sum)
{
	wlogfile(1, __FUNCTION__ ,"type=",type);
	wlogfile(1, __FUNCTION__ ,"idx=",idx);
	wlogfile(1, __FUNCTION__ ,"sum=",sum);
	if(type > 7)
	{
		for(int i = 0; i < goodInListCount ; i ++)
		{
			if(goodInList[i][0] == type && goodInList[i][1] == idx)
			{
				goodInList[i][2] -= sum;
				if(goodInList[i][2] < 0)
				{
					goodInList[i][2] += sum;
					return -1;
				}
				else if (goodInList[i][2] == 0)
				{
					for(int j = i; j < goodInListCount - 1 ; j ++)
					{
						goodInList[j][0] = goodInList[j+1][0];
						goodInList[j][1] = goodInList[j+1][1];
						goodInList[j][2] = goodInList[j+1][2];
					}
					goodInListCount --;

				}
				return 0;
			}

		}
	}
	else
	{
		for(int i = 0; i < equipInListCount ; i ++)
		{
			if(equipInList[i][0] == type && equipInList[i][1] == idx)
			{
				equipInList[i][2] -= sum;
				if(equipInList[i][2] < 0)
				{
					equipInList[i][2] += sum;
					return -1;
				}
				else if (equipInList[i][2] == 0)
				{
					for(int j = i; j < equipInListCount - 1 ; j ++)
					{
						equipInList[j][0] = equipInList[j+1][0];
						equipInList[j][1] = equipInList[j+1][1];
						equipInList[j][2] = equipInList[j+1][2];
					}
					equipInListCount --;

				}
				return 0;
			}

		}
	}
	return -1;

}


void GoodList::Clear()
{
	for(int i =0;i <2731;i++)
	{
		goodInList[i][0] = 0;
		goodInList[i][1] = 0;
		goodInList[i][2] = 0;
		equipInList[i][0] = 0;
		equipInList[i][1] = 0;
		equipInList[i][2] = 0;
	}
	goodInListCount = 0;
	equipInListCount = 0;
}

int GoodList::Del_t(int type,int idx,int sum)
{
	wlogfile(1, __FUNCTION__ ,"type=",type);
	wlogfile(1, __FUNCTION__ ,"idx=",idx);
	wlogfile(1, __FUNCTION__ ,"sum=",sum);
		for(int i = 0; i < goodInListCount ; i ++)
		{
			if(goodInList[i][0] == type && goodInList[i][1] == idx)
			{
				goodInList[i][2] -= sum;
				if(goodInList[i][2] < 0)
				{
					goodInList[i][2] += sum;
					return -1;
				}
				else if (goodInList[i][2] == 0)
				{
					for(int j = i; j < goodInListCount - 1 ; j ++)
					{
						goodInList[j][0] = goodInList[j+1][0];
						goodInList[j][1] = goodInList[j+1][1];
						goodInList[j][2] = goodInList[j+1][2];
					}
					goodInList[goodInListCount-1][0] = 0;
					goodInList[goodInListCount-1][1] = 0;
					goodInList[goodInListCount-1][2] = 0;
					goodInListCount --;
					return 1;

				}
				return 0;
			}

		}
	
		for(int i = 0; i < equipInListCount ; i ++)
		{
			if(equipInList[i][0] == type && equipInList[i][1] == idx)
			{
				equipInList[i][2] -= sum;
				if(equipInList[i][2] < 0)
				{
					equipInList[i][2] += sum;
					return -1;
				}
				else if (equipInList[i][2] == 0)
				{
					for(int j = i; j < equipInListCount - 1 ; j ++)
					{
						equipInList[j][0] = equipInList[j+1][0];
						equipInList[j][1] = equipInList[j+1][1];
						equipInList[j][2] = equipInList[j+1][2];
					}
					equipInList[equipInListCount-1][0] = 0;
					equipInList[equipInListCount-1][1] = 0;
					equipInList[equipInListCount-1][2] = 0;
					equipInListCount --;
					return 1;
				}
				return 0;
			}

		}

	return -1;

}

