Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

77 lignes
1.8 KiB

  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. #pragma once
  12. //
  13. // Various basic convenience functions
  14. // ------------------
  15. //
  16. #include <lol/base/string.h>
  17. namespace lol
  18. {
  19. //Swaps two given values.
  20. template< class T > inline void Swap( T& A, T& B )
  21. {
  22. const T Temp = A;
  23. A = B;
  24. B = Temp;
  25. }
  26. //TODO: random struct
  27. //Gets a random Element from the given array<T>, considering you have implemented what follows :
  28. //NEEDS : float T::m_weight; //if m_weight is 0, it automatically assumes that this step is ignored.
  29. template< class T > inline int GetRandom(array<T> src)
  30. {
  31. float r_total = 0.f;
  32. float r_alpha = rand(1.f);
  33. float r_value = 0.f;
  34. int r_j = 0;
  35. int i = 0;
  36. for (; i < src.Count(); ++i)
  37. {
  38. T& tmp = src[i];
  39. if (tmp.m_weight > .0f)
  40. {
  41. r_total += tmp.m_weight;
  42. float r_tmp = r_alpha * r_total;
  43. while (r_tmp > r_value + src[r_j].m_weight && r_j < i)
  44. {
  45. r_value += src[r_j].m_weight;
  46. r_j++;
  47. }
  48. }
  49. }
  50. return (r_total > .0f)?(r_j):(-1);
  51. }
  52. // Gets the value for the given LOL_SAFE_ENUM type.
  53. template<class T> inline T FindValue(const char* name)
  54. {
  55. String n = name;
  56. n.ToLower();
  57. for (int i = 0; i < T::Max; ++i)
  58. {
  59. String s = T(i).ToString().ToLower();
  60. if (s == n)
  61. return T(i);
  62. }
  63. return T::Max;
  64. }
  65. } /* namespace lol */