123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719 |
- use std::io;
- use rand::Rng;
- use std::{thread, time};
- //////////
- // Objects
- //////////
- struct Market {
- // Store current values of drugs on the market
- weed: u32,
- cocaine: u32,
- heroin: u32,
- acid: u32,
- xtc: u32,
- ludes: u32,
- }
- impl Market {
- pub fn new(weed: u32, cocaine: u32, heroin: u32, acid: u32, xtc: u32, ludes: u32) -> Self {
- Market { 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(90,600);
- let cocaine_diff = rng.gen_range(19000,40000);
- let heroin_diff = rng.gen_range(7000,20000);
- let acid_diff = rng.gen_range(300,1500);
- let xtc_diff = rng.gen_range(50,500);
- let ludes_diff = rng.gen_range(15,190);
- 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);
- }
- }
- 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<String> {
- 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<u32> {
- 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<String> {
- 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.");
- thread::sleep(one_second);
- thread::sleep(one_second);
- thread::sleep(one_second);
- } 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!");
- thread::sleep(one_second);
- 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();
- done = true;
- }
- } else {
- println!("They miss and you keep running!");
- thread::sleep(one_second);
- }
- }
- }
- }
- }
- ////////////////////////////////////////
- // "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 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();
-
- let mut rng = rand::thread_rng();
- let event_chance = rng.gen_range(0,9);
- 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 {
- println!("You died! Game over!");
- 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! **");
- }
- }
- 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);
- } 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);
- }
- }
- }
- }
- }
|