This site is a testing version, but all data is shared with the live forum.


Raised This Month: $ Target: $400
 0% 

Help me at making percentages


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Furibaito
Member
Join Date: Aug 2012
Location: Colossal Titan
Old 07-04-2013 , 00:25   Help me at making percentages
Reply With Quote #1

Hello, I'm trying to make players get random models every spawn with a percentage. So like :
50% using MODEL 1
25% using MODEL 2
25% using MODEL 3

This is easy to do like this

PHP Code:
public Action:PlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast)
{
    
    ...
    
    new 
RandomNumber GetRandomInt(1100);
    
    if (
RandomNumber <= 50 && RandomNumber 0)
    {
        
SetEntityModel(client"model_1.mdl"
    
}
    
    else if (
RandomNumber <= 75 && RandomNumber 50)
    {
        
SetEntityModel(client"model_2.mdl"
    
}
    
    else if (
RandomNumber <= 100 && RandomNumber 75)
    {
        
SetEntityModel(client"model_3.mdl"
    
}
    
    ...

But now the problem, I want to set the percentage dynamic. So like a ConVar or KeyValues can control the chances of those models. So for example like 4 models with 10%, 20%, 60%, 10% or more. (I set the max to 20)

Is this possible? If it is I'm pretty sure that I would need to use a loop. But I doesn't really know how to use loop in this kind of situation. Any help would be very appreciated
__________________
フリー

Currently making : ZRiot - like mod in CS:GO - - 45%

Can do private plugins or private community maps for small money.
Furibaito is offline
thetwistedpanda
Good Little Panda
Join Date: Sep 2008
Old 07-04-2013 , 01:53   Re: Help me at making percentages
Reply With Quote #2

An inefficient method, but one I'm quite partial to, is to assign a "weight" to each element and throw all elements into an array. The "weight" is how many times the element is thrown into the array. Of course for your application this isn't ideal, but hey, why not!

PHP Code:
new iModels 3;
new 
String:sModels[iModels][PLATFORM_MAX_PATH] = { "model1.mdl""model2.mdl""model3.mdl" };
new 
iWeights[iModels] = { 514};

new 
Handle:g_hArray CreateArray(64);
for(new 
0iModelsi++)
{
  for(new 
0iWeights[i]; j++)
  {
    
PushArrayString(g_hArraysModels[i]);
  }
}

new 
iMax GetArraySize(g_hArray);
new 
iRandom GetRandomInt(0iMax 1);
new 
String:sModel[PLATFORM_MAX_PATH];
GetArrayString(g_hArrayiRandomsModelsizeof(sModel));

model1 has 5/20 chancemodel2 has 14/20 chance, and model 3 has 1/20 chance
__________________
thetwistedpanda is offline
Bimbo1
Senior Member
Join Date: Jan 2010
Location: brazil
Old 07-04-2013 , 02:10   Re: Help me at making percentages
Reply With Quote #3

i've thought of it this way. i'm not sure if it works, as i didn't test it and my brain usually fails a lot.
Code:
#include <sourcemod>
#include <cstrike>
#include <sdktools>
#include <sdkhooks>
#include <smlib>
#include <smartdm>

#pragma semicolon 1

new maxnumber = 0;
new String:modelpath[32][64];
new modelnumber[32];

public OnPluginStart(){

	HookEvent("player_spawn", OnPlayerSpawn);
	LoadModels();

}

public OnMapStart(){

	LoadModels(true);

}

LoadModels(bool:preload = false){

	new Handle:file = OpenFile("cfg/models.txt", "rt");
	decl String:buffer[128], String:data[2][64];
	new count = 0;
	while(!IsEndOfFile(file) && ReadFileLine(file, buffer, 128)){

		ExplodeString(buffer, " ", data, 2, 64);
		maxnumber += StringToInt(data[0]);
		modelnumber[count] = maxnumber;
		strcopy(modelpath[count], 64, data[1]);
		PrecacheModel(modelpath[count], preload);
		Downloader_AddFileToDownloadsTable(modelpath[count]);
		count++;

	}
	CloseHandle(file);

}

public OnPlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast){

	if(maxnumber == 0){

		return;

	}
	new random = GetRandomInt(1, maxnumber);
	for(new i = 0; i < 32; i++){

		if(random <= modelnumber[i]){

			SetEntityModel(GetClientOfUserId(GetEventInt(event,"userid")), modelpath[i]);
			break;

		}

	}
	return;

}
cfg/models.txt:
Code:
45 hatsunemiku.mdl
44 kagaminelen.mdl
1 okumurarin.mdl
the sum doesn't have to be 100, it can be anything.
edit: i forgot closing the handle. i always forget that.
edit2: why the hell modelnumber was defined as a string?
__________________
sie sind das essen und wir sind die jäger!

Last edited by Bimbo1; 07-04-2013 at 02:16.
Bimbo1 is offline
Furibaito
Member
Join Date: Aug 2012
Location: Colossal Titan
Old 07-04-2013 , 03:52   Re: Help me at making percentages
Reply With Quote #4

Thank you for the both reply. I've learned something today

Quote:
Originally Posted by Bimbo1 View Post
sie sind das essen und wir sind die jäger!
__________________
フリー

Currently making : ZRiot - like mod in CS:GO - - 45%

Can do private plugins or private community maps for small money.
Furibaito is offline
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 -4. The time now is 12:41.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode