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.
 
 
 

76 lines
1.9 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2018 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. #include <string>
  20. namespace lol
  21. {
  22. //Swaps two given values.
  23. template< class T > inline void Swap( T& A, T& B )
  24. {
  25. const T Temp = A;
  26. A = B;
  27. B = Temp;
  28. }
  29. //TODO: random struct
  30. //Gets a random Element from the given array<T>, considering you have implemented what follows :
  31. //NEEDS : float T::m_weight; //if m_weight is 0, it automatically assumes that this step is ignored.
  32. template< class T > inline int GetRandom(array<T> src)
  33. {
  34. float r_total = 0.f;
  35. float r_alpha = rand(1.f);
  36. float r_value = 0.f;
  37. int r_j = 0;
  38. for (int i = 0; i < src.count(); ++i)
  39. {
  40. T& tmp = src[i];
  41. if (tmp.m_weight > .0f)
  42. {
  43. r_total += tmp.m_weight;
  44. float r_tmp = r_alpha * r_total;
  45. while (r_tmp > r_value + src[r_j].m_weight && r_j < i)
  46. {
  47. r_value += src[r_j].m_weight;
  48. r_j++;
  49. }
  50. }
  51. }
  52. return (r_total > .0f)?(r_j):(-1);
  53. }
  54. // Gets the value for the given enum type.
  55. template<class T> inline T FindValue(std::string const& name)
  56. {
  57. std::string needle = tolower(name);
  58. for (int i = 0; i < T::Max; ++i)
  59. if (tolower(T(i).tostring()).find(needle) != std::string::npos)
  60. return T(i);
  61. return T::Max;
  62. }
  63. } /* namespace lol */