Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 6 7 [8] 9 10 ... 13

Author Topic: Procedural content creation: Random plant creation script  (Read 35645 times)

Vorthon

  • Bay Watcher
  • Now with 50% more pointless rambling!
    • View Profile
Re: Procedural content creation: Random plant creation script
« Reply #105 on: April 04, 2011, 07:25:50 am »

By the way, I would like to suggest that the price of a plant's seeds should at least partially map to the price of the plant itself (I'm pretty sure that in-game most seeds cost half as much as the plant)

Nope, all seeds in vanilla DF cost one dorfbuck each.
Logged

huhu

  • Bay Watcher
    • View Profile
Re: Procedural content creation: Random plant creation script
« Reply #106 on: April 04, 2011, 08:06:46 am »

Code: [Select]
>>> line = """[TILE:0:1:']':'+':'[']"""

Figured out how to parse it successfully. Tried a few different tag variations but I couldn't make it choke anymore.

Code: [Select]
import re

def unbracket(item):
    return re.sub("'", '', re.sub('^[\s]*\[', '', re.sub('\][\s]*$', '', item)).strip())
   
for line in file:
    if not line or '[' not in line:
        continue
       
    items = [unbracket(item) for item in re.split(':|\][\s]*\[', line)]
    items.insert(0, re.findall('^(\t*)', line)[0])
    print items

Problems with this script:
-Whitespace other than tabs in the beginning of the line don't get handled correctly and will mess up the tab tracking
« Last Edit: April 05, 2011, 10:05:25 am by huhu »
Logged

Sphalerite

  • Bay Watcher
    • View Profile
    • Drew's Robots and stuff
Re: Procedural content creation: Random plant creation script
« Reply #107 on: April 04, 2011, 08:08:06 am »

So, this is basically more evidence that I really need to spend some time learning how to use the regular expression library rather than hand-writing my own parsers.
Logged
Any intelligent fool can make things bigger and more complex... It takes a touch of genius --- and a lot of courage to move in the opposite direction.

huhu

  • Bay Watcher
    • View Profile
Re: Procedural content creation: Random plant creation script
« Reply #108 on: April 04, 2011, 08:29:42 am »

So, this is basically more evidence that I really need to spend some time learning how to use the regular expression library rather than hand-writing my own parsers.

It seems so. This was the first script I've done with the re module, even though I've seen it in action many times.

Code: [Select]
re.split(':|\][\s]*\[', line)
I'm not sure if you're already familiar with regular expressions, but lets break it down anyway. The first argument for the splitting method reads as, "(break at the character ':') OR (break at the combination of ']' plus any combination of whitespace characters plus '[')."

Now I'm worried that a newline between brackets can bring unintended results, but it shouldn't be a problem when reading from a file like in this example...

Code: [Select]
return re.sub("'", '', re.sub('^[\s]*\[', '', re.sub('\][\s]*$', '', item)).strip())
This doesn't need to be written as a one-liner, but I've understood that there's some kind of 'coolness' factor involved in doing just that.

Code: [Select]
string = re.sub('\][\s]*$', '', source)
string = re.sub('^[\s]*\[', '', string)
string = string.strip()
return re.sub("'", '', string)

