Create random treasure chests on maps?

Hey all,


I was wondering if there was a fairly easy way to randomly place 1-3 treasure chests on existing maps every game (they can be placed at the start of a game or when you enter a map, don't care how it's done), and have them stay on those spaces during that game if you leave and come back to a map. And the placement couldn't be on any region marked with a '12' (using Yanfly's region restrictions).


Thanks for any help!


Derek
I propose this if you want:


-Create a event in automatic mode


--> variable: random chest = random(0,2)


-->erase event


-Create in your map any chests you want with 3 pages and for switch :random chest = 0 or 1 or 2


This event will appear every time you enter in this map.


If you d'ont want this, in the first event use a switch A.


I hope I help you, excuse me if my english isn't good.
No problem, thanks for replying at least. Not sure if that solves the issue with the impassable regions, but your idea could work I welcome other ideas (or coding proposals from coders, that I would be glad to pay for).
For the regions, I knows this code.


This code checked if the players is in the regions 12. I knows isn't exactly whant you want but it's can help for your code.

Code:
$gameMap.regionId($gamePlayer.x, $gamePlayer.y) === 12
I made something but there's some things to consider: are all your non passable tiles region id 12? Because if not then the solution below can move the chests inside non passable tiles.


My advice would be to make it another way: the chests can only be moved to tiles with a specific region id. I have provided instructions in the small script below should you wish to do so.


The solution spoiler'd below is working, however it requires 2 game variables per map it is used on.


If you use a plugin for self variables it can be completely automated apart from setting up the event ids, you'll just need to know the calls to set a self variable and to read it.

Spoiler
1st event page: parallel process, no condition


Script:



$gameVariables.setValue(id1,[2,3,4]) //change 2,3,4 to your chests event ids. Change id1 to whatever variable id you want.
//You can use any number of ids as long as you separate each one with a ,


Script:



var ar = [];
for (var i = 0; i < $dataMap.width; i++) {
for (var ii = 0; ii < $dataMap.height; ii++) {
if ($gameMap.regionId(i,ii)!== 12 && ($gamePlayer.x !== i && $gamePlayer.y !== ii)) {
//just replace !== 12 with === your Region Id for the alternate solution
ar.push([i,ii]); } } }
var rand; var value = []
for (i = 0; i < $gameVariables.value(id1).length; i++) { //change id1 here
rand = Math.floor(Math.random() * ar.length);
value.push(ar[rand]);
ar.slice(rand,1); }
$gameVariables.setValue(id2,value) //change id2 here to another game variable


Control Self switch A : ON


2nd Event page: parallel process, condition: self switch A ON


Script:



for (var i = 0; i < $gameVariables.value(id1).length; i++) {
$gameMap.event($gameVariables.value(id1)).setPosition($gameVariables.value(id2)[0],$gameVariables.value(id2)[1])
}
//Change id1 and id2 to what you put in the first event page


Erase Event





This will randomly select from the map a position (with region id not 12) for each event id you put in variable id1 and store it inside game variables id1 and id2 the first time the event runs. Then, and everytime the map is loaded afterwards, it will move the chests to the stored positions adequately.


The chests locations can't overlap.

Original thread: https://forums.rpgmakerweb.com/threads/create-random-treasure-chests-on-maps.71374/