Friday, December 23, 2016

Encyclopedia of Drunken Walks

- A New Paradigm -

"God does not play dice" vs "God does not waste computational resources"

Drunken walks are ideal for map generation for 3 reasons:

  1. The generated map is inherently fractal.
  2. The generated map is definitively random.
  3. The map can be generated dynamically (on the fly).  This dynamic map generation allows for unlimited map size (boundless and infinite).

Drunken walks can be biased to favor any combination of the 6 cardinal/absolute directions (NW, NE, East, SE, SW, West) and the 6 relative directions (straight forward = 12:00, right forward = 2:00, right backward = 4:00, straight backward = 6:00, left backward = 8:00, left forward = 10:00).

These biases can be encoded in 6 weights for cardinal/absolute directions, and 6 weights for relative directions (relative to the current direction of movement).

A drunken walk pattern can thus be defined by 12 weights, and we call this collection of weights the "Direction Tensor".  The word "tensor" is used here to mean roughly "an object whose shadow is a vector".  At any given point in time, the likelihood of moving in each direction is proportional to the product of the cardinal/absolute weight, and the corresponding relative weight (each relative direction corresponds to an absolute direction, with respect to the current direction of movement).

The effects of chaging the 6 cardinal/absolute weights are fairly predictable, so this study examines only the effects of relative weightings (the 6 cardinal/absolute weights are taken to be equal to each other, so there are no cardinal biases).

- Example Patterns -

Limiting movement to backwards right and left (4:00 and 8:00) leads to a relatively compact but elongated form:

50% right backwards (4:00), 50% left backwards (8:00).

66% right backwards (4:00), 33% left backwards (8:00).
Limiting movement to straight and right forward (12:00 and 2:00) generates a less compact form:

66% straight forward (12:00), 33% right forward (2:00).
Limiting motion to right forward and left forward (2:00 and 10:00) - aka clockwise and counter-clockwise - results in characteristic "holes".  This can be used for maps where regularly spaced lakes, islands, or oases are desired:

50% right forward (2:00), 50% left forward (10:00).  [example 1/2]

50% right forward (2:00), 50% left forward (10:00).  [example 2/2]
Allowing movement in any direction except for straight backwards leads to the following "balanced" typologies:
20% straight forward (12:00), 20% right forward (2:00), 20% right backward (4:00), 20% left backward (8:00), 20% left forward (10:00).  [example 1/3] 
20% straight forward (12:00), 20% right forward (2:00), 20% right backward (4:00), 20% left backward (8:00), 20% left forward (10:00).  [example 2/3] 


20% straight forward (12:00), 20% right forward (2:00), 20% right backward (4:00), 20% left backward (8:00), 20% left forward (10:00).  [example 3/3] 

- VBA Code (Class Module) -

'6 weights for the 6 absolute directions:
Public NW As Single
Public NE As Single
Public East As Single
Public SE As Single
Public SW As Single
Public West As Single

'6 weights for the 6 relative directions:
Public StraightForward As Single
Public RightForward As Single
Public RightBackward As Single
Public StraightBackward As Single
Public LeftBackward As Single
Public LeftForward As Single

'6 numbers for current fractional likelihood of each absolute direction:
Public ProbabilityNW As Single
Public ProbabilityNE As Single
Public ProbabilityEast As Single
Public ProbabilitySE As Single
Public ProbabilitySW As Single
Public ProbabilityWest As Single

Public Function GetDirection(ByVal Previous As Location, ByVal Current As Location)
    Dim Direction As String
    Dim SouthShift, EastShift As Integer
   
    SouthShift = Current.Row - Previous.Row
    EastShift = Current.Col - Previous.Col
   
    Direction = "resting"
    If SouthShift = 0 And EastShift = 1 Then Direction = "East"
    If SouthShift = 0 And EastShift = -1 Then Direction = "West"
    If SouthShift = 1 And EastShift = 0 Then Direction = "SE"
    If SouthShift = 1 And EastShift = -1 Then Direction = "SW"
    If SouthShift = -1 And EastShift = 1 Then Direction = "NE"
    If SouthShift = -1 And EastShift = 0 Then Direction = "NW"
   
    GetDirection = Direction
End Function

Public Sub CalculateProbabilities(ByVal Direction As String)
'The public variable "Direction" should be set before calling this sub.
    Dim AbsoluteWeights(6) As Single
    Dim RelativeWeights(6) As Single
    Dim CombinedWeights(6) As Single
    Dim Counter, RotatingCounter As Integer
    Dim TotalWeights As Single
   
    AbsoluteWeights(0) = Me.NW
    AbsoluteWeights(1) = Me.NE
    AbsoluteWeights(2) = Me.East
    AbsoluteWeights(3) = Me.SE
    AbsoluteWeights(4) = Me.SW
    AbsoluteWeights(5) = Me.West
   
    RelativeWeights(0) = Me.StraightForward
    RelativeWeights(1) = Me.RightForward
    RelativeWeights(2) = Me.RightBackward
    RelativeWeights(3) = Me.StraightBackward
    RelativeWeights(4) = Me.LeftBackward
    RelativeWeights(5) = Me.LeftForward
   
    TotalWeights = 0
    'Rotate RelativeWeights depending on Direction:
    Select Case Direction
    Case "NW"
        For Counter = 0 To 5
            CombinedWeights(Counter) = AbsoluteWeights(Counter) * RelativeWeights(Counter)
            TotalWeights = TotalWeights + CombinedWeights(Counter)
        Next Counter
    Case "NE"
        For Counter = 0 To 5
            RotatingCounter = (Counter - 1) Mod 6  
            'We need the next line, because vba does "mod" wrong.
            If RotatingCounter < 0 Then RotatingCounter = RotatingCounter + 6
            CombinedWeights(Counter) = AbsoluteWeights(Counter) * RelativeWeights(RotatingCounter)
            TotalWeights = TotalWeights + CombinedWeights(Counter)
        Next Counter
    Case "East"
        For Counter = 0 To 5
            RotatingCounter = (Counter - 2) Mod 6
            If RotatingCounter < 0 Then RotatingCounter = RotatingCounter + 6
            CombinedWeights(Counter) = AbsoluteWeights(Counter) * RelativeWeights(RotatingCounter)
            TotalWeights = TotalWeights + CombinedWeights(Counter)
        Next Counter
    Case "SE"
        For Counter = 0 To 5
            RotatingCounter = (Counter - 3) Mod 6
            If RotatingCounter < 0 Then RotatingCounter = RotatingCounter + 6
            CombinedWeights(Counter) = AbsoluteWeights(Counter) * RelativeWeights(RotatingCounter)
            TotalWeights = TotalWeights + CombinedWeights(Counter)
        Next Counter
    Case "SW"
        For Counter = 0 To 5
            RotatingCounter = (Counter - 4) Mod 6
            If RotatingCounter < 0 Then RotatingCounter = RotatingCounter + 6
            CombinedWeights(Counter) = AbsoluteWeights(Counter) * RelativeWeights(RotatingCounter)
            TotalWeights = TotalWeights + CombinedWeights(Counter)
        Next Counter
    Case "West"
        For Counter = 0 To 5
            RotatingCounter = (Counter - 5) Mod 6
            If RotatingCounter < 0 Then RotatingCounter = RotatingCounter + 6
            CombinedWeights(Counter) = AbsoluteWeights(Counter) * RelativeWeights(RotatingCounter)
            TotalWeights = TotalWeights + CombinedWeights(Counter)
        Next Counter
    Case "resting"
        'If the animal rested on the last turn, absolute/cardinal movement bias controls:
        For Counter = 0 To 5
            CombinedWeights(Counter) = AbsoluteWeights(Counter)
            TotalWeights = TotalWeights + CombinedWeights(Counter)
        Next Counter
    End Select
   
    'Normalize the probabilities so they add up to 1:
    If TotalWeights <> 0 Then
        For Counter = 0 To 5
            CombinedWeights(Counter) = CombinedWeights(Counter) / TotalWeights
        Next Counter
    End If
   
    Me.ProbabilityNW = CombinedWeights(0)
    Me.ProbabilityNE = CombinedWeights(1)
    Me.ProbabilityEast = CombinedWeights(2)
    Me.ProbabilitySE = CombinedWeights(3)
    Me.ProbabilitySW = CombinedWeights(4)
    Me.ProbabilityWest = CombinedWeights(5)
End Sub

