Author Topic: Choosing items simi-randomly - Solved - { By Danmer }  (Read 1831 times)

0 Members and 1 Guest are viewing this topic.

Offline Hacktank

  • Camper
  • ***
  • Posts: 462
  • Soldat Scripter
    • HTZRPG
Choosing items simi-randomly - Solved - { By Danmer }
« on: February 09, 2009, 03:46:28 am »
I am having another problem for a new skill in my rpg script : Loot Items. When you kill an enemy it will preform FLootItem but I cannot figure out how to pick an item for it to drop. I know it would be easy to do if all items had the same drop-chance but they dont, their drop chace should be goverened by constants.

I have this so far: (only going to show for 2 items)

Code: [Select]
procedure FLootItem(ID, enemy: byte);
var loot: byte; degals, mp5, ak47, styer, spaz, ruger, m79, barret, minimi, minigun, socom, knife, chainsaw, law, medkit, nade, pred, berserk, flamer, vest, cluster: integer;
begin
pred := random(0,Cpred);
barret := random(0,Cbarret);
Spawnobject(getplayerstat(enemy,'x'),getplayerstat(enemy,'y'),loot);
//WriteConsole(ID,'Looted -:- ' + -------,$ff5555ff);
end;

Cpred is 4 and Cbarret is 5.

Is there a way to fid the largets variable? Like Largest(pred,barret) would return pred if the random came out to be pred=3 and barret=2? If not how would i do what i want to do?

Thank you.
« Last Edit: February 12, 2009, 01:39:05 am by Hacktank »


Offline danmer

  • Camper
  • ***
  • Posts: 466
  • crabhead
Re: Choosing items simi-randomly
« Reply #1 on: February 09, 2009, 05:15:03 am »
thats what i use
Code: [Select]
procedure DropLoot(ID,Killer: byte);
var rand,drop: byte;
    loot: string;
begin
    if (Random(0,100) <= DROPCHANCE) then begin
    loot := '';
    drop := 0;
    rand := Random(0,100);
    case Bots[ID].Species of
      1: begin
        if rand >= 75 then begin
          if rand >= 85 then begin
            if rand >= 90 then begin
              drop := 5;
           
            end else drop := 12;
          end else drop := 11;
        end;
      end;
     
      2: begin
        if rand >= 20 then begin
          if rand >= 70 then begin
            if rand >= 85 then begin
              drop := 19;
           
            end else drop := 17;
          end else drop := 16;
        end;
      end;
     
      3: begin
        if rand >= 83 then begin
          drop := 21;
        end;
      end;
     
      4: begin
        drop := rand DIV 4;
        if (drop > 22) or (drop = 15) or (drop = 18) or (drop = 20) then drop := 0;
      end;
     
     
      6: drop := 19;
     
    end;   
   
    case drop of
      1: loot := 'Desert Eagle';
      2: loot := 'HK MP5';
      3: loot := 'AK 74';
      4: loot := 'Steyr AUG';
      5: loot := 'Spas 12';
      6: loot := 'Ruger77';
      7: loot := 'M79';
      8: loot := 'Barrett M82A1';
      9: loot := 'Minimi';
      10: loot := 'Minigun';
      11: loot := 'USSOCOM';
      12: loot := 'Combat Knife';
      13: loot := 'Chainsaw';
      14: loot := 'LAW';
      15: loot := 'Stationary Gun';
      16: loot := 'Medical Kit';
      17: loot := 'Grenade Kit';
      18: loot := 'Flamer Kit';
      19: loot := 'Vest Kit';
      20: loot := 'Predator Kit';
      21: loot := 'Berserk Kit';
      22: loot := 'Cluster Kit';
    end;

    if drop > 0 then begin
      if Bots[ID].Species = 6 then GiveBonus(Killer,3) else
        SpawnObject(GetPlayerStat(ID,'x'),GetPlayerStat(ID,'y'),drop);
      WriteConsole(Killer,'Looted: '+loot,cLoot);
    end;
   
  end;
end;
really basic, i think youll figgure it out

Offline JFK

  • Camper
  • ***
  • Posts: 255
    • My TraxInSpace Account
Re: Choosing items simi-randomly
« Reply #2 on: February 09, 2009, 04:12:53 pm »
I think damner knows better about what 'looting items' should be. But as far as I can see you just need a weighted random function. It can be handy in other cases, so I'll post it anyway.

Code: [Select]
//weighted random

function Wrand(Input : array of integer) : integer;
var
i, total, randi : integer;
begin
  result := 0;
  total := 0;
  for i:= 0 to getArrayLength(Input)-1 do inc(total, Input[i]);
  randi := random(0, total);
  total := 0;
  for i:= 0 to getArrayLength(Input)-1 do begin
    inc(total, Input[i]);
    if total >= randi then begin
      result := i;
      break;
    end;
  end;
