Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Announcement.lua - Display custom announcements  (Read 2404 times)

Meph

  • Bay Watcher
    • View Profile
    • worldbicyclist
Announcement.lua - Display custom announcements
« on: August 08, 2013, 03:50:41 pm »

Yeah, my first lua script. It is simple: When the script is run, you will get an ingame announcement. Can be any color, the examples are all yellow.

But linked with autosyndrome and reactions, or triggerautosyndrome (for the next dfhack release, can call arguments from interactions, which means simple creatures) it can greatly increase the amount of info a player will get:



Whenever an important object is created, I can let you know. Golems, Guns, Mages, anything. A masterpiece book maybe? A blueprint? A dwarf is painting a pentagram, showing up in the combat-log? No more, it can be an announcement. Same for bards that play music, or dwarves that hold speeches.

Not sure how much I can and want to convert, but its at least an option now. :)

Code: [Select]
-- makes custom announcements, for example when important items are created.
-- needs to be called with autosyndrome, using a boiling rock with synclass:announcement.

-- i dont know what this does, but ok, lets leave it here.
local utils = require 'utils'

--again, no idea, but if I delete it, it wont work anymore. I could probably delete half of it though. It has something to do with the args, but I know I dont need entities. Doesnt matter.
local function findCiv(arg)
local entities = df.global.world.entities.all
if tonumber(arg) then return arg end
if arg and not tonumber(arg) then
for eid,entity in ipairs(entities) do
if entity.entity_raw.code == arg then return entity end
end
end
end

-- yep, whatever.
local args = {...}

-- Yeah, useful code:
if not args then qerror("Needs an argument, for example: announcement golem") end

-- yep, whatever.
local EventType = args[1]

-- only works in fort/adv mode, not in main menu/startup.
if not dfhack.isMapLoaded() then
    qerror('Map is not loaded.')
end

-- list of all possible arguments.
local function eventTypeIsNotValid()
local eventTypes =
{
"golem",
"loot",
"lich",
"greatweapon",
}
for _,v in ipairs(eventTypes) do
if args[1] == v then return false end
end
return true
end

-- if your argument doesnt match the list above
if eventTypeIsNotValid() then
qerror('Invalid argument. You need the name of an announcement to call.')
end

-- here comes your text
-- you can do ~60 symbols before a line-break is added. Keep it short for best effect.
-- color should accept all color tokens that are valid in raws.
-- Text accepts all ANSI symbols, even things like ©¥¤¢†‡‰, calling tiles from the tileset.
-- If played without TrueTypeFont, this allows to make announcements that show tiles.
local function announcement_golem()
dfhack.gui.showAnnouncement(
    'A mighty golem has been created.',
    COLOR_BROWN, true
)
end

local function announcement_loot()
dfhack.gui.showAnnouncement(
    'Your warriors succesfully raided their target.',
    COLOR_BROWN, true
)
end

local function announcement_lich()
dfhack.gui.showAnnouncement(
    'Blinded by the promise of power, a dwarf has turned into a lich!',
    COLOR_BROWN, true
)
end

local function announcement_greatweapon()
dfhack.gui.showAnnouncement(
    'A two-handed weapon has been created.',
    COLOR_BROWN, true
)
end

-- for each announcement you need another line here.
if EventType=="golem" then announcement_golem()
elseif EventType=="loot" then announcement_loot()
elseif EventType=="lich" then announcement_lich()
elseif EventType=="greatweapon" then announcement_greatweapon()
end

-- This script has been made possible by code stolen from Siren.lua, ForceEvent.lua (Putnam) and brute-force coding by me, Meph. Dont laugh, and if anyone knows how to add a units name ("Dwarf1 has created a two-handed weapon") let me know.
« Last Edit: August 08, 2013, 03:53:08 pm by Meph »
Logged
::: ☼Meph Tileset☼☼Map Tileset☼- 32x graphic sets with TWBT :::
::: ☼MASTERWORK DF☼ - A comprehensive mod pack now on Patreon - 250.000+ downloads and counting :::
::: WorldBicyclist.com - Follow my bike tours around the world - 148 countries visited :::

