Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Topics - llamazing

Pages: [1]
Development / Generating a map image from map .dat file
« on: March 11, 2018, 08:41:09 am »
Given the discussion on the Ocean's Heart project topic about making a map menu, I wanted to test the feasibility of generating a mini-map image from the info in the map .dat file. The endeavor has been a success.

What I did is read the map .dat file from a lua script, which gives the size, position and stacking order of all tiles. Also reading the tileset .dat file used by that map gives the ground type (e.g. traversable, wall, etc.) for each tile. I assigned colors to the tiles based on the ground type. Then on a surface I drew rectangles whose size and position corresponds to each tile at 1/8th scale, drawn in the same order as on the map. I drew layers 0 to 2 separately and ended up with this result for the overworld maps:

Note that sometimes the layer 2 image got clipped because the window I was using was only 480 pixels in height. It didn't appear to be losing anything in the cases where it was clipped.

I then manually overlaid the layers, using 50% opacity for layers 1 & 2, and lined up the maps for how they fit together. In hindsight I realize I could have made the script to do the same thing.

Note that the Hourglass Fort Outside and Naerreturn Bay maps overlap with each other, so I moved the Hourglass map off to the side.

This mapping technique could be useful either in generating a reference image from which to create hand-drawn images of the maps, or a modified version of the script could be used to have the quest itself generate the map images to use for the map menu. For pixel-precise accuracy, the purple border between maps (so that edge teletranpsorters are visible) should be removed.

In order to reproduce my results and run the script, do the following:
1) Download version 0.1 of the Ocean's Heart project from the topic linked above (back up any existing save files if you already have the quest)
2) Change the quest size to 640 by 480 in quest.dat
3) In main.lua of that quest comment out line 5, require("scripts/features"), so that the HUD is not displayed
4) Replace the scripts/game_manager.lua file with this one:
Code: Lua
  1. local game_manager = {}
  3. local initial_game = require("scripts/initial_game")
  4. require("scripts/multi_events")
  6. -- Starts the game from the given savegame file,
  7. -- initializing it if necessary.
  8. function game_manager:start_game(file_name)
  10.         local exists =
  11.         local game =
  12.         if not exists then
  13.                 -- Initialize a new savegame.
  14.                 initial_game:initialize_new_savegame(game)
  15.         end
  16., 480) --to prevent display from scaling at 2x
  17.         game:start()
  19.         local map_layers = require("scripts/menus/display_map")
  21.         function game:on_draw(dst_surface)
  22.                 dst_surface:fill_color{0,0,0} --fill black, don't want to see what's underneath
  24.                 for layer = 0,2 do
  25.                         local surface = map_layers[layer]
  26.                         if surface then
  27.                                 local width, height = surface:get_size()
  28.                                 surface:draw(dst_surface, 1, layer*(height+1)+1) --1 px from edge of screen with images separated by 1 px
  29.                         end
  30.                 end
  31.         end
  32. end
  34. return game_manager
