Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Distance Matrix - Sugarcube


My son and I are trying to write a trading game, similar in format to Drug Wars, but without the drugs and set in feudal Japan. The idea is to move from place to place buying and selling goods, building up capital, investing in larger transports, etc.

The problem is we also want to have costs involved. The idea is to base it on a 3x3 grid (maybe larger when we understand the concept), with a cost of x to move to an adjacent square, and 2x to move to a square 2 spaces away. Using Tic-Tac-Toe as an example, if the player is in the middle square, all others are only 1 space away; if they're in a corner square, there are 3 others just one space away, and 5 that are 2 spaces away. For the life of us we can't see how to set up the calculations.

Does anyone have any advice, please? We realise it'd involve tagging each port and using a distance matrix of some sort, but don't see how to code it into Sugarcube.

Any advice gratefully received!




  • edited July 2016
    Are you allowing any-to-any movement or do you want specific paths between sites?

    For what you're doing, I'd suggest specific paths rather than allowing the player to hop around the map at random.
    <<set $cities to { 
               city1: { paths: [ { to:"city2", cost:2"}, { to:"city3", cost:3" } ] },
               city2: { paths: [ { to:"city1", cost:2}, { to:"city4", cost:1}, { to:"city5", cost:3 } ] },  
               city3: { paths: [ { to:"city1", cost:3}, { to:"city5", cost:2} ] },
               city4: { paths: [ { to:"city2", cost:1}, { to:"city5", cost:4} ] },
               city5: { paths: [ { to:"city2", cost:3}, { to:"city3", cost:2} ] }

    This sets up a limited movement map. Note that the like from city4 to city 5 is a one way link, providing an expensive backdoor out of city4 without going through city2.

    To get the paths available from a city, use:

    <<set $paths to $cities[$here].paths>>

    $paths will then be an array, where each element is on object holding the name of the destination city (.to) and the cost of travelling there (.cost). These would be
    . You could add an additional attributes to each link such as a name ("High pass", "Coast road", "Way of the Damned") or a chance of encountering bandits or other misadventure etc...

    The $cities object is set up so you can add additional attributes to each city, such as market information, descriptions or coordinates on an image map.
  • Hi mykael

    The idea was originally to have any-to-any, but seeing it written down, I realise now that that wouldn't make much sense, so your suggestion of specific paths is the way to go.

    Thanks very much for the info - I'll try it over the weekend. You've actually also answered what would probably have been a couple of subsequent questions as well, so much appreciated.

    It's become clear to us that this game would need be driven by a whole load of arrays: I've just spent my lunch break working on one for the different haulage methods, capacities, affect on movement and market cost - I'm just grateful now my son didn't want to attempt a football manager sim :)

    Thanks again

  • I am not big of a coder myself, but I thought of an alternative approach for your problem which could provide an any-to-any-solution. Your use of the word "matrix" was the key. So here is what I thought of.

    1. Give your passages coordinates in a matrix system. I guess it would be easiest to set the passage names to these coordinates. Example (using a tic-tac-toe-board):
    0|0 - 0|1 - 0|2
    1|0 - 1|1 - 1|2
    2|0 - 2|1 - 2|2

    First value equals x, second value y position.

    2. Movements from one passage to another could now be expressed using Eucledean vectors. So if you moved from the middle top field (0|1) to the bottom left field (2|0) you would calculate destionation field minus starting field e.g. (2|0) - (0|1) = (2 |-1)

    3. In order to calculate the necessary steps you need to calculate the length of the vector which is the square root of the sum of the two squared values, in our example the square root of 4+1=5, which is, 2.23. The int value of that should do, so you are moving two steps.

    Could that solve your issue?

Sign In or Register to comment.