Exemple #1
0
    void GrenadeExplosion(Game game, int grenadeEntityId)
    {
        float LocalPlayerPositionX = game.player.position.x;
        float LocalPlayerPositionY = game.player.position.y;
        float LocalPlayerPositionZ = game.player.position.z;

        Entity   grenadeEntity = game.entities[grenadeEntityId];
        Sprite   grenadeSprite = grenadeEntity.sprite;
        Grenade_ grenade       = grenadeEntity.grenade;

        game.AudioPlayAt("grenadeexplosion.ogg", grenadeSprite.positionX, grenadeSprite.positionY, grenadeSprite.positionZ);

        {
            Entity entity = new Entity();

            Sprite spritenew = new Sprite();
            spritenew.image          = "ani5.png";
            spritenew.positionX      = grenadeSprite.positionX;
            spritenew.positionY      = grenadeSprite.positionY + 1;
            spritenew.positionZ      = grenadeSprite.positionZ;
            spritenew.size           = 200;
            spritenew.animationcount = 4;

            entity.sprite  = spritenew;
            entity.expires = Expires.Create(1);
            game.EntityAddLocal(entity);
        }

        {
            Packet_ServerExplosion explosion = new Packet_ServerExplosion();
            explosion.XFloat     = game.SerializeFloat(grenadeSprite.positionX);
            explosion.YFloat     = game.SerializeFloat(grenadeSprite.positionZ);
            explosion.ZFloat     = game.SerializeFloat(grenadeSprite.positionY);
            explosion.RangeFloat = game.blocktypes[grenade.block].ExplosionRangeFloat;
            explosion.IsRelativeToPlayerPosition = 0;
            explosion.TimeFloat = game.blocktypes[grenade.block].ExplosionTimeFloat;

            Entity entity = new Entity();
            entity.push             = explosion;
            entity.expires          = new Expires();
            entity.expires.timeLeft = game.DeserializeFloat(game.blocktypes[grenade.block].ExplosionTimeFloat);
            game.EntityAddLocal(entity);
        }

        float dist = game.Dist(LocalPlayerPositionX, LocalPlayerPositionY, LocalPlayerPositionZ, grenadeSprite.positionX, grenadeSprite.positionY, grenadeSprite.positionZ);
        float dmg  = (1 - dist / game.DeserializeFloat(game.blocktypes[grenade.block].ExplosionRangeFloat)) * game.DeserializeFloat(game.blocktypes[grenade.block].DamageBodyFloat);

        if (dmg > 0)
        {
            game.ApplyDamageToPlayer(game.platform.FloatToInt(dmg), Packet_DeathReasonEnum.Explosion, grenade.sourcePlayer);
        }
    }
    internal void ProcessPacket(Packet_Server packet)
    {
        if (game.packetHandlers[packet.Id] != null)
        {
            game.packetHandlers[packet.Id].Handle(game, packet);
        }
        switch (packet.Id)
        {
        case Packet_ServerIdEnum.ServerIdentification:
        {
            string invalidversionstr = game.language.InvalidVersionConnectAnyway();

            game.serverGameVersion = packet.Identification.MdProtocolVersion;
            if (game.serverGameVersion != game.platform.GetGameVersion())
            {
                game.ChatLog("[GAME] Different game versions");
                string q = game.platform.StringFormat2(invalidversionstr, game.platform.GetGameVersion(), game.serverGameVersion);
                game.invalidVersionDrawMessage          = q;
                game.invalidVersionPacketIdentification = packet;
            }
            else
            {
                game.ProcessServerIdentification(packet);
            }
            game.ReceivedMapLength = 0;
        }
        break;

        case Packet_ServerIdEnum.Ping:
        {
            game.SendPingReply();
            game.ServerInfo.ServerPing.Send(game.platform);
        }
        break;

        case Packet_ServerIdEnum.PlayerPing:
        {
            game.ServerInfo.ServerPing.Receive(game.platform);
        }
        break;

        case Packet_ServerIdEnum.LevelInitialize:
        {
            game.ChatLog("[GAME] Initialized map loading");
            game.ReceivedMapLength = 0;
            game.InvokeMapLoadingProgress(0, 0, game.language.Connecting());
        }
        break;

        case Packet_ServerIdEnum.LevelDataChunk:
        {
            game.InvokeMapLoadingProgress(packet.LevelDataChunk.PercentComplete, game.ReceivedMapLength, packet.LevelDataChunk.Status);
        }
        break;

        case Packet_ServerIdEnum.LevelFinalize:
        {
            game.ChatLog("[GAME] Finished map loading");
        }
        break;

        case Packet_ServerIdEnum.SetBlock:
        {
            int x    = packet.SetBlock.X;
            int y    = packet.SetBlock.Y;
            int z    = packet.SetBlock.Z;
            int type = packet.SetBlock.BlockType;
            //try
            {
                game.SetTileAndUpdate(x, y, z, type);
            }
            //catch { Console.WriteLine("Cannot update tile!"); }
        }
        break;

        case Packet_ServerIdEnum.FillArea:
        {
            int ax = packet.FillArea.X1;
            int ay = packet.FillArea.Y1;
            int az = packet.FillArea.Z1;
            int bx = packet.FillArea.X2;
            int by = packet.FillArea.Y2;
            int bz = packet.FillArea.Z2;

            int startx = MathCi.MinInt(ax, bx);
            int endx   = MathCi.MaxInt(ax, bx);
            int starty = MathCi.MinInt(ay, by);
            int endy   = MathCi.MaxInt(ay, by);
            int startz = MathCi.MinInt(az, bz);
            int endz   = MathCi.MaxInt(az, bz);

            int blockCount = packet.FillArea.BlockCount;
            {
                for (int x = startx; x <= endx; x++)
                {
                    for (int y = starty; y <= endy; y++)
                    {
                        for (int z = startz; z <= endz; z++)
                        {
                            // if creative mode is off and player run out of blocks
                            if (blockCount == 0)
                            {
                                return;
                            }
                            //try
                            {
                                game.SetTileAndUpdate(x, y, z, packet.FillArea.BlockType);
                            }
                            //catch
                            //{
                            //    Console.WriteLine("Cannot update tile!");
                            //}
                            blockCount--;
                        }
                    }
                }
            }
        }
        break;

        case Packet_ServerIdEnum.FillAreaLimit:
        {
            game.fillAreaLimit = packet.FillAreaLimit.Limit;
            if (game.fillAreaLimit > 100000)
            {
                game.fillAreaLimit = 100000;
            }
        }
        break;

        case Packet_ServerIdEnum.Freemove:
        {
            game.AllowFreemove = packet.Freemove.IsEnabled != 0;
            if (!game.AllowFreemove)
            {
                game.controls.SetFreemove(FreemoveLevelEnum.None);
                game.movespeed = game.basemovespeed;
                game.Log(game.language.MoveNormal());
            }
        }
        break;

        case Packet_ServerIdEnum.PlayerSpawnPosition:
        {
            int x = packet.PlayerSpawnPosition.X;
            int y = packet.PlayerSpawnPosition.Y;
            int z = packet.PlayerSpawnPosition.Z;
            game.playerPositionSpawnX = x;
            game.playerPositionSpawnY = z;
            game.playerPositionSpawnZ = y;
            game.Log(game.platform.StringFormat(game.language.SpawnPositionSetTo(), game.platform.StringFormat3("{0},{1},{2}", game.platform.IntToString(x), game.platform.IntToString(y), game.platform.IntToString(z))));
        }
        break;

        case Packet_ServerIdEnum.Message:
        {
            game.AddChatline(packet.Message.Message);
            game.ChatLog(packet.Message.Message);
        }
        break;

        case Packet_ServerIdEnum.DisconnectPlayer:
        {
            game.ChatLog(game.platform.StringFormat("[GAME] Disconnected by the server ({0})", packet.DisconnectPlayer.DisconnectReason));
            //Exit mouse pointer lock if necessary
            if (game.platform.IsMousePointerLocked())
            {
                game.platform.ExitMousePointerLock();
            }
            //When server disconnects player, return to main menu
            game.platform.MessageBoxShowError(packet.DisconnectPlayer.DisconnectReason, "Disconnected from server");
            game.ExitToMainMenu_();
            break;
        }

        case Packet_ServerIdEnum.PlayerStats:
        {
            Packet_ServerPlayerStats p = packet.PlayerStats;
            game.PlayerStats = p;
        }
        break;

        case Packet_ServerIdEnum.FiniteInventory:
        {
            //check for null so it's possible to connect
            //to old versions of game (before 2011-05-05)
            if (packet.Inventory.Inventory != null)
            {
                //d_Inventory.CopyFrom(ConvertInventory(packet.Inventory.Inventory));
                game.UseInventory(packet.Inventory.Inventory);
            }
            //FiniteInventory = packet.FiniteInventory.BlockTypeAmount;
            //ENABLE_FINITEINVENTORY = packet.FiniteInventory.IsFinite;
            //FiniteInventoryMax = packet.FiniteInventory.Max;
        }
        break;

        case Packet_ServerIdEnum.Season:
        {
            packet.Season.Hour -= 1;
            if (packet.Season.Hour < 0)
            {
                //shouldn't happen
                packet.Season.Hour = 12 * Game.HourDetail;
            }
            int sunlight = game.NightLevels[packet.Season.Hour];
            game.SkySphereNight = sunlight < 8;
            game.d_SunMoonRenderer.day_length_in_seconds = 60 * 60 * 24 / packet.Season.DayNightCycleSpeedup;
            int hour = packet.Season.Hour / Game.HourDetail;
            if (game.d_SunMoonRenderer.GetHour() != hour)
            {
                game.d_SunMoonRenderer.SetHour(hour);
            }

            if (game.sunlight_ != sunlight)
            {
                game.sunlight_ = sunlight;
                //d_Shadows.ResetShadows();
                game.RedrawAllBlocks();
            }
        }
        break;

        case Packet_ServerIdEnum.BlobInitialize:
        {
            game.blobdownload = new CitoMemoryStream();
            //blobdownloadhash = ByteArrayToString(packet.BlobInitialize.hash);
            game.blobdownloadname = packet.BlobInitialize.Name;
            game.blobdownloadmd5  = packet.BlobInitialize.Md5;
        }
        break;

        case Packet_ServerIdEnum.BlobPart:
        {
            int length = game.platform.ByteArrayLength(packet.BlobPart.Data);
            game.blobdownload.Write(packet.BlobPart.Data, 0, length);
            game.ReceivedMapLength += length;
        }
        break;

        case Packet_ServerIdEnum.BlobFinalize:
        {
            byte[] downloaded = game.blobdownload.ToArray();

            if (game.blobdownloadname != null)         // old servers
            {
                game.SetFile(game.blobdownloadname, game.blobdownloadmd5, downloaded, game.blobdownload.Length());
            }
            game.blobdownload = null;
        }
        break;

        case Packet_ServerIdEnum.Sound:
        {
            game.PlaySoundAt(packet.Sound.Name, packet.Sound.X, packet.Sound.Y, packet.Sound.Z);
        }
        break;

        case Packet_ServerIdEnum.RemoveMonsters:
        {
            for (int i = Game.entityMonsterIdStart; i < Game.entityMonsterIdStart + Game.entityMonsterIdCount; i++)
            {
                game.entities[i] = null;
            }
        }
        break;

        case Packet_ServerIdEnum.Translation:
            game.language.Override(packet.Translation.Lang, packet.Translation.Id, packet.Translation.Translation);
            break;

        case Packet_ServerIdEnum.BlockType:
            game.NewBlockTypes[packet.BlockType.Id] = packet.BlockType.Blocktype;
            break;

        case Packet_ServerIdEnum.SunLevels:
            game.NightLevels = packet.SunLevels.Sunlevels;
            break;

        case Packet_ServerIdEnum.LightLevels:
            for (int i = 0; i < packet.LightLevels.LightlevelsCount; i++)
            {
                game.mLightLevels[i] = game.DeserializeFloat(packet.LightLevels.Lightlevels[i]);
            }
            break;

        case Packet_ServerIdEnum.Follow:
            IntRef oldFollowId = game.FollowId();
            game.Follow = packet.Follow.Client;
            if (packet.Follow.Tpp != 0)
            {
                game.SetCamera(CameraType.Overhead);
                game.player.position.rotx = Game.GetPi();
                game.GuiStateBackToGame();
            }
            else
            {
                game.SetCamera(CameraType.Fpp);
            }
            break;

        case Packet_ServerIdEnum.Bullet:
            game.EntityAddLocal(game.CreateBulletEntity(
                                    game.DeserializeFloat(packet.Bullet.FromXFloat),
                                    game.DeserializeFloat(packet.Bullet.FromYFloat),
                                    game.DeserializeFloat(packet.Bullet.FromZFloat),
                                    game.DeserializeFloat(packet.Bullet.ToXFloat),
                                    game.DeserializeFloat(packet.Bullet.ToYFloat),
                                    game.DeserializeFloat(packet.Bullet.ToZFloat),
                                    game.DeserializeFloat(packet.Bullet.SpeedFloat)));
            break;

        case Packet_ServerIdEnum.Ammo:
            if (!game.ammostarted)
            {
                game.ammostarted = true;
                for (int i = 0; i < packet.Ammo.TotalAmmoCount; i++)
                {
                    Packet_IntInt k = packet.Ammo.TotalAmmo[i];
                    game.LoadedAmmo[k.Key_] = MathCi.MinInt(k.Value_, game.blocktypes[k.Key_].AmmoMagazine);
                }
            }
            game.TotalAmmo = new int[GlobalVar.MAX_BLOCKTYPES];
            for (int i = 0; i < packet.Ammo.TotalAmmoCount; i++)
            {
                game.TotalAmmo[packet.Ammo.TotalAmmo[i].Key_] = packet.Ammo.TotalAmmo[i].Value_;
            }
            break;

        case Packet_ServerIdEnum.Explosion:
        {
            Entity entity = new Entity();
            entity.expires          = new Expires();
            entity.expires.timeLeft = game.DeserializeFloat(packet.Explosion.TimeFloat);
            entity.push             = packet.Explosion;
            game.EntityAddLocal(entity);
        }
        break;

        case Packet_ServerIdEnum.Projectile:
        {
            Entity entity = new Entity();

            Sprite sprite = new Sprite();
            sprite.image          = "ChemicalGreen.png";
            sprite.size           = 14;
            sprite.animationcount = 0;
            sprite.positionX      = game.DeserializeFloat(packet.Projectile.FromXFloat);
            sprite.positionY      = game.DeserializeFloat(packet.Projectile.FromYFloat);
            sprite.positionZ      = game.DeserializeFloat(packet.Projectile.FromZFloat);
            entity.sprite         = sprite;

            Grenade_ grenade = new Grenade_();
            grenade.velocityX    = game.DeserializeFloat(packet.Projectile.VelocityXFloat);
            grenade.velocityY    = game.DeserializeFloat(packet.Projectile.VelocityYFloat);
            grenade.velocityZ    = game.DeserializeFloat(packet.Projectile.VelocityZFloat);
            grenade.block        = packet.Projectile.BlockId;
            grenade.sourcePlayer = packet.Projectile.SourcePlayerID;
            entity.grenade       = grenade;

            entity.expires = Expires.Create(game.DeserializeFloat(packet.Projectile.ExplodesAfterFloat));

            game.EntityAddLocal(entity);
        }
        break;

        case Packet_ServerIdEnum.BlockTypes:
            game.blocktypes    = game.NewBlockTypes;
            game.NewBlockTypes = new Packet_BlockType[GlobalVar.MAX_BLOCKTYPES];

            int      textureInAtlasIdsCount = 1024;
            string[] textureInAtlasIds      = new string[textureInAtlasIdsCount];
            int      lastTextureId          = 0;
            for (int i = 0; i < GlobalVar.MAX_BLOCKTYPES; i++)
            {
                if (game.blocktypes[i] != null)
                {
                    string[] to_load       = new string[7];
                    int      to_loadLength = 7;
                    {
                        to_load[0] = game.blocktypes[i].TextureIdLeft;
                        to_load[1] = game.blocktypes[i].TextureIdRight;
                        to_load[2] = game.blocktypes[i].TextureIdFront;
                        to_load[3] = game.blocktypes[i].TextureIdBack;
                        to_load[4] = game.blocktypes[i].TextureIdTop;
                        to_load[5] = game.blocktypes[i].TextureIdBottom;
                        to_load[6] = game.blocktypes[i].TextureIdForInventory;
                    }
                    for (int k = 0; k < to_loadLength; k++)
                    {
                        if (!Contains(textureInAtlasIds, textureInAtlasIdsCount, to_load[k]))
                        {
                            textureInAtlasIds[lastTextureId++] = to_load[k];
                        }
                    }
                }
            }
            game.d_Data.UseBlockTypes(game.platform, game.blocktypes, GlobalVar.MAX_BLOCKTYPES);
            for (int i = 0; i < GlobalVar.MAX_BLOCKTYPES; i++)
            {
                Packet_BlockType b = game.blocktypes[i];
                if (b == null)
                {
                    continue;
                }
                //Indexed by block id and TileSide.
                if (textureInAtlasIds != null)
                {
                    game.TextureId[i][0]          = IndexOf(textureInAtlasIds, textureInAtlasIdsCount, b.TextureIdTop);
                    game.TextureId[i][1]          = IndexOf(textureInAtlasIds, textureInAtlasIdsCount, b.TextureIdBottom);
                    game.TextureId[i][2]          = IndexOf(textureInAtlasIds, textureInAtlasIdsCount, b.TextureIdFront);
                    game.TextureId[i][3]          = IndexOf(textureInAtlasIds, textureInAtlasIdsCount, b.TextureIdBack);
                    game.TextureId[i][4]          = IndexOf(textureInAtlasIds, textureInAtlasIdsCount, b.TextureIdLeft);
                    game.TextureId[i][5]          = IndexOf(textureInAtlasIds, textureInAtlasIdsCount, b.TextureIdRight);
                    game.TextureIdForInventory[i] = IndexOf(textureInAtlasIds, textureInAtlasIdsCount, b.TextureIdForInventory);
                }
            }
            game.UseTerrainTextures(textureInAtlasIds, textureInAtlasIdsCount);
            game.handRedraw = true;
            game.RedrawAllBlocks();
            break;

        case Packet_ServerIdEnum.ServerRedirect:
            game.ChatLog("[GAME] Received server redirect");
            //Leave current server
            game.SendLeave(Packet_LeaveReasonEnum.Leave);
            //Exit game screen and create new game instance
            game.ExitAndSwitchServer(packet.Redirect);
            break;
        }
    }
Exemple #3
0
    internal void NextBullet(Game game, int bulletsshot)
    {
        float one    = 1;
        bool  left   = game.mouseLeft;
        bool  middle = game.mouseMiddle;
        bool  right  = game.mouseRight;

        bool IsNextShot = bulletsshot != 0;

        if (!game.leftpressedpicking)
        {
            if (game.mouseleftclick)
            {
                game.leftpressedpicking = true;
            }
            else
            {
                left = false;
            }
        }
        else
        {
            if (game.mouseleftdeclick)
            {
                game.leftpressedpicking = false;
                left = false;
            }
        }
        if (!left)
        {
            game.currentAttackedBlock = null;
        }

        Packet_Item item          = game.d_Inventory.RightHand[game.ActiveMaterial];
        bool        ispistol      = (item != null && game.blocktypes[item.BlockId].IsPistol);
        bool        ispistolshoot = ispistol && left;
        bool        isgrenade     = ispistol && game.blocktypes[item.BlockId].PistolType == Packet_PistolTypeEnum.Grenade;

        if (ispistol && isgrenade)
        {
            ispistolshoot = game.mouseleftdeclick;
        }
        //grenade cooking - TODO: fix instant explosion when closing ESC menu
        if (game.mouseleftclick)
        {
            game.grenadecookingstartMilliseconds = game.platform.TimeMillisecondsFromStart();
            if (ispistol && isgrenade)
            {
                if (game.blocktypes[item.BlockId].Sounds.ShootCount > 0)
                {
                    game.AudioPlay(game.platform.StringFormat("{0}.ogg", game.blocktypes[item.BlockId].Sounds.Shoot[0]));
                }
            }
        }
        float wait = ((one * (game.platform.TimeMillisecondsFromStart() - game.grenadecookingstartMilliseconds)) / 1000);

        if (isgrenade && left)
        {
            if (wait >= game.grenadetime && isgrenade && game.grenadecookingstartMilliseconds != 0)
            {
                ispistolshoot         = true;
                game.mouseleftdeclick = true;
            }
            else
            {
                return;
            }
        }
        else
        {
            game.grenadecookingstartMilliseconds = 0;
        }

        if (ispistol && game.mouserightclick && (game.platform.TimeMillisecondsFromStart() - game.lastironsightschangeMilliseconds) >= 500)
        {
            game.IronSights = !game.IronSights;
            game.lastironsightschangeMilliseconds = game.platform.TimeMillisecondsFromStart();
        }

        IntRef pick2count = new IntRef();
        Line3D pick       = new Line3D();

        GetPickingLine(game, pick, ispistolshoot);
        BlockPosSide[] pick2 = game.Pick(game.s, pick, pick2count);

        if (left)
        {
            game.handSetAttackDestroy = true;
        }
        else if (right)
        {
            game.handSetAttackBuild = true;
        }

        if (game.overheadcamera && pick2count.value > 0 && left)
        {
            //if not picked any object, and mouse button is pressed, then walk to destination.
            if (game.Follow == null)
            {
                //Only walk to destination when not following someone
                game.playerdestination = Vector3Ref.Create(pick2[0].blockPos[0], pick2[0].blockPos[1] + 1, pick2[0].blockPos[2]);
            }
        }
        bool pickdistanceok = (pick2count.value > 0); //&& (!ispistol);

        if (pickdistanceok)
        {
            if (game.Dist(pick2[0].blockPos[0] + one / 2, pick2[0].blockPos[1] + one / 2, pick2[0].blockPos[2] + one / 2,
                          pick.Start[0], pick.Start[1], pick.Start[2]) > CurrentPickDistance(game))
            {
                pickdistanceok = false;
            }
        }
        bool playertileempty = game.IsTileEmptyForPhysics(
            game.platform.FloatToInt(game.player.position.x),
            game.platform.FloatToInt(game.player.position.z),
            game.platform.FloatToInt(game.player.position.y + (one / 2)));
        bool playertileemptyclose = game.IsTileEmptyForPhysicsClose(
            game.platform.FloatToInt(game.player.position.x),
            game.platform.FloatToInt(game.player.position.z),
            game.platform.FloatToInt(game.player.position.y + (one / 2)));
        BlockPosSide pick0 = new BlockPosSide();

        if (pick2count.value > 0 &&
            ((pickdistanceok && (playertileempty || (playertileemptyclose))) ||
             game.overheadcamera)
            )
        {
            game.SelectedBlockPositionX = game.platform.FloatToInt(pick2[0].Current()[0]);
            game.SelectedBlockPositionY = game.platform.FloatToInt(pick2[0].Current()[1]);
            game.SelectedBlockPositionZ = game.platform.FloatToInt(pick2[0].Current()[2]);
            pick0 = pick2[0];
        }
        else
        {
            game.SelectedBlockPositionX = -1;
            game.SelectedBlockPositionY = -1;
            game.SelectedBlockPositionZ = -1;
            pick0.blockPos    = new float[3];
            pick0.blockPos[0] = -1;
            pick0.blockPos[1] = -1;
            pick0.blockPos[2] = -1;
        }
        PickEntity(game, pick, pick2, pick2count);
        if (game.cameratype == CameraType.Fpp || game.cameratype == CameraType.Tpp)
        {
            int ntileX = game.platform.FloatToInt(pick0.Current()[0]);
            int ntileY = game.platform.FloatToInt(pick0.Current()[1]);
            int ntileZ = game.platform.FloatToInt(pick0.Current()[2]);
            if (game.IsUsableBlock(game.map.GetBlock(ntileX, ntileZ, ntileY)))
            {
                game.currentAttackedBlock = Vector3IntRef.Create(ntileX, ntileZ, ntileY);
            }
        }
        if (game.GetFreeMouse())
        {
            if (pick2count.value > 0)
            {
                OnPick_(pick0);
            }
            return;
        }

        if ((one * (game.platform.TimeMillisecondsFromStart() - lastbuildMilliseconds) / 1000) >= BuildDelay(game) ||
            IsNextShot)
        {
            if (left && game.d_Inventory.RightHand[game.ActiveMaterial] == null)
            {
                game.SendPacketClient(ClientPackets.MonsterHit(game.platform.FloatToInt(2 + game.rnd.NextFloat() * 4)));
            }
            if (left && !fastclicking)
            {
                //todo animation
                fastclicking = false;
            }
            if ((left || right || middle) && (!isgrenade))
            {
                lastbuildMilliseconds = game.platform.TimeMillisecondsFromStart();
            }
            if (isgrenade && game.mouseleftdeclick)
            {
                lastbuildMilliseconds = game.platform.TimeMillisecondsFromStart();
            }
            if (game.reloadstartMilliseconds != 0)
            {
                PickingEnd(left, right, middle, ispistol);
                return;
            }
            if (ispistolshoot)
            {
                if ((!(game.LoadedAmmo[item.BlockId] > 0)) ||
                    (!(game.TotalAmmo[item.BlockId] > 0)))
                {
                    game.AudioPlay("Dry Fire Gun-SoundBible.com-2053652037.ogg");
                    PickingEnd(left, right, middle, ispistol);
                    return;
                }
            }
            if (ispistolshoot)
            {
                float toX = pick.End[0];
                float toY = pick.End[1];
                float toZ = pick.End[2];
                if (pick2count.value > 0)
                {
                    toX = pick2[0].blockPos[0];
                    toY = pick2[0].blockPos[1];
                    toZ = pick2[0].blockPos[2];
                }

                Packet_ClientShot shot = new Packet_ClientShot();
                shot.FromX     = game.SerializeFloat(pick.Start[0]);
                shot.FromY     = game.SerializeFloat(pick.Start[1]);
                shot.FromZ     = game.SerializeFloat(pick.Start[2]);
                shot.ToX       = game.SerializeFloat(toX);
                shot.ToY       = game.SerializeFloat(toY);
                shot.ToZ       = game.SerializeFloat(toZ);
                shot.HitPlayer = -1;

                for (int i = 0; i < game.entitiesCount; i++)
                {
                    if (game.entities[i] == null)
                    {
                        continue;
                    }
                    if (game.entities[i].drawModel == null)
                    {
                        continue;
                    }
                    Entity p_ = game.entities[i];
                    if (p_.networkPosition == null)
                    {
                        continue;
                    }
                    if (!p_.networkPosition.PositionLoaded)
                    {
                        continue;
                    }
                    float feetposX = p_.position.x;
                    float feetposY = p_.position.y;
                    float feetposZ = p_.position.z;
                    //var p = PlayerPositionSpawn;
                    Box3D bodybox  = new Box3D();
                    float headsize = (p_.drawModel.ModelHeight - p_.drawModel.eyeHeight) * 2; //0.4f;
                    float h        = p_.drawModel.ModelHeight - headsize;
                    float r        = one * 35 / 100;

                    bodybox.AddPoint(feetposX - r, feetposY + 0, feetposZ - r);
                    bodybox.AddPoint(feetposX - r, feetposY + 0, feetposZ + r);
                    bodybox.AddPoint(feetposX + r, feetposY + 0, feetposZ - r);
                    bodybox.AddPoint(feetposX + r, feetposY + 0, feetposZ + r);

                    bodybox.AddPoint(feetposX - r, feetposY + h, feetposZ - r);
                    bodybox.AddPoint(feetposX - r, feetposY + h, feetposZ + r);
                    bodybox.AddPoint(feetposX + r, feetposY + h, feetposZ - r);
                    bodybox.AddPoint(feetposX + r, feetposY + h, feetposZ + r);

                    Box3D headbox = new Box3D();

                    headbox.AddPoint(feetposX - r, feetposY + h, feetposZ - r);
                    headbox.AddPoint(feetposX - r, feetposY + h, feetposZ + r);
                    headbox.AddPoint(feetposX + r, feetposY + h, feetposZ - r);
                    headbox.AddPoint(feetposX + r, feetposY + h, feetposZ + r);

                    headbox.AddPoint(feetposX - r, feetposY + h + headsize, feetposZ - r);
                    headbox.AddPoint(feetposX - r, feetposY + h + headsize, feetposZ + r);
                    headbox.AddPoint(feetposX + r, feetposY + h + headsize, feetposZ - r);
                    headbox.AddPoint(feetposX + r, feetposY + h + headsize, feetposZ + r);

                    float[] p;
                    float   localeyeposX = game.EyesPosX();
                    float   localeyeposY = game.EyesPosY();
                    float   localeyeposZ = game.EyesPosZ();
                    p = Intersection.CheckLineBoxExact(pick, headbox);
                    if (p != null)
                    {
                        //do not allow to shoot through terrain
                        if (pick2count.value == 0 || (game.Dist(pick2[0].blockPos[0], pick2[0].blockPos[1], pick2[0].blockPos[2], localeyeposX, localeyeposY, localeyeposZ)
                                                      > game.Dist(p[0], p[1], p[2], localeyeposX, localeyeposY, localeyeposZ)))
                        {
                            if (!isgrenade)
                            {
                                Entity entity = new Entity();
                                Sprite sprite = new Sprite();
                                sprite.positionX = p[0];
                                sprite.positionY = p[1];
                                sprite.positionZ = p[2];
                                sprite.image     = "blood.png";
                                entity.sprite    = sprite;
                                entity.expires   = Expires.Create(one * 2 / 10);
                                game.EntityAddLocal(entity);
                            }
                            shot.HitPlayer = i;
                            shot.IsHitHead = 1;
                        }
                    }
                    else
                    {
                        p = Intersection.CheckLineBoxExact(pick, bodybox);
                        if (p != null)
                        {
                            //do not allow to shoot through terrain
                            if (pick2count.value == 0 || (game.Dist(pick2[0].blockPos[0], pick2[0].blockPos[1], pick2[0].blockPos[2], localeyeposX, localeyeposY, localeyeposZ)
                                                          > game.Dist(p[0], p[1], p[2], localeyeposX, localeyeposY, localeyeposZ)))
                            {
                                if (!isgrenade)
                                {
                                    Entity entity = new Entity();
                                    Sprite sprite = new Sprite();
                                    sprite.positionX = p[0];
                                    sprite.positionY = p[1];
                                    sprite.positionZ = p[2];
                                    sprite.image     = "blood.png";
                                    entity.sprite    = sprite;
                                    entity.expires   = Expires.Create(one * 2 / 10);
                                    game.EntityAddLocal(entity);
                                }
                                shot.HitPlayer = i;
                                shot.IsHitHead = 0;
                            }
                        }
                    }
                }
                shot.WeaponBlock = item.BlockId;
                game.LoadedAmmo[item.BlockId] = game.LoadedAmmo[item.BlockId] - 1;
                game.TotalAmmo[item.BlockId]  = game.TotalAmmo[item.BlockId] - 1;
                float projectilespeed = game.DeserializeFloat(game.blocktypes[item.BlockId].ProjectileSpeedFloat);
                if (projectilespeed == 0)
                {
                    {
                        Entity entity = game.CreateBulletEntity(
                            pick.Start[0], pick.Start[1], pick.Start[2],
                            toX, toY, toZ, 150);
                        game.EntityAddLocal(entity);
                    }
                }
                else
                {
                    float vX      = toX - pick.Start[0];
                    float vY      = toY - pick.Start[1];
                    float vZ      = toZ - pick.Start[2];
                    float vLength = game.Length(vX, vY, vZ);
                    vX /= vLength;
                    vY /= vLength;
                    vZ /= vLength;
                    vX *= projectilespeed;
                    vY *= projectilespeed;
                    vZ *= projectilespeed;
                    shot.ExplodesAfter = game.SerializeFloat(game.grenadetime - wait);

                    {
                        Entity grenadeEntity = new Entity();

                        Sprite sprite = new Sprite();
                        sprite.image          = "ChemicalGreen.png";
                        sprite.size           = 14;
                        sprite.animationcount = 0;
                        sprite.positionX      = pick.Start[0];
                        sprite.positionY      = pick.Start[1];
                        sprite.positionZ      = pick.Start[2];
                        grenadeEntity.sprite  = sprite;

                        Grenade_ projectile = new Grenade_();
                        projectile.velocityX    = vX;
                        projectile.velocityY    = vY;
                        projectile.velocityZ    = vZ;
                        projectile.block        = item.BlockId;
                        projectile.sourcePlayer = game.LocalPlayerId;

                        grenadeEntity.expires = Expires.Create(game.grenadetime - wait);

                        grenadeEntity.grenade = projectile;
                        game.EntityAddLocal(grenadeEntity);
                    }
                }
                Packet_Client packet = new Packet_Client();
                packet.Id   = Packet_ClientIdEnum.Shot;
                packet.Shot = shot;
                game.SendPacketClient(packet);

                if (game.blocktypes[item.BlockId].Sounds.ShootEndCount > 0)
                {
                    game.pistolcycle = game.rnd.Next() % game.blocktypes[item.BlockId].Sounds.ShootEndCount;
                    game.AudioPlay(game.platform.StringFormat("{0}.ogg", game.blocktypes[item.BlockId].Sounds.ShootEnd[game.pistolcycle]));
                }

                bulletsshot++;
                if (bulletsshot < game.DeserializeFloat(game.blocktypes[item.BlockId].BulletsPerShotFloat))
                {
                    NextBullet(game, bulletsshot);
                }

                //recoil
                game.player.position.rotx -= game.rnd.NextFloat() * game.CurrentRecoil();
                game.player.position.roty += game.rnd.NextFloat() * game.CurrentRecoil() * 2 - game.CurrentRecoil();

                PickingEnd(left, right, middle, ispistol);
                return;
            }
            if (ispistol && right)
            {
                PickingEnd(left, right, middle, ispistol);
                return;
            }
            if (pick2count.value > 0)
            {
                if (middle)
                {
                    int newtileX = game.platform.FloatToInt(pick0.Current()[0]);
                    int newtileY = game.platform.FloatToInt(pick0.Current()[1]);
                    int newtileZ = game.platform.FloatToInt(pick0.Current()[2]);
                    if (game.map.IsValidPos(newtileX, newtileZ, newtileY))
                    {
                        int  clonesource  = game.map.GetBlock(newtileX, newtileZ, newtileY);
                        int  clonesource2 = game.d_Data.WhenPlayerPlacesGetsConvertedTo()[clonesource];
                        bool gotoDone     = false;
                        //find this block in another right hand.
                        for (int i = 0; i < 10; i++)
                        {
                            if (game.d_Inventory.RightHand[i] != null &&
                                game.d_Inventory.RightHand[i].ItemClass == Packet_ItemClassEnum.Block &&
                                game.d_Inventory.RightHand[i].BlockId == clonesource2)
                            {
                                game.ActiveMaterial = i;
                                gotoDone            = true;
                            }
                        }
                        if (!gotoDone)
                        {
                            IntRef freehand = game.d_InventoryUtil.FreeHand(game.ActiveMaterial);
                            //find this block in inventory.
                            for (int i = 0; i < game.d_Inventory.ItemsCount; i++)
                            {
                                Packet_PositionItem k = game.d_Inventory.Items[i];
                                if (k == null)
                                {
                                    continue;
                                }
                                if (k.Value_.ItemClass == Packet_ItemClassEnum.Block &&
                                    k.Value_.BlockId == clonesource2)
                                {
                                    //free hand
                                    if (freehand != null)
                                    {
                                        game.WearItem(
                                            game.InventoryPositionMainArea(k.X, k.Y),
                                            game.InventoryPositionMaterialSelector(freehand.value));
                                        break;
                                    }
                                    //try to replace current slot
                                    if (game.d_Inventory.RightHand[game.ActiveMaterial] != null &&
                                        game.d_Inventory.RightHand[game.ActiveMaterial].ItemClass == Packet_ItemClassEnum.Block)
                                    {
                                        game.MoveToInventory(
                                            game.InventoryPositionMaterialSelector(game.ActiveMaterial));
                                        game.WearItem(
                                            game.InventoryPositionMainArea(k.X, k.Y),
                                            game.InventoryPositionMaterialSelector(game.ActiveMaterial));
                                    }
                                }
                            }
                        }
                        string[] sound = game.d_Data.CloneSound()[clonesource];
                        if (sound != null)            // && sound.Length > 0)
                        {
                            game.AudioPlay(sound[0]); //todo sound cycle
                        }
                    }
                }
                if (left || right)
                {
                    BlockPosSide tile = pick0;
                    int          newtileX;
                    int          newtileY;
                    int          newtileZ;
                    if (right)
                    {
                        newtileX = game.platform.FloatToInt(tile.Translated()[0]);
                        newtileY = game.platform.FloatToInt(tile.Translated()[1]);
                        newtileZ = game.platform.FloatToInt(tile.Translated()[2]);
                    }
                    else
                    {
                        newtileX = game.platform.FloatToInt(tile.Current()[0]);
                        newtileY = game.platform.FloatToInt(tile.Current()[1]);
                        newtileZ = game.platform.FloatToInt(tile.Current()[2]);
                    }
                    if (game.map.IsValidPos(newtileX, newtileZ, newtileY))
                    {
                        //Console.WriteLine(". newtile:" + newtile + " type: " + d_Map.GetBlock(newtileX, newtileZ, newtileY));
                        if (!(pick0.blockPos[0] == -1 &&
                              pick0.blockPos[1] == -1 &&
                              pick0.blockPos[2] == -1))
                        {
                            int blocktype;
                            if (left)
                            {
                                blocktype = game.map.GetBlock(newtileX, newtileZ, newtileY);
                            }
                            else
                            {
                                blocktype = ((game.BlockInHand() == null) ? 1 : game.BlockInHand().value);
                            }
                            if (left && blocktype == game.d_Data.BlockIdAdminium())
                            {
                                PickingEnd(left, right, middle, ispistol);
                                return;
                            }
                            string[] sound = left ? game.d_Data.BreakSound()[blocktype] : game.d_Data.BuildSound()[blocktype];
                            if (sound != null)            // && sound.Length > 0)
                            {
                                game.AudioPlay(sound[0]); //todo sound cycle
                            }
                        }
                        //normal attack
                        if (!right)
                        {
                            //attack
                            int posx = newtileX;
                            int posy = newtileZ;
                            int posz = newtileY;
                            game.currentAttackedBlock = Vector3IntRef.Create(posx, posy, posz);
                            if (!game.blockHealth.ContainsKey(posx, posy, posz))
                            {
                                game.blockHealth.Set(posx, posy, posz, game.GetCurrentBlockHealth(posx, posy, posz));
                            }
                            game.blockHealth.Set(posx, posy, posz, game.blockHealth.Get(posx, posy, posz) - game.WeaponAttackStrength());
                            float health = game.GetCurrentBlockHealth(posx, posy, posz);
                            if (health <= 0)
                            {
                                if (game.currentAttackedBlock != null)
                                {
                                    game.blockHealth.Remove(posx, posy, posz);
                                }
                                game.currentAttackedBlock = null;
                                OnPick(game, game.platform.FloatToInt(newtileX), game.platform.FloatToInt(newtileZ), game.platform.FloatToInt(newtileY),
                                       game.platform.FloatToInt(tile.Current()[0]), game.platform.FloatToInt(tile.Current()[2]), game.platform.FloatToInt(tile.Current()[1]),
                                       tile.collisionPos,
                                       right);
                            }
                            PickingEnd(left, right, middle, ispistol);
                            return;
                        }
                        if (!right)
                        {
                            game.particleEffectBlockBreak.StartParticleEffect(newtileX, newtileY, newtileZ);//must be before deletion - gets ground type.
                        }
                        if (!game.map.IsValidPos(newtileX, newtileZ, newtileY))
                        {
                            game.platform.ThrowException("Error in picking - NextBullet()");
                        }
                        OnPick(game, game.platform.FloatToInt(newtileX), game.platform.FloatToInt(newtileZ), game.platform.FloatToInt(newtileY),
                               game.platform.FloatToInt(tile.Current()[0]), game.platform.FloatToInt(tile.Current()[2]), game.platform.FloatToInt(tile.Current()[1]),
                               tile.collisionPos,
                               right);
                        //network.SendSetBlock(new Vector3((int)newtile.X, (int)newtile.Z, (int)newtile.Y),
                        //    right ? BlockSetMode.Create : BlockSetMode.Destroy, (byte)MaterialSlots[activematerial]);
                    }
                }
            }
        }
        PickingEnd(left, right, middle, ispistol);
    }