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.
 
 
 

82 lines
1.9 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2015 Sam Hocevar <sam@hocevar.net>
  5. // © 2012—2015 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
  6. //
  7. // Lol Engine is free software. It comes without any warranty, to
  8. // the extent permitted by applicable law. You can redistribute it
  9. // and/or modify it under the terms of the Do What the Fuck You Want
  10. // to Public License, Version 2, as published by the WTFPL Task Force.
  11. // See http://www.wtfpl.net/ for more details.
  12. //
  13. #pragma once
  14. //
  15. // Various basic convenience functions
  16. // ------------------
  17. //
  18. #include <lol/base/string.h>
  19. namespace lol
  20. {
  21. //Swaps two given values.
  22. template< class T > inline void Swap( T& A, T& B )
  23. {
  24. const T Temp = A;
  25. A = B;
  26. B = Temp;
  27. }
  28. //TODO: random struct
  29. //Gets a random Element from the given array<T>, considering you have implemented what follows :
  30. //NEEDS : float T::m_weight; //if m_weight is 0, it automatically assumes that this step is ignored.
  31. template< class T > inline int GetRandom(array<T> src)
  32. {
  33. float r_total = 0.f;
  34. float r_alpha = rand(1.f);
  35. float r_value = 0.f;
  36. int r_j = 0;
  37. for (int i = 0; i < src.count(); ++i)
  38. {
  39. T& tmp = src[i];
  40. if (tmp.m_weight > .0f)
  41. {
  42. r_total += tmp.m_weight;
  43. float r_tmp = r_alpha * r_total;
  44. while (r_tmp > r_value + src[r_j].m_weight && r_j < i)
  45. {
  46. r_value += src[r_j].m_weight;
  47. r_j++;
  48. }
  49. }
  50. }
  51. return (r_total > .0f)?(r_j):(-1);
  52. }
  53. // Gets the value for the given enum type.
  54. template<class T> inline T FindValue(const char* name)
  55. {
  56. String n = name;
  57. n.to_lower();
  58. for (int i = 0; i < T::Max; ++i)
  59. {
  60. String s = T(i).ToString().to_lower();
  61. if (s.contains(n))
  62. return T(i);
  63. }
  64. return T::Max;
  65. }
  66. template<class T> inline T FindValue(String const& name)
  67. {
  68. return FindValue<T>(name.C());
  69. }
  70. } /* namespace lol */