end;

Haven't tested it :p but it should swallow an array of ints that represent the chances of the outcome. For example, inputing [4, 3, 5] will have a 4/12th chance of hitting 0, 3/12th chance of 1 and 5/12 chance of 2. You can input as much integers as you like.
Come join: EliteCTF
Listen to: My Music

Offline danmer

  • Camper
  • ***
  • Posts: 466
  • crabhead
Re: Choosing items simi-randomly
« Reply #3 on: February 09, 2009, 05:15:09 pm »
I think damner knows better about what 'looting items' should be. But as far as I can see you just need a weighted random function. It can be handy in other cases, so I'll post it anyway.

Code: [Select]
//weighted random

function Wrand(Input : array of integer) : integer;
var
i, total, randi : integer;
begin
  result := 0;
  total := 0;
  for i:= 0 to getArrayLength(Input)-1 do inc(total, Input[i]);
  randi := random(0, total);
  total := 0;
  for i:= 0 to getArrayLength(Input)-1 do begin
    inc(total, Input[i]);
    if total >= randi then begin
      result := i;
      break;
    end;
  end;
end;

Haven't tested it :p but it should swallow an array of ints that represent the chances of the outcome. For example, inputing [4, 3, 5] will have a 4/12th chance of hitting 0, 3/12th chance of 1 and 5/12 chance of 2. You can input as much integers as you like.
that, sir, is awesome.

Although a simple random and a couple of nested ifs might be more efficient if you only have three options to choose from. But im defo considering using this :>

Offline Hacktank

  • Camper
  • ***
  • Posts: 462
  • Soldat Scripter
    • HTZRPG
Re: Choosing items simi-randomly
« Reply #4 on: February 09, 2009, 05:20:31 pm »
thats what i use
Code: [Select]
procedure DropLoot(ID,Killer: byte);
var rand,drop: byte;
    loot: string;
begin
    if (Random(0,100) <= DROPCHANCE) then begin
    loot := '';
    drop := 0;
    rand := Random(0,100);
    case Bots[ID].Species of
      1: begin
        if rand >= 75 then begin
          if rand >= 85 then begin
            if rand >= 90 then begin
              drop := 5;
           
            end else drop := 12;
          end else drop := 11;
        end;
      end;
     
      2: begin
        if rand >= 20 then begin
          if rand >= 70 then begin
            if rand >= 85 then begin
              drop := 19;
           
            end else drop := 17;
          end else drop := 16;
        end;
      end;
     
      3: begin
        if rand >= 83 then begin
          drop := 21;
        end;
      end;
     
      4: begin
        drop := rand DIV 4;
        if (drop > 22) or (drop = 15) or (drop = 18) or (drop = 20) then drop := 0;
      end;
    
     
      6: drop := 19;
     
    end;   
  
    case drop of
      1: loot := 'Desert Eagle';
      2: loot := 'HK MP5';
      3: loot := 'AK 74';
      4: loot := 'Steyr AUG';
      5: loot := 'Spas 12';
      6: loot := 'Ruger77';
      7: loot := 'M79';
      8: loot := 'Barrett M82A1';
      9: loot := 'Minimi';
      10: loot := 'Minigun';
      11: loot := 'USSOCOM';
      12: loot := 'Combat Knife';
      13: loot := 'Chainsaw';
      14: loot := 'LAW';
      15: loot := 'Stationary Gun';
      16: loot := 'Medical Kit';
      17: loot := 'Grenade Kit';
      18: loot := 'Flamer Kit';
      19: loot := 'Vest Kit';
      20: loot := 'Predator Kit';
      21: loot := 'Berserk Kit';
      22: loot := 'Cluster Kit';
    end;

    if drop > 0 then begin
      if Bots[ID].Species = 6 then GiveBonus(Killer,3) else
        SpawnObject(GetPlayerStat(ID,'x'),GetPlayerStat(ID,'y'),drop);
      WriteConsole(Killer,'Looted: '+loot,cLoot);
    end;
   
  end;
end;
really basic, i think youll figgure it out

Thanks Guys! Also how do you do your bot changeing danmer? Do you just have a bunch of sets of zoms or what?

Im not sure wich one i will use, maby yours JFK. :)


Offline danmer

  • Camper
  • ***
  • Posts: 466
  • crabhead
Re: Choosing items simi-randomly
« Reply #5 on: February 10, 2009, 12:16:32 am »
Also how do you do your bot changeing danmer? Do you just have a bunch of sets of zoms or what?
its a huge function with a lot of different stuff... lots of magic involved :F