Guest User

Untitled

a guest
May 20th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. pub trait Cond {
  2. fn is_satisfied(&self, memory: &Memory) -> bool;
  3.  
  4. fn set_id(&mut self, id: &usize);
  5. fn get_id(&self) -> usize;
  6. fn get_name(&self) -> String;
  7. }
  8.  
  9. impl PartialEq for Cond {
  10. fn eq(&self, other: &Cond) -> bool {
  11. self.get_id() == other.get_id()
  12. }
  13. }
  14.  
  15. //ignore
  16. pub struct Memory {
  17. has_target: bool,
  18. target: String,
  19.  
  20. has_weapon: bool,
  21. free_weapon: usize,
  22.  
  23. player_is_dead: bool,
  24. }
  25.  
  26. struct HasTarget {
  27. id: usize,
  28. }
  29.  
  30. impl HasTarget {
  31. fn new() -> HasTarget {
  32. HasTarget {
  33. id: 0,
  34. }
  35. }
  36. }
  37.  
  38. impl Cond for HasTarget {
  39. fn get_name(&self) -> String {
  40. "HasTarget".to_string()
  41. }
  42. fn is_satisfied(&self, memory: &Memory) -> bool {
  43. memory.has_target
  44. }
  45.  
  46. fn set_id(&mut self, id: &usize) {
  47. self.id = id.clone()
  48. }
  49.  
  50. fn get_id(&self) -> usize {
  51. self.id.clone()
  52. }
  53. }
  54.  
  55. struct HasWeapon {
  56. id: usize,
  57. }
  58.  
  59. impl HasWeapon {
  60. fn new() -> HasTarget {
  61. HasTarget {
  62. id: 0,
  63. }
  64. }
  65. }
  66.  
  67. impl Cond for HasWeapon {
  68. fn get_name(&self) -> String {
  69. "HasWeapon".to_string()
  70. }
  71. fn is_satisfied(&self, memory: &Memory) -> bool {
  72. memory.has_weapon
  73. }
  74.  
  75. fn set_id(&mut self, id: &usize) {
  76. self.id = id.clone()
  77. }
  78.  
  79. fn get_id(&self) -> usize {
  80. self.id.clone()
  81. }
  82. }
  83.  
  84. struct PlayerDead {
  85. id: usize,
  86. }
  87.  
  88. impl PlayerDead {
  89. fn new() -> HasTarget {
  90. HasTarget {
  91. id: 0,
  92. }
  93. }
  94. }
  95.  
  96. impl Cond for PlayerDead {
  97. fn get_name(&self) -> String {
  98. "PlayerDead".to_string()
  99. }
  100. fn is_satisfied(&self, memory: &Memory) -> bool {
  101. memory.player_is_dead
  102. }
  103.  
  104. fn set_id(&mut self, id: &usize) {
  105. self.id = id.clone()
  106. }
  107.  
  108. fn get_id(&self) -> usize {
  109. self.id.clone()
  110. }
  111. }
  112.  
  113. fn main() {
  114. let mut conds: Vec<Box<Cond>> = vec![
  115. Box::new(PlayerDead::new()),
  116. Box::new(HasTarget::new()),
  117. Box::new(HasWeapon::new()),
  118. ];
  119.  
  120. for cond in conds.iter() {
  121. //why are the names the same???
  122. println!("{}", cond.get_name());
  123. }
  124. }
Add Comment
Please, Sign In to add comment