showing viewable monsters

If you have a module that you'd like comments on or would like to know how to create your very own module, post here

Moderator: Moderator

Post Reply
Message
Author
Shoob
Reaper
Posts: 1535
Joined: Mon Jan 22, 2007 6:31 pm
Location: East of the sun, west of the moon

showing viewable monsters

#1 Post by Shoob »

Code: Select all

require "engine.class"

module(..., package.seeall, class.make)

function _M:init(x, y, w, h, bgcolor)
	self.display_x = x
	self.display_y = y
	self.w, self.h = w, h
	self.bgcolor = bgcolor
	self.font = core.display.newFont("/data/font/VeraMono.ttf", 14)
	self.font_h = self.font:lineSkip()
	self.surface = core.display.newSurface(w, h)
end

--- Resize the display area
function _M:resize(x, y, w, h)
	self.display_x, self.display_y = x, y
	self.w, self.h = w, h
	self.surface = core.display.newSurface(w, h)
	self.changed = true
end

-- Displays the monsters
function _M:display()
	if not game.player or not game.player.changed then return self.surface end

	self.surface:erase(self.bgcolor[1], self.bgcolor[2], self.bgcolor[3])
	local h = 0
	local color = {0,200,255}
	
	self.surface:drawString(self.font, "Visible Monsters:", 0, h, 200, 200, 200) h = h + self.font_h

	for i, actor in ipairs(game.player.fov.actors_dist) do
		self.surface:drawString(self.font, actor.name, 0, h, actor.color_r, actor.color_g, actor.color_b) h = h + self.font_h
	end
	return self.surface
end
I use this in my module, but feel free to use it. game.lua will need to be edited to display it somewhere. (I use this instead of talents, since I dont really have any talents in game.

I am still working on this, going to eventually put up a version that combines monsters that have the same name and then puts a quantity by them.
Oliphant am I, and I never lie.

Shoob
Reaper
Posts: 1535
Joined: Mon Jan 22, 2007 6:31 pm
Location: East of the sun, west of the moon

Re: showing viewable monsters

#2 Post by Shoob »

ok... here is a version that combines like monsters... the code that does this could be made more efficient, but this works so I am happy. (the nested for loop bugs me, but I dont really see a much better solution right now, and it wont make much of a difference in game unless there are *hoards* of monsters, then I dont even want to think about it.)

Code: Select all

require "engine.class"

module(..., package.seeall, class.make)

function _M:init(x, y, w, h, bgcolor)
	self.display_x = x
	self.display_y = y
	self.w, self.h = w, h
	self.bgcolor = bgcolor
	self.font = core.display.newFont("/data/font/VeraMono.ttf", 14)
	self.font_h = self.font:lineSkip()
	self.surface = core.display.newSurface(w, h)
end

--- Resize the display area
function _M:resize(x, y, w, h)
	self.display_x, self.display_y = x, y
	self.w, self.h = w, h
	self.surface = core.display.newSurface(w, h)
	self.changed = true
end

-- Displays the monsters
function _M:display()
	if not game.player or not game.player.changed then return self.surface end

	self.surface:erase(self.bgcolor[1], self.bgcolor[2], self.bgcolor[3])
	local h = 0
	local monsters = {}
	local quantity = {}

-- initialize the array
	for i, actor in ipairs(game.player.fov.actors_dist) do
		monsters[i] = actor.name
		quantity[i] = 1
	end

-- eliminate duplicates and increase quantity
	for i, mon in ipairs(monsters) do
		for a, find_mon in ipairs(monsters) do
			if find_mon == mon then	-- yeah, don't ask, it wouldnt go in with my not in here so I did a dual layer if and an else :/
				if a == i then		--  bad me (my excuse is that this is my 2nd or 3rd day programming in lua)
				else
					if quantity[i] then quantity[i] = quantity[i] + 1 end -- only increase if exist
					quantity[a] = nil
					monsters[a] = nil --this will ensure that I dont show anything I dont want to
				end
			end
		end
	end
	
	self.surface:drawString(self.font, "Visible Monsters:", 0, h, 200, 200, 200) h = h + self.font_h

--now to make it work, keep in mind the places of each unique one is IDENTICAL to the place in this array, so calling monsters[i] should work
	for i, actor in ipairs(game.player.fov.actors_dist) do
		if monsters[i] and quantity[i] == 1 then
			self.surface:drawString(self.font, monsters[i], 0, h, actor.color_r, actor.color_g, actor.color_b) h = h + self.font_h
		elseif monsters[i] then
			self.surface:drawString(self.font, ("%s (x%d)"):format(monsters[i], quantity[i]), 0, h, actor.color_r, actor.color_g, actor.color_b) h = h + self.font_h		
		end
	end
	return self.surface
end
now to do a simple fix that will make turn a friendly/neutral/hostile npc's color to light green/blue/hostile (same color as in tactical mode), just use this instead of the last for:

Code: Select all

	for i, actor in ipairs(game.player.fov.actors_dist) do
		local color = {actor.color_r, actor.color_g, actor.color_b}
		if actor.factstate == "friendly" then color={0,255,0} end
		if actor.factstate == "neutral" then color={176,196,222} end
		if actor.factstate == "hostile" then color={255,0,0} end
		if monsters[i] and quantity[i] == 1 then
			self.surface:drawString(self.font, monsters[i], 0, h, color[1], color[2], color[3]) h = h + self.font_h
		elseif monsters[i] then
			self.surface:drawString(self.font, ("%s (x%d)"):format(monsters[i], quantity[i]), 0, h, actor.color_r, actor.color_g, actor.color_b) h = h + self.font_h		
		end
	end
ideally, there would be a keypress to change back and forth between the two.
Oliphant am I, and I never lie.

Shoob
Reaper
Posts: 1535
Joined: Mon Jan 22, 2007 6:31 pm
Location: East of the sun, west of the moon

Re: showing viewable monsters

#3 Post by Shoob »

hmmm, DG:

I changed your ActorsSeenDisplay.lua some. I changed line 69 to

Code: Select all

		if a.nb == 1 then self.surface:drawColorStringBlended(self.font, ("%s#WHITE#; distance [%s]"):format(a.name, table.concat(a.dist, ",")), 0, (i - 1) * self.font_h, a.color[1], a.color[2], a.color[3])
		else self.surface:drawColorStringBlended(self.font, ("%s (%d)#WHITE#; distance [%s]"):format(a.name, a.nb, table.concat(a.dist, ",")), 0, (i - 1) * self.font_h, a.color[1], a.color[2], a.color[3])
		end
just so that it wouldn't show the unnecessary (1) at the end, as it makes certain things (like uniques) strange looking (well, just because there is only one of them, nothing really breaks). more of a personal opinion :P

I do like yours better than mine though :)

[EDIT: Actually, I am going to change it again, but to check to see if the npc is a unique instead of just one] Here is the display adjusted for uniques instead of a quantity of 1.
Oliphant am I, and I never lie.

Post Reply