spawning just one mob fails

Post Reply
SystemError
Member
Posts: 15
Joined: Thu Dec 27, 2018 15:02
In-game: Havoc

spawning just one mob fails

by SystemError » Post

Hello!

Can somebody please tell me how to put a stop in this function so that mob won't spawn endless guards ?

Code: Select all

do_custom = function(self) 
   local pos = self.object:getpos() 
         pos.y = pos.y + 3 
      if self.health < 450 then 
         self.object:set_properties({visual_size = {x=1.8, y=1.8},}) 
         self.damage = 50 
      end 
      if self.health < 400 then 
         self.object:set_properties({ visual_size = {x=2.7, y=2.7}, collisionbox = {-1.2,-1.2,-0.9, 1.2,1.2,0.9} }) 
         self.damage = 100 
      end 
      if self.health < 350 then 
         self.object:set_properties({reach = 2.8, visual_size = {x=3.6, y=3.6},collisionbox = {-1.8,-1.8,-1.3, 1.8,1.8,1.3},}) 
         self.damage = 200 
      end 
      if self.health < 250 then 
         self.object:set_properties({reach = 3.3, visual_size = {x=4.8, y=4.8},collisionbox = {-2.4,-2.4,-2, 2.4,2.4,2},}) 
         self.damage = 400 
      end 
      if self.health < 100 then 
         self.armor = 25 
      end 
      if self.health  < 99 then 
         self.runaway = true 
      end 
      if self.health > 5 and self.health < 51 and self.state == "attack" then 
         minetest.add_entity(pos, "mobs_medieval:TBgua") 
         self.object:set_hp(self.health + 40) 
         self.object:set_properties({health = 90}) 
         return 
      end 
      if self.health > 11 and self.health < 91 then 
         self.object:set_hp(99) 
         self.object:set_properties({health = 99}) 
      end 
      if self.health > 6 and self.health < 90 then 
         self.object:set_hp(self.health + 9) 
         self.object:set_properties({health = 99}) 
      end 
   end, 
that's the mob spawning part:

Code: Select all

      if self.health > 5 and self.health < 51 and self.state == "attack" then 
         minetest.add_entity(pos, "mobs_medieval:TBgua") 
         self.object:set_hp(self.health + 40) 
         self.object:set_properties({health = 90}) 
         return 
      end 

I have already tried to end the spawning by giving the mob health back but that doesnt seem to work aswell,
so I also limited it on the mob's attack state which doesnt really help either until every enemy in range has been destroyed.. (that doeant take really long but until then i still have like 50-100 guards already in most cases )

:{

Please only show me examples, I can't program
Maybe a timer, or how to propperly end that, or how to use return, or something..
Are you threatening me !?

Astrobe
Member
Posts: 571
Joined: Sun Apr 01, 2018 10:46

Re: spawning just one mob fails

by Astrobe » Post

The function do_custom provides the time elapsed since the last invocation of that routine, that can be used as an interval timer. For instance:

Code: Select all

	do_custom=function(self, dtime)
		self.meld_timer=(self.meld_timer or 31) - dtime
		if self.meld_timer>0 then return true end
		local pos=self.object:get_pos()
		if not pos then return true end
		local entities=minetest.get_objects_inside_radius(pos, 100)
		for _,e in ipairs(entities) do
			if e:is_player() then0
				local p=minetest.find_node_near(e:get_pos(),1, "melding:node", true)
				if p then
					minetest.swap_node(p, {name="air"})
					self.meld_timer=1
					return true
				end
			end
		end
		self.meld_timer=31
		return true
	end
Context: this function is run by a friendly NPC. It checks if there's a player in a 100 m radius, and if so scans for and removes "melding" (nasty blocks) in a 1 m radius around him/her. It does so every 31 seconds (first 2 lines), except when it has removed one "melding" block, in which case it retries 1 second later. dtimer's resolution is sub-second, so you could have it execute every 3.5 or 0.5s, with relatively good accuracy.
My game? It's Minefall.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests