Advertisement
Apjjm

Amnesia TDD: Script Area Generator

Oct 21st, 2012
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.58 KB | None | 0 0
  1. from decimal import *
  2.  
  3. ################################################################
  4. #Generate a unique area name based on it's position
  5. ###############################################################
  6. DPQ = Decimal(10) ** -3
  7. def getAreaName(xPos,yPos,zPos):
  8.     return 'SAGrid_('+str(Decimal(xPos).quantize(DPQ))+','+str(Decimal(yPos).quantize(DPQ))+','+str(Decimal(zPos).quantize(DPQ))+')'
  9.  
  10. ################################################################
  11. #Writes pre-entity data to a target file
  12. ################################################################
  13. def writePreEntityData(targetFile):
  14.     targetFile.write('<Level>\n')
  15.     targetFile.write('    <MapData FogActive="false" FogColor="1 1 1 1" FogCulling="true" FogEnd="20" FogFalloffExp="1" FogStart="0"'+
  16.                      ' GlobalDecalMaxTris="300" Name="" SkyBoxActive="false" SkyBoxColor="1 1 1 1" SkyBoxTexture="">\n')
  17.     targetFile.write('        <MapContents>\n            <FileIndex_StaticObjects NumOfFiles="0" />\n')
  18.     targetFile.write('            <FileIndex_Entities NumOfFiles="0" />\n            <FileIndex_Decals NumOfFiles="0" />\n')
  19.     targetFile.write('            <StaticObjects />\n            <Primitives />\n            <Decals />\n            <Entities>\n')
  20.  
  21. ################################################################
  22. #Writes post-entity data to a target file
  23. ################################################################
  24. def writePostEntityData(targetFile):
  25.     targetFile.write('            </Entities>\n            <Misc />\n            <StaticObjectCombos />\n')
  26.     targetFile.write('        </MapContents>\n    </MapData>\n</Level>\n')
  27.    
  28. ################################################################
  29. #Add a script area to a target file as defined by the properties
  30. ################################################################
  31. def addScriptArea(targetFile,areaID,areaName,aScaleX,aScaleY,aScaleZ,aX,aY,aZ,
  32.                   playerLookAtCallback='',
  33.                   playerLookAtCallbackAutoRemove=False,
  34.                   playerInteractCallback='',
  35.                   playerInteractCallbackAutoRemove=False,
  36.                   itemInteraction=False,
  37.                   maxFocusDistance= -1):
  38.  
  39.  
  40.    
  41.     #Write the properties of the area
  42.     targetFile.write('                <Area Active="true" AreaType="Script" Group="0" ID="'+ str(areaID) +
  43.                      '" Mesh="" Name="' + str(areaName) +
  44.                      '" Rotation="0 0 0" Scale="' + str(Decimal(aScaleX).quantize(DPQ)) + ' ' +
  45.                      str(Decimal(aScaleY).quantize(DPQ)) + ' ' +
  46.                      str(Decimal(aScaleZ).quantize(DPQ)) +
  47.                      '" Tag="" WorldPos="' + str(Decimal(aX).quantize(DPQ)) + ' ' +
  48.                      str(Decimal(aY).quantize(DPQ)) + ' ' + str(Decimal(aZ).quantize(DPQ)) + '">\n')
  49.  
  50.     #Write user variables of the area of the area
  51.     targetFile.write('                    <UserVariables>\n')
  52.     targetFile.write('                        <Var Name="PlayerLookAtCallback" Value="'+str(playerLookAtCallback)+'" />\n')
  53.     targetFile.write('                        <Var Name="PlayerLookAtCallbackAutoRemove" Value="' + str(playerLookAtCallbackAutoRemove) + '" />\n')
  54.     targetFile.write('                        <Var Name="PlayerInteractCallback" Value="'+str(playerInteractCallback)+'" />\n')
  55.     targetFile.write('                        <Var Name="PlayerInteractCallbackAutoRemove" Value="'+str(playerInteractCallbackAutoRemove)+'" />\n')
  56.     targetFile.write('                        <Var Name="ItemInteraction" Value="'+str(itemInteraction)+'" />\n')
  57.     targetFile.write('                        <Var Name="MaxFocusDistance" Value="'+str(maxFocusDistance)+'" />\n')
  58.     targetFile.write('                    </UserVariables>\n')
  59.  
  60.     #Close off the area tag
  61.     targetFile.write('                </Area>\n')
  62.  
  63.  
  64. ################################################################
  65. #Generate a range based on scale factor and from-to variables
  66. ################################################################
  67. def dRange(xFrom,xTo,xStep):
  68.  
  69.     r = Decimal(xFrom)
  70.     to = Decimal(xTo)
  71.     step = Decimal(xStep)
  72.     result = []
  73.    
  74.     while r < to:
  75.         result.append(r)
  76.         r += step
  77.    
  78.     return result
  79.  
  80. ################################################################
  81. #Generate Content to an expobj file
  82. ################################################################
  83. def writeEntityData(fileName,xRange,yRange,zRange,areaScalex,areaScaley,areaScalez):
  84.     #Write pre-entity stuff to file
  85.     print('Writing to ' + fileName)
  86.     f = open(fileName,'w')
  87.     writePreEntityData(f)
  88.    
  89.    
  90.     #Write each area to the expobj file
  91.     totalAreas = 0
  92.     for x in xRange:
  93.         for y in yRange:
  94.             for z in zRange:
  95.                 areaName = getAreaName(x,y,z)
  96.                 totalAreas += 1
  97.                 addScriptArea(f,totalAreas,areaName,areaScalex,areaScaley,areaScalez,x,y,z) #Can add lookat callbacks etc here
  98.  
  99.     #Write post-entity stuff to file
  100.     writePostEntityData(f)
  101.     f.close()
  102.     print('All done: ' + fileName)
  103.  
  104.  
  105. ################################################################
  106. #Main Program
  107. ################################################################
  108. print 'scale factor (x/z):'
  109. sfx = Decimal(raw_input())
  110. print 'scale factor (y):'
  111. sfy = Decimal(raw_input())
  112. print 'x-length:'
  113. xr = dRange(0,Decimal(raw_input()),sfx)
  114. print 'y-length:'
  115. yr = dRange(0,Decimal(raw_input()),sfy)
  116. print 'z-length:'
  117. zr = dRange(0,Decimal(raw_input()),sfx)
  118. print 'filename:'
  119. writeEntityData(raw_input(),xr,yr,zr,sfx,sfy,sfx)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement