Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

77 Zeilen
1.9 KiB

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