Advertisement
Guest User

Untitled

a guest
Jun 19th, 2012
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. /**
  2. test.hps
  3. 19/06/12
  4. Updated 20/06/12
  5. @Javist
  6. **/
  7. funcdef void IsSolvedCallBack(bool solveState);
  8. class BitwisePuzzle
  9. {
  10. IsSolvedCallBack@ solvedCallBack;
  11. uint64 bitField;
  12. uint64[] bitMasks;
  13. string[] switches;
  14. int[] sols;
  15. BitwisePuzzle(string[] &in switchNames, int[] solutions, IsSolvedCallBack@ &in callback )
  16. {
  17.     switches = switchNames;
  18.     sols = solutions;
  19.     @solvedCallBack = @callback;
  20.     bitField = 0;
  21.     bitMasks.resize(switchNames.length());
  22.     for(uint i=0;i<switchNames.length();i++)
  23.     {
  24.     AddDebugMessage(" "+i, false);
  25.     bitMasks[i]=(1<<i);
  26.     }
  27.     AddDebugMessage("BWPuzzle ready. (sz "+switches.length()+")", false);
  28. }
  29. void checkValidity()
  30. {
  31.     uint64 requiredMask = 0;
  32.     for(uint i=0;i<sols.length();i++)
  33.     {
  34.         requiredMask |= bitMasks[sols[i]-1];
  35.     }
  36.     solvedCallBack(requiredMask == bitField);
  37.        
  38. }
  39. void notifySwitch(string &in switchName, int alState)
  40. {
  41.     for(uint i=0;i<switches.length();i++)
  42.     {
  43.         if(switches[i]==switchName)
  44.         {
  45.             bitField = alState > 0 ? bitField |= bitMasks[i] : bitField ^ bitMasks[i];
  46.             checkValidity();
  47.             return;
  48.         }
  49.     }
  50.     AddDebugMessage("ERR: "+switchName+" isn't declared in BWPuzzle (sz "+switches.length()+") ?!",false);
  51. }
  52. uint64 getBitField()
  53. {
  54.     return bitField;
  55. }
  56. ~BitwisePuzzle()
  57. {
  58. @solvedCallBack = null;
  59. }
  60. }
  61. BitwisePuzzle bp;
  62. /////////////////////////////////////////////
  63. /////////////////////////////////////////////
  64. /////////////////////////////////////////////
  65. /////////////////////////////////////////////
  66. void OnStart()
  67. {
  68.     GiveItemFromFile("lantern", "lantern.ent"); // Светилка. Мы ведь не негры и уголь не воруем)
  69.     IsSolvedCallBack@ cb = @puzzleSolvedCallBack;
  70.     string[] levers = {"lever_nice01_1","lever_nice01_2","lever_nice01_3","lever_nice01_4"}; // Имена ответственных рычагов. Порядок указания учитывается!
  71.     int[] solutionz = {1,2,4}; // Позиции правильных рычагов для решения паззла.
  72.     bp = BitwisePuzzle(levers,solutionz,@cb);
  73. }
  74.  
  75. void puzzleSolvedCallBack(bool solveState)
  76. {
  77. SetSwingDoorLocked("castle_1", !solveState, true);
  78. SetLampLit("hanging_lantern_wall_5", solveState, true);
  79. }
  80.  
  81.  
  82. void doSw(string &in asEntity, int alState)
  83. {
  84.     SetLeverStuckState(asEntity, alState, true);
  85.     bp.notifySwitch(asEntity, alState);
  86.     uint64 field = bp.getBitField();
  87.     for(int i=0;i<4;i++)
  88.         SetLampLit("hanging_lantern_wall_"+(i+1), (1<<i&field) > 0, true);
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement