#include "stdafx.h"
#include "GamblingSystem.h"
#include "..\common\ReadScript.h"
#include "..\common\winutil.h"
#include "logproc.h"
#include "LargeRand.h"
#include "user.h"
#include "DSProtocol.h"
CGamblingSystem g_Gambling;
CGamblingSystem::CGamblingSystem(void)
{
}
CGamblingSystem::~CGamblingSystem(void)
{
}
void CGamblingSystem::Initialize(LPSTR lpFileName)
{
this->m_bFileLoad = false;
this->m_iGamblingAllItemCount = 0;
this->m_iGamblingGroups = 0;
for(int i=0;i<MAX_GAMBLING_OPTION_RATE;i++)
{
this->m_iGamblingOptionRate[i] = 0;
}
for(int j=0;j<MAX_GAMBLING_SPECIAL_ITEM_RATE;j++)
{
this->m_iGamblingSpecialItemRate[j] = 0;
}
for(int k=0;k<MAX_GAMBLING_EXC_OPTION_RATE;k++)
{
this->m_iGamblingExcOptionRate[k] = 0;
}
for(int l=0;l<MAX_GAMBLING_EXC_LEVEL;l++)
{
this->m_iGamblingExcSpecialLevel[l] = 0;
this->m_iGamblingExcNormalLevel[l] = 0;
}
for(int m=0;m<MAX_GAMBLING_NORMAL_BAG_OPTION_RATE;m++)
{
this->m_iGamblingItemCount[m] = 0;
}
this->LoadFile(lpFileName);
}
bool CGamblingSystem::LoadFile(LPSTR lpFileName)
{
int Token;
int Group;
int count;
int Table = 0;
SMDFile = fopen(lpFileName, "r");
if(SMDFile != NULL)
{
while ( true )
{
Token = GetToken();
if(Token == END)
{
break;
}
else if(Token == NUMBER)
{
Group = TokenNumber;
switch ( Group )
{
case 0:
{
count = 0;
while ( true )
{
Token = GetToken();
if(!strcmp("end", TokenString))
break;
if ( count > 25 )
{
MsgBox("Load Script Error %s", lpFileName);
break;
}
this->m_iGamblingItemOptionRate[count].iSkillOptionRate = TokenNumber;
Token = GetToken();
this->m_iGamblingItemOptionRate[count].iLuckOptionRate = TokenNumber;
Token = GetToken();
this->m_iGamblingItemOptionRate[count].iAddOptionRate = TokenNumber;
Token = GetToken();
this->m_iGamblingItemOptionRate[count].iExOptionRate = TokenNumber;
Token = GetToken();
this->m_iGamblingItemOptionRate[count++].iSortOptionRate = TokenNumber;
}
break;
}
case 1:
{
count = 0;
while ( true )
{
Token = GetToken();
if(!strcmp("end", TokenString))
break;
if ( count > 4 )
{
MsgBox("Load Script Error %s", lpFileName);
break;
}
this->m_iGamblingOptionRate[count++] = TokenNumber;
}
break;
}
case 2:
{
count = 0;
while ( true )
{
Token = GetToken();
if(!strcmp("end", TokenString))
break;
if ( count > 5 )
{
MsgBox("Load Script Error %s", lpFileName);
break;
}
this->m_iGamblingSpecialItemRate[count++] = TokenNumber;
}
break;
}
case 3:
{
count = 0;
while ( true )
{
Token = GetToken();
if(!strcmp("end", TokenString))
break;
if ( count > 4 )
{
MsgBox("Load Script Error %s", lpFileName);
break;
}
this->m_iGamblingExcOptionRate[count++] = TokenNumber;
}
break;
}
case 4:
{
count = 0;
while ( true )
{
Token = GetToken();
if(!strcmp("end", TokenString))
break;
if ( count > 4 )
{
MsgBox("Load Script Error %s", lpFileName);
break;
}
this->m_iGamblingExcSpecialLevel[count] = TokenNumber;
Token = GetToken();
this->m_iGamblingExcNormalLevel[count++] = TokenNumber;
}
break;
}
default:
{
if ( Group >= 5 )
{
int count = 0;
while ( true )
{
Token = GetToken();
if(!strcmp("end", TokenString))
break;
this->m_iGamblingItems[Table][count].wItemIndex = TokenNumber;
Token = GetToken();
this->m_iGamblingItems[Table][count].btItemType = TokenNumber;
Token = GetToken();
this->m_iGamblingItems[Table][count].btItemLevelMin = TokenNumber;
Token = GetToken();
this->m_iGamblingItems[Table][count].btItemLevelMax = TokenNumber;
Token = GetToken();
this->m_iGamblingItems[Table][count].btSkillOption = TokenNumber;
Token = GetToken();
this->m_iGamblingItems[Table][count].btLuckOption = TokenNumber;
Token = GetToken();
this->m_iGamblingItems[Table][count].btAddOption = TokenNumber;
Token = GetToken();
this->m_iGamblingItems[Table][count].btExOption = TokenNumber;
if(this->m_iGamblingItems[Table][count].btItemLevelMin >
this->m_iGamblingItems[Table][count].btItemLevelMax)
{
MsgBox("Load Script Error %s", lpFileName);
return 0;
}
count++;
this->m_iGamblingAllItemCount++;
if(this->m_iGamblingAllItemCount > 149)
break;
}
this->m_iGamblingItemCount[Table] = count;
Table++;
if(Table > 24)
break;
this->m_iGamblingGroups = Table;
}
}
}
}
}
fclose(SMDFile);
this->m_bFileLoad = true;
}
return this->m_bFileLoad;
}
int CGamblingSystem::GetItemLevel(int Group, int Item)
{
if(Item < 0 && Item > 149)
{
return 0;
}
else
{
if(this->m_iGamblingItems[Group][Item].btItemLevelMin == this->m_iGamblingItems[Group][Item].btItemLevelMax)
{
return this->m_iGamblingItems[Group][Item].btItemLevelMin;
}
else
{
return rand() % (this->m_iGamblingItems[Group][Item].btItemLevelMax - this->m_iGamblingItems[Group][Item].btItemLevelMin +1) + this->m_iGamblingItems[Group][Item].btItemLevelMin;
}
}
}
int CGamblingSystem::GetExcellentOption(int Number)
{
int v4 = 0;
int Exc = 0;
int i;
for (i=0; ; i++)
{
if(this->m_iGamblingExcSpecialLevel[i] < GetLargeRand()%1000000)
v4++;
else
Exc |= 1 << i;
if(v4 >= 6 - Number)
break;
}
while (true)
{
i++;
if ( i >= 6 )
break;
Exc |= 1 << i;
}
return Exc;
}
bool CGamblingSystem::TryGambling(int aIndex, int a2, int a3, int a4, BYTE pos)
{
if(!this->m_bFileLoad)
return false;
int Skill = 0, Luck = 0, Option = 0, Excellent = 0, Level = 0, RandomKey = 0, ItemNumber = -1;
if ( this->GetItemCount() > 0 )
{
if ( this->m_iGamblingSpecialItemRate[pos] < GetLargeRand() % 1000000 )
{
int ItemGetMax = 0;
int ItemGetMin = 0;
int ItemCount = 0;
int StartGroup = 0;
switch ( pos )
{
case 0:
StartGroup = 0;
break;
case 2:
StartGroup = 5;
break;
case 4:
StartGroup = 10;
break;
case 6:
StartGroup = 15;
break;
case 24:
StartGroup = 20;
break;
}
RandomKey = GetLargeRand() % 1000000;
int Sort = 0;
for (int i = StartGroup; i < StartGroup+5; i++)
{
Sort += this->m_iGamblingItemOptionRate[i].iSortOptionRate;
if ( RandomKey <= Sort )
{
StartGroup = i;
break;
}
}
while ( true )
{
ItemGetMax += this->m_iGamblingItemCount[ItemCount];
if(StartGroup <= ItemCount)
break;
ItemGetMin += this->m_iGamblingItemCount[ItemCount++];
}
int ItemIndex = GetLargeRand() % (ItemGetMax-ItemGetMin);
Level = this->GetItemLevel(StartGroup, ItemIndex);
ItemNumber = ItemGetNumberMake(this->m_iGamblingItems[StartGroup][ItemIndex].wItemIndex, this->m_iGamblingItems[StartGroup][ItemIndex].btItemType);
if(ItemNumber == -1)
return 0;
if(this->m_iGamblingItems[StartGroup][ItemIndex].btSkillOption && this->m_iGamblingItemOptionRate[StartGroup].iSkillOptionRate >= GetLargeRand() % 1000000)
{
Skill = 1;
}
if(this->m_iGamblingItems[StartGroup][ItemIndex].btLuckOption && this->m_iGamblingItemOptionRate[StartGroup].iLuckOptionRate >= GetLargeRand() % 1000000)
{
Luck = 1;
}
if(this->m_iGamblingItems[StartGroup][ItemIndex].btAddOption && this->m_iGamblingItemOptionRate[StartGroup].iAddOptionRate >= GetLargeRand() % 1000000)
{
RandomKey = GetLargeRand()%1000000;
if(RandomKey > this->m_iGamblingOptionRate[0])
{
if(RandomKey > this->m_iGamblingOptionRate[1] + this->m_iGamblingOptionRate[0])
{
if(RandomKey > this->m_iGamblingOptionRate[2] + this->m_iGamblingOptionRate[1] + this->m_iGamblingOptionRate[0])
{
if(RandomKey <= this->m_iGamblingOptionRate[3] + this->m_iGamblingOptionRate[2] + this->m_iGamblingOptionRate[1] + this->m_iGamblingOptionRate[0])
{
Option = 4;
}
}
else
{
Option = 3;
}
}
else
{
Option = 2;
}
}
else
{
Option = 1;
}
}
if ( this->m_iGamblingItems[StartGroup][ItemIndex].btExOption )
{
if ( this->m_iGamblingItemOptionRate[StartGroup].iExOptionRate >= GetLargeRand() % 1000000 )
{
Excellent = 1;
Luck = 0;
Skill = 1;
Level = 0;
}
}
}
else
{
Level = 0;
Skill = 1;
Luck = 0;
RandomKey = GetLargeRand()%1000000;
if(RandomKey > this->m_iGamblingOptionRate[0])
{
if(RandomKey > this->m_iGamblingOptionRate[1] + this->m_iGamblingOptionRate[0])
{
if(RandomKey > this->m_iGamblingOptionRate[2] + this->m_iGamblingOptionRate[1] + this->m_iGamblingOptionRate[0])
{
if(RandomKey <= this->m_iGamblingOptionRate[3] + this->m_iGamblingOptionRate[2] + this->m_iGamblingOptionRate[1] + this->m_iGamblingOptionRate[0])
{
Option = 4;
}
}
else
{
Option = 3;
}
}
else
{
Option = 2;
}
}
else
{
Option = 1;
}
RandomKey = GetLargeRand()%1000000;
if(RandomKey > this->m_iGamblingExcOptionRate[0])
{
if(RandomKey > this->m_iGamblingExcOptionRate[1] + this->m_iGamblingExcOptionRate[0])
{
if(RandomKey > this->m_iGamblingExcOptionRate[2] + this->m_iGamblingExcOptionRate[1] + this->m_iGamblingExcOptionRate[0])
{
if(RandomKey <= this->m_iGamblingExcOptionRate[3] + this->m_iGamblingExcOptionRate[2] + this->m_iGamblingOptionRate[1] + this->m_iGamblingExcOptionRate[0])
{
this->GetExcellentOption(4);
}
}
else
{
this->GetExcellentOption(3);
}
}
else
{
this->GetExcellentOption(2);
}
}
else
{
this->GetExcellentOption(1);
}
switch ( pos )
{
case 0:
ItemNumber = ItemGetNumberMake(3,11);
break;
case 2:
ItemNumber = ItemGetNumberMake(5,33);
break;
case 4:
ItemNumber = ItemGetNumberMake(4,24);
break;
case 6:
ItemNumber = ItemGetNumberMake(2,18);
break;
case 24:
ItemNumber = ItemGetNumberMake(5,34);
break;
}
}
ItemSerialCreateSend(aIndex, 238, 0, 0, ItemNumber, Level, 255, Skill, Luck, Option, aIndex, Excellent, 0);
LogAddTD("[Gambling][Try Gambling] (%s)(%s) Try item: %d Level:%d op1:%d op2:%d op3:%d ExOp:%d",
gObj[aIndex].AccountID, gObj[aIndex].Name, ItemNumber, Level, Skill, Luck, Option, Excellent);
}
return 1;
}