The first one matches the character ']' if it's the very last character in the string, and replaces it with nothing. The second one matches the character '[' if it's the very first character in the string, and replaces it with nothing. The third line removes whitespace around the string, and the final string matches the single quote and removes it. the '[\s]*' means 0 or more whitespace characters.

Code: [Select]
items.insert(0, re.findall('^(\t*)', line)[0])
This pattern matches any number of tab characters in the beginning of the string. The findall() method returns a list and the parentheses make it so that the matching character is preserved so that findall can return it. This returns a list with only one item in it, that being a string with all the tab characters found from the beginning of the source string, which is then inserted to the beginning of the results so that indendation can be tracked.
« Last Edit: April 04, 2011, 08:55:36 am by huhu »
Logged

veok

  • Bay Watcher
    • View Profile
Re: Procedural content creation: Random plant creation script
« Reply #109 on: April 04, 2011, 11:07:06 pm »

Anyway to tweak the script to generate fewer plants?
Logged

dragonly

  • Bay Watcher
    • View Profile
Re: Procedural content creation: Random plant creation script
« Reply #110 on: April 05, 2011, 01:14:01 am »

Why not just delete a portion of the resulting text file?
Logged

Artanis00

  • Bay Watcher
    • View Profile
Re: Procedural content creation: Random plant creation script
« Reply #111 on: April 05, 2011, 04:07:48 am »

So, this is basically more evidence that I really need to spend some time learning how to use the regular expression library rather than hand-writing my own parsers.

I took a look at lexical analysis and parsing, and while this parsing could be done by hand, we could also put the raw format in Extended Backus-Naur Form and throw a parser generator like ANTLR at it.

Anyway to tweak the script to generate fewer plants?

If I understand this correctly, you'll have to manually edit the goals in lines 1299-1360.
« Last Edit: April 05, 2011, 04:49:52 am by Artanis00 »
Logged
Git - fast, efficient, distributed version control system
Github - Free public repositories, issue tracking, wikis, downloads...

Artanis00

  • Bay Watcher
    • View Profile
Re: Procedural content creation: Random plant creation script
« Reply #112 on: April 05, 2011, 06:57:17 pm »

I took a look at lexical analysis and parsing, and while this parsing could be done by hand, we could also put the raw format in Extended Backus-Naur Form and throw a parser generator like ANTLR at it.

Code: [Select]
[GRASS_TILES:'.':',':'`':''']
                          ^ fffffffffffffffffff---------~!

Oh dear god, there are singly quoted single quotes in the raws.

DELETE EVERYTHING BEGIN AGAIN!

edit: special cases to the rescue!

(Dear Toady One, this Christmas Earth Day all I really want is a sane data format.)
« Last Edit: April 05, 2011, 07:26:46 pm by Artanis00 »
Logged
Git - fast, efficient, distributed version control system
Github - Free public repositories, issue tracking, wikis, downloads...

huhu

  • Bay Watcher
    • View Profile
Re: Procedural content creation: Random plant creation script
« Reply #113 on: April 06, 2011, 10:16:34 am »

Oh dear god, there are singly quoted single quotes in the raws.

Got something working. It's slightly unsafe, though.

Code: [Select]
import re
line = "[TAG:BLAH:']':''':'[':BLERF][TAG2:DATA]Thisgets treatedlikecommentaryandwill beignored[TAG:'DATA']"
rule = "(?<!\')\[.*?\](?!\')"

def unbracket(item):
    return re.sub('^[\s]*\[', '', re.sub('\][\s]*$', '', item)).strip()
   
def split_tag(item):
    return [unbracket(item) if not re.findall("^[\s]*\[?[\s]*\'(.*?)\'[\s]*\]?[\s]*$", item) else unbracket(item)[1:-1] for item in item.split(':')]

print map(split_tag, re.findall(rule, line))

This will break if there are single quotes in the "commentary section" between tags next to the brackets that start or end a new tag. That means [TAG:WHEE]'comment'[TAG2:BOOYAH] won't match anything (well, it does match the whole line), and all will rest on the goodwill of the one who wrote the raw file.

Running the script produces the output:
Code: [Select]
[['TAG', 'BLAH', ']', "'", '[', 'BLERF'], ['TAG2', 'DATA'], ['TAG', 'DATA']]
It will also fail with a tag like [TAG:'mee]eep']
« Last Edit: April 06, 2011, 11:47:26 am by huhu »
Logged

Angle

  • Bay Watcher
  • 39 Indigo Spear Questions the Poor
    • View Profile
    • Agora Forum Demo!
Re: Procedural content creation: Random plant creation script
« Reply #114 on: May 19, 2011, 11:10:36 pm »

Has anyone made something like this for stones, minerals, and metals? I have very little programming skill, so I don't think I'm up to it, but it would be really cool.
Logged

Agora: open-source platform to facilitate complicated discussions between large numbers of people. Now with test site!

The Temple of the Elements: Quirky Dungeon Crawler

DwarvesH

  • Bay Watcher
    • View Profile
Re: Procedural content creation: Random plant creation script
« Reply #115 on: May 25, 2011, 05:20:33 am »

Hi Sphalerite!

Do you mind if I check out your project a little to seek some inspiration on procedurally generating content?

I will of course post here anything interesting found during my investigation, including source code.

Sphalerite

  • Bay Watcher
    • View Profile
    • Drew's Robots and stuff
Re: Procedural content creation: Random plant creation script
« Reply #116 on: May 25, 2011, 07:24:37 am »

Feel free, the source code is posted and should be reasonably easy to understand.
Logged
Any intelligent fool can make things bigger and more complex... It takes a touch of genius --- and a lot of courage to move in the opposite direction.

Thundercraft

  • Bay Watcher
    • View Profile
Re: Procedural content creation: Random plant creation script
« Reply #117 on: June 14, 2011, 01:59:29 pm »

I realize this is an uncompiled Python script and the code is right there for programming gurus to play around with. But for the rest of us mere mortals:

Are there any plans currently to release a tweaked or bug-fixed version? Or perhaps edit the OP with new info? (The OP is dated Feb. 2, with no last edit, and there's no mention of the version 1.1 update (Mar 16) with "Random trees and grass, press and oil support, and many small tweaks.")

Are the generated plant raws even compatible with 0.31.25 ?  ???
And last I heard, there's a bug in DF which prevents many [GOOD] and [EVIL] plants from appearing in-game.
« Last Edit: June 14, 2011, 02:03:28 pm by Thundercraft »
Logged

Knight Otu

  • Bay Watcher
  • ☺4[
    • View Profile
Re: Procedural content creation: Random plant creation script
« Reply #118 on: June 14, 2011, 02:10:10 pm »

There have been no changes to the plant raws since then (except possibly bamboo being added, but that didn't change anything in the way the raws work), so they quite certainly are compatible. There might need to be an update for 3?.01, but that's a bit to go yet.
Logged
Direforged Original
Random Raw Scripts - Randomly generated Beasts , Vermin, Hags, Vampires, and Civilizations
Castle Otu

Sphalerite

  • Bay Watcher
    • View Profile
    • Drew's Robots and stuff
Re: Procedural content creation: Random plant creation script
« Reply #119 on: June 14, 2011, 02:13:34 pm »

Are there any plans currently to release a tweaked or bug-fixed version? Or perhaps update the OP with new info? (The OP is dated Feb. 2, with no last edit, and there's no mention of the version 1.1 update (Mar 16) with "Random trees and grass, press and oil support, and many small tweaks.")

Is this even compatible with 0.31.25 ?  ???

It should still be compatible, since there haven't been any updates to the raws for plants lately.  I'm expecting that may change when the next revision comes out.
Logged
Any intelligent fool can make things bigger and more complex... It takes a touch of genius --- and a lot of courage to move in the opposite direction.
Pages: 1 ... 6 7 [8] 9 10 ... 13