Page 1 of 1

Implementing a speed-based turn counter system in a roguelike

Posted: Tue Sep 06, 2016 12:15 am
by Applebaps
Never mind, I figured it out on my own. See my 2nd post for the solution.

Re: Implementing a speed-based turn counter system in a roguelike

Posted: Tue Sep 06, 2016 6:35 pm
by Zireael
Are you using any libraries? 31 MB for a .love is fairly large!

Also I can't NOT recommend ROTLove if you're making a roguelike game. Their Scheduler class covers pretty much all possible scheduling variants.

EDIT: Check out my Veins of the Earth thread in games and creations :P I'm making a roguelike, too!

Re: Implementing a speed-based turn counter system in a roguelike

Posted: Tue Sep 06, 2016 6:41 pm
by raidho36
The idea is that when you make a move, you attach a specific amount of AP to it, so you can't move until those AP count down to 0. The turn manager counts down every occuring move's AP down, and when any of those reaches 0 it lets the entity make another move.

Re: Implementing a speed-based turn counter system in a roguelike

Posted: Wed Sep 07, 2016 3:12 am
by Applebaps
BIG EDIT: I figured it out.

Code: Select all

-- in "Mob.lua" module
function Mob:roll_call()
		who_is_here[self.y][self.x] = self.is_monster -- puts true or false in the space
		self.turn_counter = self.turn_counter * math.random(0.9, 1.1) -- randomizes turns a little
		table.insert(allMobs, self)
end

function Mob:move(dx, dy)
   -- update "who is here"
	 who_is_here[self.y][self.x] = nil -- erases the previous grid entry
   self.x, self.y = self.x + dx, self.y + dy -- moves
   who_is_here[self.y][self.x] = self.is_monster -- puts true or false in the space (for combat)
	 self.turn_counter = 1 * self.speed -- resets turn counter inside its own table
	 for k,v in ipairs(allMobs) do
		 if v == self then -- looks for itself in allMobs
	 		v.turn_counter = self.turn_counter -- replaces its own turn_counter in the spot paired with whatever has that name with the fresh value
		 end
	 end
	 self.my_turn = false -- flags turn is over
	 turn_active = false -- flags no active turn
end

-- in "turn.lua" module
function turn.decrement_counters(dt) -- runs in Dungeon.update(dt) and subtracts from all turn_counters
	if turn_active == false then
		for k,v in ipairs(allMobs) do -- runs down each entry in allMobs table, k is the index, v is the table associated with it (the mob)
			v.turn_counter = v.turn_counter - (10 * dt) -- v is the mob
			if v.turn_counter < 0 then
				v.turn_counter = 0 -- catches negative values and scoops em up to zero
				turn_active = true -- flags that there's an active turn (halts decrementing)
				whose_turn = v -- sends the mob that's in the #1 slot to a variable for outside use
				whose_turn.my_turn = true
				return
			end
		end
	end
end
That's it, that's all it took.