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.

преди 11 години
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2012-2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
  5. // (c) 2012-2013 Sam Hocevar <sam@hocevar.net>
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the Do What The Fuck You Want To
  8. // Public License, Version 2, as published by Sam Hocevar. See
  9. // http://www.wtfpl.net/ for more details.
  10. //
  11. //
  12. // Various basic convenience functions
  13. // ------------------
  14. //
  15. #if !defined __LOL_UTILS_H__
  16. #define __LOL_UTILS_H__
  17. #include <lol/base/string.h>
  18. namespace lol
  19. {
  20. //Swaps two given values.
  21. template< class T > inline void Swap( T& A, T& B )
  22. {
  23. const T Temp = A;
  24. A = B;
  25. B = Temp;
  26. }
  27. //TODO: random struct
  28. //Gets a random Element from the given array<T>, considering you have implemented what follows :
  29. //NEEDS : float T::m_weight; //if m_weight is 0, it automatically assumes that this step is ignored.
  30. template< class T > inline int GetRandom(array<T> src)
  31. {
  32. float r_total = 0.f;
  33. float r_alpha = rand(1.f);
  34. float r_value = 0.f;
  35. int r_j = 0;
  36. int i = 0;
  37. for (; 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 LOL_SAFE_ENUM type.
  54. template<class T> inline T FindValue(const char* name)
  55. {
  56. String n = name;
  57. n.ToLower();
  58. for (int i = 0; i < T::Max; ++i)
  59. {
  60. String s = T(i).ToString().ToLower();
  61. if (s == n)
  62. return T(i);
  63. }
  64. return T::Max;
  65. }
  66. } /* namespace lol */
  67. #endif /* __LOL_UTILS_H__ */