private void OnGetData(GetDataEventArgs e) { if (e.MsgID != PacketTypes.NpcStrike) { return; } using (MemoryStream data = new MemoryStream(e.Msg.readBuffer, e.Index, e.Length - 1)) { var id = data.ReadInt16(); var dmg = data.ReadInt16(); data.ReadSingle(); data.ReadByte(); data.ReadByte(); if (id < 0 || id > Main.maxNPCs) { return; } NPC npc = Main.npc[id]; if (npc == null) { return; } if (npc.type != NPCID.KingSlime || !npc.active) { return; } if (dmg <= 0) { return; } if (Main.rand == null) { Main.rand = new Random((int)DateTime.Now.Ticks); } for (int i = 0; i < 20; i++) { int amt = 4 + Main.rand.Next(1, 5); for (int j = 0; j < amt; j++) { int x = (int)npc.position.X + Main.rand.Next(-80, 81); int y = (int)npc.position.Y + Main.rand.Next(-80, 81); NPC.NewNPC(x, y, NPCID.BlueSlime, 0); } } } }
public void ReadInt16() { Int16 testValue = 1024; using( var stream = new MemoryStream() ) { var bytes = BitConverter.GetBytes( testValue ); stream.Write( bytes, 0, bytes.Length ); stream.Seek( 0, SeekOrigin.Begin ); var value = stream.ReadInt16(); Assert.AreEqual( value, testValue ); } }
public byte[] DecodeImaAdpcmData() { var s = new MemoryStream(RawOutput); var numBlocks = DataSize / BlockAlign; var blockDataSize = BlockAlign - (Channels * 4); var outputSize = UncompressedSize * Channels * 2; var outOffset = 0; var output = new byte[outputSize]; var predictor = new int[Channels]; var index = new int[Channels]; // Decode each block of IMA ADPCM data in RawOutput for (var block = 0; block < numBlocks; block++) { // Each block starts with a initial state per-channel for (var c = 0; c < Channels; c++) { predictor[c] = s.ReadInt16(); index[c] = s.ReadUInt8(); /* unknown/reserved */ s.ReadUInt8(); // Output first sample from input output[outOffset++] = (byte)predictor[c]; output[outOffset++] = (byte)(predictor[c] >> 8); if (outOffset >= outputSize) return output; } // Decode and output remaining data in this block var blockOffset = 0; while (blockOffset < blockDataSize) { for (var c = 0; c < Channels; c++) { // Decode 4 bytes (to 16 bytes of output) per channel var chunk = s.ReadBytes(4); var decoded = ImaAdpcmReader.LoadImaAdpcmSound(chunk, ref index[c], ref predictor[c]); // Interleave output, one sample per channel var outOffsetChannel = outOffset + (2 * c); for (var i = 0; i < decoded.Length; i += 2) { var outOffsetSample = outOffsetChannel + i; if (outOffsetSample >= outputSize) return output; output[outOffsetSample] = decoded[i]; output[outOffsetSample + 1] = decoded[i + 1]; outOffsetChannel += 2 * (Channels - 1); } blockOffset += 4; } outOffset += 16 * Channels; } } return output; }
public void OnGetData(GetDataEventArgs args) { if (args.Msg.whoAmI == player.Index) { if (args.MsgID == PacketTypes.NpcStrike) { MemoryStream data = new MemoryStream(args.Msg.readBuffer, args.Index, args.Length); int npcID = data.ReadInt16(); int damage = data.ReadInt16(); if (Main.npc[npcID].life - damage <= 0) { if (toBeKilled.ContainsKey(Main.npc[npcID].name)) toBeKilled[Main.npc[npcID].name] -= 1; if (toBeKilled[Main.npc[npcID].name] <= 0) toBeKilled.Remove(Main.npc[npcID].name); } } } }
private void checkItemDrops(GetDataEventArgs args) { if (args.MsgID == PacketTypes.ItemDrop) { if (args.Handled) return; using (var data = new MemoryStream(args.Msg.readBuffer, args.Index, args.Length)) { Int16 id = data.ReadInt16(); float posx = data.ReadSingle(); float posy = data.ReadSingle(); float velx = data.ReadSingle(); float vely = data.ReadSingle(); int stack = data.ReadByte(); int prefix = data.ReadByte(); Int16 type = data.ReadInt16(); Item item = new Item(); item.SetDefaults(type); if (id == 0) return; if (toBeCollected.ContainsKey(item.name)) { toBeCollected[item.name] -= stack; if (toBeCollected[item.name] < 0) { if (Math.Abs(toBeCollected[item.name]) > 1) player.TSPlayer.SendInfoMessage(string.Format("Returning {0} {1}s", Math.Abs(toBeCollected[item.name]), item.name)); else player.TSPlayer.SendInfoMessage(string.Format("Returning {0} {1}", Math.Abs(toBeCollected[item.name]), item.name)); args.Handled = true; player.TSPlayer.GiveItem(item.type, item.name, item.width, item.width, Math.Abs(toBeCollected[item.name])); toBeCollected.Remove(item.name); } else if (toBeCollected[item.name] > 0) { if (Math.Abs(toBeCollected[item.name]) > 1) player.TSPlayer.SendInfoMessage(string.Format("Drop another {0} {1}s, to continue", Math.Abs(toBeCollected[item.name]), item.name)); else player.TSPlayer.SendInfoMessage(string.Format("Drop {0} {1}, to continue", Math.Abs(toBeCollected[item.name]), item.name)); args.Handled = true; } else { if (stack > 1) player.TSPlayer.SendInfoMessage(string.Format("You dropped {0} {1}s", stack, item.name)); else player.TSPlayer.SendInfoMessage(string.Format("You dropped {0} {1}", stack, item.name)); toBeCollected.Remove(item.name); args.Handled = true; } } args.Handled = true; } } }
private void OnGetData(GetDataEventArgs args) { var player = Utils.GetPlayer(args.Msg.whoAmI); #region ItemDrop if (args.MsgID == PacketTypes.ItemDrop) { if (args.Handled || !player.listingReward) return; using (var data = new MemoryStream(args.Msg.readBuffer, args.Index, args.Length)) { Int16 id = data.ReadInt16(); float posx = data.ReadSingle(); float posy = data.ReadSingle(); float velx = data.ReadSingle(); float vely = data.ReadSingle(); Int16 stacks = data.ReadInt16(); int prefix = data.ReadByte(); bool nodelay = data.ReadBoolean(); Int16 netid = data.ReadInt16(); Item item = new Item(); item.SetDefaults(netid); Console.WriteLine(String.Join(", ", id, stacks, prefix, netid)); if (stacks == 0) return; player.droppedItems.Add(new BHItem(netid, stacks, prefix)); player.TSPlayer.SendInfoMessage("{0} {1}{2} has been added to bounty rewards.", stacks, (prefix == 0) ? "" : TShock.Utils.GetPrefixByIdOrName(prefix.ToString())[0].ToString() + " ", item.name); args.Handled = true; } } #endregion #region PlayerDamage if (args.MsgID == PacketTypes.PlayerDamage) { if (args.Handled) return; using (var data = new MemoryStream(args.Msg.readBuffer, args.Index, args.Length)) { var playerID = data.ReadByte(); var hitDirection = data.ReadByte(); var damage = data.ReadInt16(); var pvp = data.ReadBoolean(); var crit = data.ReadBoolean(); Utils.GetPlayer(playerID).killingPlayer = (args.Msg.whoAmI != playerID) ? Utils.GetPlayer(args.Msg.whoAmI) : null; } } #endregion #region KillMe if (args.MsgID == PacketTypes.PlayerKillMe) { if (args.Handled) return; using (var data = new MemoryStream(args.Msg.readBuffer, args.Index, args.Length)) { var playerId = data.ReadByte(); var hitDirection = data.ReadByte(); var damage = data.ReadInt16(); var pvp = data.ReadBoolean(); var plr = Utils.GetPlayer(playerId); if (plr.killingPlayer != null) { var killer = plr.killingPlayer; if (pvp) { if (Utils.CheckVictimWasTarget(plr.name, killer.activeBounties)) { List<Bounty> completedBounties = new List<Bounty>(); foreach (Bounty bounty in killer.activeBounties.Keys) { if (bounty.target == plr.name) { for (int i = 0; i < bounty.reward.Count - 1; i++) { Item item = new Item(); item.SetDefaults(bounty.reward[i].id); killer.TSPlayer.GiveItem(item.netID, item.name, item.width, item.height, bounty.reward[i].stack, bounty.reward[i].prefix); } if (bounty.reward[bounty.reward.Count - 1].money != 0) { SEconomyPlugin.Instance.WorldAccount.TransferToAsync( SEconomyPlugin.Instance.GetBankAccount(killer.TSPlayer.UserAccountName), bounty.reward[bounty.reward.Count - 1].money, Wolfje.Plugins.SEconomy.Journal.BankAccountTransferOptions.AnnounceToReceiver, String.Format("for the bounty rewards.", bounty.reward[bounty.reward.Count - 1].money), String.Format("BountyHunt: " + "receiving money for reward.")); } completedBounties.Add(bounty); } } for (int i = 0; i < completedBounties.Count; i++) { foreach (var hunter in completedBounties[i].hunter) { var foundplr = TShock.Utils.FindPlayer(hunter); foundplr[0].SendInfoMessage("{0} completed {1}, a bounty you accepted.", killer.name, completedBounties[i].name); foundplr[0].SendInfoMessage("{0} will be removed from your accepted bounties.", completedBounties[i].name); } bounties.Remove(completedBounties[i]); dbManager.DeleteBounty(completedBounties[i]); } } if (Utils.CheckVictimWasHunter(plr.name, plr.activeBounties)) { List<Bounty> failedBounties = new List<Bounty>(); foreach (Bounty bounty in plr.activeBounties.Keys) { if (bounty.target == killer.name) { plr.activeBounties[bounty]++; if (config.MaxAttemptsPerBounty != 0 && plr.activeBounties[bounty] >= config.MaxAttemptsPerBounty) { failedBounties.Add(bounty); } if (config.HunterDeathPenalty != 0) { SEconomyPlugin.Instance.GetBankAccount(plr.TSPlayer.UserAccountName).TransferToAsync( SEconomyPlugin.Instance.WorldAccount, config.HunterDeathPenalty, Wolfje.Plugins.SEconomy.Journal.BankAccountTransferOptions.AnnounceToSender, String.Format("dying to your bounty target."), String.Format("BountyHunt: " + "lost money for death on the hunt.")); if (config.AddDeathPenaltyToRewards) bounties[bounties.IndexOf(bounty)].reward[bounties[bounties.IndexOf(bounty)].reward.Count].money += config.HunterDeathPenalty; } } } for (int i = 0; i < failedBounties.Count; i++) { plr.TSPlayer.SendErrorMessage("You have failed the {0} bounty {1} time(s).", failedBounties[i].name, plr.activeBounties[failedBounties[i]]); plr.TSPlayer.SendErrorMessage("{0} will be removed from your accept bounties, and can no longer be accepted by you.", failedBounties[i]); foreach (Bounty bounty in bounties) { if (bounty == failedBounties[i]) { bounty.hunter.Remove(plr.name); bounty.failures.Add(plr.name); dbManager.UpdateHunters(bounty); dbManager.UpdateFailures(bounty); } } } } } plr.killingPlayer = null; } } } #endregion }
private void ParseData(GetDataEventArgs args) { try { PacketTypes packet = args.MsgID; using (var data = new MemoryStream(args.Msg.readBuffer, args.Index, args.Length)) { TSPlayer player = TShock.Players[args.Msg.whoAmI]; var name = player.Name; if (player.IsLoggedIn) { name = player.UserAccountName; } switch (packet) { case PacketTypes.Tile: { byte type = data.ReadInt8(); int x = data.ReadInt32(); int y = data.ReadInt32(); bool fail = true; Action act; if (type == 0 || type == 2 || type == 4) act = Action.BREAK; else if (type == 1 || type == 3) act = Action.PLACE; else act = Action.ERROR; byte tileType = 0; if (act == Action.BREAK) { tileType = Main.tile[x, y].type; fail = data.ReadBoolean(); } else if (act == Action.PLACE) { tileType = data.ReadInt8(); fail = false; } if (act != Action.ERROR && !fail) { TileEvent evt = new TileEvent(x, y, name, player.IP, act, tileType, LogTile.helper.GetTime()); queue.Enqueue(evt); } break; } case PacketTypes.TileKill: { int x = data.ReadInt32(); int y = data.ReadInt32(); TileEvent evt = new TileEvent(x, y, name, player.IP, Action.BREAK, 0x15, LogTile.helper.GetTime()); queue.Enqueue(evt); break; } case PacketTypes.ChestOpen: { int chestID = data.ReadInt16(); int x = data.ReadInt32(); int y = data.ReadInt32(); int curChest = 0; if (!chestMap.TryGetValue(player, out curChest)) // chest being opened { chestMap.Add(player, chestID); itemMap.Add(player, Main.chest[chestID].item); } else // chest is being closed { chestMap.Remove(player); itemMap.Remove(player); } break; } case PacketTypes.ChestItem: { int chestID = data.ReadInt16(); byte itemSlot = data.ReadInt8(); byte stack = data.ReadInt8(); int curChest = 0; int type = itemMap[player][itemSlot].type; if (LogTile.enableDebugOutput) Console.WriteLine(type); Item[] curItems = Main.chest[chestID].item; if (LogTile.enableDebugOutput) Console.WriteLine(curItems[itemSlot].type); itemMap.Remove(player); itemMap.Add(player, curItems); break; } case PacketTypes.ChestGetContents: { int x = data.ReadInt32(); int y = data.ReadInt32(); if (LogTile.enableDebugOutput) Console.WriteLine("GETChestContents: (" + x + ", " + y + ")"); break; } case PacketTypes.SignNew: { int id = data.ReadInt16(); int x = data.ReadInt32(); int y = data.ReadInt32(); string text = data.ReadString(); break; } } } } catch (Exception e) { Console.WriteLine(e.Message); } }
static void ReadTiles(Map map, IniFile file, int2 fullSize) { var tileset = Game.ModData.DefaultTileSets[map.Tileset]; var mapSection = file.GetSection("IsoMapPack5"); var data = Convert.FromBase64String(mapSection.Aggregate(string.Empty, (a, b) => a + b.Value)); int cells = (fullSize.X * 2 - 1) * fullSize.Y; int lzoPackSize = cells * 11 + 4; // last 4 bytes contains a lzo pack header saying no more data is left var isoMapPack = new byte[lzoPackSize]; UnpackLZO(data, isoMapPack); var mf = new MemoryStream(isoMapPack); for (var i = 0; i < cells; i++) { var rx = mf.ReadUInt16(); var ry = mf.ReadUInt16(); var tilenum = mf.ReadUInt16(); /*var zero1 = */mf.ReadInt16(); var subtile = mf.ReadUInt8(); var z = mf.ReadUInt8(); /*var zero2 = */mf.ReadUInt8(); int dx = rx - ry + fullSize.X - 1; int dy = rx + ry - fullSize.X - 1; var mapCell = new MPos(dx / 2, dy); var cell = mapCell.ToCPos(map); if (map.Tiles.Contains(cell)) { if (!tileset.Templates.ContainsKey(tilenum)) tilenum = subtile = 0; map.Tiles[cell] = new TerrainTile(tilenum, subtile); map.Height[cell] = z; } } }
private void OnGetData(GetDataEventArgs args) { if (args.MsgID != PacketTypes.NpcStrike) { return; } if (args.Msg.whoAmI < 0 || args.Msg.whoAmI > Main.maxNetPlayers) { return; } Player player = Main.player[args.Msg.whoAmI]; using (MemoryStream ms = new MemoryStream(args.Msg.readBuffer, args.Index, args.Length - 1)) { short id = ms.ReadInt16(); ms.ReadInt16(); ms.ReadSingle(); ms.ReadInt8(); bool crit = Convert.ToBoolean(ms.ReadInt8()); if (Main.npc[id] == null) { return; } if (crit) { Dictionary<string, int[]> messages; Item selected = player.inventory[player.selectedItem]; if (selected.ranged && !ItemID.Sets.Explosives[selected.type]) { messages = config.CritMessages[WeaponType.Range].Messages; } else if (selected.melee) { messages = config.CritMessages[WeaponType.Melee].Messages; } else if (selected.magic) { messages = config.CritMessages[WeaponType.Magic].Messages; } else { if (ItemID.Sets.Explosives[selected.type] || selected.type == ItemID.Grenade || selected.type == ItemID.BouncyGrenade || selected.type == ItemID.PartyGirlGrenade || selected.type == ItemID.StickyGrenade) { messages = config.CritMessages[WeaponType.Explosive].Messages; } else { messages = config.CritMessages[WeaponType.Melee].Messages; } } KeyValuePair<string, int[]> message = messages.ElementAt(r.Next(0, messages.Count)); Color c = new Color(message.Value[0], message.Value[1], message.Value[2]); NetMessage.SendData((int)PacketTypes.CreateCombatText, -1, -1, message.Key, (int)c.PackedValue, Main.npc[id].position.X, Main.npc[id].position.Y); } } }
// ParseData +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ private void ParseData( GetDataEventArgs args ) { try { PacketTypes packet = args.MsgID; using ( var data = new MemoryStream( args.Msg.readBuffer, args.Index, args.Length ) ) { TSPlayer player = TShock.Players[args.Msg.whoAmI]; switch ( packet ) { case PacketTypes.ChestItem: { string action, itemName; short chestID = data.ReadInt16(); byte itemSlot = data.ReadInt8(); byte itemStack = data.ReadInt8(); byte prefix = data.ReadInt8(); short itemType = data.ReadInt16(); var oldItem = Main.chest[chestID].item[itemSlot]; if ( oldItem.name != null && oldItem.name.Length > 0 ) { action = "cGet"; itemName = oldItem.name; } // if else { var newItem = new Item(); newItem.netDefaults( itemType ); newItem.Prefix( prefix ); newItem.AffixName(); action = "cPut"; itemName = newItem.name; } // else if ( itemType != prevItemType ) { _ircClient.SendMessage( SendType.Message, _actionChannel, string.Format( "{0} ({1}): {2}: {3}", player.Name, player.Group.Name, action, itemName ) ); prevItemType = itemType; } // if break; } // case } // switch } // using } // try catch ( Exception exception ) { Console.WriteLine( exception.Message + "(" + exception.StackTrace + ")" ); } // catch }