Opened 8 years ago

Closed 5 years ago

Last modified 5 years ago

#3578 closed defect (fixed)

[PATCH] Seed random sounds

Reported by: Thomas Owned by: Thomas
Priority: Should Have Milestone: Alpha 24
Component: Core engine Keywords:
Cc: Patch: Phab:D1584

Description (last modified by Stan)

Visual Actors now use the same seed for every sound.

Closes #1786.

Before using the random function it uses srand() with the actor id to seed the random function. I also made changes to rand.cpp. Apparently the first value returned by rand() is very dependent on the seed (see http://stackoverflow.com/questions/8599238/random-number-generation-in-c-first-number-not-very-random). Most actors were returning the same random value as they have similar seeds. Using modulo instead of dividing uses the least significant bits of the random value, yielding better results.

This is my first patch, so please tell me if I'm doing something wrong.

Attachments (4)

patch.patch (1.5 KB ) - added by Thomas 8 years ago.
Patch
patch2.patch (2.3 KB ) - added by Thomas 8 years ago.
patch2
sound.patch (1.9 KB ) - added by Thomas 8 years ago.
DeterministicSound.patch (1.8 KB ) - added by Thomas 8 years ago.

Download all attachments as: .zip

Change History (26)

by Thomas, 8 years ago

Attachment: patch.patch added

Patch

comment:1 by Stan, 8 years ago

Milestone: Alpha 19Alpha 20
Owner: set to Stan

We are currently in feature freeze so this will be for next Alpha version. Please attach patches to older tickets instead of creating new ones :)

Thanks for your patch it will hopefully be reviewed soon after release (in a few weeks) Feel free to take any other tickets.

Please edit this ticket and assign it to you next time you comment :) Feel free to work on any other tickets :)

EDIT : On a quick glance on the patch, if you edit a file you need to make sure the dates are correct : Rand.cpp should be updated to 2015 for now it's 2010

/* Copyright (c) 2010 Wildfire Games
 *

see the programmers section in the wiki http://trac.wildfiregames.com/wiki

Last edited 8 years ago by Stan (previous) (diff)

comment:2 by Stan, 8 years ago

Owner: Stan removed

comment:3 by Thomas, 8 years ago

Owner: set to Thomas

comment:4 by Thomas, 8 years ago

I see. Thanks for the info, I'll keep it in mind for next time.

by Thomas, 8 years ago

Attachment: patch2.patch added

patch2

comment:5 by Thomas, 8 years ago

I made a new patch and changed the copyright header.

comment:6 by Stan, 8 years ago

Thanks for your work :)

comment:7 by wraitii, 8 years ago

Patch looks fine and seems to work (or at least not to fail). I'm not sure what it changes though? Are sounds more varied? Not sure how to test this.

in reply to:  7 comment:8 by historic_bruno, 8 years ago

Keywords: review removed

Replying to wraitii:

Patch looks fine and seems to work (or at least not to fail). I'm not sure what it changes though? Are sounds more varied? Not sure how to test this.

The problem was explained in the other ticket. If you selected a unit, the voice pitch would be different each time. Each unit should have their own constant pitch.

This patch doesn't look like the right solution, it's calling srand() which reseeds the non-simulation RNG for the entire game? That is surely not desirable :) I think what you want to do is use a local rng_t or something like we do for actor variations in CObjectBase::CalculateRandomRemainingSelections.

comment:9 by elexis, 8 years ago

Milestone: Alpha 20Backlog

Backlogging due to lack of progress.

by Thomas, 8 years ago

Attachment: sound.patch added

comment:10 by Thomas, 8 years ago

Do you mean something like this? Unfortunately this code doesn't compile for me, and so far I couldn't figure out why, but the idea should work.

comment:11 by elexis, 8 years ago

Keywords: rfc added

comment:12 by Stan, 8 years ago

Thanks for keeping working on it: http://pastebin.com/zCXm3LmG Here is the build log in case that helps.

Last edited 8 years ago by Stan (previous) (diff)

comment:13 by Thomas, 8 years ago

It's been a while, but I thought I'd start again. I think I'm using Boost incorrectly, since most errors occur there.

comment:14 by echotangoecho, 8 years ago

I'm not sure whether you found it already, but the uni_dist expects a reference, not a pointer. I would change rng_t* rng to rng_t& t. Also, maybe it would be better if we used the C++11 <random> for this? I'm not sure whether this is allowed by our coding conventions though...

by Thomas, 8 years ago

Attachment: DeterministicSound.patch added

comment:15 by Thomas, 8 years ago

Thanks. The patch is working now.

comment:16 by wraitii, 7 years ago

Keywords: review added; rfc removed
Milestone: BacklogAlpha 22

Patch applies fine and seems to work following historicbruno's comment above.

I'm not entirely sure we need the complexity of Boost for the random number, but it's simple enough that that's nota big issue here. Bumping to review queue, if nobody sees concern with boost, I think we can commit this.

comment:17 by elexis, 7 years ago

Milestone: Alpha 22Work In Progress

Moving to the new WIP milestone.

comment:18 by echotangoecho, 7 years ago

We are already using boost in some parts of the code for random number generation. However, as <random> is supported by all our target compilers, it might be better to use that (which is pretty much equivalent to boost random).

Other than that, the patch looks good to me.

comment:19 by Stan, 6 years ago

Description: modified (diff)
Patch: D1584

Uploaded the patch, it was ready for committing back then so it should be pretty easy to check.

comment:20 by Stan, 6 years ago

Keywords: patch review removed
Patch: D1584Phab:D1584

comment:21 by Stan, 5 years ago

Resolution: fixed
Status: newclosed

In 22231:

Seed unit sounds so that their pitch and their gain are always the same for the same unit.
Fixes #3578
Reviewed by: @vladislavbelov

Differential Revision: https://code.wildfiregames.com/D1584

comment:22 by Stan, 5 years ago

Milestone: Work In ProgressAlpha 24
Note: See TracTickets for help on using tickets.