Shared life pools

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

Shared life pools

Post by Emera Nova »

So I'm currently working on getting two different enemies to share health in an odd way. Basically the script I have going atm looks like this

Code: Select all

function increasehealth()
    if (findEntity("Zen_1") ~= nil) and (findEntity("Zen_2") ~= nil) then
            Zen_1.monster:setHealth(Zen_2.monster:getHealth())
        end
    end
then I have another code doing

Code: Select all

function increasehealth()
    if (findEntity("Zen_1") ~= nil) and (findEntity("Zen_2") ~= nil) then
            Zen_2.monster:setHealth(Zen_1.monster:getHealth())
        end
    end
The goal is to make it so the player has to switch between attacking the two creatures to bring down their lifepools. If they dont attack both of them constantly they get healed up to the highest health between the two of them.

What its currently doing is when Zen_1 takes damage and the script runs it will deal that damage to Zen_2 instead of healing itself up to what ever health Zen_2 is at. However if you attack Zen_2 it will heal itself up to what Zen_1's health is if its lower than Zen_1.
Eburt
Posts: 69
Joined: Thu Feb 05, 2015 5:44 am

Re: Shared life pools

Post by Eburt »

You need to check to make sure the monster being healed actually has less health (otherwise it can just hurt it instead).

Also, it will be impossible for the party to ever kill the monsters unless they can one-hit them. Any time damage is dealt to one, it will just be reset to the other's health pool, i.e., full health. So I would include some logic to say that it can only heal to the other's health minus some amount. There are several ways of implementing, depends on the behavior you want...
Emera Nova
Posts: 146
Joined: Wed Jun 11, 2014 1:31 am

Re: Shared life pools

Post by Emera Nova »

As far as it being impossible, its not, theres a timer set at like 45 seconds that activates the script. The idea being that inside that 45 seconds you have to switch targets and deal damage to both of them to keep both of them damaged.

IE. Monster A has 100 hp and Monster B has 100 hp.
Player hits monster A for a total of 35 damage, and Monster B for 20 damage.
The timer then triggers healing monster A for 15 damage because the player failed to match the two damages.
New Life totals being 80 and 80.
User avatar
zimberzimber
Posts: 432
Joined: Fri Feb 08, 2013 8:06 pm

Re: Shared life pools

Post by zimberzimber »

If I understand your intentions correctly, this should work:

Code: Select all

function healup()
	local higherHP = 0
	local zen1 = findEntity("zen_1")
	local zen2 = findEntity("zen_2")
	if not zen1 or not zen2 then
		zen_timer.timer:disable()
		return
	end
	
	if zen1.monster:getHealth() > zen2.monster:getHealth() then
		higherHP = zen1.monster:getHealth()
	else
		higherHP = zen2.monster:getHealth()
	end
	
	zen1.monster:setHealth(higherHP)
	zen2.monster:setHealth(higherHP)
end
Name the timer that runs this script zen_timer

The problem with your scripts was that it first checked for the first monsters health, equalized the second monsters health to the first ones, and then set the second monsters health to the first ones. Something like...

Code: Select all

m1 = 75   m2 = 50
  do m2 = m1
m1 = 50   m2 = 50
  do m1 = m2
m1 = 50   m2 = 50
My asset pack [v1.10]
Features a bit of everything! :D
Emera Nova
Posts: 146
Joined: Wed Jun 11, 2014 1:31 am

Re: Shared life pools

Post by Emera Nova »

Thanks, was getting ready to try a if A < B then set A = B and if B < A then set B = A once I got off work. Glad I checked out the forums xD
User avatar
Isaac
Posts: 3172
Joined: Fri Mar 02, 2012 10:02 pm

Re: Shared life pools

Post by Isaac »

Here is yet another way to do it...

Code: Select all

-- Added to the monster component. Function expects the monsters to have the same name, and the id suffix '_twin1' and  '_twin2' 
			

			onDamage = function(self, damage, damageType)
					--if damageType ~= "fire" then  --optional filter for monsters with tile damager fire attacks (that would trigger self healing)
						for x = 1, 2 do
							if not self.go.id:match(x) then
							 local twin = findEntity(self.go.name.."_twin"..x)
							 if twin and twin.monster:getHealth() > self.go.monster:getHealth() then 
							 	self.go.monster:showDamageText("\n\nHealed by\nTwin's Essence!", vec(200,200,256))  self.go.monster:setHealth(twin.monster:getHealth())
							 	--optional healing effect  [can be commented out]
								if not self.go.effect then
							 		local effect = self.go:createComponent("Particle", "effect")
							 		effect:setParticleSystem("monster_heal")
							 		effect:setDestroySelf(true)
							 	end
								-----------------------------
							 end
							end 
						end
						return true
					--end
				 end,

-- Visible Healing particle effect [optional]
defineParticleSystem{
	name = "monster_heal",
	emitters = {
		-- fog
		{
			emissionRate = 10,
			emissionTime = 0,
			maxParticles = 1000,
			spawnBurst = true,
			boxMin = {-1.5, 0.0,-1.5},
			boxMax = { 1.5, 3.0, 1.5},
			sprayAngle = {0,360},
			velocity = {0.1,1},
			objectSpace = true,
			texture = "assets/textures/particles/fog.tga",
			lifetime = {3,3},
			color0 = {0.190294, 0.365000, 0.450490},
			opacity = 1,
			fadeIn = 2,
			fadeOut = 2,
			size = {2, 2},
			gravity = {0,0,0},
			airResistance = 0.1,
			rotationSpeed = 0.3,
			blendMode = "Additive",
		},

		-- stars
		{
			emissionRate = 200,
			emissionTime = 0,
			maxParticles = 1000,
			spawnBurst = true,
			boxMin = {-1.5,-0.5,-1.5},
			boxMax = { 1.5, 2.5, 1.5},
			sprayAngle = {0,30},
			velocity = {0.5,1.5},
			objectSpace = true,
			texture = "assets/textures/particles/teleporter.tga",
			lifetime = {1,1},
			color0 = {1.8,1.8,1.8},
			opacity = 1,
			fadeIn = 0.1,
			fadeOut = 0.1,
			size = {0.1, 0.3},
			gravity = {0,-1,0},
			airResistance = 0.1,
			rotationSpeed = 2,
			blendMode = "Additive",
		},

		-- small stars
		{
			emissionRate = 300,
			emissionTime = 0,
			maxParticles = 1000,
			spawnBurst = true,
			boxMin = {-1.5,-0.5,-1.5},
			boxMax = { 1.5, 2.5, 1.5},
			sprayAngle = {0,30},
			velocity = {0.5,1.0},
			objectSpace = true,
			texture = "assets/textures/particles/teleporter.tga",
			lifetime = {1,1},
			color0 = {1.6,1.8,1.8},
			opacity = 1,
			fadeIn = 0.1,
			fadeOut = 0.1,
			size = {0.05, 0.1},
			gravity = {0,-0.5,0},
			airResistance = 0.1,
			rotationSpeed = 2,
			blendMode = "Additive",
		}
	}
}

*Working editor project map
https://www.dropbox.com/s/qwdymjx3amhn6 ... s.zip?dl=0

I found it to be a fun fight. 8-)
Post Reply