Changeset 38


Ignore:
Timestamp:
04/17/09 15:20:40 (11 years ago)
Author:
maximinus_parpg
Message:

Finished utilities/transition.py, code to automatically add transition
tiles to maps
Updated world.py to cope with >1 transition layer
Edited 1 transiton file due to small error

Location:
trunk/demo
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/demo/maps/map.xml

    r37 r38  
    3939                <instances> 
    4040                        <i r="0" z="0.0" x="2.0" o="grass-a" y="-2.0" ns="PARPG"></i> 
    41                         <i x="1.0" o="grass-a" z="0.0" y="-1.0" r="0"></i> 
     41                        <i x="1.0" o="brick" z="0.0" y="-1.0" r="0"></i> 
    4242                        <i x="0.0" o="grass-a" z="0.0" y="0.0" r="0"></i> 
    4343                        <i x="-1.0" o="grass-a" z="0.0" y="1.0" r="0"></i> 
    4444                        <i x="0.0" o="grass-a" z="0.0" y="1.0" r="0"></i> 
    45                         <i x="1.0" o="grass-b" z="0.0" y="0.0" r="0"></i> 
    46                         <i x="2.0" o="grass-b" z="0.0" y="-1.0" r="0"></i> 
    47                         <i x="1.0" o="grass-b" z="0.0" y="-2.0" r="0"></i> 
    48                         <i x="0.0" o="grass-b" z="0.0" y="-1.0" r="0"></i> 
    49                         <i x="0.0" o="grass-b" z="0.0" y="-1.0" r="0"></i> 
    50                         <i x="-1.0" o="grass-b" z="0.0" y="0.0" r="0"></i> 
    51                         <i x="-1.0" o="grass-b" z="0.0" y="-1.0" r="0"></i> 
    52                         <i x="0.0" o="grass-b" z="0.0" y="-2.0" r="0"></i> 
    53                         <i x="2.0" o="grass-b" z="0.0" y="0.0" r="0"></i> 
    54                         <i x="1.0" o="grass-b" z="0.0" y="1.0" r="0"></i> 
    55                         <i x="2.0" o="grass-b" z="0.0" y="1.0" r="0"></i> 
    56                         <i x="-1.0" o="grass-a" z="0.0" y="-2.0" r="0"></i> 
    57                         <i x="-2.0" o="gravel" z="0.0" y="-2.0" r="0"></i> 
    58                         <i x="-2.0" o="gravel" z="0.0" y="-1.0" r="0"></i> 
    59                         <i x="-2.0" o="gravel" z="0.0" y="0.0" r="0"></i> 
    60                         <i x="-2.0" o="gravel" z="0.0" y="1.0" r="0"></i> 
    61                         <i x="-2.0" o="gravel" z="0.0" y="2.0" r="0"></i> 
    62                         <i x="-2.0" o="gravel" z="0.0" y="-3.0" r="0"></i> 
     45                        <i x="1.0" o="brick" z="0.0" y="0.0" r="0"></i> 
     46                        <i x="2.0" o="grass-a" z="0.0" y="-1.0" r="0"></i> 
     47                        <i x="1.0" o="grass-a" z="0.0" y="-2.0" r="0"></i> 
     48                        <i x="0.0" o="grass-a" z="0.0" y="-1.0" r="0"></i> 
     49                        <i x="0.0" o="grass-a" z="0.0" y="-1.0" r="0"></i> 
     50                        <i x="-1.0" o="grass-a" z="0.0" y="0.0" r="0"></i> 
     51                        <i x="-1.0" o="grass-a" z="0.0" y="-1.0" r="0"></i> 
     52                        <i x="0.0" o="grass-a" z="0.0" y="-2.0" r="0"></i> 
     53                        <i x="2.0" o="brick" z="0.0" y="0.0" r="0"></i> 
     54                        <i x="1.0" o="brick" z="0.0" y="1.0" r="0"></i> 
     55                        <i x="2.0" o="grass-a" z="0.0" y="1.0" r="0"></i> 
     56                        <i x="-1.0" o="brick" z="0.0" y="-2.0" r="0"></i> 
     57                        <i x="-2.0" o="grass-a" z="0.0" y="-2.0" r="0"></i> 
     58                        <i x="-2.0" o="grass-a" z="0.0" y="-1.0" r="0"></i> 
     59                        <i x="-2.0" o="grass-a" z="0.0" y="0.0" r="0"></i> 
     60                        <i x="-2.0" o="grass-a" z="0.0" y="1.0" r="0"></i> 
     61                        <i x="-2.0" o="grass-a" z="0.0" y="2.0" r="0"></i> 
     62                        <i x="-2.0" o="grass-a" z="0.0" y="-3.0" r="0"></i> 
    6363                        <i x="-1.0" o="grass-a" z="0.0" y="2.0" r="0"></i> 
    64                         <i x="0.0" o="grass-a" z="0.0" y="2.0" r="0"></i> 
    65                         <i x="1.0" o="grass-a" z="0.0" y="2.0" r="0"></i> 
     64                        <i x="0.0" o="brick" z="0.0" y="2.0" r="0"></i> 
     65                        <i x="1.0" o="brick" z="0.0" y="2.0" r="0"></i> 
    6666                        <i x="2.0" o="grass-a" z="0.0" y="2.0" r="0"></i> 
    67                         <i x="-1.0" o="grass-a" z="0.0" y="-3.0" r="0"></i> 
    68                         <i x="0.0" o="grass-a" z="0.0" y="-3.0" r="0"></i> 
    69                         <i x="1.0" o="grass-b" z="0.0" y="-3.0" r="0"></i> 
    70                         <i x="2.0" o="grass-b" z="0.0" y="-3.0" r="0"></i> 
     67                        <i x="-1.0" o="brick" z="0.0" y="-3.0" r="0"></i> 
     68                        <i x="0.0" o="brick" z="0.0" y="-3.0" r="0"></i> 
     69                        <i x="1.0" o="brick" z="0.0" y="-3.0" r="0"></i> 
     70                        <i x="2.0" o="brick" z="0.0" y="-3.0" r="0"></i> 
    7171                        <i x="3.0" o="brick" z="0.0" y="-3.0" r="0"></i> 
    72                         <i x="3.0" o="brick" z="0.0" y="-2.0" r="0"></i> 
    73                         <i x="3.0" o="brick" z="0.0" y="-1.0" r="0"></i> 
    74                         <i x="3.0" o="brick" z="0.0" y="0.0" r="0"></i> 
    75                         <i x="3.0" o="brick" z="0.0" y="1.0" r="0"></i> 
    76                         <i x="3.0" o="brick" z="0.0" y="2.0" r="0"></i> 
     72                        <i x="3.0" o="grass-a" z="0.0" y="-2.0" r="0"></i> 
     73                        <i x="3.0" o="grass-a" z="0.0" y="-1.0" r="0"></i> 
     74                        <i x="3.0" o="grass-a" z="0.0" y="0.0" r="0"></i> 
     75                        <i x="3.0" o="grass-a" z="0.0" y="1.0" r="0"></i> 
     76                        <i x="3.0" o="grass-a" z="0.0" y="2.0" r="0"></i> 
    7777                </instances> 
    7878        </layer> 
     79 
     80    <layer x_offset="0.0" pathing="cell_edges_and_diagonals" y_offset="0.0" grid_type="square" id="TransitionLayer0" x_scale="1" y_scale="1" rotation="0.0"> 
     81        <instances> 
     82            <i x="1.0" o="grass-ts13" y="-1.0" r="0" z="0.0"></i> 
     83            <i x="1.0" o="grass-tc03" y="0.0" r="0" z="0.0"></i> 
     84            <i x="2.0" o="grass-ts11" y="0.0" r="0" z="0.0"></i> 
     85            <i x="1.0" o="grass-ts05" y="1.0" r="0" z="0.0"></i> 
     86            <i x="-1.0" o="grass-ts07" y="-2.0" r="0" z="0.0"></i> 
     87            <i x="0.0" o="grass-ts12" y="2.0" r="0" z="0.0"></i> 
     88            <i x="1.0" o="grass-tc08" y="2.0" r="0" z="0.0"></i> 
     89            <i x="-1.0" o="grass-tc02" y="-3.0" r="0" z="0.0"></i> 
     90            <i x="0.0" o="grass-ts02" y="-3.0" r="0" z="0.0"></i> 
     91            <i x="1.0" o="grass-ts02" y="-3.0" r="0" z="0.0"></i> 
     92            <i x="2.0" o="grass-ts02" y="-3.0" r="0" z="0.0"></i> 
     93            <i x="3.0" o="grass-ts02" y="-3.0" r="0" z="0.0"></i> 
     94        </instances> 
     95    </layer> 
     96    <layer x_offset="0.0" pathing="cell_edges_and_diagonals" y_offset="0.0" grid_type="square" id="TransitionLayer1" x_scale="1" y_scale="1" rotation="0.0"> 
     97        <instances> 
     98            <i x="1.0" o="grass-ts04" y="0.0" r="0" z="0.0"></i> 
     99            <i x="1.0" o="grass-ts01" y="2.0" r="0" z="0.0"></i> 
     100            <i x="-1.0" o="grass-ts04" y="-3.0" r="0" z="0.0"></i> 
     101        </instances> 
     102    </layer> 
    79103 
    80104        <layer x_offset="0.0" pathing="cell_edges_and_diagonals" y_offset="0.0" grid_type="square" id="ObjectLayer" x_scale="1" y_scale="1" rotation="0.0"> 
  • trunk/demo/maps/test_map.xml

    r36 r38  
    66        <import file="../objects/ground/gravel/gravel.xml"></import> 
    77        <import file="../objects/ground/brick/brick.xml"></import> 
    8         <import file="../objects/ground/grass/transitions/grass-t01.xml"></import> 
    9         <import file="../objects/ground/grass/transitions/grass-t02.xml"></import> 
    10         <import file="../objects/ground/grass/transitions/grass-t03.xml"></import> 
    11         <import file="../objects/ground/grass/transitions/grass-t04.xml"></import> 
    12         <import file="../objects/ground/grass/transitions/grass-t05.xml"></import> 
    13         <import file="../objects/ground/grass/transitions/grass-t06.xml"></import> 
    14         <import file="../objects/ground/grass/transitions/grass-t07.xml"></import> 
    15         <import file="../objects/ground/grass/transitions/grass-t08.xml"></import> 
    16         <import file="../objects/ground/grass/transitions/grass-t09.xml"></import> 
    17         <import file="../objects/ground/grass/transitions/grass-t10.xml"></import> 
    18         <import file="../objects/ground/grass/transitions/grass-t11.xml"></import> 
    19         <import file="../objects/ground/grass/transitions/grass-t12.xml"></import> 
    20         <import file="../objects/ground/grass/transitions/grass-t13.xml"></import> 
    21         <import file="../objects/ground/grass/transitions/grass-t14.xml"></import> 
    22         <import file="../objects/ground/grass/transitions/grass-t15.xml"></import> 
    23         <import file="../objects/ground/grass/transitions/grass-t16.xml"></import> 
    24         <import file="../objects/ground/grass/transitions/grass-t17.xml"></import> 
    25         <import file="../objects/ground/gravel/transitions/gravel-t01.xml"></import> 
    26         <import file="../objects/ground/gravel/transitions/gravel-t02.xml"></import> 
    27         <import file="../objects/ground/gravel/transitions/gravel-t03.xml"></import> 
    28         <import file="../objects/ground/gravel/transitions/gravel-t04.xml"></import> 
    29         <import file="../objects/ground/gravel/transitions/gravel-t05.xml"></import> 
    30         <import file="../objects/ground/gravel/transitions/gravel-t06.xml"></import> 
    31         <import file="../objects/ground/gravel/transitions/gravel-t07.xml"></import> 
    32         <import file="../objects/ground/gravel/transitions/gravel-t08.xml"></import> 
    33         <import file="../objects/ground/gravel/transitions/gravel-t09.xml"></import> 
    34         <import file="../objects/ground/gravel/transitions/gravel-t10.xml"></import> 
    35         <import file="../objects/ground/gravel/transitions/gravel-t11.xml"></import> 
    36         <import file="../objects/ground/gravel/transitions/gravel-t12.xml"></import> 
    37         <import file="../objects/ground/gravel/transitions/gravel-t13.xml"></import> 
    38         <import file="../objects/ground/gravel/transitions/gravel-t14.xml"></import> 
    39         <import file="../objects/ground/gravel/transitions/gravel-t15.xml"></import> 
    40         <import file="../objects/ground/gravel/transitions/gravel-t16.xml"></import> 
    41         <import file="../objects/ground/gravel/transitions/gravel-t17.xml"></import> 
     8        <import file="../objects/ground/grass/transition/grass-tc01.xml"></import> 
     9        <import file="../objects/ground/grass/transition/grass-tc02.xml"></import> 
     10        <import file="../objects/ground/grass/transition/grass-tc03.xml"></import> 
     11        <import file="../objects/ground/grass/transition/grass-tc04.xml"></import> 
     12        <import file="../objects/ground/grass/transition/grass-tc05.xml"></import> 
     13        <import file="../objects/ground/grass/transition/grass-tc06.xml"></import> 
     14        <import file="../objects/ground/grass/transition/grass-tc07.xml"></import> 
     15        <import file="../objects/ground/grass/transition/grass-tc08.xml"></import> 
     16        <import file="../objects/ground/grass/transition/grass-tc09.xml"></import> 
     17        <import file="../objects/ground/grass/transition/grass-tc10.xml"></import> 
     18        <import file="../objects/ground/grass/transition/grass-tc11.xml"></import> 
     19        <import file="../objects/ground/grass/transition/grass-tc12.xml"></import> 
     20        <import file="../objects/ground/grass/transition/grass-tc13.xml"></import> 
     21        <import file="../objects/ground/grass/transition/grass-tc14.xml"></import> 
     22        <import file="../objects/ground/grass/transition/grass-tc15.xml"></import> 
     23        <import file="../objects/ground/grass/transition/grass-ts01.xml"></import> 
     24        <import file="../objects/ground/grass/transition/grass-ts02.xml"></import> 
     25        <import file="../objects/ground/grass/transition/grass-ts03.xml"></import> 
     26        <import file="../objects/ground/grass/transition/grass-ts04.xml"></import> 
     27        <import file="../objects/ground/grass/transition/grass-ts05.xml"></import> 
     28        <import file="../objects/ground/grass/transition/grass-ts06.xml"></import> 
     29        <import file="../objects/ground/grass/transition/grass-ts07.xml"></import> 
     30        <import file="../objects/ground/grass/transition/grass-ts08.xml"></import> 
     31        <import file="../objects/ground/grass/transition/grass-ts09.xml"></import> 
     32        <import file="../objects/ground/grass/transition/grass-ts10.xml"></import> 
     33        <import file="../objects/ground/grass/transition/grass-ts11.xml"></import> 
     34        <import file="../objects/ground/grass/transition/grass-ts12.xml"></import> 
     35        <import file="../objects/ground/grass/transition/grass-ts13.xml"></import> 
     36        <import file="../objects/ground/grass/transition/grass-ts14.xml"></import> 
     37        <import file="../objects/ground/grass/transition/grass-ts15.xml"></import> 
    4238        <layer grid_type="square" id="GroundLayer" x_scale="1.0" pathing="cell_edges_only" y_scale="1.0" rotation="0.0" x_offset="0.0" y_offset="0.0"> 
    4339                <instances> 
    4440                        <i r="0" z="0.0" x="2.0" o="grass-a" y="-2.0" ns="PARPG"></i> 
    45                         <i x="1.0" o="grass-a" z="0.0" y="-1.0" r="0"></i> 
     41                        <i x="1.0" o="brick" z="0.0" y="-1.0" r="0"></i> 
    4642                        <i x="0.0" o="grass-a" z="0.0" y="0.0" r="0"></i> 
    4743                        <i x="-1.0" o="grass-a" z="0.0" y="1.0" r="0"></i> 
    48                         <i x="0.0" o="grass-a" z="0.0" y="1.0" r="0"></i> 
    49                         <i x="1.0" o="grass-b" z="0.0" y="0.0" r="0"></i> 
    50                         <i x="2.0" o="grass-b" z="0.0" y="-1.0" r="0"></i> 
    51                         <i x="1.0" o="grass-b" z="0.0" y="-2.0" r="0"></i> 
    52                         <i x="0.0" o="grass-b" z="0.0" y="-1.0" r="0"></i> 
    53                         <i x="0.0" o="grass-b" z="0.0" y="-1.0" r="0"></i> 
    54                         <i x="-1.0" o="grass-b" z="0.0" y="0.0" r="0"></i> 
    55                         <i x="-1.0" o="grass-b" z="0.0" y="-1.0" r="0"></i> 
    56                         <i x="0.0" o="grass-b" z="0.0" y="-2.0" r="0"></i> 
    57                         <i x="2.0" o="grass-b" z="0.0" y="0.0" r="0"></i> 
    58                         <i x="1.0" o="grass-b" z="0.0" y="1.0" r="0"></i> 
    59                         <i x="2.0" o="grass-b" z="0.0" y="1.0" r="0"></i> 
     44                        <i x="0.0" o="grass-b" z="0.0" y="1.0" r="0"></i> 
     45                        <i x="1.0" o="brick" z="0.0" y="0.0" r="0"></i> 
     46                        <i x="2.0" o="grass-a" z="0.0" y="-1.0" r="0"></i> 
     47                        <i x="1.0" o="grass-a" z="0.0" y="-2.0" r="0"></i> 
     48                        <i x="0.0" o="grass-a" z="0.0" y="-1.0" r="0"></i> 
     49                        <i x="0.0" o="grass-a" z="0.0" y="-1.0" r="0"></i> 
     50                        <i x="-1.0" o="grass-a" z="0.0" y="0.0" r="0"></i> 
     51                        <i x="-1.0" o="grass-a" z="0.0" y="-1.0" r="0"></i> 
     52                        <i x="0.0" o="grass-a" z="0.0" y="-2.0" r="0"></i> 
     53                        <i x="2.0" o="grass-a" z="0.0" y="0.0" r="0"></i> 
     54                        <i x="1.0" o="grass-a" z="0.0" y="1.0" r="0"></i> 
     55                        <i x="2.0" o="grass-a" z="0.0" y="1.0" r="0"></i> 
    6056                        <i x="-1.0" o="grass-a" z="0.0" y="-2.0" r="0"></i> 
    61                         <i x="-2.0" o="gravel" z="0.0" y="-2.0" r="0"></i> 
    62                         <i x="-2.0" o="gravel" z="0.0" y="-1.0" r="0"></i> 
    63                         <i x="-2.0" o="gravel" z="0.0" y="0.0" r="0"></i> 
    64                         <i x="-2.0" o="gravel" z="0.0" y="1.0" r="0"></i> 
    65                         <i x="-2.0" o="gravel" z="0.0" y="2.0" r="0"></i> 
    66                         <i x="-2.0" o="gravel" z="0.0" y="-3.0" r="0"></i> 
     57                        <i x="-2.0" o="grass-a" z="0.0" y="-2.0" r="0"></i> 
     58                        <i x="-2.0" o="grass-a" z="0.0" y="-1.0" r="0"></i> 
     59                        <i x="-2.0" o="grass-a" z="0.0" y="0.0" r="0"></i> 
     60                        <i x="-2.0" o="grass-a" z="0.0" y="1.0" r="0"></i> 
     61                        <i x="-2.0" o="grass-a" z="0.0" y="2.0" r="0"></i> 
     62                        <i x="-2.0" o="grass-a" z="0.0" y="-3.0" r="0"></i> 
    6763                        <i x="-1.0" o="grass-a" z="0.0" y="2.0" r="0"></i> 
    6864                        <i x="0.0" o="grass-a" z="0.0" y="2.0" r="0"></i> 
    69                         <i x="1.0" o="grass-a" z="0.0" y="2.0" r="0"></i> 
     65                        <i x="1.0" o="grass-b" z="0.0" y="2.0" r="0"></i> 
    7066                        <i x="2.0" o="grass-a" z="0.0" y="2.0" r="0"></i> 
    7167                        <i x="-1.0" o="grass-a" z="0.0" y="-3.0" r="0"></i> 
    7268                        <i x="0.0" o="grass-a" z="0.0" y="-3.0" r="0"></i> 
    73                         <i x="1.0" o="grass-b" z="0.0" y="-3.0" r="0"></i> 
    74                         <i x="2.0" o="grass-b" z="0.0" y="-3.0" r="0"></i> 
    75                         <i x="3.0" o="brick" z="0.0" y="-3.0" r="0"></i> 
    76                         <i x="3.0" o="brick" z="0.0" y="-2.0" r="0"></i> 
    77                         <i x="3.0" o="brick" z="0.0" y="-1.0" r="0"></i> 
    78                         <i x="3.0" o="brick" z="0.0" y="0.0" r="0"></i> 
    79                         <i x="3.0" o="brick" z="0.0" y="1.0" r="0"></i> 
    80                         <i x="3.0" o="brick" z="0.0" y="2.0" r="0"></i> 
     69                        <i x="1.0" o="grass-a" z="0.0" y="-3.0" r="0"></i> 
     70                        <i x="2.0" o="grass-a" z="0.0" y="-3.0" r="0"></i> 
     71                        <i x="3.0" o="grass-a" z="0.0" y="-3.0" r="0"></i> 
     72                        <i x="3.0" o="grass-a" z="0.0" y="-2.0" r="0"></i> 
     73                        <i x="3.0" o="grass-a" z="0.0" y="-1.0" r="0"></i> 
     74                        <i x="3.0" o="grass-a" z="0.0" y="0.0" r="0"></i> 
     75                        <i x="3.0" o="grass-a" z="0.0" y="1.0" r="0"></i> 
     76                        <i x="3.0" o="grass-a" z="0.0" y="2.0" r="0"></i> 
    8177                </instances> 
    8278        </layer> 
  • trunk/demo/objects/ground/grass/transition/grass-tc08.xml

    r37 r38  
    11<?fife type="object"?> 
    2 <object id="grass-tc09" namespace="PARPG" blocking="0" static="1"> 
    3         <image source="grass-tc09.png" direction="0" /> 
     2<object id="grass-tc08" namespace="PARPG" blocking="0" static="1"> 
     3        <image source="grass-tc08.png" direction="0" /> 
    44</object> 
  • trunk/demo/scripts/world.py

    r37 r38  
    5353        self.filename = '' 
    5454        self.instance_to_agent = {} 
    55         self.transitions=None 
     55        self.transitions=[] 
    5656 
    5757    def reset(self): 
     
    7272        self.agentlayer = self.map.getLayer('ObjectLayer') 
    7373        # it's possible there's no transition layer 
     74        size=len('TransitionLayer') 
    7475        for layer in self.map.getLayers(): 
    75             # will have to check for >1 layers in reality 
    76             if(layer.getId()=='TransitionLayer'): 
    77                 self.transitions = self.map.getLayer('TransitionLayer') 
     76            # could be many layers, but hopefully no more than 3 
     77            if(layer.getId()[:size]=='TransitionLayer'): 
     78                self.transitions.append(self.map.getLayer(layer.getId())) 
    7879        self.PC = Hero(self.model,'PC',self.agentlayer) 
    7980        self.instance_to_agent[self.PC.agent.getFifeId()] = self.PC 
  • trunk/demo/utilities/transition.py

    r35 r38  
    2020from xml.sax.handler import ContentHandler  
    2121 
    22 # code is for building the transition layer for the map 
     22# this code is for building the transition layer for the map 
    2323# the world map is built of two layers: one for the world floor, and the other 
    2424# for the all the objects (including the player and NPC) 
     
    2929# this is experimental code for the moment 
    3030# awaiting rest of tile graphics for full testing 
     31 
     32# some simple defines for each part of the tile 
     33TOP             =   1 
     34RIGHT           =   2 
     35BOTTOM          =   4 
     36LEFT            =   8 
     37TOP_LEFT        =   16 
     38TOP_RIGHT       =   32 
     39BOTTOM_RIGHT    =   64 
     40BOTTOM_LEFT     =   128 
     41 
     42# side transition tiles always block corner tiles 
     43# but which ones? 
     44TOP_BLOCK       =   TOP_RIGHT+TOP_LEFT 
     45RIGHT_BLOCK     =   TOP_RIGHT+BOTTOM_RIGHT 
     46BOTTOM_BLOCK    =   BOTTOM_RIGHT+BOTTOM_LEFT 
     47LEFT_BLOCK      =   TOP_LEFT+BOTTOM_LEFT 
     48NONE            =   0 
     49 
     50# now for each of the 15 different possible side variations we 
     51# can know what corner pieces do not need to be drawn 
     52# this table stores all of the allowed combinations 
     53# based on the bit pattern for the side elements 
     54 
     55CORNER_LOOKUP   =   [BOTTOM_BLOCK,  LEFT_BLOCK, 
     56                     BOTTOM_LEFT,   TOP_BLOCK, 
     57                     NONE,          TOP_LEFT, 
     58                     NONE,          RIGHT_BLOCK, 
     59                     BOTTOM_RIGHT,  NONE, 
     60                     NONE,          TOP_RIGHT, 
     61                     NONE,          NONE, 
     62                     NONE] 
    3163 
    3264class XMLTileData: 
     
    103135    def __init__(self): 
    104136        self.layers = [] 
    105         self.ttile = [] 
     137        self.ttiles = [] 
     138        self.render_tiles =[] 
    106139        self.min_x = 0 
    107140        self.max_x = 0 
     
    109142        self.max_y = 0 
    110143 
    111     def OutputTransLayer(self,l_file): 
    112         if(len(ttiles)==0): 
     144    def OutputTransLayer(self,l_file,l_count): 
     145        if(len(self.render_tiles)==0): 
    113146            return True 
    114147        try: 
    115             l_file.write('''    <layer x_offset="0.0" pathing="') 
    116                           cell_edges_and_diagonals" y_offset="0.0"  
    117                           grid_type="square" id="TransitionLayer" 
    118                           x_scale="1" y_scale="1" rotation="0.0">\n''') 
    119             l_file.write('        <instances>"') 
    120             for tile in ttile: 
     148            layer_name="TransitionLayer"+str(l_count) 
     149            l_file.write('''    <layer x_offset="0.0" pathing="''') 
     150            l_file.write('''cell_edges_and_diagonals" y_offset="0.0"''') 
     151            l_file.write(''' grid_type="square" id="''') 
     152            l_file.write(layer_name+'''"''') 
     153            l_file.write(''' x_scale="1" y_scale="1" rotation="0.0">\n''') 
     154            l_file.write('        <instances>\n') 
     155            for tile in self.render_tiles: 
    121156                l_file.write('''            <i x="''') 
    122                 l_file.write(str(ttile.x)) 
    123                 l_file.write('''"" o="''') 
    124                 l_file.write(i.name) 
     157                l_file.write(str(tile.x)) 
     158                l_file.write('''" o="''') 
     159                l_file.write(tile.object) 
    125160                l_file.write('''" y="''') 
    126                 l_file.write(str(ttile.y)) 
     161                l_file.write(str(tile.y)) 
    127162                l_file.write('''" r="0" z="0.0"></i>\n''') 
    128                 l_file.write('        </instances>\n    </layer>') 
    129                 l_file.write('</layer>') 
     163            l_file.write('        </instances>\n    </layer>\n') 
    130164        except(IOError): 
    131165            sys.stderr.write("Error: Couldn't write data") 
     
    133167        return True 
    134168 
    135     def RenderTransLayer(self,search): 
    136         """Build up the data for a transition layer""" 
     169    def GetSurroundings(self,x,y,search): 
     170        """Function called by BuildTransLayer to see if a tile needs to 
     171           display transition graphics over it (drawn on another layer)""" 
     172        # check all of the tiles around the current tile 
     173        value=0 
     174        if(self.PMatchSearch(x,y+1,search)==True): 
     175            value+=RIGHT 
     176        if(self.PMatchSearch(x-1,y+1,search)==True): 
     177            value+=BOTTOM_RIGHT 
     178        if(self.PMatchSearch(x-1,y,search)==True): 
     179            value+=BOTTOM 
     180        if(self.PMatchSearch(x-1,y-1,search)==True): 
     181            value+=BOTTOM_LEFT 
     182        if(self.PMatchSearch(x,y-1,search)==True): 
     183            value+=LEFT 
     184        if(self.PMatchSearch(x+1,y-1,search)==True): 
     185            value+=TOP_LEFT 
     186        if(self.PMatchSearch(x+1,y,search)==True): 
     187            value+=TOP 
     188        if(self.PMatchSearch(x+1,y+1,search)==True): 
     189            value+=TOP_RIGHT 
     190        return value 
     191 
     192    def GetTransitionTiles(self,search): 
     193        """Build up and return a list of the tiles that might 
     194           need a transition tiles layed over them""" 
    137195        size=len(search) 
    138196        tiles=[] 
    139         trans=[] 
    140197        for t in self.layers[0].tiles: 
    141             if t.object!=None and t.object[:size]==search: 
    142                 # found a match, now calculate 
    143                 tiles.append([t.x,t.y]) 
    144                 # touch all of the tiles around this one 
    145                 if(self.CheckRange(t.x+1,t.y)==True): 
    146                     tiles.append([t.x+1,t.y]) 
    147                 if(self.CheckRange(t.x+1,t.y-1)==True): 
    148                     tiles.append([t.x+1,t.y-1]) 
    149                 if(self.CheckRange(t.x+1,t.y-1)==True): 
    150                     tiles.append([t.x+1,t.y-1]) 
    151                 if(self.CheckRange(t.x,t.y-1)==True): 
    152                     tiles.append([t.x,t.y-1]) 
    153                 if(self.CheckRange(t.x,t.y+1)==True): 
    154                     tiles.append([t.x,t.y+1]) 
    155                 if(self.CheckRange(t.x-1,t.y-1)==True): 
    156                     tiles.append([t.x-1,t.y-1]) 
    157                 if(self.CheckRange(t.x-1,t.y)==True): 
    158                     tiles.append([t.x-1,t.y]) 
    159                 if(self.CheckRange(t.x-1,t.y+1)==True): 
    160                     tiles.append([t.x-1,t.y+1]) 
    161         # now run down the render tiles 
     198            # we are only interested in tiles that DON'T have what we are 
     199            # are looking for (because they might need a transition gfx) 
     200            if t.object!=None and t.object[:size]!=search: 
     201                # whereas now we we need to check all the tiles around 
     202                # this tile 
     203                trans_value=self.GetSurroundings(t.x,t.y,search) 
     204                if(trans_value!=0): 
     205                    # we found an actual real transition 
     206                    tiles.append([t.x,t.y,trans_value]) 
     207        return tiles 
     208 
     209    def GetTransitionName(self,base,value,corner=False): 
     210        if(corner==False): 
     211            name=base+"-ts" 
     212        else: 
     213            name=base+"-tc" 
     214        if(value<10): 
     215            name+="0" 
     216        name+=str(value) 
     217        return name 
     218 
     219    def BuildTransLayer(self,search): 
     220        """Build up the data for a transition layer 
     221           search is the string that matches the start of the name of 
     222           each tile that we are looking for""" 
     223        transition_tiles=self.GetTransitionTiles(search)        
     224        # now we have all the possible tiles, lets see what they 
     225        # actually need to have rendered 
     226        for t in transition_tiles: 
     227            # first we calculate the side tiles: 
     228            sides=(t[2]&15) 
     229            if(sides!=0): 
     230                # there are some side tiles to be drawn. Now we just 
     231                # need to see if there are any corners to be done 
     232                corners=(t[2]&240)&(CORNER_LOOKUP[sides-1])                     
     233                if(corners!=0): 
     234                    # we must add a corner piece as well 
     235                    corners=corners/16 
     236                    name=self.GetTransitionName(search,corners,True) 
     237                    self.ttiles.append(XMLTileData(t[0],t[1],0,name)) 
     238                # add the side tile pieces 
     239                name=self.GetTransitionName(search,sides,False) 
     240                self.ttiles.append(XMLTileData(t[0],t[1],0,name)) 
     241            else: 
     242                # there are no side tiles, so let's just look at 
     243                # the corners (quite easy): 
     244                corners=(t[2]&240)/16 
     245                if(corners!=0): 
     246                    # there is a corner piece needed 
     247                    name=self.GetTransitionName(search,corners,True) 
     248                    self.ttiles.append(XMLTileData(t[0],t[1],0,name)) 
    162249 
    163250    def LoadFromXML(self,filename): 
     
    194281        """Grid co-ords in range?""" 
    195282        if((x<self.min_x)or(x>self.max_x)or 
    196            (y<self.min_y)or(y>self.may_y)): 
     283           (y<self.min_y)or(y>self.max_y)): 
    197284           return False 
     285        return True 
     286 
     287    def PMatchSearch(self,x,y,search): 
     288        """Brute force method used for matching grid""" 
     289        # is the tile even in range? 
     290        if(self.CheckRange(x,y)==False): 
     291            return False 
     292        size=len(search) 
     293        for t in self.layers[0].tiles: 
     294            if((t.x==x)and(t.y==y)and(t.object[:size]==search)): 
     295                return(True) 
     296        # no match 
     297        return False 
     298 
     299    def CoordsMatch(self,x,y,tiles): 
     300        """Helper routine to check wether the list of tiles 
     301           in tiles has any contain the coords x,y""" 
     302        for t in tiles: 
     303            if((t.x==x)and(t.y==y)): 
     304                return True 
     305        # obviously no match 
     306        return False 
     307 
     308    def SaveMap(self,filename): 
     309        """Save the new map""" 
     310        # open the new files for writing 
     311        try: 
     312            map_file=open(filename,'wt') 
     313        except(IOError): 
     314            sys.stderr.write("Error: Couldn't save map\n") 
     315            return(False) 
     316        # we don't know how many layers we need, let's do that now 
     317        # this is a brute force solution but it does work, and speed 
     318        # is not required in this utility 
     319        layer_count=0 
     320        while(self.ttiles!=[]): 
     321            recycled_tiles=[] 
     322            self.render_tiles=[] 
     323            for t in self.ttiles: 
     324                if(self.CoordsMatch(t.x,t.y,self.render_tiles)==False): 
     325                    # no matching tile in the grid so far, so add it 
     326                    self.render_tiles.append(t) 
     327                else: 
     328                    # we must save this for another layer 
     329                    recycled_tiles.append(t) 
     330            # render this layer 
     331            if(self.OutputTransLayer(map_file,layer_count)==False): 
     332                return False 
     333            layer_count+=1 
     334            self.ttiles=recycled_tiles 
     335        # phew, that was it 
     336        map_file.close() 
     337        print "Output new file as new.xml" 
     338        print "Had to render",layer_count,"layers" 
    198339        return True 
    199340     
     
    201342        """Debugging routine to output some details about the map 
    202343           Used to check the map loaded ok""" 
    203         # display each layer, then the details 
     344        # display each layer name, then the details 
     345        print "Layer ID's:", 
    204346        for l in self.layers: 
    205             print "Layer id:",l.name 
    206         print "Map Dimensions: X=",(self.max_x-self.min_x)+1, 
     347            print l.name, 
     348        print "\nMap Dimensions: X=",(self.max_x-self.min_x)+1, 
    207349        print " Y=",(self.max_y-self.min_y)+1 
    208350 
     
    215357    if(new_map.LoadFromXML(sys.argv[1])==True): 
    216358        new_map.GetSize() 
    217         new_map.RenderTransLayer("grass") 
     359        new_map.BuildTransLayer("grass") 
     360        new_map.SaveMap("new.xml") 
    218361        new_map.PrintDetails() 
    219362 
Note: See TracChangeset for help on using the changeset viewer.