5) Add this script to the scripts/menus directory, named display_map.lua:
Code: Lua
  1. local map_id = "new_limestone/limestone_present"
  2. --local map_id = "goatshead_island/goatshead_harbor"
  3. --local map_id = "goatshead_island/west_goat"
  4. --local map_id = "goatshead_island/goat_hill"
  5. --local map_id = "goatshead_island/lighthouse_cape"
  6. --local map_id = "goatshead_island/poplar_forest"
  7. --local map_id = "goatshead_island/poplar_coast"
  8. --local map_id = "goatshead_island/crabhook_village"
  9. --local map_id = "Yarrowmouth/yarrowmouth_village"
  10. --local map_id = "Yarrowmouth/juniper_grove"
  11. --local map_id = "Yarrowmouth/foothills"
  12. --local map_id = "Yarrowmouth/silent_glade"
  13. --local map_id = "Yarrowmouth/naerreturn_bay"
  14. --local map_id = "Yarrowmouth/kingsdown"
  15. --local map_id = "Yarrowmouth/hourglass_fort/outside"
  16. --local map_id = "ballast_harbor/ballast_harbor"
  18. local tilesets = {}
  19. local maps = {}
  21. local SCALE_FACTOR = 8 --draw maps at 1/8th actual size
  23. --color to draw tile based on ground property; ignore if not listed
  24. local tile_colors = {
  25.         traversable = {0, 255, 0}, --green
  26.         wall = {127, 127, 127}, --grey
  27.         low_wall = {204, 204, 204}, --light grey
  28.         teletransporter = {255, 255, 0}, --yellow; this is a type of entity, not a tile ground property
  29.         shallow_water = {0, 255, 255}, --light blue
  30.         deep_water = {0, 0, 255}, --blue
  31.         hole = {0, 0, 0}, --black
  32.         ladder = {255, 0, 255}, --pink
  33.         lava = {255, 0, 0}, --red
  34.         prickles = {255, 127, 0}, --orange
  35.         ice = {127, 255, 255}, --pale blue
  36. }
  38. --load tileset .dat file to determine ground property of each tile
  39. local function load_tileset(tileset_id)
  40.         local tileset = {}
  42.         local env = {}
  43.         function env.tile_pattern(properties)
  44.                 local id =
  45.                 assert(id, "tile pattern without id")
  46.                 id = tonumber(id)
  47.                 assert(id, "tile pattern id must be a number")
  49.                 local ground = properties.ground
  50.                 assert(ground, "tile pattern without ground")
  52.                 if tile_colors[ground] then --ignore ground properties that don't have a color assigned
  53.                         tileset[id] = tile_colors[ground] --link the color to use with the tile id
  54.                 end
  55.         end
  57.         setmetatable(env, {__index = function() return function() end end})
  59.         local chunk = sol.main.load_file("tilesets/"..tileset_id..".dat")
  60.         setfenv(chunk, env)
  61.         chunk()
  63.         return tileset
  64. end
  66. --load map .dat file to get list of tiles used
  67. local function load_map(map_id)
  68.         local map = { tiles = {} }
  70.         local env = {}
  72.         --properties stores the size and coordinates for the map and the tileset used
  73.         function
  74.                 local x = tonumber(properties.x)
  75.                 assert(x, "property x must be a number")
  76.                 local y = tonumber(properties.y)
  77.                 assert(y, "property y must be a number")
  79.                 local width = tonumber(properties.width)
  80.                 assert(width, "property width must be a number")
  81.                 local height = tonumber(properties.height)
  82.                 assert(height, "property height must be a number")
  84.                 local tileset = properties.tileset
  85.                 assert(tileset, "properties without tileset")
  87.                 map.x = x
  88.                 map.y = y
  89.                 map.width = width
  90.                 map.height = height
  91.                 map.tileset = tileset
  92.         end
  94.         --each tile defines a size, coordinates and layer as well as the tile id to use
  95.         function env.tile(properties)
  96.                 local pattern = properties.pattern --pattern is the tile id
  97.                 assert(pattern, "tile without pattern")
  98.                 pattern = tonumber(pattern)
  99.                 assert(pattern, "tile pattern must be a number")
  101.                 local layer = properties.layer
  102.                 assert(layer, "tile without layer")
  103.                 layer = tonumber(layer)
  104.                 assert(layer, "tile layer must be a number")
  106.                 local x = tonumber(properties.x)
  107.                 assert(x, "tile x must be a number")
  108.                 local y = tonumber(properties.y)
  109.                 assert(y, "tile y must be a number")
  111.                 local width = tonumber(properties.width)
  112.                 assert(width, "tile width must be a number")
  113.                 local height = tonumber(properties.height)
  114.                 assert(height, "tile height must be a number")
  116.                 table.insert(map.tiles, {
  117.                         pattern = pattern,
  118.                         layer = layer,
  119.                         x = x,
  120.                         y = y,
  121.                         width = width,
  122.                         height = height,
  123.                 })
  124.         end
  126.         --also extract teletransporter usage to be able to draw teletransporter locations
  127.         function env.teletransporter(properties)
  128.                 local layer = properties.layer
  129.                 assert(layer, "tile without layer")
  130.                 layer = tonumber(layer)
  131.                 assert(layer, "tile layer must be a number")
  133.                 local x = tonumber(properties.x)
  134.                 assert(x, "tile x must be a number")
  135.                 local y = tonumber(properties.y)
  136.                 assert(y, "tile y must be a number")
  138.                 local width = tonumber(properties.width)
  139.                 assert(width, "tile width must be a number")
  140.                 local height = tonumber(properties.height)
  141.                 assert(height, "tile height must be a number")
  143.                 table.insert(map.tiles, {
  144.                         pattern = "teletransporter", --instead of using tile pattern to determine color, tile_colors has a "teletransporter" entry
  145.                         layer = layer,
  146.                         x = x,
  147.                         y = y,
  148.                         width = width,
  149.                         height = height,
  150.                 })
  151.         end
  153.         setmetatable(env, {__index = function() return function() end end})
  155.         local chunk, err = sol.main.load_file("maps/"..map_id..".dat")
  156.         setfenv(chunk, env)
  157.         chunk()
  159.         return map
  160. end
  162. local function generate_map(map_id)
  163.         assert(type(map_id)=="string", "Bad argument #1 to 'generate_map' (string expected)")
  165.         --load map info from .dat file if not already loaded
  166.         if not maps[map_id] then maps[map_id] = load_map(map_id) end
  167.         local map = maps[map_id]
  169.         --load tileset info from .dat file if not already loaded
  170.         if not tilesets[map.tileset] then tilesets[map.tileset] = load_tileset(map.tileset) end
  171.         local tileset = tilesets[map.tileset]
  173.         --create a surface for each layer
  174.         local layers = {}
  175.         for layer = 0,2 do
  176.                 layers[layer] = sol.surface.create(map.width/SCALE_FACTOR+2, map.height/SCALE_FACTOR+2) --include room for 1px border
  177.                 layers[layer]:fill_color({127, 0, 255}, 0, 0, map.width/SCALE_FACTOR+2, 1) --top 1px purple border
  178.                 layers[layer]:fill_color({127, 0, 255}, 0, 0, 1, map.height/SCALE_FACTOR+2) --left 1px purple border
  179.                 layers[layer]:fill_color({127, 0, 255}, 0, map.height/SCALE_FACTOR+1, map.width/SCALE_FACTOR+2, 1) --bottom 1px purple border
  180.                 layers[layer]:fill_color({127, 0, 255}, map.width/SCALE_FACTOR+1, 0, 1, map.height/SCALE_FACTOR+2) --right 1px purple border
  181.         end
  183.         --draw each tile
  184.         for _,tile in ipairs(map.tiles) do
  185.                 local pattern = tile.pattern
  186.                 local tile_color = tile_colors[pattern] or tileset[pattern]
  188.                 --draw the tile as solid color on surface corresponding to the correct layer
  189.                 if tile_color and layers[tile.layer] then --ignore corner and empty tiles
  190.                         layers[tile.layer]:fill_color(
  191.                                 tile_color,
  192.                                 tile.x/SCALE_FACTOR+1,
  193.                                 tile.y/SCALE_FACTOR+1,
  194.                                 tile.width/SCALE_FACTOR,
  195.                                 tile.height/SCALE_FACTOR
  196.                         )
  197.                 end
  198.         end
  200.         return layers
  201. end
  203. return generate_map(map_id)

