Skip to content

Commit

Permalink
fix flattening with None
Browse files Browse the repository at this point in the history
  • Loading branch information
arpruss committed Jun 18, 2015
1 parent 0240915 commit aa9eb68
Show file tree
Hide file tree
Showing 15 changed files with 162 additions and 152 deletions.
2 changes: 1 addition & 1 deletion RaspberryJamMod.iss
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName "RaspberryJamMod"
#define MyAppVersion "0.25"
#define MyAppVersion "0.28"
#define MyAppPublisher "Omega Centauri Software"
#define MyAppURL "http://github.com/arpruss/raspberryjammod"

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ buildscript {

apply plugin: 'forge'

version = "0.25"
version = "0.28"
group= "mobi.omegacentauri.raspberryjammod" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = "RaspberryJamMod"

Expand Down
Binary file modified python2-scripts.zip
Binary file not shown.
246 changes: 124 additions & 122 deletions python2-scripts/mcpipy/lsystem.py
Original file line number Diff line number Diff line change
@@ -1,122 +1,124 @@
#
# Code under the MIT license by Alexander Pruss
#
# L-system with turtle graphics
#

import collections
import random
import mcpi.settings
from mcturtle import *

def playProgram(s, dictionary):
for c in s:
if c in dictionary:
dictionary[c]()


def transform(c, t):
if isinstance(t, basestring):
return t
else:
r = random.random()
for p,out in t:
if r<p:
return out
r -= p
return c

def evolve(axiom, rules, levelCount):
for i in range(levelCount):
out = ""
for c in axiom:
if c in rules:
out += transform(c, rules[c])
else:
out += c
axiom = out
return axiom


def lsystem(axiom, rules, dictionary, levelCount):
out = evolve(axiom, rules, levelCount)
playProgram(out, dictionary)

if __name__ == "__main__":
t = Turtle()
t.pendelay(0)
t.penup()
t.turtle(None)
t.go(10)
t.verticalangle(90)
t.pendown()
t.penblock(WOOD)

# a fairly simple example with rules from http://www.nbb.cornell.edu/neurobio/land/OldStudentProjects/cs490-94to95/hwchen/
# rules = {'F':'F[-&<F][<++&F]||F[--&>F][+&F]'}
#
# angle = 22.5
#
# dictionary = {
# '[': t.push,
# ']': t.pop,
# 'F': lambda: t.go(5),
# '-': lambda: t.yaw(-angle),
# '+': lambda: t.yaw(angle),
# '&': lambda: t.pitch(angle),
# '^': lambda: t.pitch(-angle),
# '<': lambda: t.roll(-angle),
# '>': lambda: t.roll(angle),
# '|': lambda: t.pitch(180)
# }
#
# lsystem('F', rules, dictionary, 3)


#
# A more complex example with
# rules based on http://www.geekyblogger.com/2008/04/tree-and-l-system.html
#
rules = {'A': '^f[^^f>>>>>>A]>>>[^^f>>>>>>A]>>>>>[^^f>>>>>>A]'}

#randomized version:
# rules = {'A': [(0.75,'^f[^^f>>>>>>A]>>>[^^f>>>>>>A]>>>>>[^^f>>>>>>A]'),
# (0.25,'^f>>[^^f>>>>>>A]>>>[^^f>>>>>>A]')]}

axiom = 'fA'
angle = 15
thickness = 8
length = 10 if mcpi.settings.isPE else 15;
material = WOOD
t.penwidth(thickness)
t.penblock(material)

stack = []
def push():
global length
global thickness
stack.append((length,thickness))
t.push()
thickness = thickness * 0.6
length = length * 0.75
if thickness < 1:
thickness = 1
if length <= 1:
t.penblock(LEAVES_OAK_PERMANENT)
t.penwidth(thickness)

def pop():
global length
global thickness
length,thickness = stack.pop()
t.pop()

dictionary = {
'[': push,
']': pop,
'^': lambda: t.pitch(angle),
'>': lambda: t.roll(angle),
'f': lambda: t.go(length)
}

lsystem(axiom, rules, dictionary, 9 if mcpi.settings.isPE else 11)
#
# Code under the MIT license by Alexander Pruss
#
# L-system with turtle graphics
#

import collections
import random
import mcpi.settings
from mcturtle import *

def playProgram(s, dictionary):
for c in s:
if c in dictionary:
dictionary[c]()


def transform(c, t):
if isinstance(t, basestring):
return t
else:
r = random.random()
for p,out in t:
if r<p:
return out
r -= p
return c

def evolveGenerator(axiom):
for c in axiom:
if c in rules:
yield transform(c, rules[c])
else:
yield c

def evolve(axiom, rules, levelCount):
for i in range(levelCount):
axiom = ''.join(evolveGenerator(axiom))
return axiom


def lsystem(axiom, rules, dictionary, levelCount):
out = evolve(axiom, rules, levelCount)
playProgram(out, dictionary)

if __name__ == "__main__":
t = Turtle()
t.pendelay(0)
t.penup()
t.turtle(None)
t.go(10)
t.verticalangle(90)
t.pendown()
t.penblock(WOOD)

# a fairly simple example with rules from http://www.nbb.cornell.edu/neurobio/land/OldStudentProjects/cs490-94to95/hwchen/
# rules = {'F':'F[-&<F][<++&F]||F[--&>F][+&F]'}
#
# angle = 22.5
#
# dictionary = {
# '[': t.push,
# ']': t.pop,
# 'F': lambda: t.go(5),
# '-': lambda: t.yaw(-angle),
# '+': lambda: t.yaw(angle),
# '&': lambda: t.pitch(angle),
# '^': lambda: t.pitch(-angle),
# '<': lambda: t.roll(-angle),
# '>': lambda: t.roll(angle),
# '|': lambda: t.pitch(180)
# }
#
# lsystem('F', rules, dictionary, 3)


#
# A more complex example with
# rules based on http://www.geekyblogger.com/2008/04/tree-and-l-system.html
#
rules = {'A': '^f[^^f>>>>>>A]>>>[^^f>>>>>>A]>>>>>[^^f>>>>>>A]'}

#randomized version:
# rules = {'A': [(0.75,'^f[^^f>>>>>>A]>>>[^^f>>>>>>A]>>>>>[^^f>>>>>>A]'),
# (0.25,'^f>>[^^f>>>>>>A]>>>[^^f>>>>>>A]')]}

axiom = 'fA'
angle = 15
thickness = 8
length = 10 if mcpi.settings.isPE else 15;
material = WOOD
t.penwidth(thickness)
t.penblock(material)

stack = []
def push():
global length
global thickness
stack.append((length,thickness))
t.push()
thickness = thickness * 0.6
length = length * 0.75
if thickness < 1:
thickness = 1
if length <= 1:
t.penblock(LEAVES_OAK_PERMANENT)
t.penwidth(thickness)

def pop():
global length
global thickness
length,thickness = stack.pop()
t.pop()

dictionary = {
'[': push,
']': pop,
'^': lambda: t.pitch(angle),
'>': lambda: t.roll(angle),
'f': lambda: t.go(length)
}

lsystem(axiom, rules, dictionary, 9 if mcpi.settings.isPE else 11)

1 change: 1 addition & 0 deletions python2-scripts/mcpipy/mcpi/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def __repr__(self):
MELON = Block(103)
FENCE_GATE = Block(107)
REDSTONE_BLOCK = Block(152)
QUARTZ_BLOCK = Block(155)

if settings.isPE:
HARDENED_CLAY_STAINED = WOOL
Expand Down
3 changes: 1 addition & 2 deletions python2-scripts/mcpipy/mcpi/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,14 @@ def send(self, f, *data):

def send_flat(self, f, data):
"""Sends data. Note that a trailing newline '\n' is added here"""
#print "f,data:",f,ddata
s = "%s(%s)\n"%(f, ",".join(data))
#print "f,data:",f,data
self.drain()
self.lastSent = s
self.socket.sendall(s.encode('utf-8'))

def receive(self):
"""Receives data. Note that the trailing newline '\n' is trimmed"""
# s = self.socket.makefile("r").readline().rstrip("\n")
s = self.readFile.readline().rstrip("\n")
if s == Connection.RequestFailed:
raise RequestError("%s failed"%self.lastSent.strip())
Expand Down
2 changes: 1 addition & 1 deletion python2-scripts/mcpipy/mcpi/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def floorFlatten(l):
yield str(e)
elif isinstance(e, float):
yield str(int(math.floor(e)))
else:
elif not e is None:
for ee in floorFlatten(e): yield ee

def flatten_parameters_to_string(l):
Expand Down
Binary file modified python3-scripts.zip
Binary file not shown.
16 changes: 9 additions & 7 deletions python3-scripts/mcpipy/lsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ def transform(c, t):
r -= p
return c

def evolveGenerator(axiom):
for c in axiom:
if c in rules:
yield transform(c, rules[c])
else:
yield c

def evolve(axiom, rules, levelCount):
for i in range(levelCount):
out = ""
for c in axiom:
if c in rules:
out += transform(c, rules[c])
else:
out += c
axiom = out
axiom = ''.join(evolveGenerator(axiom))
return axiom


Expand Down Expand Up @@ -120,3 +121,4 @@ def pop():
}

