Simply Pointless

But hey, why not?

By

Pool Of Randomness

For my current Unity project I had to convert Kipp Ashford’s Weighted Array script into something I could use in my C# environment. I had a similar solution already working in ISIS but Ashford’s script was much more elegant since it includes a number of useful functions my bare-boned version was lacking.

A clever (or so I hope) use of a hashtable allowed me to trim some of the variables the orginial script had used to keep it as slim as possible.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// WeightedRandomPool.cs
// 2013-10-09 by Thomas Touzimsky
// http://www.simplypointless.com/
//
// Adds, deletes and returns a random item from a pool of objects based on each item's associated weight
//
// A conversion of the original JS script by Kipp Ashford
// http://blog.teamthinklabs.com/index.php/2011/08/23/grabbing-items-from-an-array-based-on-probability/
 
using UnityEngine;
using System.Collections;
 
public class WeightedRandomPool
{
 
    // Our pool is stored in a hashtable where
    // key   = (Object)item
    // value = (int)weight
    public Hashtable htItems = new Hashtable();
 
 
    /// <summary>
    /// Adds a new item to the hashtable or changes its weight if has been added before
    /// </summary>
    /// <param name="item">The object you want to add to the pool</param>
    /// <param name="weight">The object's weight within the pool</param>
    public void Add(Object item, int weight)
    {
        if (!htItems.ContainsKey(item))
        {
            htItems.Add(item, weight);
        }
        else
        {
            Debug.Log("WeightedRandomPool::Add() --- WARNING: Object is already added. Changing weight of object instead.");
 
            htItems[item] = weight;
        }
    }
 
 
    /// <summary>
    /// Changes the weight of an object within the pool
    /// </summary>
    /// <param name="item">The object whose weight you want to change</param>
    /// <param name="nWeight">The object's new weight within the pool</param>
    public void ChangeWeight(Object item, int nWeight)
    {
        if (htItems.ContainsKey(item))
        {
            htItems[item] = nWeight;
        }
        else
        {
            Debug.Log("WeightedRandomPool::ChangeWeight() --- WARNING: Object '" + item + "' is not a member of this pool.");
        }
    }
 
 
    /// <summary>
    /// Removes an object from the pool
    /// </summary>
    /// <param name="item">The object you want to remove</param>
    public void Remove(Object item)
    {
        if (htItems.ContainsKey(item))
        {
            htItems.Remove(item);
        }
        else
        {
            Debug.Log("WeightedRandomPool::ChangeWeight() --- WARNING: Object '" + item + "' is not a member of this pool.");
        }
    }
 
 
    /// <summary>
    /// Returns a randomly chosen object from the pool depending on all members' weight
    /// </summary>
    /// <returns></returns>
    public Object Get()
    {
        int nSumOfWeights = 0;
 
        foreach (DictionaryEntry item in htItems)
        {
            nSumOfWeights += (int)item.Value;
        }
 
        int k = Random.Range(0, nSumOfWeights + 1);
        // +1 to make sure we can actually find the last item if it has a weight of 1
        // since Random.Range never hits the maximum value
 
        foreach (DictionaryEntry item in htItems)
        {
            // walk the pool one item at a time and see whether its weight is lower than k
            // if yes, return the current item ; if no, subtract the item's weight from k
 
            // if, for example, the pool includes three items with a weight of 40/40/60 and
            // k = 70, the first loop's result will be negative and k gets reduced by 40;
            // the next loop's result is positive since k (now 30) is lower than the second
            // item's weight
 
            if (k > (int)item.Value)
            {
                k -= (int)item.Value;
            }
            else
            {
                return (Object)item.Key;
            }
        }
 
        return null;
    }
 
 
    /// <summary>
    /// Clears the pool
    /// </summary>
    public void Clear()
    {
        htItems.Clear();
    }
 
}

The class itself is used just like Ashford’s original.

WeightedRandomPool myPool = new WeightedRandomPool();
 
myPool.Add(myFirstItem, 15);
myPool.Add(mySecondItem, 5);
myPool.Add(myThirdItem, 2);
 
UnityEngine.Object myRandomItem = myPool.Get();

Feel free to suggest any ideas on how to improve the script – after all, I am still a novice coder. :)

By

Take A Walk On The Tiled Side