Bugs & Feature requests / Straight movement bug with set_max_distance?
« on: September 08, 2017, 07:31:41 am »
I'm trying to use a straight movement, and the straight_movement:set_max_distance(max_distance) function is not working the way that I'd expect.

Below is simplified code of what I am trying to do. At 4 seconds the table obj is moved 100 pixels to the right, and then 3 seconds after that it is moved 100 pixels to the left to return it to its original position.

The problem is that the set_max_distance function appears to be calculating the distance from (0,0), when I'd expect it to calculate the distance from what the coordinates were at the start of the movement. The result is that the second movement ends up being a 200 pixel movement to the left instead of the 100 pixels that I wanted (the object stops when x=-100, which is 100 pixels from (0,0)).

Code: Lua
  1. local obj = {x=0, y=0}
  2. sol.timer.start(4000, function()
  3.   print(obj.x) --0 at 4 seconds
  5.   local movt1 = sol.movement.create"straight"
  6.   movt1:set_speed(100)
  7.   movt1:set_angle(0)
  8.   movt1:set_max_distance(100)
  9.   movt1:start(obj)
  11.   function movt1:on_finished() print(obj.x) end --obj.x=100 at 5 seconds
  13.   sol.timer.start(3000, function()
  14.     print(obj.x) --100 at 7 seconds
  16.     local movt2 = sol.movement.create"straight"
  17.     movt2:set_speed(100)
  18.     movt2:set_angle(math.pi)
  19.     movt2:set_max_distance(100)
  20.     movt2:start(obj)
  22.     function movt2:on_finished() print(obj.x) end --obj.x=-100 at 9 seconds; expected obj.x=0 at 8 seconds
  23.   end)
  24. end)

Your projects / Tech Demo using Cythera style interactive dialogs
« on: January 25, 2017, 09:13:09 am »
I'm not expecting anyone to have heard of the game Cythera, which is a mac exclusive tile from the late 90s. It had an interesting dialog system where certain words said by NPCs would be highlighted, and the player could ask about them go into more depth on that topic. I've discussed this type of dialog system previously in this topic.

I've put together a quick little sample quest that tries to replicate the dialog system used by Cythera. It replicates the dialog text of three characters from the game (one of which is fairly complicated and a good testbed to push my dialog script to its limits).

When talking to NPCs, pretty much all of them respond to the key words "name", "job" and "bye" (to close the dialog). Beyond that, certain key words are highlighted in blue denoting that the player can take the conversation further by asking about those topics. The player can either type the keyword into a text box or select them from a running list by using the mouse.

A little quirk to be aware of is that only the first 5 letters of whatever the player types in are considered (it can be less than 5 letters too, depending on the word). This makes it easy for "fish", "fishing" or "fisherman" to all give the same response. Note that while Cythera only ever considered the first 4 letters entered, I decided to bump the limit up to 5 for a little more versatility. I don't think that it would ever be necessary to use more than 5 letters, though.

See if you can figure out how to complete this short little quest. I will say that there are two possible solutions. No items are required, and there are not any enemies.

Installation instructions:
The Solarus ALTTP resource pack is required for this quest. Confirmed to work with version 1.5.6, which can be found here. Download and extract the ALTTP resource pack. Then download my quest and extract it to the the same directory as the resource pack. For any duplicate files between the two, the ones from my quest should overwrite the ones from the resource pack.

Feel free to borrow and or modify the scripts for your own usage. Note that most of the images and sounds included in the quest actually come from Cythera and should not be used. If I end up going further with this project, it will ultimately not use any Cythera assets or text.

This demo quest has the following features:
  • Interactive dialog system
    • NPC portraits
    • clickable hyperlink text
    • text input
    • dynamic dialog content that depends on a multitude of factors
  • Preserve a log of NPC dialog for later reference
  • Objective tracking
  • Day/night cycle that darkens the map at night
    • lluminating light sources at night
    • Different music between night & day
    • NPC movements synchronized with the time
    • sleep in a bed to advance time
  • Read-only console that prints notifications for the player
  • Various UI elements with mouse/keyboard interaction

The quest can be downloaded from its github project page

Development / Pickable Item with No Shadow (Solved)
« on: December 25, 2016, 12:02:10 am »
Using the ALTTP resource pack, I wanted to create a pickable treasure for the bottle, which looks bad if it has a shadow.

According to the Equipment Items documentation, the method item:set_shadow(shadow_animation), where shadow_animation is nil gives no shadow. However, when I try doing item:set_shadow(), I get the following error:

Error: In on_created: [string "items/bottle_1.lua"]:9: bad argument #1 to set_shadow (string expected, got no value))

I also tried item:set_shadow(false) and item:set_shadow("nil") to no avail. What am I doing wrong here?

Development / Clear contents of Quest Editor console?
« on: December 04, 2016, 10:16:36 am »
Is there a way to clear what's displayed in the console of the Quest Editor? When there are a bunch of errors that I've fixed by editing my scripts, it would be nice to reset the console to a blank slate before running the quest again in order to verify the fix.

I checked the menus and right-click pop-up menu but didn't see anything to clear the contents of the console. I'm wondering if maybe there is a command that can be typed into the console to clear it?

Development / Getting the sprite of a Dynmaic Tile
« on: September 25, 2016, 08:31:09 pm »
I have a tile that I converted to a dynamic tile (pattern from the ALTTP resource pack), but when I call get_sprite() for the dynamic tile from in the map:on_draw() function, it returns nil. What am I doing wrong?

What I want to do is read the frame number so that I can synchronize an overlay with the animation.

Development / NPC movement paths and obstacle detection
« on: September 23, 2016, 10:44:15 pm »
I've been playing around with assigning path movements to npcs in order to have them follow a fixed route. The problem I'm running into is obstacle avoidance. Now it's easy enough to choose a route for the NPC that is clear of obstacles on the map, and I'd only have the NPCs moving around inside of towns where there are no enemies, so collisions with enemies are not a concern either. It's just collisions with the hero that I have to worry about.

I started off by setting set_ignore_obstacles(true) for the NPC movement. For the most part, that works. If the NPC's path goes through the hero's position then the hero will just get stuck for a little while until the NPC passes the hero, and it's just a little annoyance. The problem occurs if the hero happens to be standing where the NPCs path stops (and the NPC just stands in that spot for a while). Now the hero cannot move at all until the NPC starts a new path movement.

So then I played around with trying to push the hero out of the way when there's a collision with the NPC. So in the NPC movement's on_position_changed() function (any time the NPC moves), I check if the NPC and hero overlap with NPC:overlaps(hero, "overlapping"), and if they do, I assign a movement to the hero to nudge the hero out of the NPC's path.