lsystem(axiom, rules, dictionary, 9 if mcpi.settings.isPE else 11)

1 change: 1 addition & 0 deletions python3-scripts/mcpipy/mcpi/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def __repr__(self):
MELON = Block(103)
FENCE_GATE = Block(107)
REDSTONE_BLOCK = Block(152)
QUARTZ_BLOCK = Block(155)

if settings.isPE:
HARDENED_CLAY_STAINED = WOOL
Expand Down
3 changes: 1 addition & 2 deletions python3-scripts/mcpipy/mcpi/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,14 @@ def send(self, f, *data):

def send_flat(self, f, data):
"""Sends data. Note that a trailing newline '\n' is added here"""
#print "f,data:",f,ddata
s = "%s(%s)\n"%(f, ",".join(data))
#print "f,data:",f,data
self.drain()
self.lastSent = s
self.socket.sendall(s.encode('utf-8'))

def receive(self):
"""Receives data. Note that the trailing newline '\n' is trimmed"""
# s = self.socket.makefile("r").readline().rstrip("\n")
s = self.readFile.readline().rstrip("\n")
if s == Connection.RequestFailed:
raise RequestError("%s failed"%self.lastSent.strip())
Expand Down
2 changes: 1 addition & 1 deletion python3-scripts/mcpipy/mcpi/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def floorFlatten(l):
yield str(e)
elif isinstance(e, float):
yield str(int(math.floor(e)))
else:
elif not e is None:
for ee in floorFlatten(e): yield ee

def flatten_parameters_to_string(l):
Expand Down
Loading

0 comments on commit aa9eb68

Please sign in to comment.