Came across a great write-up by Rodrigo Monteiro on the many ways of how to deal with moving characters in a 2D environment and the many obstacles one has to tackle when faced with slopes, moving platforms or ladders.

The Guide To Implementing 2D Platformers

Unfortunately his website hasn’t been updated in a year, but I hope he’s just busy coding at the moment.

By

In The Meantime, Here’s Some Music

First, my most recent mashup. Nu-skool breaks vs. not-so-nu-skool anymore breaks. What comes after nu-skool? Middleschool breaks?

And the DJ set it was featured in.

Kid Panel – ‘R ‘U Ready? [The Pooty Club Records]
Colombo – California [iBreaks]
Deenk & Geon – Subtronic [Hard & Hits]
Vize – Killin’ Speakers (Blacklist Remix) [Breakz R Boss Records]
Mutantbreakz – Creation (Kid Panel Remix) [Rat Records UK]
Outer Kid – Panic In Funktron [Elektroshok Records]
Lo IQ? – Around The World [Breakz R Boss Records]
Fisso & Spark – Killer On The Dancefloor [DUBTRXX]
Chelydra – Vulpecula (Colombo Remix) [Acida Records]
Meat Katie & Elite Force vs. kuplay – Divine F*** (c-Row’s infusion vs. kid panel reboot) [CD-R]
Dustin Hulton – F*** U (Kid Panel Remix) [Sound Break Records]
Chevy One – Now I’m Letting It Go [iBreaks]

I had a hard time not turning this into a Kid Panel only set.

Also, sorry for the lack of anything game-related.

By

If It Does Not Break…

… break it. Preferably by adding some beats to it, I say.

By

Dirty Beats Done Dirt Cheap .09

Here’s episode 9 of my rather irregular series, as aired on Friday 12th on Kick Radio to some great feedback. Thanks, folks! :)

Pag “I Believe In Cosmic Love” (Loquai Remix) [Mistique Music]
Maxim Hix “Look Forward” [Zebra 40]
Suffused “Year 2008″ (Alfoa Remix) [Mistique Music]
Airwave “Chiricahua” [L*C*D Recordings]
Adrian Heathcote “The Assassin” [VIM Records]
Tee-Ex “Indestructable” [Morphosis Limited]
DJ Shinobi & Losing Rays “Only You” (Retroid Remix) [Morphosis Records]
Vadim Koks “Painter” [Dextrous Records]
Dave LeBon “Biface” (Digital Department Remix) [Undefined Records]
Ire “Take Over” (Line Of Sight Remix) [VIM Records]
Logistics “Running Late” [Hospital Records]

By

Next Friday Is Gonna Be Dirty

Just a quick note that episode 09 of Dirty Beats Done Dirt Cheap is wrapped up and scheduled to air this Friday in Leigh Stone’s Friday Social radio show on Kick Radio. Tune in around 8 PM (GMT) and join the chat!

By

Knee-Deep In The… Eucalyptus Oil?

Today’s addition to my shelf is one of the classic video game releases – id Software’s Doom. While in rather good shape for its age, the box came with some sticker residues on it, but I successfully managed to get it all off without damaging the box…

I love the smell of a hot herbal bath in the morning!

… thanks to a suggestion I read on a forum of fellow video game collectors. Applying a bit of eucalyptus oil to the residue makes it easy to remove it while keeping the box intact. And it doesn’t smell too bad either.

By

WCRI – A Round-Up

Now that we have a thread about collecting old PC games over at the RPS forums, I decided I should upload some pictures of the current collection rather than posting a long list of names.

By

We Can Rebuild It – January Edition

More goodies to fill up shelf space. Seems like the only big box edition of Turrican II was the PC release, but that’s misssing some of the outer artwork. Also, Golden Axe, North & South and a still shrinkwrapped box of Machines (“Rrrrreaper online!”).

Size doesn’t matter.

Furthermore here’s the (new to me) long box edition of the first Command & Conquer which I think looks much better than the Worldwide Warfare edition. The latter comes with Red Alert (+ expansions) and the soundtrack, but since I already got the latter and want a proper box of Red Alert I gave in and bought this one instead.

Three’s a crowd.

February is off to a good start as well – Dungeon Keeper and Crusader: No Regret should arrive soon.

By

My Latest Mashup: Adele vs. Pendulum

Get it while it’s available. ;-)