Randomizing attacks

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
Emera Nova
Posts: 146
Joined: Wed Jun 11, 2014 1:31 am

Randomizing attacks

Post by Emera Nova »

So I'm looking to add a bit more randomness to my boss fights when it comes to attacks happening at certain points of their health. I'd like to build a boss that can have certain attacks fire off in different orders each time you face it.

Basically for example at 500 lp the room will have fireballs fall down from the ceiling above, now if you are on your second or third try of the boss you would have figured out the pattern of which they fall and where. I want to make it so when it triggers to fall from the ceiling they will launch down in a different order by using a random check and based on that check activate a particular spawner.

This part I'm able to get down pretty easy using this script

Code: Select all

function setatta()
	local h = 1
	local r = math.random()
		if r < 0.3 then
		spawner_9.controller:activate()
		elseif r < 0.6 then
			spawner_10.controller:activate()
		else
			spawner_11.controller:activate()
		end
	end
But could I use this random roll code to make it so the phases are randomised? Making it so each time you fight the boss the order of the special attacks are different, or maybe even making it so there are 5 total things that could happen and it runs 3 of them. Of course I would need some way of checking to see if it already ran(wouldn't want them to run a second time(or depending on the attack you would)).

So yea just looking for some feedback and help on it, do you think it could work using that code or would another one be better to use?
User avatar
Isaac
Posts: 3192
Joined: Fri Mar 02, 2012 10:02 pm

Re: Randomizing attacks

Post by Isaac »

Perhaps a table of spawner IDs, where you randomly pick one key to use; remove it, then randomly pick another ~until there aren't any more.
Emera Nova
Posts: 146
Joined: Wed Jun 11, 2014 1:31 am

Re: Randomizing attacks

Post by Emera Nova »

Could you put in an example of how to do that? :o
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Re: Randomizing attacks

Post by cromcrom »

Isaac's better
Last edited by cromcrom on Wed Nov 11, 2015 9:03 am, edited 1 time in total.
A trip of a thousand leagues starts with a step.
User avatar
Isaac
Posts: 3192
Joined: Fri Mar 02, 2012 10:02 pm

Re: Randomizing attacks

Post by Isaac »

An example:
Call the script from the console to see the output.

Code: Select all

print(math.random(1000), "unseeded ~random")

function randomize()
	local seed1 = spawn("base_floor_decoration").id
	local seed2 = spawn("base_floor_decoration").id
	local seed = tonumber(seed1) * tonumber(seed2)
	math.randomseed(seed)
	findEntity(seed1):destroyDelayed()
	findEntity(seed2):destroyDelayed()
	
	print(seed, "seeded random; (the one we use).")
end

randomize()

function randomizedEffects()
	local   effect = {}
			effect.spawnerIDs = {
								"spawner_1",
								"spawner_2",
								"spawner_3",
								"spawner_4",
								"spawner_5",
								"spawner_6",
								"spawner_7",
								"spawner_8",
								"spawner_9",
								"spawner_10",
								"spawner_11",
								"spawner_12",
								"spawner_13",
								"spawner_14",
								"spawner_15",
								"spawner_16",
								"spawner_17",
								"spawner_18",
								"spawner_19",
								"spawner_20",
								}
			effect.randomized = {}
	
	for x = #effect.spawnerIDs, 1, -1 do
		local id = math.random(x)
		table.insert(effect.randomized, effect.spawnerIDs[id])
		table.remove(effect.spawnerIDs, id)	
	end
	
	for x = 1, #effect.randomized do
		print(effect.randomized[x])
	end
end

*Note: The randomize function is a bit of an experiment; I've not actually used it before in a serious map... It just occurred as an odd thought to try using a generated ID as the seed. It seems to work fairly well, so far. I'd be interested in learning of any downsides to this.
SpoilerShow
Experimental alternate version that picks two at a time, instead of one:

Code: Select all

    print(math.random(1000), "unseeded ~random")

    function randomize()
       local seed1 = spawn("base_floor_decoration").id
       local seed2 = spawn("base_floor_decoration").id
       local seed = tonumber(seed1) * tonumber(seed2)
       math.randomseed(seed)
       findEntity(seed1):destroyDelayed()
       findEntity(seed2):destroyDelayed()
       
       print(seed, "seeded random; (the one we use).")
    end

    randomize()

    function randomizedEffects()
       local   effect = {}
             effect.spawnerIDs = {
                            "spawner_1",
                            "spawner_2",
                            "spawner_3",
                            "spawner_4",
                            "spawner_5",
                            "spawner_6",
                            "spawner_7",
                            "spawner_8",
                            "spawner_9",
                            "spawner_10",
                            "spawner_11",
                            "spawner_12",
                            "spawner_13",
                            "spawner_14",
                            "spawner_15",
                            "spawner_16",
                            "spawner_17",
                            "spawner_18",
                            "spawner_19",
                            "spawner_20",
                            }
             effect.randomized = {}
       
       for x = #effect.spawnerIDs, 1, -2 do
          local id = math.random(x)
		  local id2 = math.random(x)
				for x = 1, 999 do --ha!
					if id ~= id2 then
						break
					end
					id2 = math.random(x)
				end	
		
          table.insert(effect.randomized, effect.spawnerIDs[id])
          table.remove(effect.spawnerIDs, id)
		  table.insert(effect.randomized, effect.spawnerIDs[id2]) 
		  table.remove(effect.spawnerIDs, id2)   
       end
       
       for x = 1, #effect.randomized do
          print(effect.randomized[x])
       end
    end

Post Reply