I am making a 2d sandbox game with procedural terrain generation, and for a few weeks, I have used this noise function by joeiddon, slightly modified so I can have multiple noise instances. The problem was that this function doesn't have a seed option, so there's no way to tell it to generate the same world two times. To solve that, I have been googling and coding for a few hours. Eventually, I stumbled upon this function (also slightly modified), which does support seeds. The problem is that when I use this function, there is almost no variation in the worlds. They can all be divided in five categories, and the difference between different worlds in the same category will never be more than a few blocks. So, basically, I either need a way to make the first function work with seeds, or I need to find out why the second function doesn't give enough variation. I don't know if this will help, but here is the code that initializes the noises based on a random seed:
seed = Math.random();
grassNoise = new Perlin(seed);
goldMaxNoise = new Perlin(seed/7);
goldMinNoise = new Perlin(seed/2);
treeNoise = new Perlin(seed*3/2);
cloudUpNoise = new Perlin(seed);
cloudDownNoise = new Perlin(seed*1.2);
coalMaxNoise = new Perlin((seed - 78) / 3);
coalMinNoise = new Perlin(seed*7);
I only use the perlin2 function. Could anyone please point out what I'm doing wrong?
Related
I'm creating bot for online dynamic game. In this case dynamic means that hero ingame can move around and background is changing when moving. Global variables like monsters are changing dynamic when moving as well.
My bot is using puppeteer. Since I need this monsters object I have function which get those monsters from page context every 2 - 3 seconds (randomize for anti-detection).
This solution is far from being perfect. Two main downsides are:
My bot kill monster and I want to go to next one it still see this monster which has got killed because next refresh is for example 1500ms from that point of time.
Performance of getting monsters is bad.
To solve first one I could just execute function which download monsters every time after killing one. On the other hand then second downside will be even stronger because I will perform much more getting monster which already is slow.
It all comes to second issue which is performance. You may ask how do I know that performance is bad?
When hero is moving it's relatively smooth but when monsters are being downloaded I see micro lag, like for a part of second hero stop. It's really maybe 100ms of lag but I can see it with human eye and if I will perform getting monster more frequently this lag will get stronger (to be clear - lag will not be longer but more often).
Downloading object from global window is long. The reason why is that game maintainers developed it so monsters are within big object npc which contains everything that is in dashboard and contains even empty elements so the total amount of this npc object is between 100k-200k elements. I'm doing lots of filters in order to get final monster data which I care about.
I'll show how I'm actually getting this monsters. So I execute 3 async functions:
const allNpc = await getAllNpc(page);
let monsters = await filterMonsters(page, allNpc, monstersToHunt);
monsters.hunt = await deleteAvoidedMonsters(page, monsters.hunt, monstersToOmit);
First one getAllNpc just get entire npc object (this big one which I mentioned above)
return await page.evaluate(() => {
if(window.g) return g.npc;
});
second function filter actual monsters and monsters which I want to kill from npc:
return new Promise((resolve, reject) => {
const validNpc = allNpc.filter(el => !!el);
const allMonsters = validNpc.filter(e => e.lvl !== 0);
const names = new Set(monstersNames);
const huntMonsters = validNpc
.filter(it => names.has(it.nick))
.map(({ nick, x, y, grp, id }) => ({ nick, x, y, grp, id }));
resolve({all: allMonsters, hunt: huntMonsters});
});
I'm using Set here to get rid of O(n) / O(n^2) algorithms and I think this is fastest I can achieve with javascript. Third function is same as this one but additionally filtering special monsters which I want to avoid.
Now my questions are:
is there a way to get this object on every this object and only this object change? I know there is function in puppeteer that can watch for DOM changes but is there something to watch global window object?
can I do anything more to speed it up? I read about worker_threads in NodeJS, can it help getting rid of this micro lag or something else? Clustering?
What I realized after a while is that bot was "lagging" because I was passing this huge g.npc array as argument to function. By that I mean I was resolving it from getAllNpc and then passing it to filterMonsters.
When I changed it so I execute .filter in getAllNpc inside evaluated script within page context and then resolving and passing array with hundreds not millions elements it is much faster and do not cause any lag or freeze.
I'm quite new to WebGL, and I'm trying to create cake with animated candles. So, I have found this examples: https://stemkoski.github.io/Three.js/Particle-Engine.html with candle flame and tried to change code due to my task (cake). But I struggled with creating several flames into same scene (I found some information how to create cubes or spheres, but unfortunately it is unhelpful in this case).
As far as I understand animated flame displaying is in the code:
this.engine = new ParticleEngine();
engine.setValues( Examples.fountain );
engine.initialize();
but I can't figure out how to create 5 different examples same time.
UPD: I finally created several instances, here is my code:
var engines = [];
var coordinates = [
{x:5, y:105},
{x:-40, y:115},
{x:-75,y:111},
{x:37, y:117},
{x:68, y:110}
];
coordinates.forEach(function(item, i){
var params = clone(Examples.candle);
params.positionBase = new THREE.Vector3(item.x, item.y, 10);
var engine = new ParticleEngine();
engine.setValues(instances[i]);
engine.initialize();
engines.push(engine);
}
But when I tried to set different example (smoke from examples) to one instance all other instance's parameters had been changed too. Can't figure out how to change each instance separately without any influence others.
I'm tried to make some world generation mechanism using Math.random() whenever I needed something random, but then decided that I wanted it seed-based, so, given a seed, I changed all of the Math.random() to Math.sin(seed++)/2+0.5, hoping it would do the same thing, but would be the same if the seed was the same seed.
Then someone made me notice that the sin wave hasn't got even distribution, and finally I saw why some of my code was working strangely.
I was wondering if there was a simple fix, or if there isn't, another very simple seed based randomizer like this
So, I looked at your method, t1wc, and I found that it isn't actually evenly distributed. It is significantly more likely to spit out numbers near 0 or near 1 than it is to spit out numbers near 0.5, for example. This is just a consequence of the way that the sine function works.
Instead, you might try using a method called Blum Blum Shub (named after the authors of the original paper, wonderfully). It is evenly distributed and quite fast. Given a seed, it works as follows:
Square the seed and put the result in a temporary variable (x).
Take the mod of x base M.
M is a product of two large primes.
The value of x is a new seed to be used for future calculations.
Return x/M as your pseudo-random number. It will be evenly distributed between 0 and 1.
Below is a simple implementation of a Blum Blum Shub:
var SeededRand = function(seed, mod1, mod2)
{
return function()
{
seed = (seed*seed) % (mod1*mod2);
return seed/(mod1*mod2);
};
};
If you want to make a new random number generator, you just call:
var rand = SeededRand(seed, mod1, mod2);
Where seed is some initial seed (1234567890 works well), and mod1 and mod2 are some large primes (7247 and 7823 work well). rand is just a variable that I've defined to hold the output.
Now, to start getting random values, you just call:
rand();
Which will spit out a different value each time you run it.
If you have any questions, please ask!
There is a very nice seed-based randomizing script already made. It can be found here.
ok guys, found out this is what I'm really looking for:
(((Math.sin(seed.value++)/2+0.5)*10000)%100)/100
It sends out even spreaded numbers, and I guess it's a lot simpler than any other number generator I've seen
I'm fairly new to javascript and box2d, i was wondering if someone knows how i can call a custom function when two objects collide. I tried using some examples that uses the b2ContactListener without any succes. I've placed an object above another and let the standard Box2d physics do it's thing.
I recieve two console outputs, the first is null and the second is Ball with the following code:
var listener = new Box2D.Dynamics.b2ContactListener;
listener.BeginContact = function(contact) {
console.log(contact.GetFixtureA().GetBody().GetUserData());
console.log(contact.GetFixtureB().GetBody().GetUserData());
};.
The two objects that need to collide are a b2_dynamicbody (ball) and a b2PolygonShape. (rectangle). Using bodyDef.userData = "Ball"; in my Ball.js and bodyDef.userData = "Mouse"; in my Mouse.js i try to identify if they are hit. Instead only the ball is displayed.
Next to that i'm sure this is not the correct way for detecting collision :P I hope i've explained it well enough, could somebody steer me in the right direction?
Ok I solved it myself, apparently I had to add my custom event to the world I create with box2d. So, the issue was solved by me reading big chunks of box2d documentation/manual which can be found here:
I started with adding a String as UserData() to every object which can collide and has to do something else next to just colliding. Using the following code:
bodyDef.userData = "Car";
note: every object has to have it's own unique string.
Then I created a new contact listener (formulated in my question above) and listened for fixtures colliding with each other. When that happens, I 'save' the fixtures UserData() in variables which I can then use to look what objects collide with each other.
var contactListener = new Box2D.Dynamics.b2ContactListener;
contactListener.BeginContact = function(contact) {
var fixA = contact.GetFixtureA().GetBody().GetUserData();
var fixB = contact.GetFixtureB().GetBody().GetUserData();
// if else statement here...
};
world.SetContactListener(contactListener);
Finally, I added the last statement world.SetContactListener(contactListener); to add the event to the world, making it possible for it to listen to collisions, which I forgot to add and thus was my problem.
Hope someone finds this usefull!
I'm working on a mini html5 game and I've stumbled upon an interesting problem, for me at least. And that's how to write the formulas for spells. For example:
SpellOne = baseDmg + level*dmgPerLvl + ratio*someStat;
that would be only one formula, but my point is, obviously if I write it like that it will calculate the damage from the start and SpellOne will become just a number, not a formula anymore. I can only think of two ways to do it, please lend me your help and give me any advice you have on this.
SpellOne.baseDmg = 50;
SpellOne.dmgPerLvl = 30;
SpellOne.baseCd = 15; // cd = spell cooldown
SpellOne.CdPerLvl = 2; // cd decreases by 2 every level
SpellOne.baseCost = 50; // mana or whatever resource
SPellOne.CostPerLvl = 20; // increases by 20 each level
and then once the player uses the spell it calculates the latest update:
SpellOneDamage = baseDmg + level*dmgPerLevel
The other method I have is something that came to me recently. I have around 100 heroes, each with like a dozen stats and then several spells that also have their own stats. But only 1 or 2 heroes will be loaded ever at one time, so am I wasting 'resources' or adding to many useless variables to the dom tree? Since only 1% of them are being used? Which made me think of the following solution:
Make each hero one long function, and when a person changes their hero, it runs the function and for example the 'currentHero' variable takes upon itself ALL the stats from the hero, spell stats too. So when the person changes heroes again, all that's really happening is 'currentHero' variable changes its values and thus you wont have to load ALL the hero stats pointlessly? It only loads a hero when selected. Any suggestions you guys have are greatly appreciated, thank you in advance!
Unless you're recalculating the damage for each spell many times per second, why not just make it a function and add it to the prototype of your character?
Character.prototype.getSpellDamage = function(name) {
var spell = Spells[name];
return spell.baseDamage
+ this.level * spell.damagePerLevel
+ spell.ratio * this[spell.someStat];
}
Now, you can just store the spells in a simple object:
var Spells = {
'foo bar': {
baseDamage: 40,
damagePerLevel: 20,
ratio: 0.5,
someStat: 'foo'
},
...
};