public Tile() { int c = Utils.Random(100); if (c < 70) type = Type.Empty; else if (c < 90) type = Type.Wall; else type = Type.Door; unit = null; items = null; flags = 0; }
bool TryLoad() { string filename = string.Format("{0}.sav", player_name); if(!File.Exists(filename)) return false; try { using(BinaryReader f = new BinaryReader(File.Open(filename, FileMode.Open))) { // header char[] sign = new char[3]; f.Read(sign, 0, 3); if (sign[0] != 'H' || sign[1] != 'U' || sign[2] != 'N') throw new Exception(string.Format("Invalid file signature '{0}{1}{2}'.", sign[0], sign[1], sign[2])); byte ver; f.Read(out ver); if (ver != 0) throw new Exception(string.Format("Invalid file version '{0}'.", ver)); // game vars Utils.rnd.Load(f); player_name = f.ReadString(); int player_id = f.ReadInt32(); // units int count = f.ReadInt32(); units.Clear(); for(int i=0; i<count; ++i) { Unit u = new Unit(); u.Load(f); units.Add(u); } Unit.units = units; // map map.Load(f); if (player_id < 0 || player_id >= units.Count || units[player_id].ai) throw new Exception(string.Format("Invalid player id '{0}'.", player_id)); player = units[player_id]; throw_prev = null; throw_target = null; mode = Mode.Game; return true; } } catch(Exception e) { Log("Failed to load game from file {0}: {1}", filename, e.ToString()); mode = Mode.Invalid; ShowDialogAction("Failed to load game.\nCheck log file for details.", () => Environment.Exit(1)); while(true) { Draw(); UpdateDialog(); } } }
void PickThrowTarget() { var targets = units.Where(x => x != player) .Select(unit => new { unit, dist = unit.pos.Distance(player.pos) }) .Where(x => x.dist < 12); if(!targets.Any(x => x.unit == throw_target)) { // unit don't have old target, chose nearest if(targets.Any()) { throw_target = targets.MaxBy(x => x.dist).unit; look_pos = throw_target.pos; } else { throw_target = null; look_pos = player.pos; } } else { // use old target look_pos = throw_target.pos; } look_timer = 0; look_blink = true; }
void NewGame() { mode = Mode.Game; player = new Unit { pos = new Pos(5, 5), ai = false }; player.weapon = new GameItem(Item.Find("knife"), 1); player.armor = new GameItem(Item.Find("ljacket"), 1); player.items.Add(new GameItem(Item.Find("pistol"), 1, 10)); player.items.Add(new GameItem(Item.Find("ammo9mm"), 30)); player.items.Add(new GameItem(Item.Find("potion"), 2)); player.items.Add(new GameItem(Item.Find("stuff"), 15)); Tile t = map[player.pos]; t.unit = player; t.type = Tile.Type.Empty; units = new List<Unit>(); units.Add(player); Unit ai = new Unit { pos = new Pos(20, 10), ai = true }; t = map[ai.pos]; t.unit = ai; t.type = Tile.Type.Empty; units.Add(ai); ai = new Unit { pos = new Pos(10, 20), ai = true }; t = map[ai.pos]; t.unit = ai; t.type = Tile.Type.Empty; units.Add(ai); AddText("Welcome {0}! Press ? for controls.", player_name); // reset on new game/load throw_prev = null; throw_target = null; map.CalculateFov(player.pos, RADIUS); Log("Started new game."); }
void Attack(Unit a, Unit b) { string action; if (a.weapon != null) action = "stab"; else action = (Utils.K2() ? "punch" : "kick"); string s; if(Utils.Chance(70 + (a.ai ? 0 : 5))) { // hit int dmg = Utils.Random(1, 5) + (a.ai ? 0 : 1); b.hp -= dmg; if(b.hp <= 0) { if (b.ai) s = string.Format("You {0} enemy for {1} damage and kills him.", action, dmg); else s = string.Format("Enemy {0} you for {1} damage and kills you.", action, dmg); map[b.pos].unit = null; units.Remove(b); } else { if (b.ai) s = string.Format("You {0} enemy for {1} damage.", action, dmg); else s = string.Format("Enemy {0} you for {1} damage.", action, dmg); } } else { // miss if (b.ai) s = string.Format("You try to {0} enemy but misses.", action); else s = string.Format("Enemy try to {0} you but misses.", action); } AddText(s); }