main.rs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. use std::io;
  2. use rand::Rng;
  3. use std::{thread, time};
  4. //////////
  5. // Objects
  6. //////////
  7. struct Market {
  8. // Store current values of drugs on the market
  9. weed_min: u32,
  10. weed_max: u32,
  11. cocaine_min: u32,
  12. cocaine_max: u32,
  13. heroin_min: u32,
  14. heroin_max: u32,
  15. acid_min: u32,
  16. acid_max: u32,
  17. xtc_min: u32,
  18. xtc_max: u32,
  19. ludes_min: u32,
  20. ludes_max: u32,
  21. weed: u32,
  22. cocaine: u32,
  23. heroin: u32,
  24. acid: u32,
  25. xtc: u32,
  26. ludes: u32,
  27. }
  28. // Market::new(100,600,18000,28000,3000,10000,700,2000,120,500,15,200,0,0,0,0,0,0)
  29. impl Market {
  30. pub fn new(weed_min: u32, weed_max: u32, cocaine_min: u32, cocaine_max: u32, heroin_min: u32, heroin_max: u32, acid_min: u32, acid_max: u32, xtc_min: u32, xtc_max: u32, ludes_min: u32, ludes_max: u32, weed: u32, cocaine: u32, heroin: u32, acid: u32, xtc: u32, ludes: u32) -> Self {
  31. Market { weed_min, weed_max, cocaine_min, cocaine_max, heroin_min, heroin_max, acid_min, acid_max, xtc_min, xtc_max, ludes_min, ludes_max, weed, cocaine, heroin, acid, xtc, ludes }
  32. }
  33. // This is normal market fluctuation when moving locations, as opposed to an event that drives prices up/down
  34. pub fn change_prices(&mut self) {
  35. let mut rng = rand::thread_rng();
  36. let weed_diff = rng.gen_range(self.weed_min,self.weed_max);
  37. let cocaine_diff = rng.gen_range(self.cocaine_min,self.cocaine_max);
  38. let heroin_diff = rng.gen_range(self.heroin_min,self.heroin_max);
  39. let acid_diff = rng.gen_range(self.acid_min,self.acid_max);
  40. let xtc_diff = rng.gen_range(self.xtc_min,self.xtc_max);
  41. let ludes_diff = rng.gen_range(self.ludes_min,self.ludes_max);
  42. self.weed = weed_diff;
  43. self.cocaine = cocaine_diff;
  44. self.heroin = heroin_diff;
  45. self.acid = acid_diff;
  46. self.xtc = xtc_diff;
  47. self.ludes = ludes_diff;
  48. }
  49. pub fn dump(&self) {
  50. println!("Prices\nWeed: ${}\tCocaine: ${}\nHeroin: ${}\tAcid: ${}\nXTC: ${}\tLudes: ${}\n", self.weed, self.cocaine, self.heroin, self.acid, self.xtc, self.ludes);
  51. }
  52. pub fn set_price(&mut self, drug_to_set: &String, price_to_set: u32) {
  53. match drug_to_set.as_ref() {
  54. "weed" => self.weed = price_to_set,
  55. "cocaine" => self.cocaine = price_to_set,
  56. "heroin" => self.heroin = price_to_set,
  57. "acid" => self.acid = price_to_set,
  58. "xtc" => self.xtc = price_to_set,
  59. "ludes" => self.ludes = price_to_set,
  60. _ => panic!("Market::set_price got a drug_to_set value that doesn't make sense"),
  61. }
  62. }
  63. pub fn get_current_price(&self, drug_to_get: &String) -> u32 {
  64. let current_price: u32;
  65. match drug_to_get.as_ref() {
  66. "weed" => current_price = self.weed,
  67. "cocaine" => current_price = self.cocaine,
  68. "heroin" => current_price = self.heroin,
  69. "acid" => current_price = self.acid,
  70. "xtc" => current_price = self.xtc,
  71. "ludes" => current_price = self.ludes,
  72. _ => panic!("Market::get_current_price got a drug_to_get value that doesn't make sense"),
  73. }
  74. return current_price;
  75. }
  76. pub fn get_min_price(&self, drug_to_get: &String) -> u32 {
  77. let min_price: u32;
  78. match drug_to_get.as_ref() {
  79. "weed" => min_price = self.weed_min,
  80. "cocaine" => min_price = self.cocaine_min,
  81. "heroin" => min_price = self.heroin_min,
  82. "acid" => min_price = self.acid_min,
  83. "xtc" => min_price = self.xtc_min,
  84. "ludes" => min_price = self.ludes_min,
  85. _ => panic!("Market::get_min_price got a drug_to_get value that doesn't make sense"),
  86. }
  87. return min_price;
  88. }
  89. pub fn get_max_price(&self, drug_to_get: &String) -> u32 {
  90. let max_price: u32;
  91. match drug_to_get.as_ref() {
  92. "weed" => max_price = self.weed_max,
  93. "cocaine" => max_price = self.cocaine_max,
  94. "heroin" => max_price = self.heroin_max,
  95. "acid" => max_price = self.acid_max,
  96. "xtc" => max_price = self.xtc_max,
  97. "ludes" => max_price = self.ludes_max,
  98. _ => panic!("Market::get_min_price got a drug_to_get value that doesn't make sense"),
  99. }
  100. return max_price;
  101. }
  102. }
  103. struct HeadStash {
  104. weed: u32,
  105. cocaine: u32,
  106. heroin: u32,
  107. acid: u32,
  108. xtc: u32,
  109. ludes: u32,
  110. money: u32,
  111. }
  112. impl HeadStash {
  113. pub fn new(weed: u32, cocaine: u32, heroin: u32, acid: u32, xtc: u32, ludes: u32, money: u32) -> Self {
  114. HeadStash { weed, cocaine, heroin, acid, xtc, ludes, money }
  115. }
  116. pub fn dump(&self) {
  117. println!("You've got ${} in your head stash",self.money);
  118. println!("You're head stash has:\nWeed: {} Cocaine: {}\nHeroin: {} Acid: {}\nXTC: {} Ludes: {}\n", self.weed, self.cocaine, self.heroin, self.acid, self.xtc, self.ludes);
  119. }
  120. pub fn move_drug_to_stash(&mut self, drug_to_move: &String, amount_to_move: &u32) {
  121. match drug_to_move.as_ref() {
  122. "weed" => self.weed += amount_to_move,
  123. "cocaine" => self.cocaine += amount_to_move,
  124. "heroin" => self.heroin += amount_to_move,
  125. "acid" => self.acid += amount_to_move,
  126. "xtc" => self.xtc += amount_to_move,
  127. "ludes" => self.ludes += amount_to_move,
  128. _ => panic!("move_drug_to_stash got a drug_to_move value that doesn't make sense"),
  129. }
  130. }
  131. pub fn grab_from_stash(&mut self, drug_to_grab: &String, amount_to_grab: &u32) {
  132. match drug_to_grab.as_ref() {
  133. "weed" => self.weed -= amount_to_grab,
  134. "cocaine" => self.cocaine -= amount_to_grab,
  135. "heroin" => self.heroin -= amount_to_grab,
  136. "acid" => self.acid -= amount_to_grab,
  137. "xtc" => self.xtc -= amount_to_grab,
  138. "ludes" => self.ludes -= amount_to_grab,
  139. _ => panic!("sell_transaction got a drug_to_sell value that doesn't make sense"),
  140. }
  141. }
  142. pub fn bank_stash(&mut self, amount_to_bank: &u32) {
  143. self.money += amount_to_bank;
  144. }
  145. pub fn withdraw_stash(&mut self, amount_to_withdraw: &u32) {
  146. self.money -= amount_to_withdraw;
  147. }
  148. pub fn get_head_stash_amount(&self, drug: &String) -> u32 {
  149. let amount;
  150. match drug.as_ref() {
  151. "weed" => amount = self.weed,
  152. "cocaine" => amount = self.cocaine,
  153. "heroin" => amount = self.heroin,
  154. "acid" => amount = self.acid,
  155. "xtc" => amount = self.xtc,
  156. "ludes" => amount = self.ludes,
  157. _ => amount = 0,
  158. }
  159. return amount;
  160. }
  161. }
  162. struct Player {
  163. health: u32,
  164. money: u32,
  165. debt: u32,
  166. weed: u32,
  167. cocaine: u32,
  168. heroin: u32,
  169. acid: u32,
  170. xtc: u32,
  171. ludes: u32,
  172. location: String,
  173. loan_timer: u32,
  174. stash_size: u32,
  175. }
  176. // TODO
  177. // A lot of the functions for Player duplicate code, need to make some "generic" functions
  178. impl Player {
  179. pub fn new(health: u32, money: u32, debt: u32, weed: u32, cocaine: u32, heroin: u32, acid: u32, xtc: u32, ludes: u32, location: String, loan_timer: u32, stash_size: u32 ) -> Self {
  180. Player { health, money, debt, weed, cocaine, heroin, acid, xtc, ludes, location, loan_timer, stash_size }
  181. }
  182. pub fn dump(&self) {
  183. println!("Health: {}\nMoney: ${}\tDebt: ${}", self.health, self.money, self.debt);
  184. println!("You're in {}\n", self.location);
  185. println!("You can hold {} units in your stash",self.stash_size);
  186. println!("You're holding:\nWeed: {} Cocaine: {}\nHeroin: {} Acid: {}\nXTC: {} Ludes: {}\n", self.weed, self.cocaine, self.heroin, self.acid, self.xtc, self.ludes);
  187. }
  188. pub fn change_location(&mut self, new_location: String) {
  189. self.location = new_location;
  190. }
  191. pub fn get_stash_amount(&self, drug: &String) -> u32 {
  192. let amount;
  193. match drug.as_ref() {
  194. "weed" => amount = self.weed,
  195. "cocaine" => amount = self.cocaine,
  196. "heroin" => amount = self.heroin,
  197. "acid" => amount = self.acid,
  198. "xtc" => amount = self.xtc,
  199. "ludes" => amount = self.ludes,
  200. _ => amount = 0,
  201. }
  202. return amount;
  203. }
  204. pub fn buy_transaction(&mut self, drug_to_buy: &String, amount_to_buy: &u32, market_price: &u32) {
  205. let cost = market_price * amount_to_buy;
  206. self.money -= cost;
  207. let add_amount = amount_to_buy;
  208. self.gain_drug(&drug_to_buy,&add_amount);
  209. }
  210. pub fn sell_transaction(&mut self, drug_to_sell: &String, amount_to_sell: &u32, market_price: &u32) {
  211. let revenue = market_price * amount_to_sell;
  212. self.lose_drug(&drug_to_sell,&amount_to_sell);
  213. self.money += revenue;
  214. }
  215. pub fn debt_interest(&mut self) {
  216. let interest_amount: u32;
  217. match self.debt {
  218. 0 => interest_amount = 0,
  219. _ => interest_amount = ( self.debt as f32 * 0.2 ) as u32,
  220. }
  221. self.debt += interest_amount;
  222. }
  223. pub fn remove_debt(&mut self, payback_amount: u32) {
  224. if (self.debt as i32 - payback_amount as i32) < 0 {
  225. self.money -= payback_amount;
  226. self.debt = 0;
  227. } else {
  228. self.money -= payback_amount;
  229. self.debt -= payback_amount;
  230. }
  231. }
  232. pub fn borrow_from_shark(&mut self, borrow_amount: u32) {
  233. self.money += borrow_amount;
  234. self.debt += borrow_amount;
  235. }
  236. pub fn take_damage(&mut self, damage_amount: u32) {
  237. if (self.health as i32 - damage_amount as i32) < 0 {
  238. self.health = 0;
  239. } else {
  240. self.health -= damage_amount;
  241. }
  242. }
  243. pub fn decrease_loan_timer(&mut self, timer_amount: u32) {
  244. if (self.loan_timer as i32 - timer_amount as i32) < 0 {
  245. self.loan_timer = 0;
  246. } else {
  247. self.loan_timer -= timer_amount;
  248. }
  249. }
  250. pub fn increase_loan_timer(&mut self, timer_amount: u32) {
  251. self.loan_timer += timer_amount;
  252. }
  253. pub fn buy_trenchcoat(&mut self, cost: u32) {
  254. self.stash_size += 25;
  255. self.money -= cost;
  256. }
  257. pub fn buy_body_armor(&mut self, cost: u32) {
  258. self.health += 2;
  259. self.money -= cost;
  260. }
  261. pub fn stash_fill(&self) -> u32 {
  262. let stash_fill = self.weed + self.cocaine + self.heroin + self.xtc + self.acid + self.ludes;
  263. return stash_fill;
  264. }
  265. pub fn holding(&self) -> bool {
  266. if self.stash_fill() == 0 {
  267. return false;
  268. } else {
  269. return true;
  270. }
  271. }
  272. pub fn arrested(&mut self) {
  273. self.money = 0;
  274. self.weed = 0;
  275. self.cocaine = 0;
  276. self.heroin = 0;
  277. self.xtc = 0;
  278. self.acid = 0;
  279. self.ludes = 0;
  280. }
  281. pub fn lose_money(&mut self, amount: &u32) {
  282. self.money -= amount;
  283. }
  284. pub fn gain_money(&mut self, amount: &u32) {
  285. self.money += amount;
  286. }
  287. pub fn lose_drug(&mut self, drug: &String, amount: &u32) {
  288. match drug.as_ref() {
  289. "weed" => self.weed -= amount,
  290. "cocaine" => self.cocaine -= amount,
  291. "heroin" => self.heroin -= amount,
  292. "acid" => self.acid -= amount,
  293. "xtc" => self.xtc -= amount,
  294. "ludes" => self.ludes -= amount,
  295. _ => panic!("lose_drug got a drug value that doesn't make sense"),
  296. }
  297. }
  298. pub fn gain_drug(&mut self, drug: &String, amount: &u32) {
  299. match drug.as_ref() {
  300. "weed" => self.weed += amount,
  301. "cocaine" => self.cocaine += amount,
  302. "heroin" => self.heroin += amount,
  303. "acid" => self.acid += amount,
  304. "xtc" => self.xtc += amount,
  305. "ludes" => self.ludes += amount,
  306. _ => panic!("lose_drug got a drug value that doesn't make sense"),
  307. }
  308. }
  309. }
  310. ////////////////////////////////////////////////////////
  311. // "Low-level" Fuctions, called by "Top-level" functions
  312. ////////////////////////////////////////////////////////
  313. fn prompt(prompt_text: String) -> io::Result<String> {
  314. println!("$ {}",prompt_text);
  315. let mut input = String::new();
  316. std::io::stdin().read_line(&mut input)?;
  317. let value: String = input.trim().parse().unwrap();
  318. Ok(value)
  319. }
  320. // This hurts portability but works for now
  321. fn clear_term() {
  322. print!("{}[2J", 27 as char);
  323. }
  324. fn get_drug_market_value(drug_to_buy: &String, market: &Market) -> io::Result<u32> {
  325. let market_value: u32;
  326. match drug_to_buy.as_ref() {
  327. "weed" => market_value = market.weed,
  328. "cocaine" => market_value = market.cocaine,
  329. "heroin" => market_value = market.heroin,
  330. "acid" => market_value = market.acid,
  331. "xtc" => market_value = market.xtc,
  332. "ludes" => market_value = market.ludes,
  333. _ => panic!("get_drug_market_value got a drug_to_buy value that doesn't make sense!"),
  334. }
  335. Ok(market_value)
  336. }
  337. // This is maybe stupid? Or could be more idomatic ?? TODO?
  338. // This honestly might not even be needed? Don't really
  339. // understand why I made this
  340. // May should use match inside of let ?
  341. fn get_drug_as_string(drug: &String) -> io::Result<String> {
  342. let mut drug_as_string: String;
  343. match drug.as_ref() {
  344. "weed" => drug_as_string = "weed".to_string(),
  345. "cocaine" => drug_as_string = "cocaine".to_string(),
  346. "heroin" => drug_as_string = "heroin".to_string(),
  347. "acid" => drug_as_string = "acid".to_string(),
  348. "xtc" => drug_as_string = "xtc".to_string(),
  349. "ludes" => drug_as_string = "ludes".to_string(),
  350. _ => drug_as_string = "null".to_string(),
  351. }
  352. Ok(drug_as_string)
  353. }
  354. ////////////////////////////////////
  355. // Events -- happen randomly in main
  356. ////////////////////////////////////
  357. fn trenchcoat_event(player: &mut Player) {
  358. let mut relative_cost = ( player.money as f64 * 0.25 ) as u32;
  359. if relative_cost < 200 {
  360. relative_cost = 200;
  361. }
  362. println!("A guy is selling his trench count, it'll allow you to carry 25 more units, want to buy it for ${} ?", relative_cost);
  363. println!("You have ${}", player.money);
  364. let response = prompt("Yes/no?".to_string()).unwrap();
  365. if response.contains("yes") {
  366. if player.money < relative_cost {
  367. println!("You cant afford this!");
  368. } else {
  369. player.buy_trenchcoat(relative_cost);
  370. }
  371. }
  372. }
  373. fn body_armor_event(player: &mut Player) {
  374. let mut relative_cost = ( player.money as f64 * 0.3 ) as u32;
  375. if relative_cost < 300 {
  376. relative_cost = 300;
  377. }
  378. println!("A guy is selling some body armor, it'll increase your health by 2, want to buy it for ${} ?", relative_cost);
  379. println!("You have ${}", player.money);
  380. let response = prompt("Yes/no?".to_string()).unwrap();
  381. if response.contains("yes") {
  382. if player.money < relative_cost {
  383. println!("You cant afford this!");
  384. } else {
  385. player.buy_body_armor(relative_cost);
  386. }
  387. }
  388. }
  389. fn cops_event(player: &mut Player) {
  390. let one_second = time::Duration::from_secs(1);
  391. if ! player.holding() {
  392. println!("The cops found you, but you're not holding so it's chill.");
  393. let _placeholder = prompt("".to_string());
  394. } else {
  395. println!("The cops found you! Run!");
  396. thread::sleep(one_second);
  397. let mut rng = rand::thread_rng();
  398. let mut escape: u32;
  399. let mut hit: u32;
  400. let mut done = false;
  401. while ! done {
  402. escape = rng.gen_range(0,5);
  403. hit = rng.gen_range(0,3);
  404. if escape == 0 {
  405. println!("You got away!");
  406. let _placeholder = prompt("".to_string());
  407. done = true;
  408. } else {
  409. println!("You cant get away, the cops are firing!");
  410. thread::sleep(one_second);
  411. if hit == 0 {
  412. println!("You're hit for 4 damage!");
  413. player.take_damage(4);
  414. thread::sleep(one_second);
  415. let arrest = rng.gen_range(0,1);
  416. if arrest == 0 {
  417. println!("The cops got you! They confiscate your cash and stash!");
  418. player.arrested();
  419. let _placeholder = prompt("".to_string());
  420. done = true;
  421. }
  422. } else {
  423. println!("They miss and you keep running!");
  424. thread::sleep(one_second);
  425. }
  426. }
  427. }
  428. }
  429. }
  430. fn price_event(market: &mut Market) {
  431. let mut rng = rand::thread_rng();
  432. let raise_drop = rng.gen_range(0,2);
  433. let choose_drug = rng.gen_range(0,5);
  434. let drug_to_edit: String;
  435. match choose_drug {
  436. 0 => drug_to_edit = "weed".to_string(),
  437. 1 => drug_to_edit = "cocaine".to_string(),
  438. 2 => drug_to_edit = "heroin".to_string(),
  439. 3 => drug_to_edit = "acid".to_string(),
  440. 4 => drug_to_edit = "xtc".to_string(),
  441. 5 => drug_to_edit = "ludes".to_string(),
  442. _ => panic!("drug_to_edit match in price_drop_event got a match that doesn't make sense"),
  443. }
  444. if raise_drop == 0 {
  445. // Price drop
  446. println!("A new shipment has flooded the market and caused the price of {} to dive!",drug_to_edit);
  447. let temp_price = (market.get_current_price(&drug_to_edit) as f64 * 0.2) as u32;
  448. market.set_price(&drug_to_edit,temp_price);
  449. let _placeholder = prompt("".to_string());
  450. } else {
  451. // Price raise
  452. println!("A bust has caused the price of {} to soar!",drug_to_edit);
  453. let temp_price = market.get_current_price(&drug_to_edit) + market.get_max_price(&drug_to_edit);
  454. market.set_price(&drug_to_edit,temp_price);
  455. let _placeholder = prompt("".to_string());
  456. }
  457. }
  458. ////////////////////////////////////////
  459. // "Top-level" Functions, called by main
  460. ////////////////////////////////////////
  461. fn go_to_location(player: &mut Player, market: &mut Market) {
  462. let mut loc_loop = false;
  463. while ! loc_loop {
  464. let mut new_location = prompt("Select a new location, locations are\nBrooklyn Midtown Harlem CentralPark".to_string()).unwrap();
  465. match new_location.as_ref() {
  466. "Brooklyn" => println!("Going to Brooklyn"),
  467. "Midtown" => println!("Going to Midtown"),
  468. "Harlem" => println!("Going to Harlem"),
  469. "CentralPark" => println!("Going to CentralPark"),
  470. _ => new_location = "null".to_string(),
  471. }
  472. if new_location != "null".to_string() {
  473. if new_location == player.location {
  474. println!("Youre already in {}", player.location);
  475. } else {
  476. player.change_location(new_location);
  477. market.change_prices();
  478. loc_loop = true;
  479. }
  480. } else {
  481. println!("Not a valid location, try again");
  482. }
  483. }
  484. }
  485. fn buy_drugs(player: &mut Player, market: &mut Market) {
  486. let mut done = false;
  487. let mut drug_to_buy: String = String::new();
  488. while ! done {
  489. drug_to_buy = prompt("What drug will you buy?".to_string()).unwrap();
  490. drug_to_buy = get_drug_as_string(&drug_to_buy).unwrap();
  491. if drug_to_buy != "null".to_string() {
  492. done = true;
  493. } else {
  494. println!("Try again");
  495. }
  496. }
  497. let mut done1 = false;
  498. while ! done1 {
  499. let drug_market_value: u32 = get_drug_market_value(&drug_to_buy, &market).unwrap();
  500. let affordable_amount: u32 = (player.money / drug_market_value) as u32;
  501. println!("You can afford {} {}", affordable_amount, drug_to_buy);
  502. let amount_to_buy = prompt("How many will you buy?".to_string()).unwrap();
  503. let amount_to_buy_int: u32 = amount_to_buy.parse().unwrap();
  504. let current_stash_fill = player.stash_fill();
  505. if amount_to_buy_int > affordable_amount {
  506. println!("You cant afford that many");
  507. } else if amount_to_buy_int > player.stash_size {
  508. println!("You cant hold that much!");
  509. } else if (amount_to_buy_int + current_stash_fill) > player.stash_size {
  510. println!("You cant hold that much!");
  511. } else {
  512. println!("Buying {} {}", amount_to_buy_int, drug_to_buy);
  513. player.buy_transaction(&drug_to_buy,&amount_to_buy_int,&drug_market_value);
  514. done1 = true;
  515. }
  516. }
  517. }
  518. fn sell_drugs(player: &mut Player, market: &mut Market) {
  519. let mut done = false;
  520. let mut drug_to_sell: String = String::new();
  521. while ! done {
  522. drug_to_sell = prompt("What drug will you sell?".to_string()).unwrap();
  523. drug_to_sell = get_drug_as_string(&drug_to_sell).unwrap();
  524. if drug_to_sell != "null".to_string() {
  525. done = true;
  526. } else {
  527. println!("Try again");
  528. }
  529. }
  530. let mut done1 = false;
  531. let mut amount_to_sell: String;
  532. while ! done1 {
  533. amount_to_sell = prompt("How many to sell?".to_string()).unwrap();
  534. let amount_to_sell_int: u32 = amount_to_sell.parse().unwrap();
  535. let drug_market_value: u32 = get_drug_market_value(&drug_to_sell, &market).unwrap();
  536. let amount_possible_to_sell: u32 = player.get_stash_amount(&drug_to_sell);
  537. if amount_to_sell_int > amount_possible_to_sell {
  538. println!("You don't have that many {}, try again", drug_to_sell);
  539. } else {
  540. println!("Selling {} {}", amount_to_sell_int, drug_to_sell);
  541. player.sell_transaction(&drug_to_sell,&amount_to_sell_int,&drug_market_value);
  542. done1 = true;
  543. }
  544. }
  545. }
  546. fn loan_shark(player: &mut Player) {
  547. if player.location != "Brooklyn".to_string() {
  548. println!("Can only talk to the loanshark in Brooklyn");
  549. } else {
  550. let mut loan_done = false;
  551. while ! loan_done {
  552. let action = prompt("What do you want to do? (borrow,repay)".to_string()).unwrap();
  553. if action.contains("repay") {
  554. let payback_amount: u32 = prompt("How much to pay back?".to_string()).unwrap().parse().unwrap();
  555. if payback_amount > player.money {
  556. println!("You dont have that much money");
  557. } else {
  558. player.remove_debt(payback_amount);
  559. println!("You now owe the loanshark: ${}", player.debt);
  560. loan_done = true;
  561. }
  562. } else if action.contains("borrow") {
  563. let borrow_amount = prompt("How much do you want to borrow? (Limit 20000)".to_string()).unwrap().parse().unwrap();
  564. if borrow_amount > 20000 {
  565. println!("You cant borrow that much");
  566. } else {
  567. player.borrow_from_shark(borrow_amount);
  568. loan_done = true;
  569. }
  570. }
  571. }
  572. }
  573. }
  574. fn head_stash_op(player: &mut Player, head_stash: &mut HeadStash) {
  575. if player.location != "Brooklyn".to_string() {
  576. println!("Your head stash is in Brooklyn");
  577. } else {
  578. let mut head_stash_done = false;
  579. while ! head_stash_done {
  580. let action = prompt("What do you want to do? (bank,withdraw)".to_string()).unwrap();
  581. if action.contains("bank") {
  582. let bank_type = prompt("What do you want to bank? (cash,drugs)".to_string()).unwrap();
  583. if bank_type.contains("cash") {
  584. let bank_amount: u32 = prompt("How much do you want to bank?".to_string()).unwrap().parse().unwrap();
  585. if bank_amount > player.money {
  586. println!("You don't have that much money on you!");
  587. } else {
  588. head_stash.bank_stash(&bank_amount);
  589. player.lose_money(&bank_amount);
  590. head_stash_done = true;
  591. }
  592. }
  593. if bank_type.contains("drugs") {
  594. let mut drug_type = prompt("What drug to you want to stash?".to_string()).unwrap();
  595. drug_type = get_drug_as_string(&drug_type).unwrap();
  596. if drug_type != "null".to_string() {
  597. let stash_amount: u32 = prompt("How much do you want to stash?".to_string()).unwrap().parse().unwrap();
  598. if stash_amount > player.get_stash_amount(&drug_type) {
  599. println!("You don't have that much in your stash!");
  600. } else {
  601. head_stash.move_drug_to_stash(&drug_type,&stash_amount);
  602. player.lose_drug(&drug_type,&stash_amount);
  603. head_stash_done = true;
  604. }
  605. } else {
  606. println!("Try again");
  607. }
  608. }
  609. }
  610. if action.contains("withdraw") {
  611. let withdraw_type = prompt("What do you want to withdraw? (cash,drugs)".to_string()).unwrap();
  612. if withdraw_type.contains("cash") {
  613. let withdraw_amount: u32 = prompt("How much do you want to withdraw?".to_string()).unwrap().parse().unwrap();
  614. if withdraw_amount > head_stash.money {
  615. println!("You don't have that much money in the stash!");
  616. } else {
  617. head_stash.withdraw_stash(&withdraw_amount);
  618. player.gain_money(&withdraw_amount);
  619. head_stash_done = true;
  620. }
  621. }
  622. if withdraw_type.contains("drugs") {
  623. let mut drug_type = prompt("What drug to you want to withdraw?".to_string()).unwrap();
  624. drug_type = get_drug_as_string(&drug_type).unwrap();
  625. if drug_type != "null".to_string() {
  626. let withdraw_amount: u32 = prompt("How much do you want to stash?".to_string()).unwrap().parse().unwrap();
  627. if withdraw_amount > head_stash.get_head_stash_amount(&drug_type) {
  628. println!("You dont have that much {} in your stash!",&drug_type);
  629. } else {
  630. head_stash.grab_from_stash(&drug_type,&withdraw_amount);
  631. player.gain_drug(&drug_type,&withdraw_amount);
  632. head_stash_done = true;
  633. }
  634. } else {
  635. println!("Try again");
  636. }
  637. }
  638. }
  639. }
  640. }
  641. }
  642. ///////
  643. // Main
  644. ///////
  645. fn main() {
  646. let mut player = Player::new(10,2000,2050,0,0,0,0,0,0,"Brooklyn".to_string(),10,50);
  647. // let mut market = Market::new(0,0,0,0,0,0);
  648. let mut market = Market::new(100,600,18000,28000,3000,10000,700,2000,120,500,15,200,0,0,0,0,0,0);
  649. let mut head_stash = HeadStash::new(0,0,0,0,0,0,0);
  650. Market::change_prices(&mut market);
  651. let mut done = false;
  652. while ! done {
  653. let input = prompt("Type start, quit, or help".to_string()).unwrap();
  654. if input == "quit".to_string() {
  655. done = true;
  656. } else if input == "start".to_string() {
  657. println!("Starting the game");
  658. let mut main_loop = false;
  659. while ! main_loop {
  660. clear_term();
  661. if player.health == 0 {
  662. clear_term();
  663. println!("You died! Game over!");
  664. let _placeholder = prompt("".to_string());
  665. main_loop = true;
  666. }
  667. if player.debt != 0 {
  668. if player.loan_timer == 0 {
  669. println!("The loan shark wants his money! He beats you to a pulp to remind you!");
  670. player.take_damage(2);
  671. player.increase_loan_timer(5);
  672. println!("You take 2 damage!");
  673. let _placeholder = prompt("".to_string());
  674. }
  675. }
  676. player.dump();
  677. head_stash.dump();
  678. market.dump();
  679. if player.location == "Brooklyn".to_string() {
  680. println!("You're currently in {}, what will you do? (buy,sell,jet,loanshark,headstash)",player.location);
  681. } else {
  682. println!("You're currently in {}, what will you do? (buy,sell,jet)",player.location);
  683. }
  684. let action = prompt("".to_string()).unwrap();
  685. if action.contains("buy") {
  686. buy_drugs(&mut player, &mut market);
  687. } else if action.contains("sell") {
  688. sell_drugs(&mut player, &mut market);
  689. } else if action.contains("jet") {
  690. go_to_location(&mut player, &mut market);
  691. player.debt_interest();
  692. player.decrease_loan_timer(1);
  693. // Only have events trigger after 'jetting'
  694. let mut rng = rand::thread_rng();
  695. let event_chance = rng.gen_range(0,9);
  696. if event_chance == 3 {
  697. price_event(&mut market);
  698. }
  699. if event_chance == 2 {
  700. trenchcoat_event(&mut player);
  701. }
  702. if event_chance == 1 {
  703. body_armor_event(&mut player);
  704. }
  705. if event_chance == 0 {
  706. cops_event(&mut player);
  707. }
  708. if player.health == 0 {
  709. clear_term();
  710. println!("You died! Game over!");
  711. let _placeholder = prompt("".to_string());
  712. main_loop = true;
  713. }
  714. } else if action.contains("loanshark") {
  715. loan_shark(&mut player);
  716. } else if action.contains("headstash") {
  717. head_stash_op(&mut player, &mut head_stash);
  718. } else if action == "quit".to_string() {
  719. std::process::exit(0);
  720. }
  721. }
  722. }
  723. }
  724. }