Source code for redeclipse.textures

import os
import shutil
import colorsys
import random
from redeclipse.textures.basetex import Sky, Default, SimpleColourTex

comment = """
// {map_name} by {author} (maps/base)
// Config generated by Red Eclipse Map Generator

// Variables stored in map file, may be uncommented here, or changed from editmode.
"""


[docs]class TextureManager: """ The .cfg file contains texture and resource mappings as well as hand-editable map parameters. We will completely ignore those parameters because they're commented out and stored in the binary file. Textures -------- Here's an example :: setshaderparam specscale 0.250000 0.250000 0.250000 0.000000 texture c "trak/trak6/tile3.jpg" 0 0 0 0.250000 // 2 texture n "trak/trak6/tile3_nm.png" texture s "trak/trak6/tile3_gloss.jpg" The texture statement seems to be: :: texture c [file] [args] then one or more of :: texture [type] [category] [file] Type Characters: +--------------+-----------+ | Short Code | Type | +==============+===========+ | ``c`` | Diffuse | +--------------+-----------+ | ``u`` | Unknown | +--------------+-----------+ | ``d`` | Decal | +--------------+-----------+ | ``n`` | Normal | +--------------+-----------+ | ``g`` | Glow | +--------------+-----------+ | ``s`` | Spec | +--------------+-----------+ | ``z`` | Depth | +--------------+-----------+ | ``e`` | Envmap | +--------------+-----------+ Unknown ------- :: texture water "textures/waterfall.jpg" 0 0 0 1.000000 texture 1 "textures/waterfall.jpg" texture 1 "textures/watern.jpg" texture 1 "textures/waterdudv.jpg" texture 1 "textures/waterfalln.jpg" texture 1 "textures/waterfalldudv.jpg" texture water2 "textures/waterfall.jpg" 0 0 0 1.000000 texture 1 "textures/waterfall.jpg" texture 1 "textures/watern.jpg" texture 1 "textures/waterdudv.jpg" texture 1 "textures/waterfalln.jpg" texture 1 "textures/waterfalldudv.jpg" texture water3 "textures/waterfall.jpg" 0 0 0 1.000000 texture 1 "textures/waterfall.jpg" texture 1 "snipergoth/watern.jpg" texture 1 "snipergoth/waterdudv.jpg" texture 1 "textures/waterfalln.jpg" texture 1 "textures/waterfalldudv.jpg" texture water4 "textures/waterfall.jpg" 0 0 0 1.000000 texture 1 "textures/waterfall.jpg" texture 1 "snipergoth/watern.jpg" texture 1 "snipergoth/waterdudv.jpg" texture 1 "textures/waterfalln.jpg" texture 1 "textures/waterfalldudv.jpg" texture lava "textures/lava.jpg" 0 0 0 1.000000 texture 1 "textures/lava.jpg" texture lava2 "textures/lava.jpg" 0 0 0 1.000000 texture 1 "textures/lava.jpg" texture lava3 "textures/lava.jpg" 0 0 0 1.000000 texture 1 "textures/lava.jpg" texture lava4 "textures/lava.jpg" 0 0 0 1.000000 texture 1 "textures/lava.jpg" setshader stdworld texture c "textures/sky.png" 0 0 0 1.000000 // 0 setshader stdworld texture c "textures/default.png" 0 0 0 0.500000 // 1 setshader bumpspecmapworld setshaderparam specscale 0.250000 0.250000 0.250000 0.000000 texture c "trak/trak6/tile3.jpg" 0 0 0 0.250000 // 2 texture n "trak/trak6/tile3_nm.png" texture s "trak/trak6/tile3_gloss.jpg" setshader bumpworld texture c "appleflap/randomwoodthing.jpg" 0 0 0 1.000000 // 3 texture n "appleflap/randomwoodthing_nm.jpg" setshader bumpspecmapglowworld texture c "appleflap/floor.jpg" 0 0 0 1.000000 // 4 texture n "appleflap/floor_nm.jpg" texture s "appleflap/floor_spec.jpg" texture g "appleflap/floor_glow.jpg" setshader glowworld texture c "appleflap/applequote.jpg" 0 0 0 1.000000 // 5 texture g "appleflap/applequote_g.jpg" texscroll 0.100000 0.000000 setshader bumpspecmapparallaxworld setshaderparam specscale 1.000000 1.000000 1.000000 0.000000 setshaderparam parallaxscale 0.028000 0.000000 0.000000 0.000000 texture c "dziq/boards01.jpg" 0 0 0 1.000000 // 6 texture n "dziq/boards01_n.jpg" texture z "dziq/boards01_b.jpg" texture s "dziq/boards01_s.jpg" """ def __init__(self): self.atlas = {} self.atlas_backref = {} self.texref = {} # Init self.discover_textures() self.get('sky') self.get('default')
[docs] def discover_textures(self): self.texref['sky'] = Sky() self.texref['default'] = Default()
[docs] def get(self, name): if name not in self.atlas_backref: # Insert tex tex = self.texref[name] pos = len(self.atlas) self.atlas[pos] = tex # Now insert backref self.atlas_backref[name] = pos return self.atlas_backref[name]
[docs] def random(self): return self.get(random.choice([ 'gold', 'admin', 'stone', 'dirt', 'wood' ]))
[docs] def emit_conf(self, map_output_path, author="Python", map_name="test", ): filename = map_output_path.name.replace('.mpz', '.cfg') with open(filename, 'w') as handle: handle.write(comment.format(author=author, map_name=map_name)) for idx, (tex_key, tex) in enumerate(self.atlas.items()): handle.write(tex.conf(idx=idx))
[docs] def copy_data(self): outdir = "/home/hxr/games/redeclipse-1.5.3/data/hxr/textures/" if not os.path.exists(outdir): os.makedirs(outdir) for idx, (tex_key, tex) in enumerate(self.atlas.items()): if not hasattr(tex, 'files'): continue for texfile in tex.files(): shutil.copy( os.path.join(os.path.dirname(os.path.realpath(__file__)), tex.srcpath, texfile), os.path.join(outdir, texfile) )
[docs]class ThemedTextureManager(TextureManager):
[docs] def get_c(self, category): if category == 'generic': return self.get(random.choice(self.theme.GenericMaterial)) elif category == 'column': return self.get(random.choice(self.theme.ColumnMaterial)) elif category == 'floor': return self.get(random.choice(self.theme.FloorMaterial)) elif category == 'wall': return self.get(random.choice(self.theme.WallMaterial)) elif category == 'accent': return self.get(random.choice(self.theme.AccentMaterial)) return random.choice(self.theme.GenericMaterial)
[docs] def discover_textures(self): super().discover_textures() for cls in dir(self.textures): mod = getattr(self.textures, cls) if 'type' in str(type(mod)): if any(['redeclipse.textures.' in str(c) for c in mod.__bases__]): self.texref[cls.lower()] = mod()
[docs]class MinecraftThemedTextureManager(ThemedTextureManager): from redeclipse.textures import minecraft, minecraft_theme textures = minecraft theme = minecraft_theme
[docs]class DefaultThemedTextureManager(ThemedTextureManager): from redeclipse.textures import dull, dull_theme textures = dull theme = dull_theme
[docs]class PaperThemedTextureManager(ThemedTextureManager): from redeclipse.textures import paper, paper_theme textures = paper theme = paper_theme
[docs]class PrimaryThemedTextureManager(ThemedTextureManager): from redeclipse.textures import primary, primary_theme textures = primary theme = primary_theme
[docs]class AutomatedMagicaTextureManager(TextureManager):
[docs] def get_colour(self, r, g, b): return self._get_colour(r, g, b)
def _get_colour(self, r, g, b): name = 'c_%s_%s_%s' % (r, g, b) if name not in self.atlas_backref: tmp = SimpleColourTex() tmp.r = r tmp.g = g tmp.b = b self.texref[name] = tmp # Insert tex tex = self.texref[name] pos = len(self.atlas) self.atlas[pos] = tex # Now insert backref self.atlas_backref[name] = pos return self.atlas_backref[name]
[docs] def get_c(self, category): if category == 'generic': return self.get_colour(0, 0, 0) elif category == 'column': return self.get_colour(0.5, 0.5, 0.5) elif category == 'floor': return self.get_colour(0.1, 0.1, 0.1) elif category == 'wall': return self.get_colour(0.9, 0.9, 0.9) elif category == 'accent': return self.get_colour(1, 0, 0) return self.get_colour(0, 0, 1)
[docs] def discover_textures(self): super().discover_textures()
[docs]class RainbowPukeTextureManager(AutomatedMagicaTextureManager): """Kanker."""
[docs] def get_c(self, category): # Ignore category. hue = random.randint(0, 64) * 4 (red, grn, blu) = colorsys.hsv_to_rgb(hue / 256, 1, 255) colour = self.get_colour(red / 255, grn / 255, blu / 255) return colour
[docs] def get_colour(self, r, g, b): hue = random.randint(0, 64) * 4 (red, grn, blu) = colorsys.hsv_to_rgb(hue / 256, 1, 255) colour = self._get_colour(red / 255, grn / 255, blu / 255) return colour