ID:152103
 
Hey y'all! I have been toiling near-silently, producing my little Pirate Role Playing Game. But I have come across a philosophical query that my brain alone cannot solve. On each island there is a market, and players can build a fortune by trading between them.

I.e. Player Jimmy goes to Southsea and purchases 100 Wood for 100 Gold (for there is a forest on Southsea) then sails to Maenisle and sells the 100 Wood for 400 Gold (for there are few trees on Maenisle)

So far prices on islands are calculated as a Constant (foor the type of resource) multiplied by the fraction of the total amount of said resource that each island has. So if Southsea had 40% of all the Wood in the world, its price would be very low.

My question is this; how can I simulate the background production of Wood/Gold/whatever-resource into the merchants stock? Surely an infinate-loop is not the way to go, as they are the bane of all programming? How can I get my program to constantly add 1 Wood every three minutes to a Merchant's stock? If there are 20 Merchants in the world, each with 10 different resources, the overtime for lots of loops would build up horrifically would they not?

Also - any suggests with additions to my merchants system would be greatly appreciated, as I am no master (or even novice) in economics. *points to the ElecEng degree* =P

Many thanks,

~Ease~
Infinite loops aren't always bad. Isn't that why they were invented?
In response to Kaiochao
Aren't they a bi-product of the useful 'loop' ability. I've been out of the loop in regards to programming for a while, so I could be very wrong about the infinate loop subject.
var/const/updaterate = 600

obj/city
var/woodrate = 1
var/ironrate = 2
var/lastupdate = 0
var/wood = 0
var/iron = 0
proc/update()
//get the elapsed time
var/diff = world.time - lastupdate
//stop the proc if the elapsed time is less than a minute
if(diff <= updaterate) return
//express in terms of minutes
diff = round(diff, updaterate)
//add resources
wood += woodrate * diff
iron += ironrate * diff
//set the time of the update
lastupdate = lastupdate + diff * updaterate


You will need to call update() every time you need to check resources. However, over these time frames, an infinite loop is going to end up being more efficient. If you had to update resources every, say, 10 seconds, then this would be the better method.
Ease wrote:
My question is this; how can I simulate the background production of Wood/Gold/whatever-resource into the merchants stock? Surely an infinate-loop is not the way to go, as they are the bane of all programming? How can I get my program to constantly add 1 Wood every three minutes to a Merchant's stock? If there are 20 Merchants in the world, each with 10 different resources, the overtime for lots of loops would build up horrifically would they not?

Rather than using infinite loops, why don't you use a proc that still spawn a version of itself every 3 minutes?
mob/NPC/storekeeper
var/wood = 0
proc/GainInventory()
src.wood++
spawn(1800)
if(src) src.GainInventory()
New()
..()
spawn() src.GainInventory()

While it would still have the same concept as an infinite loop, it wont have any sleeps that still have the original proc stay in effect over a long period of time. Though that is still called for every storekeeper you place in the world.

Another way is if you have the items in a list in each storekeeper, you could make it so that in the storekeepers New(), you add them to a list of storekeepers in the world, and then make a global proc on the world New(), and have that spawn it self every 3 minutes. What that would do it cycle through every mob in the shopkeeper list, have a look at the list inventory, and increase the amount for each value in the list, though that would work if you work with associative lists in a particular way.
In response to Hassifa
Unless if you have a DAMN good reason (you don't), you should not use spawn() for infinite loops. It just creates MORE overhead. And it's still an infinite loop.
Ease wrote:
How can I get my program to constantly add 1 Wood every three minutes to a Merchant's stock?

Just keep track of the amount of stock the merchant had at last purchase, and the time of last purchase. The next time someone checks to see how much stock the merchant has, then just check the current time against the time of last purchase to see how many "3 minutes" have elapsed since then.

I think that's essentially what Garthor said, though.
In response to Foomer
Yer, I think that's what Garthor said pretty much. Thanks very much all of you, especially Garthor and Foomer! Now to get programming!

~Ease~
As others have mentioned, only updating the island the next time it's visited is good enough.

As far as adjusting prices and such, I think your best bet here is to consider the larger world beyond just what the player does. For example, if you can buy 100 wood for 100 gold on one island and sell it for 400 gold on another, several things have happened:

  • You've increased demand for wood on the island that sells it cheaply, causing pressure to raise prices.
  • You've decreased demand for wood on the island that buys it at a premium, causing pressure to lower prices.
  • You've increased the gold supply and decreased the wood supply on the island that sells wood cheaply.
  • You've increased the wood supply and decreased the gold supply on the island that buys wood at a premium.

    Obviously if your source of lumber starts running dry, their price is gonna skyrocket. Likewise your buyer isn't gonna want to spend 4× the going rate for very long. Supply of wood and gold on each island will change with time too; one is producing wood, the other probably mostly just consuming at a slow net rate.

    On top of this, you can add in a market stabilization effect by including "ghost traders". These traders would randomly scout out ports to find the best deals they could make, and then they'd start running that route until it was no longer profitable to do so. Your own trading activity could provide hints to these traders, such that they might start using the same route you do and equalizing prices. They could even provide clues to you in return, like if you learn about their movements by frequenting the local establishments.

    Ultimately your easiest way of monitoring supply and demand is to keep track of how much of each good is available and how much is produced/used at each island. (You can also consider sustainability, so that foresting past a certain point reduces tree cover which in turn reduces the amount of wood available per cycle. That's more of an advanced feature though and I wouldn't worry about it till you've got something running.) Production also depends on population, which requires food, and food is also a resource. As demand changes, an island might also shift its production to different goods.

    Lummox JR