Browsing articles tagged with " PHP"

CityGen: A random city block generator

May 3, 2011
Mark
Comments Off on CityGen: A random city block generator

CityGen was originally designed as a precursor to KORPG’s What’s at this Intersection?. It was also a proof of concept utility to generate images on the fly with PHP. Sound crazy? The overall idea was simple on paper but caused a lot of frustration during the implementation.

The premise of the idea was to randomly determine commercial, residential, and residential location types and present them as geomorph style buildings. The random portion was pretty trivial. Artwork isn’t something I do well. Kevin over at KORPG produced some great art for his variant. In the end, he couldn’t fold in the prototype I wrote and I’m unlikely to produce art. Result? Two variations of a general concept that are both useful.

I may get around to stripping out the on-the-fly image generation. It is not necessary for my simplistic variation. As Kevin has done, layout with tables using stock imagery is far faster. Unless you need to rotate, scale or do fancy stuff. Fancy is unlikely.


Comments Off on CityGen: A random city block generator

Labyrinth Lord Treasure Bugs, Part II

Apr 20, 2011
Mark
Comments Off on Labyrinth Lord Treasure Bugs, Part II

Black Wyvern found yet another bug in the treasure generation system. His post on the Goblinoid Games Forums correctly identified that magical armor wasn’t being generated properly. Additionally he pointed out that extraneous commas were still be shown in certain situations. Part of that was the armor issue and part of it was a database issue.

Those issues have been corrected. Thanks Black Wyvern.

Tags: ,

Comments Off on Labyrinth Lord Treasure Bugs, Part II

Treasure Book Tweaking

Apr 16, 2011
Mark
Comments Off on Treasure Book Tweaking

After several hours of tweaking and poking at the implementation of the Treasure Book on Demand, I’m declaring a temporary failure. No matter what I attempted, the production time is still limited by the PDF creation library. My routines are a minimal fraction of the overall time. I’m not certain what to try next. For now, I’ll leave it alone.

As a trade off, I added a daily production of the book that is available for immediate download. A 100-entry PDF is generated nightly. With seven days available, most people are probably covered. For a sample, you can grab the Treasure Book of the Day. The other six issues are available on the generator page.

Additionally, I added HTML and text output methods for people that would rather not have the results in PDF format. The non-PDF generation is far faster. I left the HTML very simple so it isn’t cluttered if someone chooses to print it.

Tags:

Comments Off on Treasure Book Tweaking

Bottlenecks: Not the friendly top of a longneck

Apr 14, 2011
Mark
Comments Off on Bottlenecks: Not the friendly top of a longneck

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil – Donald Knuth

My Treasure Book generator has serious speed issues. It’s slower than frozen molasses. PHP isn’t my language of choice and my experience is limited. When I wrote the generator, I made quick and dirty decisions. Mostly I just wanted a proof of concept to demonstrate the functionality of the idea. Now, I need to address the fact that it’s slow.

The first issue I tackled was single-returns per hoard type. Shifting it over to array value returns resulted in next to nothing. It had worked previously for the Modern Name Generator. The difference is that the name generator was IO bound. The treasure book is not. Most of the calls are pretty lightweight and quick. Something was sucking up serious resources and my ad-hoc approach to fixing the application wasn’t producing results.

I really needed to figure out what the issue was. Like many programmers, I was quick to jump on prior knowledge without looking. When that failed, I was forced to find out how to profile php code. Turns out its not that hard. Enter Xdebug and KCacheGrind ( For the Windows folks, WinCacheGrind is the equivalent to KCacheGrind.). The former is a quick install and the latter is packaged with KDE. Tweak php.ini as described and you can profile php easily.

TreasureBook Profile

LLTreasureBook's Profile

My code isn’t really the issue. How I used TCPDF is the heart of the problem. If I’d bothered to RTFM, I’d have known I was doing it wrong. As the Performances (sic) page states,

Avoid using the HTML syntax (writeHTML and writeHTMLCell methods) if not strictly required;

It isn’t. It was convenient at the time. Now I have to rework to do a new layout.

Tags:

Comments Off on Bottlenecks: Not the friendly top of a longneck

PHP Random Array

Apr 4, 2011
Mark
Comments Off on PHP Random Array

For my Spell Book utility, I needed a quick array of non-repeating results to select elements from another array. Being a relative novice to the PHP language, I came up with rather inefficient mechanism for the original attempt. The rand_array() method works but could result in a lot of unnecessary loops.


<?php

// simple function to give a random set of values between min and max
// where number  is the number of values to return.
function rand_array($min, $max, $number)
{
    $rnd = array();

    // If number exceeds the span of the min/max range, 
    // just give all results in the span
    if($number > ($max - $min + 1))
    {
        foreach(range($min, $max) as $val)
        {
            $rnd[] = $val;
        }
        return $rnd;
    }

    while($number != 0)
    {
        $val = mt_rand($min, $max);

        if(in_array($val, $rnd))
        {
            continue;
        }
        else
        {
            $rnd[] = $val;
            --$number;
        }
    }

    return $rnd;

}

$result = rand_array(1, 31, 9);

print_r($result);

?>

Obviously, extra looping is just more load and latency. A much simpler solution would just utilize range(), shuffle(), and array_slice(). Not only is it more efficient, it is far simpler and infinitely more readable.


function rand_array($min, $max, $number)
{
    $rnd = range($min, $max);
    shuffle($rnd);

    if($number > ($max - $min + 1))
    {
        return $rnd;
    }

    return array_slice($rnd, 0, $number);
}

Far better.

Tags:

Comments Off on PHP Random Array