Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
7.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "importkeyvaluebase.h"
  7. #include "dmserializers.h"
  8. #include "datamodel/idatamodel.h"
  9. #include "datamodel/dmelement.h"
  10. #include "tier1/KeyValues.h"
  11. #include "tier1/utlbuffer.h"
  12. #include "datamodel/dmattribute.h"
  13. //-----------------------------------------------------------------------------
  14. // Serialization class for Key Values
  15. //-----------------------------------------------------------------------------
  16. class CImportActBusy : public CImportKeyValueBase
  17. {
  18. public:
  19. virtual const char *GetName() const { return "actbusy"; }
  20. virtual const char *GetDescription() const { return "ActBusy Script File"; }
  21. virtual int GetCurrentVersion() const { return 0; } // doesn't store a version
  22. bool Serialize( CUtlBuffer &outBuf, CDmElement *pRoot );
  23. CDmElement* UnserializeFromKeyValues( KeyValues *pKeyValues );
  24. private:
  25. // Reads a single element
  26. bool UnserializeActBusyKey( CDmAttribute *pChildren, KeyValues *pKeyValues );
  27. // Writes out the actbusy header
  28. void SerializeHeader( CUtlBuffer &buf );
  29. };
  30. //-----------------------------------------------------------------------------
  31. // Singleton instance
  32. //-----------------------------------------------------------------------------
  33. static CImportActBusy s_ImportActBusy;
  34. void InstallActBusyImporter( IDataModel *pFactory )
  35. {
  36. pFactory->AddSerializer( &s_ImportActBusy );
  37. }
  38. //-----------------------------------------------------------------------------
  39. // Writes out the actbusy header
  40. //-----------------------------------------------------------------------------
  41. void CImportActBusy::SerializeHeader( CUtlBuffer &buf )
  42. {
  43. buf.Printf( "// \"act busy name\"\t\tThis is the name that a mapmaker must specify in the hint node.\n" );
  44. buf.Printf( "// {\n" );
  45. buf.Printf( "// \t\"busy_anim\"\t\t\t\"Activity Name\".\n" );
  46. buf.Printf( "// \t\"entry_anim\"\t\t\"Activity Name\"\n" );
  47. buf.Printf( "// \t\"exit_anim\"\t\t\t\"Activity Name\"\n" );
  48. buf.Printf( "// \t\"busy_sequence\"\t\t\"Sequence Name\". If specified, this is used over the activity name. Specify it in the hint node.\n" );
  49. buf.Printf( "// \t\"entry_sequence\"\t\"Sequence Name\". If specified, this is used over the entry anim.\n" );
  50. buf.Printf( "// \t\"exit_sequence\"\t\t\"Sequence Name\". If specified, this is used over the exit anim.\n" );
  51. buf.Printf( "// \t\"min_time\"\t\t\t\"Minimum time to spend in this busy anim\"\n" );
  52. buf.Printf( "// \t\"max_time\"\t\t\t\"Maximum time to spend in this busy anim\" 0 = only stop when interrupted by external event\n" );
  53. buf.Printf( "// \t\"interrupts\"\t\tOne of:\n" );
  54. buf.Printf( "// \t\t\t\t\t\t\"BA_INT_NONE\"\t\tbreak out only when time runs out. No external influence will break me out.\n" );
  55. buf.Printf( "// \t\t\t\t\t\t\"BA_INT_DANGER\"\t\tbreak out of this anim only if threatened\n" );
  56. buf.Printf( "// \t\t\t\t\t\t\"BA_INT_PLAYER\"\t\tbreak out of this anim if I can see the player, or I'm threatened\n" );
  57. buf.Printf( "// \t\t\t\t\t\t\"BA_INT_AMBUSH\"\t\tsomeone please define this - I have no idea what it does\n" );
  58. buf.Printf( "// \t\t\t\t\t\t\"BA_INT_COMBAT\"\t\tbreak out of this anim if combat occurs in my line of sight (bullet hits, grenades, etc), -OR- the max time is reached\n" );
  59. buf.Printf( "// }\n" );
  60. buf.Printf( "//\n" );
  61. }
  62. //-----------------------------------------------------------------------------
  63. // Writes out a new actbusy file
  64. //-----------------------------------------------------------------------------
  65. bool CImportActBusy::Serialize( CUtlBuffer &buf, CDmElement *pRoot )
  66. {
  67. SerializeHeader( buf );
  68. buf.Printf( "\"ActBusy.txt\"\n" );
  69. buf.Printf( "{\n" );
  70. CDmAttribute *pChildren = pRoot->GetAttribute( "children" );
  71. if ( !pChildren || pChildren->GetType() != AT_ELEMENT_ARRAY )
  72. return NULL;
  73. CDmrElementArray<> children( pChildren );
  74. int nCount = children.Count();
  75. buf.PushTab();
  76. for ( int i = 0; i < nCount; ++i )
  77. {
  78. CDmElement *pChild = children[i];
  79. buf.Printf( "\"%s\"\n", pChild->GetName() );
  80. buf.Printf( "{\n" );
  81. buf.PushTab();
  82. PrintStringAttribute( pChild, buf, "busy_anim", true );
  83. PrintStringAttribute( pChild, buf, "entry_anim", true );
  84. PrintStringAttribute( pChild, buf, "exit_anim", true );
  85. PrintStringAttribute( pChild, buf, "busy_sequence", true );
  86. PrintStringAttribute( pChild, buf, "entry_sequence", true );
  87. PrintStringAttribute( pChild, buf, "exit_sequence", true );
  88. PrintFloatAttribute( pChild, buf, "min_time" );
  89. PrintFloatAttribute( pChild, buf, "max_time" );
  90. PrintStringAttribute( pChild, buf, "interrupts" );
  91. buf.PopTab();
  92. buf.Printf( "}\n" );
  93. }
  94. buf.PopTab();
  95. buf.Printf( "}\n" );
  96. return true;
  97. }
  98. //-----------------------------------------------------------------------------
  99. // Reads a single element
  100. //-----------------------------------------------------------------------------
  101. bool CImportActBusy::UnserializeActBusyKey( CDmAttribute *pChildren, KeyValues *pKeyValues )
  102. {
  103. CDmElement *pActBusy = CreateDmElement( "DmElement", pKeyValues->GetName(), NULL );
  104. if ( !pActBusy )
  105. return false;
  106. // Each act busy needs to have an editortype associated with it so it displays nicely in editors
  107. pActBusy->SetValue( "editorType", "actBusy" );
  108. float flZero = 0.0f;
  109. AddStringAttribute( pActBusy, pKeyValues, "busy_anim", "" );
  110. AddStringAttribute( pActBusy, pKeyValues, "entry_anim", "" );
  111. AddStringAttribute( pActBusy, pKeyValues, "exit_anim", "" );
  112. AddStringAttribute( pActBusy, pKeyValues, "busy_sequence", "" );
  113. AddStringAttribute( pActBusy, pKeyValues, "entry_sequence", "" );
  114. AddStringAttribute( pActBusy, pKeyValues, "exit_sequence", "" );
  115. AddFloatAttribute( pActBusy, pKeyValues, "min_time", &flZero );
  116. AddFloatAttribute( pActBusy, pKeyValues, "max_time", &flZero );
  117. AddStringAttribute( pActBusy, pKeyValues, "interrupts", "BA_INT_NONE" );
  118. CDmrElementArray<> children( pChildren );
  119. children.AddToTail( pActBusy );
  120. return true;
  121. }
  122. //-----------------------------------------------------------------------------
  123. // Main entry point for the unserialization
  124. //-----------------------------------------------------------------------------
  125. CDmElement* CImportActBusy::UnserializeFromKeyValues( KeyValues *pKeyValues )
  126. {
  127. // Create the main element
  128. CDmElement *pElement = CreateDmElement( "DmElement", "ActBusyList", NULL );
  129. if ( !pElement )
  130. return NULL;
  131. // Each act busy list needs to have an editortype associated with it so it displays nicely in editors
  132. pElement->SetValue( "editorType", "actBusyList" );
  133. // All actbusy keys are elements of a single element array attribute 'children'
  134. CDmAttribute *pChildren = pElement->AddAttribute( "children", AT_ELEMENT_ARRAY );
  135. if ( !pChildren )
  136. return NULL;
  137. // Under the root are all the actbusy keys
  138. for ( KeyValues *pActBusyKey = pKeyValues->GetFirstTrueSubKey(); pActBusyKey != NULL; pActBusyKey = pActBusyKey->GetNextTrueSubKey() )
  139. {
  140. if ( !UnserializeActBusyKey( pChildren, pActBusyKey ) )
  141. {
  142. Warning( "Error importing actbusy element %s\n", pActBusyKey->GetName() );
  143. return NULL;
  144. }
  145. }
  146. // Resolve all element references recursively
  147. RecursivelyResolveElement( pElement );
  148. return pElement;
  149. }