ПЗ (1229304), страница 15
Текст из файла (страница 15)
return a;
}
//----------------------------------------------------------------------------
int cur_index(int start_index)
{
// start_index - это число присваемое сервером при продключении
int index = start_index;
for (int i = 0; i <= start_index; i++)
{
if (dead[i] == -1)
index--; // уменьшаем до кол-ва отключенных
}
return index;
}
//----------------------------------------------------------------------------
int Get_NonDead_List()
{
int count = 0;
for (int i = 0; i < quantity_of_users; i++)
{
if (dead[i] != -1)
{
count++;
}
}
return count;
}
//----------------------------------------------------------------------------
void clear_var()
{
for (int i = 0; i < quantity_of_users; i++)
{
ArrEnter[i].Number_Client = -1; // ставим признак не участвующего, а когда клиент
//отправит отчет, инциализируется своим номером.
// Делаем так, что бы можно было проверить те кте не отправил отчет во время.
ArrEnter[i].cur_cash = 0;
ArrEnter[i].inv = 0;
ArrEnter[i].pow_cur_lev = 0;
ArrEnter[i].SRW_level = 0;
ArrEnter[i].SRW_inc_price = 0;
ArrEnter[i].inv_SRW = 0;
ArrEnter[i].marketing = 0;
ArrEnter[i].part_mark = 0;
ArrEnter[i].planed_output = 0;
ArrEnter[i].pow_use_lev = 0;
ArrEnter[i].trg_inn_price = 0;
ArrEnter[i].cur_inn_price = 0;
ArrEnter[i].trg_output = 0;
ArrEnter[i].pow_load = 0;
ArrEnter[i].cur_amort_perc = 0;
ArrEnter[i].cur_occupy = 0;
ArrEnter[i].step_occupy = 0;
ArrEnter[i].repayment = 0;
ArrEnter[i].attractivness = 0;
ArrEnter[i].part_attr = 0;
ArrEnter[i].price_prod = 0;
ArrEnter[i].store = 0;
ArrEnter[i].gross_profit = 0;
ArrEnter[i].part_gross_profit= 0;
ArrEnter[i].sold = 0;
ArrEnter[i].part_sold = 0;
ArrEnter[i].NoBuy = 0;
}
}
//----------------------------------------------------------------------------
void* MakeBuf(int * pdatasz, UnicodeString ustr)
{
void* pBuf = 0;
int length = ustr.Length();
*pdatasz = sizeof(length)+sizeof(wchar_t)*(length);
pBuf = malloc(*pdatasz);
*((int*)pBuf)=length;
for (int i=0; i < length; i++) {
((wchar_t*)(((int*)(pBuf))+1))[i]=ustr[i+1];
}
return pBuf;
};
//----------------------------------------------------------------------------
void ReadUSFromBuf(TCustomIpClient *ClientSocket, UnicodeString* p_result_string)
{
int length = 0;
ClientSocket->ReceiveBuf(&length,sizeof(int),0);
length++;
wchar_t * fff = new wchar_t [length];
memset(fff,0,length*sizeof(wchar_t));
ClientSocket->ReceiveBuf(fff,length*sizeof(wchar_t),0);
*p_result_string = fff;
delete[]fff;
};
//----------------------------------------------------------------------------
void Send_to_Client(UnicodeString Host, UnicodeString Port, UnicodeString str)
{
Form1->TcpClient1->RemoteHost = Host;
Form1->TcpClient1->RemotePort = Port;
int datasz = 0;
void* pBuf = MakeBuf(&datasz,str);
Form1->TcpClient1->Active = true;
if (Form1->TcpClient1->Connect())
{
Form1->TcpClient1->SendBuf(pBuf, datasz ,0);
Form1->TcpClient1->Active = false;
}
free(pBuf);
}
//----------------------------------------------------------------------------
Модуль UMainModule
//---------------------------------------------------------------------------
#pragma hdrstop
#include <math.h>
#include "UMainModule.h"
#include "UServer.h"
#include "UFuncList.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
void bank_operations()
{
for (int i = 0; i < quantity_of_users; i++)
{
if (dead[i] != -1) // необрабатываем не участвующих в симуляции
{
if (ArrEnter[i].cur_occupy < ArrEnter[i].repayment) // если клиент вернул банку больше чем
{ // нужно, то задолжность погашается,
ArrEnter[i].cur_cash -= ArrEnter[i].repayment; // и остальная наличность возвращается
ArrEnter[i].repayment -= ArrEnter[i].cur_occupy; // клиенту
ArrEnter[i].cur_cash += ArrEnter[i].repayment;
ArrEnter[i].repayment = ArrEnter[i].cur_occupy; // сколько вернули банку
ArrEnter[i].cur_occupy = 0;
}
else
{
ArrEnter[i].cur_cash -= ArrEnter[i].repayment;
ArrEnter[i].cur_occupy -= ArrEnter[i].repayment;
ArrEnter[i].cur_occupy += 1.0*ArrEnter[i].cur_occupy * ArrEnter[i].bank_perc/100;
}
}
}
func_log("bank_operations");
}
//---------------------------------------------------------------------------
void recount_fonds()
{
for (int i = 0; i < quantity_of_users; i++)
{
if (dead[i] != -1) // необрабатываем не участвующих в симуляции
{
ArrEnter[i].cur_cash -= ArrEnter[i].inv;
ArrEnter[i].pow_cur_lev += 1.0*ArrEnter[i].inv/ArrEnter[i].pow_inc_price;
}
}
func_log("recount_fonds");
}
//---------------------------------------------------------------------------
void recount_NIR()
{
for (int i = 0; i < quantity_of_users; i++)
{
if (dead[i] != -1) // необрабатываем не участвующих в симуляции
{
ArrEnter[i].cur_cash -= ArrEnter[i].inv_SRW;
ArrEnter[i].SRW_level += 1.0*ArrEnter[i].inv_SRW/ArrEnter[i].SRW_inc_price;
ArrEnter[i].SRW_inc_price = ArrEnter[i].SRW_start_inc_price + ArrEnter[i].SRW_level;
}
}
func_log("recount_NIR");
}
//---------------------------------------------------------------------------
void recount_marketing()
{
for (int i = 0; i < quantity_of_users; i++)
{
if (dead[i] != -1) // необрабатываем не участвующих в симуляции
{
ArrEnter[i].cur_cash -= ArrEnter[i].marketing;
}
}
func_log("recount_marketing");
}
//---------------------------------------------------------------------------
void compute_output()
{
for (int i = 0; i < quantity_of_users; i++)
{
if (dead[i] != -1) // необрабатываем не участвующих в симуляции
{
ArrEnter[i].trg_inn_price = 0.2*ArrEnter[i].st_inner_price+((0.8*ArrEnter[i].st_inner_price)/(1+ ArrEnter[i].SRW_level + 0.3));
ArrEnter[i].cur_inn_price = ArrEnter[i].trg_inn_price+exp(-1*(ArrEnter[i].pow_cur_lev*ArrEnter[i].pow_cur_lev)/(4000000));
if (ArrEnter[i].planed_output > ArrEnter[i].pow_cur_lev) // на тот случай если мощность
ArrEnter[i].planed_output = ArrEnter[i].pow_cur_lev; // превышает выпуск продукции
// расчитываем стоимость всей продукции
ArrEnter[i].trg_output = ArrEnter[i].cur_inn_price * ArrEnter[i].planed_output;
if (ArrEnter[i].cur_cash < ArrEnter[i].trg_output)
{
// выполняется при недостаточной текущей наличности
if (ArrEnter[i].trg_output > (ArrEnter[i].max_occupy + ArrEnter[i].cur_cash - ArrEnter[i].cur_occupy))
{
// Если стоимость запланированного выпуска превышает тек. наличности и
// макс. займ у банка то выпуск расчитываем на те средства на кот.
// игрок может претендовать.
// расчитали выпуск продукции
ArrEnter[i].planed_output =
1.0*((ArrEnter[i].cur_cash + (ArrEnter[i].max_occupy - ArrEnter[i].cur_occupy))/ArrEnter[i].cur_inn_price);
// расчитали стоимость выпуска продукции
ArrEnter[i].trg_output = ArrEnter[i].cur_inn_price * ArrEnter[i].planed_output;
// и теперь долг банку составляет max_occupy т.е максимальный,
// а наличности нет.
ArrEnter[i].step_occupy = abs(ArrEnter[i].cur_cash - ArrEnter[i].trg_output);
ArrEnter[i].cur_occupy = ArrEnter[i].cur_occupy + ArrEnter[i].step_occupy;
ArrEnter[i].cur_cash = 0;
}
else
{
// иначе просто увиличивается задолжность игрока
ArrEnter[i].step_occupy = 1.0*abs(ArrEnter[i].cur_cash - ArrEnter[i].trg_output);
ArrEnter[i].cur_occupy = 1.0*ArrEnter[i].cur_occupy + ArrEnter[i].step_occupy;
ArrEnter[i].cur_cash = 0;
}
}
else
{
ArrEnter[i].cur_cash -= ArrEnter[i].trg_output;
}
ArrEnter[i].pow_use_lev = ArrEnter[i].planed_output; // кол-во товара было выпущено
ArrEnter[i].store += ArrEnter[i].pow_use_lev; // на складе прибавилось продукции
}
}
func_log("compute_output");
}
//---------------------------------------------------------------------------
void compute_interest()
{
for (int i = 0; i < quantity_of_users; i++)
{
if (dead[i] != -1) // необрабатываем не участвующих в симуляции
{
ArrEnter[i].attractivness = pow(ArrEnter[i].SRW_level,(0.75)) * ArrEnter[i].marketing / (ArrEnter[i].price_prod+1);
}
}
func_log("compute_interest");
}
//---------------------------------------------------------------------------
void compute_market_condition()
{
int sum_marketing=0;
V_market = V_market + (V_market*USD/100);
Mas_V = new int[V_market];
for (int i=0; i<V_market;i++)
Mas_V[i] = (58000/V_market)*i+2000; //заполняем массил ожиданий
for (int i=0; i < quantity_of_users; i++)
{
if (dead[i] != -1) // необрабатываем не участвующих в симуляции
{
sum_marketing = sum_marketing + ArrEnter[i].marketing; //сумма всех маркетингов
}
}
if (sum_marketing == 0) sum_marketing = 1; // чтоб не получить деление на 0
// разделение рынка по маркетингу
for (int i = 0; i < quantity_of_users; i++)
{
if (dead[i] != -1) // необрабатываем не участвующих в симуляции
{
ArrEnter[i].part_mark = V_market * ArrEnter[i].marketing/sum_marketing; //предварительная доля
}
}
func_log("compute_market_condition");
}
//---------------------------------------------------------------------------
void Selles(int i, int part)
{
randomize();
for (int j=0; j<part;j++)
{
int r = random(V_market);
if (Mas_V[r] <= ArrEnter[i].attractivness)
{
if ((Mas_V[r]!=0) || (Mas_V[r]!=-1))
{
if (ArrEnter[i].store == 0) break; // выходим из цикла если на складе нет продукции
Mas_V[r] = 0; //спрос удовлетворен
ArrEnter[i].gross_profit += ArrEnter[i].price_prod; //наличность увеличилась на ед. прод.
ArrEnter[i].sold += 1; // кол-во продукции увеличивается на 1
ArrEnter[i].store -= 1; //склад игрока уменьшился
}
}
else
{
Mas_V[r] = -1; // спрос не удовлетворен - Это неудовлетворенные ожидания
ArrEnter[i].NoBuy += 1;
}
}
func_log("Selles");
}
//---------------------------------------------------------------------------
void ReNew_Market()
{
int sum_attractivness = 0;
int notbuy = 0; // кол-во не купивших товар покупателей
for (int i = 0; i < V_market; i++)
{
if ((Mas_V[i] == -1))
{
Mas_V[i] = (58000/V_market)*i+2000;
notbuy++;
}
}
//сумма всех значений привлекательности
for (int i = 0; i < quantity_of_users; i++)
{
if (dead[i] != -1) // необрабатываем не участвующих в симуляции
{
sum_attractivness += ArrEnter[i].attractivness;
}
}
if (sum_attractivness == 0) sum_attractivness = 1;
// разделение рынка по привлекательности
for (int i = 0; i < quantity_of_users; i++)
{
if (dead[i] != -1) // необрабатываем не участвующих в симуляции
{
ArrEnter[i].part_attr =notbuy * ArrEnter[i].attractivness/sum_attractivness;
}
}
func_log("ReNew_Market");
}
//---------------------------------------------------------------------------
void compute_sells()
{
//применение гаммы функции первый раз
for (int i = 0; i < quantity_of_users; i++)















