Author Topic: ghoul balance system [solved]  (Read 778 times)

0 Members and 1 Guest are viewing this topic.

Offline y0uRd34th

  • Camper
  • ***
  • Posts: 325
  • [i]Look Signature![/i]
ghoul balance system [solved]
« on: December 03, 2009, 11:13:49 am »
Well, i hope you can understand what i wrote.. :D

all you need to know goes here:
It will be executed whenever a human joins a team or leaves the game.
So the basics:
first of all there are at least 4 ghouls (min_ghouls) and for every player on the server would be
2 more ghouls. When there are 2 people and one quits, 2 ghouls would be kicked.
The species[]: need: number of players requiered to spawn at least one of this ghoul type.
So as you can read out of the imploding ghoul there would be requiered 3 players, when so, one ghoul will be kicked and the imploding ghoul added. And again, when a guy leaves, the imploding ghoul will be kicked and the normal ghoul joins again.

5 minutes ago i tested if any bots would join, but nothing happened, the server console just told me (OnJoinGame) Division by zero, same for OnLeaveGame.

As far as i can think, the basics here should work so far (I hope so ::))

Thanks alot for helping!
If you dont use notepad++ some lines are longer or shorter than normal because it adds TABs from itself and their longer in normal notepad or others..

Code: (pascal) [Select]
const
// zombie related stuff
min_ghouls      = 4;
GhoulsPerPlayer = 2;
BotSpecies = 2;

// teams
GhoulTeam = 1;
HumanTeam = 2;

type
TBot = Record
  species, armor, chest: integer; // Species; 1: Ghoul; 2: Imploding Ghoul;
  kick: boolean;
end;

TSpecies = Record
  armor, chest, need: integer;
  name: string;
end;

var
Bot: Array [1..30] of TBot;
species: array [1..BotSpecies] of TSpecies;


function HighestID(): byte;
var i: byte;
begin
  for i := 1 to 30 do
    if GetPlayerStat(i,'Active') = true then
  Result := i;
end;

procedure SetSpecies();
begin
  species[1].name  := 'Ghoul';
  species[1].armor := 8000;
  species[1].chest := 10;
  species[1].need  := 1;
  species[2].name  := 'Imploding Ghoul';
  species[2].armor := 12000;
  species[2].chest := 30
  species[2].need  := 3;
end;

function GetBotSpecies(ID: byte): integer;
var i: byte;
begin
  for i := 1 to BotSpecies do begin
    if IDToName(ID) = species[i].name then
Result := i;
  end;
end;

function GetBotCount(Specie: integer): byte;
var i: byte;
begin
  Result := 0;
  for i := 1 to HighestID do begin
    if GetPlayerStat(i,'Active') = true then
  if GetPlayerStat(i,'Human') = false then
    if IDToName(i) = species[Specie].name then
      Result := Result + 1;
  end;
end;

function GetBot(Specie: integer): integer;
var i: byte;
begin
  for i := 1 to HighestID do begin
    if GetPlayerStat(i,'Active') = true then
  if GetPlayerStat(i,'Human') = false then
    if IDToName(i) = species[specie].name then begin
  Result := i;
  break;
end;
  end;
end;

procedure CalculateGhouls();
var botcount, num: array [1..BotSpecies] of integer; humans, i, j, present, ghouls : integer;
begin
  humans := 0;
  present := 0;
  for i := 1 to BotSpecies do
    botcount[i] := GetBotCount(i);

  for i := 1 to HighestID do begin
    if GetPlayerStat(i,'Active') = true then
  if GetPlayerStat(i,'Human') = true then
    humans := humans + 1;
  end;
 
  ghouls := GhoulsPerPlayer * humans + min_Ghouls;
 
  for i := 1 to BotSpecies do
    present := present + botcount[i];

  // less bots then minimal number of them? set it to the minimum number
  if present < min_ghouls then
    present := min_ghouls;

// not enough bots on server for humans? add them
  if present < ghouls then
    for i := 1 to ghouls - present do begin
      Command('/addbot' + IntToStr(GhoulTeam) + ' ' + species[1].name);
  botcount[1] := botcount[1] + 1;
 
  // too much bots per player there? kick them to balance
  if present > ghouls then
    for i := 1 to present - ghouls do begin
  j := GetBot(1);
      KickPlayer(j);
  botcount[1] := botcount[1] - 1;
end;
  end;

  if humans > 0 then begin
 
    // setting up the number of bots required to add
    for i := 1 to BotSpecies do begin
  if humans > species[i].need then
    num[i] := humans div species[i].need;
end;

// now add or kick them
for i := 1 to BotSpecies do begin
      // not enough bots there? add them
  if botcount[i] < num[i] then
    while botcount[i] < num[i] do begin
  Command('/addbot' + IntToStr(GhoulTeam) + ' ' + species[i].name);
  j := GetBot(1);
  KickPlayer(j);
  botcount[i] := botcount[i] + 1;
  botcount[1] := botcount[1] - 1;
end;
      // too much on the server? kick them
  if botcount[i] > num[i] then
    while botcount[i] > num[i] do begin
  j := GetBot(i);
  KickPlayer(j);
  botcount[i] := botcount[i] - 1;
  if i > 1 then begin
    Command('/addbot' + IntToStr(GhoulTeam) + ' ' + species[1].name);
botcount[1] := botcount[1] + 1;
      end;
end;
    end;

  end;
end;

procedure ActivateServer();
begin
  SetSpecies;
end;

procedure OnJoinTeam(ID, team: byte);
begin
  calculateghouls;
end;

procedure OnLeaveGame(ID, team: byte; kicked: boolean);
begin
  calculateghouls;
end;

« Last Edit: December 04, 2009, 04:13:49 pm by y0uRd34th »

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: ghoul balance system
« Reply #1 on: December 03, 2009, 01:37:15 pm »
A quick glance makes me suggest
Code: [Select]
num[i] := humans div species[i].need;check if species[ i ].need is 0.

Offline y0uRd34th

  • Camper
  • ***
  • Posts: 325
  • [i]Look Signature![/i]
Re: ghoul balance system
« Reply #2 on: December 03, 2009, 02:16:48 pm »
It cant be 0 because i goes from 1 to GhoulSpecies (2) and species[1,2].need are 1,3 :/ but im testing it with WriteLn() fast

okey, forgot to add SetSpecies; into ActivateServer;
Well, now it works, but adds infinite ghouls to the server

this is kinda complicated; please post any ideas you got how you would think to make this.
« Last Edit: December 03, 2009, 02:29:34 pm by y0uRd34th »

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: ghoul balance system
« Reply #3 on: December 03, 2009, 04:23:58 pm »
Maybe OnJoinGame and OnLeaveGame check if its a bot joining or leaving, but I would guess calculategouls() should work properly without that.

Offline y0uRd34th

  • Camper
  • ***
  • Posts: 325
  • [i]Look Signature![/i]
Re: ghoul balance system
« Reply #4 on: December 04, 2009, 04:13:38 pm »
Solved with help, was alot easier than i made it up there...