Example #1
0
        f.write("\x00\x00\x00\x00")
    
    for i in imgs:
        if i:
            f.write(''.join([chr(c) for c in realconvert(i).transpose(Image.ROTATE_270)
                             .transpose(Image.FLIP_LEFT_RIGHT).getdata()]))
        
    f.close()
    return morefils
spcre=re.compile(r'\s+');

from textures import textures

#imgmap = imgdict([(num, tex) for num, (ingame, tex) in textures.iteritems() if ingame], None)
imgmap={}
for texnum, (ingame, texfile) in textures.iteritems():
    dr = 'buildicons/'
    texture=Image.open(dr+texfile+'.png')
    if ingame and (texture.size[0] != 128 or texture.size[1] != 128):
        filt = Image.BICUBIC
        if texture.size[0] > 128:
            filt = Image.ANTIALIAS
        texture = texture.resize((128,128),filt)

    imgmap[texnum]=texture

curartfile=0

while writeartfile(imgmap,curartfile*256,curartfile*256+255,'tiles%03d.art'%curartfile):
    print 'foo'
    curartfile += 1
Example #2
0
def convert(inputmaps, output, mangler):

    sttime = time.time()

    from textures import textures

    imgmap = imgdict([(num, tex) for num, (ingame, tex) in textures.iteritems() if ingame], None)

    def hook(fname, *args):
        
        if hasattr(mangler, fname):
            timer.begin("hook: %s"%fname, False)
            getattr(mangler, fname)(*args)
            timer.end()

    hook('preconvert')

    # Step 1: convert coordinates

    world = World()
    mangler.map = world

    for i, m in enumerate(inputmaps):
        preconvertmap(world, imgmap, i, m)


    # Step 5: convert actors

    hook('preconvertactors')



    iglue = {}
    gluelnk = {}

    
    timer.begin("convert actors")
    for ar in world.areas:
        if ar.hitag == 1:
            gluelnk.setdefault(ar.lotag, []).append(ar)
    
    for ar in world.areas:
        for a in ar.actors:
            a.map = world
            if a.clas: continue
            clas = None
            extra = ''
            if (a.cstat&224) == 0:
                a.z += 0.25
                
            if type(a) == Glue:
                if a.lotag == 1:
                    iglue.setdefault(a.hitag, []).append(a)
                elif a.lotag == 2:
                    gluelnk.setdefault(a.hitag, []).append(a.area)

    for ar in world.areas:
        for a in ar.actors:
            a.link()
    timer.end()

    hook('postconvertactors')


    hook('preglue')

    timer.begin('processglue')
    processglue(iglue)
    timer.end()


    areagroups=[]

    timer.begin("transform",len(world.areas))
    for ar in world.areas:
        timer.progress()
        if not ar.tform.offset:
            lst = AreaGroup()
            ar.tform.transform(lst)
            areagroups.append(lst)
    
    timer.end()

    for g in areagroups:
        for ar in g.areas:
            ar.group = g

    timer.begin("build wall index", len(world.areas))
    #for v in gluelnk.values():
    wallmap = {}
    for ar in world.areas:
        timer.progress()
        for w in ar.walls:
            wallmap.setdefault((w.x1, w.y1, w.x2, w.y2), []).append(w)
    timer.end()
    
    timer.begin("find links", len(world.areas))
    for ar in world.areas:
        timer.progress()
        for w in ar.walls:
            for w2 in wallmap.get((w.x2, w.y2, w.x1, w.y1), ()):
                if not w2.area in w.links:
                    if w.blockall or w2.blockall:
                        w.links[w2.area] = 2
                    else:
                        w.links[w2.area] = 0
                    
                        
    timer.end()
        

    hook('postglue')



    hook('preculllink')

    timer.begin("cull links",len(world.areas))

    # Step 7: eliminate impossible links
    for ar in world.areas:
        afl = ar.floor.getz
        acl = ar.ceil.getz
        timer.progress()
        for w in ar.walls:
            mfza = afl(w.x1, w.y1)
            mfzb = afl(w.x2, w.y2)
            mcza = acl(w.x1, w.y1)
            mczb = acl(w.x2, w.y2)
            wlinks = w.links
            for oa in wlinks.keys():
                ofza = oa.floor.getz(w.x1, w.y1)
                ofzb = oa.floor.getz(w.x2, w.y2)
                ocza = oa.ceil.getz(w.x1, w.y1)
                oczb = oa.ceil.getz(w.x2, w.y2)
                if ((mfza > ocza and mfzb > oczb) or
                    (mcza < ofza and mczb < ofzb)):
                    del wlinks[oa]
    timer.end()
    
    hook('postculllink')


    # Process WALLMODS and WATER
    
    timer.begin("water and wallmod")
    for ar in world.areas:
        for a in ar.actors:
            if type(a) == WallMod:
                mind = None
                minw = None
                for w in ar.walls:
                    mx = (w.x1+w.x2)/2
                    my = (w.y1+w.y2)/2
                    rx = a.x - mx
                    ry = a.y - my
                    d2w = math.sqrt(sqr(rx) + sqr(ry))
                    if mind is None or d2w < mind:
                        mind = d2w
                        minw = w
                ow = None
                for oa in minw.links.keys():
                    if a.z >= oa.floor.getz(a.x, a.y) and \
                           a.z <= oa.ceil.getz(a.x, a.y):
                        for w2 in oa.walls:
                            if w2.x1 == minw.x2 and w2.y == minw.y2 and \
                                   minw.x1 == w2.x2 and minw.y1 == w2.y2:
                                ow = w2
                                break
                        break
                
                w = minw
                if a.hitag == 1:
                    del minw.links[ow.area]
                    del ow.links[minw.area]
                elif a.hitag == 2:
                    if ow:
                        minw.links[ow.area] |= a.lotag
                        ow.links[minw.area] |= a.lotag
                elif a.hitag == 3:
                    if ow:
                        minw.links[ow.area] |= a.lotag
                        
    for ar in world.areas:
        for a in ar.actors:
            if type(a) == Water:
                a.area.wateractor = a
                if a.hitag == 0:
                    a.area.waterlevel = a.z
                    a.area.noflood = True
                    
    for ar in world.areas:
        for a in ar.actors:
            if type(a) == Water:
                a.area.wateractor = a
                if a.hitag == 1:
                    floodarea(a.area, a.z)

    dbgwater = ('water', 'flagpad', 'skidpad', 'suckpad', 'checker', 'yellowrock', 'ceiltex', 'marble1',
                'checker2', 'brick', 'dmud')

    for ar in world.areas:
        if ar.waterlevel is not None:
            if hasattr(ar,'wateractor'):
                act = ar.wateractor
            else:
                act = Water()
                ar.actors.append(act)
            act.x, act.y = ar.findpt()
            act.z = ar.waterlevel
            #act.clas = "water"
            act.area = ar
            act.ang = 0
            act.picnum = -1

            dec = Decoration();
            dec.x, dec.y, dec.z = act.x, act.y, act.z
            dec.area = ar
            dec.ang = 0
            dec.aclass = 'water'
            dec.params[0] = dbgwater[0]
            dec.params[1] = 0.5
            #if hasattr(ar, 'wdebug_trans'):
            #    dec.params[1] = ar.wdebug_trans
            ar.actors.append(dec)


    timer.end()

    for ar in world.areas:
        for w in ar.walls:
            for oa, fl in w.links.items():
                if oa.remove:
                    del w.links[oa]

    
    hook('pregenparts')

    timer.begin("genparts",len(world.areas))
    # Step 11: generate wallparts

    for ar in world.areas:
        timer.progress()
        for w in ar.walls:
            genparts(w)
    timer.end()

    hook('postconvert')

    world.solidgroups={}

    maingroup = AreaGroup()
    newgroups = [maingroup]

    for g in areagroups:
        for ar in g.areas:
            for act in ar.actors:
                if type(act) == Solid:
                    break
            else:
                continue
            break
        else:
            maingroup.areas.extend(g.areas)
            continue
        extraactors = []
        newgroups.append(g)
        world.solidgroups[act.hitag] = g, extraactors
        for ar in g.areas:
            #ar.invert = True
            ar.ceil.x -= act.x
            ar.ceil.y -= act.y
            ar.ceil.z -= act.z
            ar.floor.x -= act.x
            ar.floor.y -= act.y
            ar.floor.z -= act.z
            ar.ceil.xpan += act.x
            ar.ceil.ypan += act.y
            ar.floor.xpan += act.x
            ar.floor.ypan += act.y

            ar.ceil.xpan -= math.floor(ar.ceil.xpan)
            ar.ceil.ypan -= math.floor(ar.ceil.ypan)
            ar.floor.xpan -= math.floor(ar.floor.xpan)
            ar.floor.xpan -= math.floor(ar.floor.ypan)
            for w in ar.walls:
                w.point2.point0 = w
                w.ox = w.x1 - act.x
                w.oy = w.y1 - act.y
            for w in ar.walls:
                for p in w.parts:
                    nxp = w.len / p.xs + p.xpan
                    nxs = -p.xs

                    #if w.ox+act.x == (43008/1024.0) and w.oy+act.y == (-10240.0/-1024):
                    #    print w.len, p.xpan, p.xs, nxp, nxs

                    p.xpan = nxp
                    p.xs = nxs

                    p.ypan -= act.z / p.ys
                    p.xpan -= math.floor(p.xpan)
                    p.ypan -= math.floor(p.ypan)
                w.x = w.point2.ox
                w.y = w.point2.oy
                w.point2 = w.point0
                w.links={}
            extraactors.extend(ar.actors);
            ar.actors=[]
            
        

    for i, g in enumerate(newgroups):
        nareas = []
        for ar in g.areas:
            if not ar.remove:
                nareas.append(ar)
        g.areas = nareas

        g.idx = i
        
    # Step 13: write
    totvol = 0
    totarea = 0

    if False:
        totvol, totarea = calcvolume(world)

    #calcvis(newgroups[0].areas)

    timer.begin("write",sum(len(g.areas) for g in newgroups))


    #f = datastream(gzip.GzipFile(output+".gz", 'wb'));
    f = datastream(file(join(OUTPUT_DIR,output), 'wb'));

    f.writeu16(len(newgroups))


    stats = [0] * 6
    for g in newgroups:
        nstat = g.writeto(f)
        stats = [a + b for a, b in zip(stats, nstat)]
        
    
    f.close();
   
    timer.end();

    print "total: %f"%(time.time()-sttime)

    nareas, ncont, nwalls, nlinks, nparts, nactors = stats

    print '============== STATS ==============='
    print 'Groups:',len(newgroups)
    print 'Areas:',nareas
    print 'Walls:',nwalls
    print 'Wallparts:',nparts
    print 'Contours:',ncont
    print 'Actors:',nactors
    print 'Links:',nlinks
    if totvol:
        print
        print '2d area:',totarea
        print 'Volume:',totvol
        print 'Average area:',(totarea/len(world.areas))
        print 'Average volume:',(totvol/len(world.areas))