darksoke

[Release] PHP char/acc/creature/item flags parser script

4 posts in this topic

Well I've been searching everywhere on the internet for a damn hex to dec comparison script and myeah i failed so that was the moment when i remember I've used to learn mathematics in high school so i spent few minutes to create some basic combination functions.

 

<?php    

// For this example I took the data for `characters`.`characters` => extra_flags
// The following function can be used to check whatever flags you want as much as you respect the structure of the given arrays

$flagnames = array(
    "PLAYER_EXTRA_GM_ON", //flag number 1
    "PLAYER_EXTRA_GM_ACCEPT_TICKETS", //flag number 2
    "PLAYER_EXTRA_ACCEPT_WHISPERS", //flag number 4
    "PLAYER_EXTRA_TAXICHEAT", //flag number 8
    "PLAYER_EXTRA_GM_INVISIBLE", //flag number 16
    "PLAYER_EXTRA_GM_CHAT", //flag number 32
    "PLAYER_EXTRA_HAS_310_FLYER", //flag number 64
    "PLAYER_EXTRA_PVP_DEATH" //flag number 256
);

$flagnumbers = array(1, 2, 4, 8, 16, 32, 64, 256);

echo hasFlag(65, "PLAYER_EXTRA_GM_ON", $flagnames, $flagnumbers).'<br><br>'; // Should print true
echo hasFlag(65, "PLAYER_EXTRA_GM_ACCEPT_TICKETS", $flagnames, $flagnumbers).'<br><br>'; // Should print false

function hasFlag($flags, $flagstr, $flagNames = array(), $flagNums = array()){
    // $flags -> The sum of flags retrieved from characters.characters database note is the decimal value not hexadecimal
    // $flagstr -> The flag we are checking if exist
    // $flagNames -> an array of flag names , you can define flag names however you want i decided to define them as default names
    // $flagNums -> numbers associated to every specific flag ex PLAYER_EXTRA_GM_ON = 1 (Took from trinitycore)
    //
    // return true if player has the flag, either return false
    //
    // Get all flags combinations
    $return = $this->CombineArrays($flagNames);
    
    // Get all flags decimal combinations
    $extra_return = $this->CombineArrays($flagNums);

    // Define the variables we are going to work with
    $newarray = array();
    $numb = 0;
    
    // Parse the combined arrays and merge them to a single array(['decNumber'],array(['arrayOfFlagNames']))
    for($i = 0; $i < count($return); $i++){
        foreach($extra_return[$i] as $dupe)
            $numb += $dupe;
        array_push($newarray, array("number" => $numb, "flags" => $return[$i]));
        $numb = 0;
    }
    
    // Parse the newly created array and check if if the combination of numbers and flag names exist in the new array
    foreach($newarray as $found)
        if($found['number'] == $flags)
            if(in_array($flagstr, $found['flags']))
                 return true;
    return false;
 }
function CombineArrays($in, $minLength = 1, $max = 2000) {
    // Check the length of the array we are going to combine
    $count = count($in);
    
    // Raise to a specific power to get every single possible combination
    $members = pow(2, $count);
    
    // Define the array we are going to work with, every combination will be stored here as an array of arrays
    $return = array();
    
    // Parse the array to get the real combinations 
    for($i = 0; $i < $members; $i ++) {
        $b = sprintf("%0" . $count . "b", $i);
        $out = array();
        for($j = 0; $j < $count; $j ++) {
            $b{$j} == '1' and $out[] = $in[$j];
        }
        // Check if array length is between the specified limits to save some memory
        count($out) >= $minLength && count($out) <= $max and $return[] = $out;
    }
    // End of default combination function 
    
    // Return the newly created array which contains every combination
    return $return;
}


 

This script is free to use if you know better implementation ways or better scripting method feel free to suggest or edit the script. All i ask if you do it please post it in here so other members can also use them. Thank you for checking this and have a good day/evening.

PS if i haven't commented enough on the script just tell me what and where you don't understand :))

Valtorei, Death and Unrecognized like this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.