I'm assuming that I need to move the hero one pixel at a time so that if the hero gets pushed over a 16x16 teletransporter (say inside a doorway), that the hero doesn't get pushed too far and skip the teletransporter. Is that correct? Pixel movement also seems preferable over path movement so that the hero can be moved just a couple pixels out of the way of the NPC instead of multiples of 8 pixels.

So I got something setup to move the hero when an NPC walks into him, but now I'm running into problems with making sure the hero doesn't get pushed into an obstacle when nudged. As an example, consider an NPCs walking along a path in the North direction with the Hero to the North of the NPC. When a collision occurs, I want to nudge the hero to the NW because if I only nudge the hero N, then the NPC could end up pushing the hero for the entire length of the path.

To nudge the hero to the NW, I give the hero's pixel movement a trajectory with several entries of {-1, -1}. The problem now is if there is an obstruction on the map to the west of the hero, I still want the hero to be moved to the N. It seems the hero's movement just calls on_obstacle_reached() and doesn't move the hero at all, even though there is no obstacle to the North.

The workaround I then used for the above problem is to give a trajectory that only moves one pixel W then one pixel N, alternating (i.e. {{-1,0}, {0,-1}, {-1,0}, {0,-1}...etc.}). This actually works for the most part, but there are still a few corner cases that don't quite work.

See the example image below, where the green 16x16 box is the hero, the blue is the npc, who is walking to the north to the doorway in the building above. The grey pixels (and the doorway) are traversable. So then the first time the NPC and hero bounding boxes overlap, there are six pixels (cyan) where the overlap occurs. Now obviously nudging the hero to the north fails because there is a building in the way, but nudging the hero to the east also fails because if the hero is moved 1 pixel to the east, the hero still overlaps the NPC by 5 pixels.

I don't have a good solution to this problem. The only thing I can think of is to do set_ignore_obstacles(true) for the hero and do my own manual obstacle detection, but that's tricky because it runs the risk of pushing the hero into the side of a building and getting stuck. I'm assuming I'd have to use map:get_ground() to do my own collision text, but how does that work? With a "wall_top_right" tile, for example, the bottom left corner of the tile is traversable and the top right corner is not. So does that mean that a coordinate in the top right half of the tile returns "wall_top_right" whereas a coordinate in the bottom left half of the tile returns "traversable"?

Or does anyone have any better ideas?

Development / Pattern matching and UTF-8
« on: September 08, 2016, 02:47:52 am »
I'm working on a custom dialog manager script for the features I described in this topic. A few questions have arisen in the process regarding UTF-8 characters:

1) In the text of my dialogs.dat file, keywords are denoted by prefacing the keyword with the "@" character. My original concept was to iterate through the keywords in the text using the following:
Code: [Select]
text:gmatch("@(%w+)")which works great for ASCII text and forces keywords to only be alpha-numeric characters. However, my limited testing seems to conclude that it will not work as intended if UTF-8 characters are used.

It looks like a better solution is to instead use the following:
Code: [Select]
text:gmatch("@([^%p%s]+)")which grabs all characters between the @ character and the first punctuation or space character (it only looks at one line at a time, so new-line characters are excluded).

Am I correct that pattern matching doesn't work with UTF-8 characters, and is my second proposed solution viable for when UTF-8 characters are used?

2) I also convert the text input of the player (in their native language) to lowercase with string.lower() so that case does not matter. My testing seems to indicate that string.lower() does not do anything for UTF-8 characters, but I see a note in the lua manual for the string.lower() entry that it depends on the locale.

How does the locale affect string.lower() and is the locale set automatically by the Solarus engine? Or perhaps by the operating system?

3) I've noticed that string.len() doesn't work the way I want it to in regard to UTF-8 (multibyte) characters since it returns the number of bytes in the string, and not the number of actual characters. It's easy enough to write my own custom function to count the number of characters in a string, but I figure I may as well ask...

Are there any included lua functions in the Solarus engine that return the number of characters in a string? It looks like lua 5.3 might have support for that, but as far as I can tell, Solarus appears to be using lua 5.1.

Development / Getting all strings in strings.dat?
« on: September 04, 2016, 06:22:04 pm »
It there a good way to either get all key/value entries in strings.dat for a given language or just the ones with keys beginning with a certain pattern?

Or do I have to parse strings.dat in my own script to extract the info myself?

I was trying to manually keep a list of strings.dat keys I'm interested in, but there's too many and it's becoming too much of a hassle.

Pages: [1]