use std::io; use rand::Rng; use std::{thread, time}; ////////// // Objects ////////// struct Market { // Store current values of drugs on the market 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, } // Market::new(100,600,18000,28000,3000,10000,700,2000,120,500,15,200,0,0,0,0,0,0) impl Market { 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 { 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 } } // This is normal market fluctuation when moving locations, as opposed to an event that drives prices up/down pub fn change_prices(&mut self) { let mut rng = rand::thread_rng(); let weed_diff = rng.gen_range(self.weed_min,self.weed_max); let cocaine_diff = rng.gen_range(self.cocaine_min,self.cocaine_max); let heroin_diff = rng.gen_range(self.heroin_min,self.heroin_max); let acid_diff = rng.gen_range(self.acid_min,self.acid_max); let xtc_diff = rng.gen_range(self.xtc_min,self.xtc_max); let ludes_diff = rng.gen_range(self.ludes_min,self.ludes_max); self.weed = weed_diff; self.cocaine = cocaine_diff; self.heroin = heroin_diff; self.acid = acid_diff; self.xtc = xtc_diff; self.ludes = ludes_diff; } pub fn dump(&self) { println!("Prices\nWeed: ${}\tCocaine: ${}\nHeroin: ${}\tAcid: ${}\nXTC: ${}\tLudes: ${}\n", self.weed, self.cocaine, self.heroin, self.acid, self.xtc, self.ludes); } pub fn set_price(&mut self, drug_to_set: &String, price_to_set: u32) { match drug_to_set.as_ref() { "weed" => self.weed = price_to_set, "cocaine" => self.cocaine = price_to_set, "heroin" => self.heroin = price_to_set, "acid" => self.acid = price_to_set, "xtc" => self.xtc = price_to_set, "ludes" => self.ludes = price_to_set, _ => panic!("Market::set_price got a drug_to_set value that doesn't make sense"), } } pub fn get_current_price(&self, drug_to_get: &String) -> u32 { let current_price: u32; match drug_to_get.as_ref() { "weed" => current_price = self.weed, "cocaine" => current_price = self.cocaine, "heroin" => current_price = self.heroin, "acid" => current_price = self.acid, "xtc" => current_price = self.xtc, "ludes" => current_price = self.ludes, _ => panic!("Market::get_current_price got a drug_to_get value that doesn't make sense"), } return current_price; } pub fn get_min_price(&self, drug_to_get: &String) -> u32 { let min_price: u32; match drug_to_get.as_ref() { "weed" => min_price = self.weed_min, "cocaine" => min_price = self.cocaine_min, "heroin" => min_price = self.heroin_min, "acid" => min_price = self.acid_min, "xtc" => min_price = self.xtc_min, "ludes" => min_price = self.ludes_min, _ => panic!("Market::get_min_price got a drug_to_get value that doesn't make sense"), } return min_price; } pub fn get_max_price(&self, drug_to_get: &String) -> u32 { let max_price: u32; match drug_to_get.as_ref() { "weed" => max_price = self.weed_max, "cocaine" => max_price = self.cocaine_max, "heroin" => max_price = self.heroin_max, "acid" => max_price = self.acid_max, "xtc" => max_price = self.xtc_max, "ludes" => max_price = self.ludes_max, _ => panic!("Market::get_min_price got a drug_to_get value that doesn't make sense"), } return max_price; } } struct HeadStash { weed: u32, cocaine: u32, heroin: u32, acid: u32, xtc: u32, ludes: u32, money: u32, } impl HeadStash { pub fn new(weed: u32, cocaine: u32, heroin: u32, acid: u32, xtc: u32, ludes: u32, money: u32) -> Self { HeadStash { weed, cocaine, heroin, acid, xtc, ludes, money } } pub fn dump(&self) { println!("You've got ${} in your head stash",self.money); 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); } pub fn move_drug_to_stash(&mut self, drug_to_move: &String, amount_to_move: &u32) { match drug_to_move.as_ref() { "weed" => self.weed += amount_to_move, "cocaine" => self.cocaine += amount_to_move, "heroin" => self.heroin += amount_to_move, "acid" => self.acid += amount_to_move, "xtc" => self.xtc += amount_to_move, "ludes" => self.ludes += amount_to_move, _ => panic!("move_drug_to_stash got a drug_to_move value that doesn't make sense"), } } pub fn grab_from_stash(&mut self, drug_to_grab: &String, amount_to_grab: &u32) { match drug_to_grab.as_ref() { "weed" => self.weed -= amount_to_grab, "cocaine" => self.cocaine -= amount_to_grab, "heroin" => self.heroin -= amount_to_grab, "acid" => self.acid -= amount_to_grab, "xtc" => self.xtc -= amount_to_grab, "ludes" => self.ludes -= amount_to_grab, _ => panic!("sell_transaction got a drug_to_sell value that doesn't make sense"), } } pub fn bank_stash(&mut self, amount_to_bank: &u32) { self.money += amount_to_bank; } pub fn withdraw_stash(&mut self, amount_to_withdraw: &u32) { self.money -= amount_to_withdraw; } pub fn get_head_stash_amount(&self, drug: &String) -> u32 { let amount; match drug.as_ref() { "weed" => amount = self.weed, "cocaine" => amount = self.cocaine, "heroin" => amount = self.heroin, "acid" => amount = self.acid, "xtc" => amount = self.xtc, "ludes" => amount = self.ludes, _ => amount = 0, } return amount; } } struct Player { 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, } // TODO // A lot of the functions for Player duplicate code, need to make some "generic" functions impl Player { 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 { Player { health, money, debt, weed, cocaine, heroin, acid, xtc, ludes, location, loan_timer, stash_size } } pub fn dump(&self) { println!("Health: {}\nMoney: ${}\tDebt: ${}", self.health, self.money, self.debt); println!("You're in {}\n", self.location); println!("You can hold {} units in your stash",self.stash_size); println!("You're holding:\nWeed: {} Cocaine: {}\nHeroin: {} Acid: {}\nXTC: {} Ludes: {}\n", self.weed, self.cocaine, self.heroin, self.acid, self.xtc, self.ludes); } pub fn change_location(&mut self, new_location: String) { self.location = new_location; } pub fn get_stash_amount(&self, drug: &String) -> u32 { let amount; match drug.as_ref() { "weed" => amount = self.weed, "cocaine" => amount = self.cocaine, "heroin" => amount = self.heroin, "acid" => amount = self.acid, "xtc" => amount = self.xtc, "ludes" => amount = self.ludes, _ => amount = 0, } return amount; } pub fn buy_transaction(&mut self, drug_to_buy: &String, amount_to_buy: &u32, market_price: &u32) { let cost = market_price * amount_to_buy; self.money -= cost; let add_amount = amount_to_buy; self.gain_drug(&drug_to_buy,&add_amount); } pub fn sell_transaction(&mut self, drug_to_sell: &String, amount_to_sell: &u32, market_price: &u32) { let revenue = market_price * amount_to_sell; self.lose_drug(&drug_to_sell,&amount_to_sell); self.money += revenue; } pub fn debt_interest(&mut self) { let interest_amount: u32; match self.debt { 0 => interest_amount = 0, _ => interest_amount = ( self.debt as f32 * 0.2 ) as u32, } self.debt += interest_amount; } pub fn remove_debt(&mut self, payback_amount: u32) { if (self.debt as i32 - payback_amount as i32) < 0 { self.money -= payback_amount; self.debt = 0; } else { self.money -= payback_amount; self.debt -= payback_amount; } } pub fn borrow_from_shark(&mut self, borrow_amount: u32) { self.money += borrow_amount; self.debt += borrow_amount; } pub fn take_damage(&mut self, damage_amount: u32) { if (self.health as i32 - damage_amount as i32) < 0 { self.health = 0; } else { self.health -= damage_amount; } } pub fn decrease_loan_timer(&mut self, timer_amount: u32) { if (self.loan_timer as i32 - timer_amount as i32) < 0 { self.loan_timer = 0; } else { self.loan_timer -= timer_amount; } } pub fn increase_loan_timer(&mut self, timer_amount: u32) { self.loan_timer += timer_amount; } pub fn buy_trenchcoat(&mut self, cost: u32) { self.stash_size += 25; self.money -= cost; } pub fn buy_body_armor(&mut self, cost: u32) { self.health += 2; self.money -= cost; } pub fn stash_fill(&self) -> u32 { let stash_fill = self.weed + self.cocaine + self.heroin + self.xtc + self.acid + self.ludes; return stash_fill; } pub fn holding(&self) -> bool { if self.stash_fill() == 0 { return false; } else { return true; } } pub fn arrested(&mut self) { self.money = 0; self.weed = 0; self.cocaine = 0; self.heroin = 0; self.xtc = 0; self.acid = 0; self.ludes = 0; } pub fn lose_money(&mut self, amount: &u32) { self.money -= amount; } pub fn gain_money(&mut self, amount: &u32) { self.money += amount; } pub fn lose_drug(&mut self, drug: &String, amount: &u32) { match drug.as_ref() { "weed" => self.weed -= amount, "cocaine" => self.cocaine -= amount, "heroin" => self.heroin -= amount, "acid" => self.acid -= amount, "xtc" => self.xtc -= amount, "ludes" => self.ludes -= amount, _ => panic!("lose_drug got a drug value that doesn't make sense"), } } pub fn gain_drug(&mut self, drug: &String, amount: &u32) { match drug.as_ref() { "weed" => self.weed += amount, "cocaine" => self.cocaine += amount, "heroin" => self.heroin += amount, "acid" => self.acid += amount, "xtc" => self.xtc += amount, "ludes" => self.ludes += amount, _ => panic!("lose_drug got a drug value that doesn't make sense"), } } } //////////////////////////////////////////////////////// // "Low-level" Fuctions, called by "Top-level" functions //////////////////////////////////////////////////////// fn prompt(prompt_text: String) -> io::Result { println!("$ {}",prompt_text); let mut input = String::new(); std::io::stdin().read_line(&mut input)?; let value: String = input.trim().parse().unwrap(); Ok(value) } // This hurts portability but works for now fn clear_term() { print!("{}[2J", 27 as char); } fn get_drug_market_value(drug_to_buy: &String, market: &Market) -> io::Result { let market_value: u32; match drug_to_buy.as_ref() { "weed" => market_value = market.weed, "cocaine" => market_value = market.cocaine, "heroin" => market_value = market.heroin, "acid" => market_value = market.acid, "xtc" => market_value = market.xtc, "ludes" => market_value = market.ludes, _ => panic!("get_drug_market_value got a drug_to_buy value that doesn't make sense!"), } Ok(market_value) } // This is maybe stupid? Or could be more idomatic ?? TODO? // This honestly might not even be needed? Don't really // understand why I made this // May should use match inside of let ? fn get_drug_as_string(drug: &String) -> io::Result { let mut drug_as_string: String; match drug.as_ref() { "weed" => drug_as_string = "weed".to_string(), "cocaine" => drug_as_string = "cocaine".to_string(), "heroin" => drug_as_string = "heroin".to_string(), "acid" => drug_as_string = "acid".to_string(), "xtc" => drug_as_string = "xtc".to_string(), "ludes" => drug_as_string = "ludes".to_string(), _ => drug_as_string = "null".to_string(), } Ok(drug_as_string) } //////////////////////////////////// // Events -- happen randomly in main //////////////////////////////////// fn trenchcoat_event(player: &mut Player) { let mut relative_cost = ( player.money as f64 * 0.25 ) as u32; if relative_cost < 200 { relative_cost = 200; } println!("A guy is selling his trench count, it'll allow you to carry 25 more units, want to buy it for ${} ?", relative_cost); println!("You have ${}", player.money); let response = prompt("Yes/no?".to_string()).unwrap(); if response.contains("yes") { if player.money < relative_cost { println!("You cant afford this!"); } else { player.buy_trenchcoat(relative_cost); } } } fn body_armor_event(player: &mut Player) { let mut relative_cost = ( player.money as f64 * 0.3 ) as u32; if relative_cost < 300 { relative_cost = 300; } println!("A guy is selling some body armor, it'll increase your health by 2, want to buy it for ${} ?", relative_cost); println!("You have ${}", player.money); let response = prompt("Yes/no?".to_string()).unwrap(); if response.contains("yes") { if player.money < relative_cost { println!("You cant afford this!"); } else { player.buy_body_armor(relative_cost); } } } fn cops_event(player: &mut Player) { let one_second = time::Duration::from_secs(1); if ! player.holding() { println!("The cops found you, but you're not holding so it's chill."); let _placeholder = prompt("".to_string()); } else { println!("The cops found you! Run!"); thread::sleep(one_second); let mut rng = rand::thread_rng(); let mut escape: u32; let mut hit: u32; let mut done = false; while ! done { escape = rng.gen_range(0,5); hit = rng.gen_range(0,3); if escape == 0 { println!("You got away!"); let _placeholder = prompt("".to_string()); done = true; } else { println!("You cant get away, the cops are firing!"); thread::sleep(one_second); if hit == 0 { println!("You're hit for 4 damage!"); player.take_damage(4); thread::sleep(one_second); let arrest = rng.gen_range(0,1); if arrest == 0 { println!("The cops got you! They confiscate your cash and stash!"); player.arrested(); let _placeholder = prompt("".to_string()); done = true; } } else { println!("They miss and you keep running!"); thread::sleep(one_second); } } } } } fn price_event(market: &mut Market) { let mut rng = rand::thread_rng(); let raise_drop = rng.gen_range(0,2); let choose_drug = rng.gen_range(0,5); let drug_to_edit: String; match choose_drug { 0 => drug_to_edit = "weed".to_string(), 1 => drug_to_edit = "cocaine".to_string(), 2 => drug_to_edit = "heroin".to_string(), 3 => drug_to_edit = "acid".to_string(), 4 => drug_to_edit = "xtc".to_string(), 5 => drug_to_edit = "ludes".to_string(), _ => panic!("drug_to_edit match in price_drop_event got a match that doesn't make sense"), } if raise_drop == 0 { // Price drop println!("A new shipment has flooded the market and caused the price of {} to dive!",drug_to_edit); let temp_price = (market.get_current_price(&drug_to_edit) as f64 * 0.2) as u32; market.set_price(&drug_to_edit,temp_price); let _placeholder = prompt("".to_string()); } else { // Price raise println!("A bust has caused the price of {} to soar!",drug_to_edit); let temp_price = market.get_current_price(&drug_to_edit) + market.get_max_price(&drug_to_edit); market.set_price(&drug_to_edit,temp_price); let _placeholder = prompt("".to_string()); } } //////////////////////////////////////// // "Top-level" Functions, called by main //////////////////////////////////////// fn go_to_location(player: &mut Player, market: &mut Market) { let mut loc_loop = false; while ! loc_loop { let mut new_location = prompt("Select a new location, locations are\nBrooklyn Midtown Harlem CentralPark".to_string()).unwrap(); match new_location.as_ref() { "Brooklyn" => println!("Going to Brooklyn"), "Midtown" => println!("Going to Midtown"), "Harlem" => println!("Going to Harlem"), "CentralPark" => println!("Going to CentralPark"), _ => new_location = "null".to_string(), } if new_location != "null".to_string() { if new_location == player.location { println!("Youre already in {}", player.location); } else { player.change_location(new_location); market.change_prices(); loc_loop = true; } } else { println!("Not a valid location, try again"); } } } fn buy_drugs(player: &mut Player, market: &mut Market) { let mut done = false; let mut drug_to_buy: String = String::new(); while ! done { drug_to_buy = prompt("What drug will you buy?".to_string()).unwrap(); drug_to_buy = get_drug_as_string(&drug_to_buy).unwrap(); if drug_to_buy != "null".to_string() { done = true; } else { println!("Try again"); } } let mut done1 = false; while ! done1 { let drug_market_value: u32 = get_drug_market_value(&drug_to_buy, &market).unwrap(); let affordable_amount: u32 = (player.money / drug_market_value) as u32; println!("You can afford {} {}", affordable_amount, drug_to_buy); let amount_to_buy = prompt("How many will you buy?".to_string()).unwrap(); let amount_to_buy_int: u32 = amount_to_buy.parse().unwrap(); let current_stash_fill = player.stash_fill(); if amount_to_buy_int > affordable_amount { println!("You cant afford that many"); } else if amount_to_buy_int > player.stash_size { println!("You cant hold that much!"); } else if (amount_to_buy_int + current_stash_fill) > player.stash_size { println!("You cant hold that much!"); } else { println!("Buying {} {}", amount_to_buy_int, drug_to_buy); player.buy_transaction(&drug_to_buy,&amount_to_buy_int,&drug_market_value); done1 = true; } } } fn sell_drugs(player: &mut Player, market: &mut Market) { let mut done = false; let mut drug_to_sell: String = String::new(); while ! done { drug_to_sell = prompt("What drug will you sell?".to_string()).unwrap(); drug_to_sell = get_drug_as_string(&drug_to_sell).unwrap(); if drug_to_sell != "null".to_string() { done = true; } else { println!("Try again"); } } let mut done1 = false; let mut amount_to_sell: String; while ! done1 { amount_to_sell = prompt("How many to sell?".to_string()).unwrap(); let amount_to_sell_int: u32 = amount_to_sell.parse().unwrap(); let drug_market_value: u32 = get_drug_market_value(&drug_to_sell, &market).unwrap(); let amount_possible_to_sell: u32 = player.get_stash_amount(&drug_to_sell); if amount_to_sell_int > amount_possible_to_sell { println!("You don't have that many {}, try again", drug_to_sell); } else { println!("Selling {} {}", amount_to_sell_int, drug_to_sell); player.sell_transaction(&drug_to_sell,&amount_to_sell_int,&drug_market_value); done1 = true; } } } fn loan_shark(player: &mut Player) { if player.location != "Brooklyn".to_string() { println!("Can only talk to the loanshark in Brooklyn"); } else { let mut loan_done = false; while ! loan_done { let action = prompt("What do you want to do? (borrow,repay)".to_string()).unwrap(); if action.contains("repay") { let payback_amount: u32 = prompt("How much to pay back?".to_string()).unwrap().parse().unwrap(); if payback_amount > player.money { println!("You dont have that much money"); } else { player.remove_debt(payback_amount); println!("You now owe the loanshark: ${}", player.debt); loan_done = true; } } else if action.contains("borrow") { let borrow_amount = prompt("How much do you want to borrow? (Limit 20000)".to_string()).unwrap().parse().unwrap(); if borrow_amount > 20000 { println!("You cant borrow that much"); } else { player.borrow_from_shark(borrow_amount); loan_done = true; } } } } } fn head_stash_op(player: &mut Player, head_stash: &mut HeadStash) { if player.location != "Brooklyn".to_string() { println!("Your head stash is in Brooklyn"); } else { let mut head_stash_done = false; while ! head_stash_done { let action = prompt("What do you want to do? (bank,withdraw)".to_string()).unwrap(); if action.contains("bank") { let bank_type = prompt("What do you want to bank? (cash,drugs)".to_string()).unwrap(); if bank_type.contains("cash") { let bank_amount: u32 = prompt("How much do you want to bank?".to_string()).unwrap().parse().unwrap(); if bank_amount > player.money { println!("You don't have that much money on you!"); } else { head_stash.bank_stash(&bank_amount); player.lose_money(&bank_amount); head_stash_done = true; } } if bank_type.contains("drugs") { let mut drug_type = prompt("What drug to you want to stash?".to_string()).unwrap(); drug_type = get_drug_as_string(&drug_type).unwrap(); if drug_type != "null".to_string() { let stash_amount: u32 = prompt("How much do you want to stash?".to_string()).unwrap().parse().unwrap(); if stash_amount > player.get_stash_amount(&drug_type) { println!("You don't have that much in your stash!"); } else { head_stash.move_drug_to_stash(&drug_type,&stash_amount); player.lose_drug(&drug_type,&stash_amount); head_stash_done = true; } } else { println!("Try again"); } } } if action.contains("withdraw") { let withdraw_type = prompt("What do you want to withdraw? (cash,drugs)".to_string()).unwrap(); if withdraw_type.contains("cash") { let withdraw_amount: u32 = prompt("How much do you want to withdraw?".to_string()).unwrap().parse().unwrap(); if withdraw_amount > head_stash.money { println!("You don't have that much money in the stash!"); } else { head_stash.withdraw_stash(&withdraw_amount); player.gain_money(&withdraw_amount); head_stash_done = true; } } if withdraw_type.contains("drugs") { let mut drug_type = prompt("What drug to you want to withdraw?".to_string()).unwrap(); drug_type = get_drug_as_string(&drug_type).unwrap(); if drug_type != "null".to_string() { let withdraw_amount: u32 = prompt("How much do you want to stash?".to_string()).unwrap().parse().unwrap(); if withdraw_amount > head_stash.get_head_stash_amount(&drug_type) { println!("You dont have that much {} in your stash!",&drug_type); } else { head_stash.grab_from_stash(&drug_type,&withdraw_amount); player.gain_drug(&drug_type,&withdraw_amount); head_stash_done = true; } } else { println!("Try again"); } } } } } } /////// // Main /////// fn main() { let mut player = Player::new(10,2000,2050,0,0,0,0,0,0,"Brooklyn".to_string(),10,50); // let mut market = Market::new(0,0,0,0,0,0); let mut market = Market::new(100,600,18000,28000,3000,10000,700,2000,120,500,15,200,0,0,0,0,0,0); let mut head_stash = HeadStash::new(0,0,0,0,0,0,0); Market::change_prices(&mut market); let mut done = false; while ! done { let input = prompt("Type start, quit, or help".to_string()).unwrap(); if input == "quit".to_string() { done = true; } else if input == "start".to_string() { println!("Starting the game"); let mut main_loop = false; while ! main_loop { clear_term(); if player.health == 0 { clear_term(); println!("You died! Game over!"); let _placeholder = prompt("".to_string()); main_loop = true; } if player.debt != 0 { if player.loan_timer == 0 { println!("The loan shark wants his money! He beats you to a pulp to remind you!"); player.take_damage(2); player.increase_loan_timer(5); println!("You take 2 damage!"); let _placeholder = prompt("".to_string()); } } player.dump(); head_stash.dump(); market.dump(); if player.location == "Brooklyn".to_string() { println!("You're currently in {}, what will you do? (buy,sell,jet,loanshark,headstash)",player.location); } else { println!("You're currently in {}, what will you do? (buy,sell,jet)",player.location); } let action = prompt("".to_string()).unwrap(); if action.contains("buy") { buy_drugs(&mut player, &mut market); } else if action.contains("sell") { sell_drugs(&mut player, &mut market); } else if action.contains("jet") { go_to_location(&mut player, &mut market); player.debt_interest(); player.decrease_loan_timer(1); // Only have events trigger after 'jetting' let mut rng = rand::thread_rng(); let event_chance = rng.gen_range(0,9); if event_chance == 3 { price_event(&mut market); } if event_chance == 2 { trenchcoat_event(&mut player); } if event_chance == 1 { body_armor_event(&mut player); } if event_chance == 0 { cops_event(&mut player); } if player.health == 0 { clear_term(); println!("You died! Game over!"); let _placeholder = prompt("".to_string()); main_loop = true; } } else if action.contains("loanshark") { loan_shark(&mut player); } else if action.contains("headstash") { head_stash_op(&mut player, &mut head_stash); } else if action == "quit".to_string() { std::process::exit(0); } } } } }