Public Sub NormalizeProbabilities(ByVal TotalWeights As Single)
    'Use zero to indicate "recalculate TotalWeights":
    If TotalWeights = 0 Then
        TotalWeights = Me.ProbabilityNW + Me.ProbabilityNE + Me.ProbabilityEast _
        + Me.ProbabilitySE + Me.ProbabilitySW + Me.ProbabilityWest
    End If
   
    'If it actually is zero (based on math), then use "absolute values":
    If TotalWeights = 0 Then
        Me.CalculateProbabilities ("resting")
    Else
        Me.ProbabilityNW = Me.ProbabilityNW / TotalWeights
        Me.ProbabilityNE = Me.ProbabilityNE / TotalWeights
        Me.ProbabilityEast = Me.ProbabilityEast / TotalWeights
        Me.ProbabilitySE = Me.ProbabilitySE / TotalWeights
        Me.ProbabilitySW = Me.ProbabilitySW / TotalWeights
        Me.ProbabilityWest = Me.ProbabilityWest / TotalWeights
    End If
End Sub

Copyright 2016 Matthew J Curran.  

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this code except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.




Monday, November 28, 2016

Exodus To Kobe 2.0


This version allows domestication of animal herds.

Click "Update Radar" any time you move over or next to a new herd.
Click "Show All my Herds" to view all the animals you have domesticated.


Click "Show my Fastest Herds" to view your movement cost over different terrain.
You can play as either Germany, Russia, or Korea (single or multi-player).
Your goal is still to find Kobe.
For single player mode, simply delete the contents of rows 7 & 8 (col 1) in "Maps" tab.  

You must enable macros for this game to run in Excel.  Never run Excel macros from questionable or unverified websites.



Copyright 2016 Matthew J Curran.  Full license details in Module 1 of the VBA code.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Sunday, November 20, 2016

Co-Ex World Explorer - Third Release: Exodus To Kobe

In this mini-game, you can play as either Germany or Korea (single or multi-player).  Your goal is to find Kobe.

Most of the key game features (e.g. domestication and evolution of animals) still remain to be implemented.  This fully functional mini-game serves primarily to illustrate the power of Dynamic Map Generation (行動的ダイナミック地図開発).

Here are five unique starting maps, all dynamically generated from the same seed map (with the location of Japan semi-randomized):

Germany in NW, Korea in NE, Japan to the south.

In this map, the Korean peninsula may turn out to be an island.

In this map, a Denmark-like formation separates the North Sea and the Baltic. 

In this map, Germany and Japan have grown together to form a single continent.

The Osaka plain is clearly visible to the east of Kobe (forested tile).

For single player mode, simply delete the contents of row 7, col 1 in "Maps" tab.  

To add a third player, type "Germany", "Korea", or "random" in row 8, col 1 of "Maps" tab, and also make sure to create three additional tabs, which you must "move to end".  Type a color index in row 8, col 2.  Of course, due to the semi-randomized location of Japan, the third player will be at a disadvantage.



You must enable macros for this game to run in Excel.  Never run Excel macros from questionable or unverified websites.


Copyright 2016.  Full license details in Module 1 of the VBA code.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Monday, November 7, 2016

Preview of New Features: Graphics

The next release will feature (optional) fancy graphics:


These can be very easily customized simply by editing the "Painter" tab:



Saturday, October 29, 2016

Columbian Exchange World Explorer: Second Release

This second release features some major new features:

  1. Advanced Map Generation.  The Map is now created dynamically by a swarm of autonomous map generators (henceforth "animals").
  2. Animals.  For now you can enjoy watching them migrate.  In future versions, you can domesticate and evolve them - or hunt them to extinction.
  3. Terrain.  The game now features a variety of terrains: forest will slow you down; snow capped mountains will bring you to a crawl.
A GIF showing a dozen turns of migration
Watching the animals migrate. 
game in progress.  looks somewhat like a map of Mexico with south at the top.
A game in progress.
about 30 turns of example game play.
Example game (single player).

Use the buttons on the "Map" tab to start a new game, resume a game in progress, or simply watch the animals migrate.

You must enable macros for this game to run in Excel.  Never run Excel macros from questionable or unverified websites.


Copyright 2016.  Full license details in Module 1 of the VBA code.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.


Monday, October 24, 2016

Preview of New Features: Animals

The next release will feature migrating animals!  Here is a sneak peek:


The animals are represented by dashed diagonal lines.  Here we can see a single herd of sheep, five herds of dolphins, and five herds of forest elephants.