Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 04-20-2007, 02:07 PM
Novo Novo is offline
[TServerDeveloper]
Join Date: Jun 2006
Posts: 448
Novo will become famous soon enough
Probabilities

PHP Code:
/****
  table: { {item, unit}, {item, unit}, ... }

(+) getRandom( table )
  - returns a given item from a list
(+) getProbability( table, item )
  - calculates the probability of a given outcome
****/

public function getRandomtable )
  {
  if ( 
table.type() != )
    return 
false;

  
temp.total 0;
  for ( 
temp.tableCelltable )
    
temp.total += temp.tableCell[1];

  
temp.random random(0temp.total);
  for ( 
temp.tableCelltable )
    {
    if ( 
temp.random temp.tableCell[1] )
      return 
temp.tableCell[0];

    
temp.random -= temp.tableCell[1];
    }
  return 
false;
  }

public function 
getProbabilitytablecell )
  {
  if ( 
table.type() != )
    return 
0;
  
  
temp.probability temp.total 0;
  for ( 
temp.tableCelltable )
    {
    
temp.total += temp.tableCell[1];
    if ( 
temp.tableCell[0] == cell )
      
temp.probability += temp.tableCell[1];
    }

  return ( 
temp.probability temp.total );
  } 
Basically uses a probability table with a randomizer.

This is useful when you have a list of objects that carry different weight... And you want to select a random object.

One classical example would be mining: The the higher classed minerals have a lower rate of obtaining it.

PHP Code:
mineTable = {
  {
"nothing"50},
  {
"iron"10},
  {
"gold"3},
  {
"diamond"1}
 } 
Nothing has a 50 in 64 chance of being seleted. iron, 10 in 64... etc etc.

I am planning on adding different probability functions to it later.
Reply With Quote
  #2  
Old 04-20-2007, 03:04 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Very nice Novo
__________________
Reply With Quote
  #3  
Old 04-20-2007, 06:55 PM
killerogue killerogue is offline
Registered Omega
killerogue's Avatar
Join Date: Apr 2006
Location: United States
Posts: 1,920
killerogue is on a distinguished road
Send a message via AIM to killerogue Send a message via MSN to killerogue
Excellent job here Novo!

It's really flexible in terms of uses. Like with the mine table or even a fish table.

But, a question, would running a for loop through your table and setting it as the second param net you different things from the table but make the most probable the one you get the most?

I'm at school and don't have the ability to test for another couple hours or so and was thinking about it, got very curious as to how I could work with that.

E: Also where'd you get the 64 max prob num from?
__________________


REMEMBER, IF YOU REP ME, LEAVE A NAME!

Quote:
Originally Posted by haunter View Post
Graal admins don't die. They go to hell and regroup.
Quote:
Originally Posted by Inverness View Post
Without scripters, your graphics and levels wouldn't do anything but sit there and look pretty.

Last edited by killerogue; 04-20-2007 at 07:14 PM..
Reply With Quote
  #4  
Old 04-20-2007, 07:26 PM
Chandler Chandler is offline
Banned
Join Date: Jan 2007
Posts: 656
Chandler will become famous soon enough
You released it! I like it, nice work!
Reply With Quote
  #5  
Old 04-20-2007, 08:16 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Quote:
Originally Posted by killerogue View Post
E: Also where'd you get the 64 max prob num from?
PHP Code:
mineTable = {
  {
"nothing"50},
  {
"iron"10},
  {
"gold"3},
  {
"diamond"1}
 } 
50 + 10 + 3 + 1 = 64

Probably from there
__________________
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 10:01 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.