03.07.2012, 08:00
Quote:
I'm just scripting a minigame, the one where in a grid of square objects a random object is deleted every now and then and players have to survive. (I think it's called "Don't Get Wet" so the general prefix is "DGW")
I'm concerned about how I select the random object, right now I have this code: pawn Код:
When there is only one object left, this code can take a lot of time and keep the server waiting sometimes, because it's random there's no knowing what object it might choose. For instance, for 1000 calls of that while loop the randomiser could always choose an invalid one and keep missing the one remaining block. I couldn't think of a better way (Probably because I woke up not long ago!) can anyone else? I'm sure there's a much better way to do this! ![]() |
I suppose you know starting object IDs which can be deleted( so you are not deleting some random houses in LV if the game is in LS. You want only rocks in game to be able to be deleted ).
If so, create an array where you store the IDs and at the end '\0'. The number of IDs in your array equals strlen(array)-you can use in radom().
Once you delete a object, clean the place in array and put in order other IDs. ( I know this is confusing. Whit the example it will be a much clearer. IE: Starting array array[]={1,34,45,23,5,35,76,67,...}, once you have randomly deleted 23 array will look like array[]={1,34,45,?,5,35,76,67,...}, now you need every array slot after 23 move one place left so it will look like array[]={1,34,45,5,35,76,67,...} and you can start from beginning. Don't forget to move the '\0' also one place left. )
I think this is the best way.