silentdeth

  • Bay Watcher
    • View Profile
    • Let's Play Dwarf Fortress: Mastwork Mod Season 1
Re: Announcement.lua - Display custom announcements
« Reply #1 on: August 08, 2013, 04:37:31 pm »

Really glad to be rid of the combat spam.

In case you really are unclear about what those lines do in the code, and not just joking:
Code: [Select]
local utils = require 'utils'
Imports the lua file called utils, that is the "require 'utils'" part. This allows you to use the functions defined in the file utils. The 'local utils' defines how you can access those functions, it is an alias for the file utils.lua, you are also calling it utils. You would then access any function in utils.lua with utils.functionname(), the same way you use dfhack.isMapLoaded().

Code: [Select]
-- yep, whatever.
local args = {...}

This captures the input arguments sent to the script, the stuff typed after the script name. i.e. In 'clean all',  all is the arg.

Code: [Select]
-- Yeah, useful code:
if not args then qerror("Needs an argument, for example: announcement golem") end

Check to see if any arguments were passed, if not prints an error message. Most other scripts print a help menu here.

Code: [Select]
-- yep, whatever.
local EventType = args[1]

Above you defined args as the arguments passed the script. More specifically you defined args as an array. In programming that is essentually a column of data value like a column in excel. To access each cell of the column you type the array name follow by the index of the value you want inclosded in square brackets [1] in this instance. LUA starts counting at 1, many other programming languages starts at 0. Thus here args[1] is the first value in the array args, in C++ it would be args[0].

In total:
Code: [Select]
local EventType = args[1]
Creates a new variable and gives it the value of the first argument. You don't have to do this, you could just use args[1] directly, but it doesn't really hurt anything.

Code: [Select]
-- list of all possible arguments.
-snip-
{
"golem",
"loot",
"lich",
"greatweapon",
this list is probably what you should print out instead of the error message.

Code: [Select]
--again, no idea, but if I delete it, it wont work anymore. I could probably delete half of it though. It has something to do with the args, but I know I dont need entities. Doesnt matter.
local function findCiv(arg)
local entities = df.global.world.entities.all
if tonumber(arg) then return arg end
if arg and not tonumber(arg) then
for eid,entity in ipairs(entities) do
if entity.entity_raw.code == arg then return entity end
end
end
end

It does not look like you actually use this. I can not see why it would not work if it was not there.


In the screenshot it looks yellow, color says brown?

Add a 'you have discovered magma' for the blood of armok, and make it only run once. You shouldn't have too much difficulty figuring out that (there are some examples in the dfhack folder)
Logged
Let's Play Dwarf Fortress: Masterwork Mod Season 1 | Season 2
Let's Play Kobold Fortress: Masterwork Mod Season 1 | Season 2

Meph

  • Bay Watcher
    • View Profile
    • worldbicyclist
Re: Announcement.lua - Display custom announcements
« Reply #2 on: August 09, 2013, 07:40:55 pm »

Thank you, I am really is ignorant as I sound in the code comments. ;) Your info helps a lot for possible additions. I implemented this code into the research system for a test, and it works beautiful.



I will see which parts of the mod can benefit from this, and slowly convert everything to give custom announcements. :) It can easily be made optional, because the announcement is triggered by a boiling rock. I simply disable the product, and no more announcements. I'll start with item production, the "triggered by interaction/creatures" has to wait till dfhack r4.
Logged
::: ☼Meph Tileset☼☼Map Tileset☼- 32x graphic sets with TWBT :::
::: ☼MASTERWORK DF☼ - A comprehensive mod pack now on Patreon - 250.000+ downloads and counting :::
::: WorldBicyclist.com - Follow my bike tours around the world - 148 countries visited :::