Allow using F5 to toggle rain in classic mode

This commit is contained in:
UnknownShadow200 2018-07-26 03:16:56 +10:00
parent 8e3f7d8162
commit a50c40f500
31 changed files with 244 additions and 432 deletions

View File

@ -28,7 +28,7 @@ namespace ClassicalSharp.Gui.Screens {
public override void Init() { public override void Init() {
int size = game.Drawer2D.UseBitmappedChat ? 16 : 11; int size = game.Drawer2D.UseBitmappedChat ? 16 : 11;
playerFont = new Font(game.FontName, size); playerFont = new Font(game.FontName, size);
hotbar = game.Mode.MakeHotbar(); hotbar = new HotbarWidget(game);
hotbar.Init(); hotbar.Init();
chat = new ChatScreen(game, this); chat = new ChatScreen(game, this);
chat.Init(); chat.Init();

View File

@ -201,8 +201,6 @@
<Compile Include="MeshBuilder\NormalBuilder.cs" /> <Compile Include="MeshBuilder\NormalBuilder.cs" />
<Compile Include="MeshBuilder\AdvLightingBuilder.cs" /> <Compile Include="MeshBuilder\AdvLightingBuilder.cs" />
<Compile Include="MeshBuilder\TileDrawer.cs" /> <Compile Include="MeshBuilder\TileDrawer.cs" />
<Compile Include="Mode\Creative.cs" />
<Compile Include="Mode\IGameMode.cs" />
<Compile Include="Mode\Survival.cs" /> <Compile Include="Mode\Survival.cs" />
<Compile Include="Network\CPESupport.cs" /> <Compile Include="Network\CPESupport.cs" />
<Compile Include="Network\Protocols\BlockDefs.cs" /> <Compile Include="Network\Protocols\BlockDefs.cs" />

View File

@ -9,7 +9,6 @@ using ClassicalSharp.Entities;
using ClassicalSharp.GraphicsAPI; using ClassicalSharp.GraphicsAPI;
using ClassicalSharp.Gui.Screens; using ClassicalSharp.Gui.Screens;
using ClassicalSharp.Map; using ClassicalSharp.Map;
using ClassicalSharp.Mode;
using ClassicalSharp.Model; using ClassicalSharp.Model;
using ClassicalSharp.Network; using ClassicalSharp.Network;
using ClassicalSharp.Particles; using ClassicalSharp.Particles;
@ -53,10 +52,7 @@ namespace ClassicalSharp {
} else { } else {
Mode = new CreativeGameMode(); Mode = new CreativeGameMode();
} }
#else
Mode = new CreativeGameMode();
#endif #endif
Components.Add(Mode);
Input = new InputHandler(this); Input = new InputHandler(this);
ParticleManager = new ParticleManager(); Components.Add(ParticleManager); ParticleManager = new ParticleManager(); Components.Add(ParticleManager);

View File

@ -10,7 +10,6 @@ using ClassicalSharp.Entities;
using ClassicalSharp.Events; using ClassicalSharp.Events;
using ClassicalSharp.GraphicsAPI; using ClassicalSharp.GraphicsAPI;
using ClassicalSharp.Map; using ClassicalSharp.Map;
using ClassicalSharp.Mode;
using ClassicalSharp.Model; using ClassicalSharp.Model;
using ClassicalSharp.Network; using ClassicalSharp.Network;
using ClassicalSharp.Particles; using ClassicalSharp.Particles;
@ -51,7 +50,6 @@ namespace ClassicalSharp {
public partial class Game { public partial class Game {
public IGraphicsApi Graphics; public IGraphicsApi Graphics;
public IGameMode Mode;
public World World; public World World;
public IServerConnection Server; public IServerConnection Server;

View File

@ -313,7 +313,6 @@ namespace ClassicalSharp {
Graphics.BindIb(Graphics.defaultIb); Graphics.BindIb(Graphics.defaultIb);
accumulator += delta; accumulator += delta;
Vertices = 0; Vertices = 0;
Mode.BeginFrame(delta);
Camera.UpdateMouse(); Camera.UpdateMouse();
if (!Focused && !Gui.ActiveScreen.HandlesAllInput) { if (!Focused && !Gui.ActiveScreen.HandlesAllInput) {
@ -344,7 +343,6 @@ namespace ClassicalSharp {
Gui.Render(delta); Gui.Render(delta);
if (screenshotRequested) TakeScreenshot(); if (screenshotRequested) TakeScreenshot();
Mode.EndFrame(delta);
Graphics.EndFrame(this); Graphics.EndFrame(this);
LimitFPS(); LimitFPS();
} }

View File

@ -4,6 +4,7 @@ using ClassicalSharp.Entities;
using ClassicalSharp.Gui; using ClassicalSharp.Gui;
using ClassicalSharp.Gui.Screens; using ClassicalSharp.Gui.Screens;
using ClassicalSharp.Hotkeys; using ClassicalSharp.Hotkeys;
using ClassicalSharp.Map;
using OpenTK; using OpenTK;
using OpenTK.Input; using OpenTK.Input;
@ -162,7 +163,7 @@ namespace ClassicalSharp {
} else if (key == Keys[KeyBind.Screenshot]) { } else if (key == Keys[KeyBind.Screenshot]) {
game.screenshotRequested = true; game.screenshotRequested = true;
} else if (!game.Gui.ActiveScreen.HandlesKeyDown(key)) { } else if (!game.Gui.ActiveScreen.HandlesKeyDown(key)) {
if (!HandleBuiltinKey(key) && !game.LocalPlayer.HandlesKey(key)) if (!HandleCoreKey(key) && !game.LocalPlayer.HandlesKey(key))
HandleHotkey(key); HandleHotkey(key);
} }
} }
@ -199,17 +200,9 @@ namespace ClassicalSharp {
return true; return true;
} }
bool HandleBuiltinKey(Key key) { bool HandleNonClassicKey(Key key) {
if (key == Keys[KeyBind.HideGui]) { if (key == Keys[KeyBind.HideGui]) {
game.HideGui = !game.HideGui; game.HideGui = !game.HideGui;
} else if (key == Keys[KeyBind.HideFps]) {
game.ShowFPS = !game.ShowFPS;
} else if (key == Keys[KeyBind.Fullscreen]) {
WindowState state = game.window.WindowState;
if (state != WindowState.Minimized) {
game.window.WindowState = state == WindowState.Fullscreen ?
WindowState.Normal : WindowState.Fullscreen;
}
} else if (key == Keys[KeyBind.SmoothCamera]) { } else if (key == Keys[KeyBind.SmoothCamera]) {
Toggle(key, ref game.SmoothCamera, Toggle(key, ref game.SmoothCamera,
" &eSmooth camera is &aenabled", " &eSmooth camera is &aenabled",
@ -224,16 +217,14 @@ namespace ClassicalSharp {
" &eAuto rotate is &cdisabled"); " &eAuto rotate is &cdisabled");
} else if (key == Keys[KeyBind.ThirdPerson]) { } else if (key == Keys[KeyBind.ThirdPerson]) {
game.CycleCamera(); game.CycleCamera();
} else if (key == Keys[KeyBind.ToggleFog]) { } else if (key == game.Mapping(KeyBind.DropBlock)) {
int[] viewDists = game.UseClassicOptions ? classicViewDists : normViewDists; Inventory inv = game.Inventory;
if (game.Input.ShiftDown) { if (inv.CanChangeSelected() && inv.Selected != Block.Air) {
CycleDistanceBackwards(viewDists); // Don't assign Selected directly, because we don't want held block
} else { // switching positions if they already have air in their inventory hotbar.
CycleDistanceForwards(viewDists); inv[inv.SelectedIndex] = Block.Air;
game.Events.RaiseHeldBlockChanged();
} }
} else if ((key == Keys[KeyBind.PauseOrExit] || key == Key.Pause) && !game.Gui.ActiveScreen.HandlesAllInput) {
game.Gui.SetNewScreen(new PauseScreen(game));
} else if (game.Mode.HandlesKeyDown(key)) {
} else if (key == Keys[KeyBind.IDOverlay]) { } else if (key == Keys[KeyBind.IDOverlay]) {
if (game.Gui.overlays.Count > 0) return true; if (game.Gui.overlays.Count > 0) return true;
game.Gui.ShowOverlay(new TexIdsOverlay(game), false); game.Gui.ShowOverlay(new TexIdsOverlay(game), false);
@ -246,6 +237,35 @@ namespace ClassicalSharp {
} }
return true; return true;
} }
bool HandleCoreKey(Key key) {
if (key == Keys[KeyBind.HideFps]) {
game.ShowFPS = !game.ShowFPS;
} else if (key == Keys[KeyBind.Fullscreen]) {
WindowState state = game.window.WindowState;
if (state != WindowState.Minimized) {
game.window.WindowState = state == WindowState.Fullscreen ?
WindowState.Normal : WindowState.Fullscreen;
}
} else if (key == Keys[KeyBind.ToggleFog]) {
int[] viewDists = game.UseClassicOptions ? classicViewDists : normViewDists;
if (game.Input.ShiftDown) {
CycleDistanceBackwards(viewDists);
} else {
CycleDistanceForwards(viewDists);
}
} else if ((key == Keys[KeyBind.PauseOrExit] || key == Key.Pause) && !game.Gui.ActiveScreen.HandlesAllInput) {
game.Gui.SetNewScreen(new PauseScreen(game));
} else if (key == game.Mapping(KeyBind.Inventory) && game.Gui.ActiveScreen == game.Gui.hudScreen) {
game.Gui.SetNewScreen(new InventoryScreen(game));
} else if (key == Key.F5 && game.ClassicMode) {
Weather weather = game.World.Env.Weather == Weather.Sunny ? Weather.Rainy : Weather.Sunny;
game.World.Env.SetWeather(weather);
} else if (!game.ClassicMode) {
return HandleNonClassicKey(key);
}
return true;
}
void Toggle(Key key, ref bool target, string enableMsg, string disableMsg) { void Toggle(Key key, ref bool target, string enableMsg, string disableMsg) {
target = !target; target = !target;

View File

@ -10,6 +10,9 @@ namespace ClassicalSharp {
void IGameComponent.Init(Game game) { void IGameComponent.Init(Game game) {
this.game = game; this.game = game;
Reset(game); Reset(game);
this[0] = Block.Stone; this[1] = Block.Cobblestone; this[2] = Block.Brick;
this[3] = Block.Dirt; this[4] = Block.Wood; this[5] = Block.Log;
this[6] = Block.Leaves; this[7] = Block.Grass; this[8] = Block.Slab;
} }
public void Reset(Game game) { public void Reset(Game game) {

View File

@ -36,15 +36,18 @@ namespace ClassicalSharp {
if (game.Gui.ActiveScreen.HandlesAllInput || !inv.CanPick) return; if (game.Gui.ActiveScreen.HandlesAllInput || !inv.CanPick) return;
if (left) { if (left) {
if (game.Mode.PickingLeft()) return; // always play delete animations, even if we aren't picking a block.
game.HeldBlockRenderer.ClickAnim(true);
Vector3I pos = game.SelectedPos.BlockPos; Vector3I pos = game.SelectedPos.BlockPos;
if (!game.SelectedPos.Valid || !game.World.IsValidPos(pos)) return; if (!game.SelectedPos.Valid || !game.World.IsValidPos(pos)) return;
BlockID old = game.World.GetBlock(pos); BlockID old = game.World.GetBlock(pos);
if (BlockInfo.Draw[old] == DrawType.Gas || !BlockInfo.CanDelete[old]) return; if (BlockInfo.Draw[old] == DrawType.Gas || !BlockInfo.CanDelete[old]) return;
game.Mode.PickLeft(old);
game.UpdateBlock(pos.X, pos.Y, pos.Z, Block.Air);
game.UserEvents.RaiseBlockChanged(pos, old, Block.Air);
} else if (right) { } else if (right) {
if (game.Mode.PickingRight()) return;
Vector3I pos = game.SelectedPos.TranslatedPos; Vector3I pos = game.SelectedPos.TranslatedPos;
if (!game.SelectedPos.Valid || !game.World.IsValidPos(pos)) return; if (!game.SelectedPos.Valid || !game.World.IsValidPos(pos)) return;
@ -58,13 +61,34 @@ namespace ClassicalSharp {
if (BlockInfo.Draw[block] == DrawType.Gas && BlockInfo.Draw[old] != DrawType.Gas) return; if (BlockInfo.Draw[block] == DrawType.Gas && BlockInfo.Draw[old] != DrawType.Gas) return;
if (!PickingHandler.CheckIsFree(game, block)) return; if (!PickingHandler.CheckIsFree(game, block)) return;
game.Mode.PickRight(old, block); game.UpdateBlock(pos.X, pos.Y, pos.Z, block);
game.UserEvents.RaiseBlockChanged(pos, old, block);
} else if (middle) { } else if (middle) {
Vector3I pos = game.SelectedPos.BlockPos; Vector3I pos = game.SelectedPos.BlockPos;
if (!game.SelectedPos.Valid || !game.World.IsValidPos(pos)) return; if (!game.SelectedPos.Valid || !game.World.IsValidPos(pos)) return;
BlockID cur = game.World.GetBlock(pos);
BlockID old = game.World.GetBlock(pos); if (BlockInfo.Draw[cur] == DrawType.Gas) return;
game.Mode.PickMiddle(old); if (!(BlockInfo.CanPlace[cur] || BlockInfo.CanDelete[cur])) return;
if (!inv.CanChangeSelected() || inv.Selected == cur) return;
// Is the currently selected block an empty slot
if (inv[inv.SelectedIndex] == Block.Air) {
inv.Selected = cur; return;
}
// Try to replace same block
for (int i = 0; i < Inventory.BlocksPerRow; i++) {
if (inv[i] != cur) continue;
inv.SelectedIndex = i; return;
}
// Try to replace empty slots
for (int i = 0; i < Inventory.BlocksPerRow; i++) {
if (inv[i] != Block.Air) continue;
inv[i] = cur;
inv.SelectedIndex = i; return;
}
// Finally, replace the currently selected block.
inv.Selected = cur;
} }
} }
@ -117,7 +141,7 @@ namespace ClassicalSharp {
} }
// exclude exact map boundaries, otherwise player can get stuck outside map // exclude exact map boundaries, otherwise player can get stuck outside map
bool validPos = bool validPos =
adjPos.X > 0 && adjPos.Y >= 0 && adjPos.Z > 0 && adjPos.X > 0 && adjPos.Y >= 0 && adjPos.Z > 0 &&
adjPos.X < game.World.Width && adjPos.Z < game.World.Length; adjPos.X < game.World.Width && adjPos.Z < game.World.Length;
if (!validPos) return false; if (!validPos) return false;
@ -133,7 +157,7 @@ namespace ClassicalSharp {
p.SetLocation(update, false); p.SetLocation(update, false);
return true; return true;
} }
static Predicate<BlockID> touchesAnySolid = IsSolidCollide; static Predicate<BlockID> touchesAnySolid = IsSolidCollide;
static bool IsSolidCollide(BlockID b) { return BlockInfo.Collide[b] == CollideType.Solid; } static bool IsSolidCollide(BlockID b) { return BlockInfo.Collide[b] == CollideType.Solid; }

View File

@ -1,101 +0,0 @@
// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
using System;
using ClassicalSharp.Gui.Screens;
using ClassicalSharp.Gui;
using ClassicalSharp.Gui.Widgets;
using OpenTK.Input;
using BlockID = System.UInt16;
namespace ClassicalSharp.Mode {
public sealed class CreativeGameMode : IGameMode {
Game game;
public bool HandlesKeyDown(Key key) {
if (key == game.Mapping(KeyBind.Inventory) && game.Gui.ActiveScreen == game.Gui.hudScreen) {
game.Gui.SetNewScreen(new InventoryScreen(game));
return true;
} else if (key == game.Mapping(KeyBind.DropBlock) && !game.ClassicMode) {
Inventory inv = game.Inventory;
if (inv.CanChangeSelected() && inv.Selected != Block.Air) {
// Don't assign Selected directly, because we don't want held block
// switching positions if they already have air in their inventory hotbar.
inv[inv.SelectedIndex] = Block.Air;
game.Events.RaiseHeldBlockChanged();
}
return true;
}
return false;
}
public bool PickingLeft() {
// always play delete animations, even if we aren't picking a block.
game.HeldBlockRenderer.ClickAnim(true);
return false;
}
public bool PickingRight() { return false; }
public void PickLeft(BlockID old) {
Vector3I pos = game.SelectedPos.BlockPos;
game.UpdateBlock(pos.X, pos.Y, pos.Z, Block.Air);
game.UserEvents.RaiseBlockChanged(pos, old, Block.Air);
}
public void PickMiddle(BlockID old) {
Inventory inv = game.Inventory;
if (BlockInfo.Draw[old] == DrawType.Gas) return;
if (!(BlockInfo.CanPlace[old] || BlockInfo.CanDelete[old])) return;
if (!inv.CanChangeSelected() || inv.Selected == old) return;
// Is the currently selected block an empty slot
if (inv[inv.SelectedIndex] == Block.Air) {
inv.Selected = old; return;
}
// Try to replace same block
for (int i = 0; i < Inventory.BlocksPerRow; i++) {
if (inv[i] != old) continue;
inv.SelectedIndex = i; return;
}
// Try to replace empty slots
for (int i = 0; i < Inventory.BlocksPerRow; i++) {
if (inv[i] != Block.Air) continue;
inv[i] = old;
inv.SelectedIndex = i; return;
}
// Finally, replace the currently selected block.
inv.Selected = old;
}
public void PickRight(BlockID old, BlockID block) {
Vector3I pos = game.SelectedPos.TranslatedPos;
game.UpdateBlock(pos.X, pos.Y, pos.Z, block);
game.UserEvents.RaiseBlockChanged(pos, old, block);
}
public Widget MakeHotbar() { return new HotbarWidget(game); }
void IGameComponent.OnNewMapLoaded(Game game) { }
void IGameComponent.Init(Game game) {
this.game = game;
Inventory inv = game.Inventory;
inv[0] = Block.Stone; inv[1] = Block.Cobblestone; inv[2] = Block.Brick;
inv[3] = Block.Dirt; inv[4] = Block.Wood; inv[5] = Block.Log;
inv[6] = Block.Leaves; inv[7] = Block.Grass; inv[8] = Block.Slab;
}
void IGameComponent.Ready(Game game) { }
void IGameComponent.Reset(Game game) { }
void IGameComponent.OnNewMap(Game game) { }
void IDisposable.Dispose() { }
public void BeginFrame(double delta) { }
public void EndFrame(double delta) { }
}
}

View File

@ -1,21 +0,0 @@
// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
using System;
using ClassicalSharp.Gui;
using OpenTK.Input;
using BlockID = System.UInt16;
namespace ClassicalSharp.Mode {
public interface IGameMode : IGameComponent {
bool HandlesKeyDown(Key key);
bool PickingLeft();
bool PickingRight();
void PickLeft(BlockID old);
void PickMiddle(BlockID old);
void PickRight(BlockID old, BlockID block);
Widget MakeHotbar();
void BeginFrame(double delta);
void EndFrame(double delta);
}
}

View File

@ -91,7 +91,7 @@ namespace Launcher {
void LoadFont() { void LoadFont() {
Options.Load(); Options.Load();
FontName = Options.Get("gui-fontname", "Arial");; FontName = Options.Get("gui-fontname", "Arial");
try { try {
using (Font f = new Font(FontName, 16)) { } using (Font f = new Font(FontName, 16)) { }
} catch (Exception) { } catch (Exception) {

View File

@ -1,6 +1,5 @@
#include "Animations.h" #include "Animations.h"
#include "ExtMath.h" #include "ExtMath.h"
#include "Random.h"
#include "TerrainAtlas.h" #include "TerrainAtlas.h"
#include "Platform.h" #include "Platform.h"
#include "Event.h" #include "Event.h"

View File

@ -1,5 +1,4 @@
#include "BlockPhysics.h" #include "BlockPhysics.h"
#include "Random.h"
#include "World.h" #include "World.h"
#include "Constants.h" #include "Constants.h"
#include "Funcs.h" #include "Funcs.h"

View File

@ -8,7 +8,7 @@
#include "GraphicsAPI.h" #include "GraphicsAPI.h"
#include "ErrorHandler.h" #include "ErrorHandler.h"
#include "Drawer.h" #include "Drawer.h"
#include "Random.h" #include "ExtMath.h"
#include "ChunkUpdater.h" #include "ChunkUpdater.h"
#include "BlockID.h" #include "BlockID.h"
#include "Block.h" #include "Block.h"

View File

@ -202,7 +202,6 @@
<ClInclude Include="Event.h" /> <ClInclude Include="Event.h" />
<ClInclude Include="Formats.h" /> <ClInclude Include="Formats.h" />
<ClInclude Include="Deflate.h" /> <ClInclude Include="Deflate.h" />
<ClInclude Include="GameMode.h" />
<ClInclude Include="Gui.h" /> <ClInclude Include="Gui.h" />
<ClInclude Include="HeldBlockRenderer.h" /> <ClInclude Include="HeldBlockRenderer.h" />
<ClInclude Include="IModel.h" /> <ClInclude Include="IModel.h" />
@ -238,7 +237,6 @@
<ClInclude Include="GraphicsAPI.h" /> <ClInclude Include="GraphicsAPI.h" />
<ClInclude Include="GraphicsCommon.h" /> <ClInclude Include="GraphicsCommon.h" />
<ClInclude Include="Platform.h" /> <ClInclude Include="Platform.h" />
<ClInclude Include="Random.h" />
<ClInclude Include="String.h" /> <ClInclude Include="String.h" />
<ClInclude Include="Texture.h" /> <ClInclude Include="Texture.h" />
<ClInclude Include="Typedefs.h" /> <ClInclude Include="Typedefs.h" />
@ -268,7 +266,6 @@
<ClCompile Include="ExtMath.c" /> <ClCompile Include="ExtMath.c" />
<ClCompile Include="Formats.c" /> <ClCompile Include="Formats.c" />
<ClCompile Include="Game.c" /> <ClCompile Include="Game.c" />
<ClCompile Include="GameMode.c" />
<ClCompile Include="Gui.c" /> <ClCompile Include="Gui.c" />
<ClCompile Include="HeldBlockRenderer.c" /> <ClCompile Include="HeldBlockRenderer.c" />
<ClCompile Include="InputHandler.c" /> <ClCompile Include="InputHandler.c" />
@ -297,7 +294,6 @@
<ClCompile Include="NixErrorHandler.c" /> <ClCompile Include="NixErrorHandler.c" />
<ClCompile Include="NixPlatform.c" /> <ClCompile Include="NixPlatform.c" />
<ClCompile Include="Program.c" /> <ClCompile Include="Program.c" />
<ClCompile Include="Random.c" />
<ClCompile Include="Screens.c" /> <ClCompile Include="Screens.c" />
<ClCompile Include="SelectionBox.c" /> <ClCompile Include="SelectionBox.c" />
<ClCompile Include="ServerConnection.c" /> <ClCompile Include="ServerConnection.c" />

View File

@ -222,9 +222,6 @@
<ClInclude Include="Constants.h"> <ClInclude Include="Constants.h">
<Filter>Header Files\Defines</Filter> <Filter>Header Files\Defines</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Random.h">
<Filter>Header Files\Math</Filter>
</ClInclude>
<ClInclude Include="2DStructs.h"> <ClInclude Include="2DStructs.h">
<Filter>Header Files\2D\Utils</Filter> <Filter>Header Files\2D\Utils</Filter>
</ClInclude> </ClInclude>
@ -291,9 +288,6 @@
<ClInclude Include="Screens.h"> <ClInclude Include="Screens.h">
<Filter>Header Files\2D</Filter> <Filter>Header Files\2D</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="GameMode.h">
<Filter>Header Files\Game</Filter>
</ClInclude>
<ClInclude Include="Chat.h"> <ClInclude Include="Chat.h">
<Filter>Header Files\Game</Filter> <Filter>Header Files\Game</Filter>
</ClInclude> </ClInclude>
@ -392,9 +386,6 @@
<ClCompile Include="MapRenderer.c"> <ClCompile Include="MapRenderer.c">
<Filter>Source Files\Rendering\Map</Filter> <Filter>Source Files\Rendering\Map</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Random.c">
<Filter>Source Files\Math</Filter>
</ClCompile>
<ClCompile Include="2DStructs.c"> <ClCompile Include="2DStructs.c">
<Filter>Source Files\2D\Utils</Filter> <Filter>Source Files\2D\Utils</Filter>
</ClCompile> </ClCompile>
@ -482,9 +473,6 @@
<ClCompile Include="Screens.c"> <ClCompile Include="Screens.c">
<Filter>Source Files\2D</Filter> <Filter>Source Files\2D</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="GameMode.c">
<Filter>Source Files\Game</Filter>
</ClCompile>
<ClCompile Include="Chat.c"> <ClCompile Include="Chat.c">
<Filter>Source Files\Game</Filter> <Filter>Source Files\Game</Filter>
</ClCompile> </ClCompile>

View File

@ -1,4 +1,6 @@
#include "ExtMath.h" #include "ExtMath.h"
#include "Platform.h"
#include "Utils.h"
#include <math.h> #include <math.h>
/* TODO: Replace with own functions that don't rely on stdlib */ /* TODO: Replace with own functions that don't rely on stdlib */
@ -105,3 +107,48 @@ Real64 Math_FastExp(Real64 x) {
/* now need to work out exp(f) */ /* now need to work out exp(f) */
} }
/*########################################################################################################################*
*--------------------------------------------------Random number generator------------------------------------------------*
*#########################################################################################################################*/
#define RND_VALUE (0x5DEECE66DULL)
#define RND_MASK ((1ULL << 48) - 1)
void Random_Init(Random* seed, Int32 seedInit) { Random_SetSeed(seed, seedInit); }
void Random_InitFromCurrentTime(Random* rnd) {
DateTime now; Platform_CurrentUTCTime(&now);
Int64 totalMs = DateTime_TotalMs(&now);
Random_Init(rnd, (Int32)totalMs);
}
void Random_SetSeed(Random* seed, Int32 seedInit) {
*seed = (seedInit ^ RND_VALUE) & RND_MASK;
}
Int32 Random_Range(Random* seed, Int32 min, Int32 max) {
return min + Random_Next(seed, max - min);
}
Int32 Random_Next(Random* seed, Int32 n) {
if ((n & -n) == n) { /* i.e., n is a power of 2 */
*seed = (*seed * RND_VALUE + 0xBLL) & RND_MASK;
Int64 raw = (Int64)(*seed >> (48 - 31));
return (Int32)((n * raw) >> 31);
}
Int32 bits, val;
do {
*seed = (*seed * RND_VALUE + 0xBLL) & RND_MASK;
bits = (Int32)(*seed >> (48 - 31));
val = bits % n;
} while (bits - val + (n - 1) < 0);
return val;
}
Real32 Random_Float(Random* seed) {
*seed = (*seed * RND_VALUE + 0xBLL) & RND_MASK;
Int32 raw = (Int32)(*seed >> (48 - 24));
return raw / ((Real32)(1 << 24));
}

View File

@ -1,7 +1,8 @@
#ifndef CC_MATH_H #ifndef CC_MATH_H
#define CC_MATH_H #define CC_MATH_H
#include "Typedefs.h" #include "Typedefs.h"
/* Simple math functions and constants. /* Simple math functions and constants. Also implements a RNG algorithm, based on
Java's implementation from https://docs.oracle.com/javase/7/docs/api/java/util/Random.html
Copyright 2017 ClassicalSharp | Licensed under BSD-3 Copyright 2017 ClassicalSharp | Licensed under BSD-3
*/ */
@ -40,8 +41,16 @@ Real32 Math_LerpAngle(Real32 leftAngle, Real32 rightAngle, Real32 t);
Int32 Math_NextPowOf2(Int32 value); Int32 Math_NextPowOf2(Int32 value);
bool Math_IsPowOf2(Int32 value); bool Math_IsPowOf2(Int32 value);
#define Math_Clamp(val, min, max) val = val < (min) ? (min) : val; val = val > (max) ? (max) : val;
#define Math_Clamp(value, min, max)\
value = value < (min) ? (min) : value;\ typedef UInt64 Random;
value = value > (max) ? (max) : value; void Random_Init(Random* rnd, Int32 seed);
void Random_InitFromCurrentTime(Random* rnd);
void Random_SetSeed(Random* rnd, Int32 seed);
/* Returns integer from min inclusive to max exclusive */
Int32 Random_Range(Random* rnd, Int32 min, Int32 max);
/* Returns integer from 0 inclusive to n exclusive */
Int32 Random_Next(Random* rnd, Int32 n);
/* Returns real from 0 inclusive to 1 exclusive */
Real32 Random_Float(Random* rnd);
#endif #endif

View File

@ -16,7 +16,6 @@
#include "Entity.h" #include "Entity.h"
#include "Chat.h" #include "Chat.h"
#include "Platform.h" #include "Platform.h"
#include "GameMode.h"
#include "Drawer2D.h" #include "Drawer2D.h"
#include "ModelCache.h" #include "ModelCache.h"
#include "Particle.h" #include "Particle.h"
@ -443,7 +442,6 @@ void Game_Load(void) {
Entities_Init(); Entities_Init();
TextureCache_Init(); TextureCache_Init();
/* TODO: Survival vs Creative game mode */ /* TODO: Survival vs Creative game mode */
GameMode_MakeComponent(&comp); Game_AddComponent(&comp);
InputHandler_Init(); InputHandler_Init();
Particles_MakeComponent(&comp); Game_AddComponent(&comp); Particles_MakeComponent(&comp); Game_AddComponent(&comp);
@ -687,7 +685,6 @@ static void Game_RenderFrame(Real64 delta) {
Gfx_BindIb(GfxCommon_defaultIb); Gfx_BindIb(GfxCommon_defaultIb);
Game_Accumulator += delta; Game_Accumulator += delta;
Game_Vertices = 0; Game_Vertices = 0;
GameMode_BeginFrame(delta);
Camera_Active->UpdateMouse(); Camera_Active->UpdateMouse();
if (!Window_Focused && !Gui_GetActiveScreen()->HandlesAllInput) { if (!Window_Focused && !Gui_GetActiveScreen()->HandlesAllInput) {
@ -720,7 +717,6 @@ static void Game_RenderFrame(Real64 delta) {
Gui_RenderGui(delta); Gui_RenderGui(delta);
if (Game_ScreenshotRequested) Game_TakeScreenshot(); if (Game_ScreenshotRequested) Game_TakeScreenshot();
GameMode_EndFrame(delta);
Gfx_EndFrame(); Gfx_EndFrame();
Game_LimitFPS(); Game_LimitFPS();
} }

View File

@ -1,89 +0,0 @@
#include "GameMode.h"
#include "Inventory.h"
#include "Widgets.h"
#include "Game.h"
#include "Screens.h"
#include "Block.h"
#include "Event.h"
#include "HeldBlockRenderer.h"
#include "GameStructs.h"
void GameMode_Init(void) {
BlockID* inv = Inventory_Table;
inv[0] = BLOCK_STONE; inv[1] = BLOCK_COBBLE; inv[2] = BLOCK_BRICK;
inv[3] = BLOCK_DIRT; inv[4] = BLOCK_WOOD; inv[5] = BLOCK_LOG;
inv[6] = BLOCK_LEAVES; inv[7] = BLOCK_GRASS; inv[8] = BLOCK_SLAB;
}
void GameMode_MakeComponent(struct IGameComponent* comp) {
comp->Init = GameMode_Init;
}
bool GameMode_HandlesKeyDown(Key key) {
struct Screen* activeScreen = Gui_GetActiveScreen();
if (key == KeyBind_Get(KeyBind_Inventory) && activeScreen == Gui_HUD) {
Gui_ReplaceActive(InventoryScreen_MakeInstance());
return true;
} else if (key == KeyBind_Get(KeyBind_DropBlock) && !Game_ClassicMode) {
if (Inventory_CanChangeSelected() && Inventory_SelectedBlock != BLOCK_AIR) {
/* Don't assign SelectedIndex directly, because we don't want held block
switching positions if they already have air in their inventory hotbar. */
Inventory_Set(Inventory_SelectedIndex, BLOCK_AIR);
Event_RaiseVoid(&UserEvents_HeldBlockChanged);
}
return true;
}
return false;
}
bool GameMode_PickingLeft(void) {
/* always play delete animations, even if we aren't picking a block */
HeldBlockRenderer_ClickAnim(true);
return false;
}
bool GameMode_PickingRight(void) { return false; }
void GameMode_PickLeft(BlockID old) {
Vector3I pos = Game_SelectedPos.BlockPos;
Game_UpdateBlock(pos.X, pos.Y, pos.Z, BLOCK_AIR);
Event_RaiseBlock(&UserEvents_BlockChanged, pos, old, BLOCK_AIR);
}
void GameMode_PickMiddle(BlockID old) {
if (Block_Draw[old] == DRAW_GAS) return;
if (!(Block_CanPlace[old] || Block_CanDelete[old])) return;
if (!Inventory_CanChangeSelected() || Inventory_SelectedBlock == old) return;
UInt32 i;
/* Is the currently selected block an empty slot */
if (Inventory_Get(Inventory_SelectedIndex) == BLOCK_AIR) {
Inventory_SetSelectedBlock(old); return;
}
/* Try to replace same block */
for (i = 0; i < INVENTORY_BLOCKS_PER_HOTBAR; i++) {
if (Inventory_Get(i) != old) continue;
Inventory_SetSelectedIndex(i); return;
}
/* Try to replace empty slots */
for (i = 0; i < INVENTORY_BLOCKS_PER_HOTBAR; i++) {
if (Inventory_Get(i) != BLOCK_AIR) continue;
Inventory_Set(i, old);
Inventory_SetSelectedIndex(i); return;
}
/* Finally, replace the currently selected block */
Inventory_SetSelectedBlock(old);
}
void GameMode_PickRight(BlockID old, BlockID block) {
Vector3I pos = Game_SelectedPos.TranslatedPos;
Game_UpdateBlock(pos.X, pos.Y, pos.Z, block);
Event_RaiseBlock(&UserEvents_BlockChanged, pos, old, block);
}
struct HotbarWidget GameMode_Hotbar;
struct HotbarWidget* GameMode_MakeHotbar(void) { return &GameMode_Hotbar; }
void GameMode_BeginFrame(Real64 delta) { }
void GameMode_EndFrame(Real64 delta) { }

View File

@ -1,21 +0,0 @@
#ifndef CC_GAMEMODE_H
#define CC_GAMEMODE_H
#include "Input.h"
/* Implements behaviour specific for creative / survival game modes.
Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
*/
struct IGameComponent;
struct HorbarWidget;
void GameMode_MakeComponent(struct IGameComponent* comp);
bool GameMode_HandlesKeyDown(Key key);
bool GameMode_PickingLeft(void);
bool GameMode_PickingRight(void);
void GameMode_PickLeft(BlockID old);
void GameMode_PickMiddle(BlockID old);
void GameMode_PickRight(BlockID old, BlockID block);
struct HotbarWidget* GameMode_MakeHotbar(void);
void GameMode_BeginFrame(Real64 delta);
void GameMode_EndFrame(Real64 delta);
#endif

View File

@ -21,12 +21,12 @@ void GfxCommon_UpdateDynamicVb_Lines(GfxResourceID vb, void* vertices, Int32 vCo
This method also replaces the dynamic vertex buffer's data first with the given vertices before drawing. */ This method also replaces the dynamic vertex buffer's data first with the given vertices before drawing. */
void GfxCommon_UpdateDynamicVb_IndexedTris(GfxResourceID vb, void* vertices, Int32 vCount); void GfxCommon_UpdateDynamicVb_IndexedTris(GfxResourceID vb, void* vertices, Int32 vCount);
GfxResourceID GfxCommon_quadVb; GfxResourceID GfxCommon_quadVb, GfxCommon_texVb;
void GfxCommon_Draw2DFlat(Int32 x, Int32 y, Int32 width, Int32 height, PackedCol col); void GfxCommon_Draw2DFlat(Int32 x, Int32 y, Int32 width, Int32 height, PackedCol col);
void GfxCommon_Draw2DGradient(Int32 x, Int32 y, Int32 width, Int32 height, PackedCol top, PackedCol bottom); void GfxCommon_Draw2DGradient(Int32 x, Int32 y, Int32 width, Int32 height, PackedCol top, PackedCol bottom);
GfxResourceID GfxCommon_texVb;
void GfxCommon_Draw2DTexture(struct Texture* tex, PackedCol col); void GfxCommon_Draw2DTexture(struct Texture* tex, PackedCol col);
void GfxCommon_Make2DQuad(struct Texture* tex, PackedCol col, VertexP3fT2fC4b** vertices); void GfxCommon_Make2DQuad(struct Texture* tex, PackedCol col, VertexP3fT2fC4b** vertices);
void GfxCommon_Mode2D(Int32 width, Int32 height); void GfxCommon_Mode2D(Int32 width, Int32 height);
void GfxCommon_Mode3D(void); void GfxCommon_Mode3D(void);

View File

@ -1,6 +1,7 @@
#include "InputHandler.h" #include "InputHandler.h"
#include "Utils.h" #include "Utils.h"
#include "ServerConnection.h" #include "ServerConnection.h"
#include "HeldBlockRenderer.h"
#include "Game.h" #include "Game.h"
#include "Platform.h" #include "Platform.h"
#include "ExtMath.h" #include "ExtMath.h"
@ -8,7 +9,6 @@
#include "Inventory.h" #include "Inventory.h"
#include "World.h" #include "World.h"
#include "Event.h" #include "Event.h"
#include "GameMode.h"
#include "Window.h" #include "Window.h"
#include "Entity.h" #include "Entity.h"
#include "Chat.h" #include "Chat.h"
@ -139,44 +139,18 @@ static bool InputHandler_DoFovZoom(Real32 deltaPrecise) {
return InputHandler_SetFOV((Int32)input_fovIndex, true); return InputHandler_SetFOV((Int32)input_fovIndex, true);
} }
static bool InputHandler_HandleCoreKey(Key key) { static bool InputHandler_HandleNonClassicKey(Key key) {
if (key == KeyBind_Get(KeyBind_HideGui)) { if (key == KeyBind_Get(KeyBind_HideGui)) {
Game_HideGui = !Game_HideGui; Game_HideGui = !Game_HideGui;
} else if (key == KeyBind_Get(KeyBind_HideFps)) {
Game_ShowFPS = !Game_ShowFPS;
} else if (key == KeyBind_Get(KeyBind_Fullscreen)) {
UInt8 state = Window_GetWindowState();
if (state != WINDOW_STATE_MAXIMISED) {
bool fullscreen = state == WINDOW_STATE_FULLSCREEN;
Window_SetWindowState(fullscreen ? WINDOW_STATE_NORMAL : WINDOW_STATE_FULLSCREEN);
}
} else if (key == KeyBind_Get(KeyBind_SmoothCamera)) {
InputHandler_Toggle(key, &Game_SmoothCamera,
" &eSmooth camera is &aenabled",
" &eSmooth camera is &cdisabled");
} else if (key == KeyBind_Get(KeyBind_AxisLines)) {
InputHandler_Toggle(key, &Game_ShowAxisLines,
" &eAxis lines (&4X&e, &2Y&e, &1Z&e) now show",
" &eAxis lines no longer show");
} else if (key == KeyBind_Get(KeyBind_Autorotate)) {
InputHandler_Toggle(key, &Game_AutoRotate,
" &eAuto rotate is &aenabled",
" &eAuto rotate is &cdisabled");
} else if (key == KeyBind_Get(KeyBind_ThirdPerson)) { } else if (key == KeyBind_Get(KeyBind_ThirdPerson)) {
Camera_CycleActive(); Camera_CycleActive();
} else if (key == KeyBind_Get(KeyBind_ToggleFog)) { } else if (key == KeyBind_Get(KeyBind_DropBlock)) {
Int32* viewDists = Game_UseClassicOptions ? input_classicViewDists : input_normViewDists; if (Inventory_CanChangeSelected() && Inventory_SelectedBlock != BLOCK_AIR) {
Int32 count = Game_UseClassicOptions ? Array_Elems(input_classicViewDists) : Array_Elems(input_normViewDists); /* Don't assign SelectedIndex directly, because we don't want held block
switching positions if they already have air in their inventory hotbar. */
if (Key_IsShiftPressed()) { Inventory_Set(Inventory_SelectedIndex, BLOCK_AIR);
InputHandler_CycleDistanceBackwards(viewDists, count); Event_RaiseVoid(&UserEvents_HeldBlockChanged);
} else {
InputHandler_CycleDistanceForwards(viewDists, count);
} }
} else if ((key == KeyBind_Get(KeyBind_PauseOrExit) || key == Key_Pause) && !Gui_GetActiveScreen()->HandlesAllInput) {
Gui_FreeActive();
Gui_SetActive(PauseScreen_MakeInstance());
} else if (GameMode_HandlesKeyDown(key)) {
} else if (key == KeyBind_Get(KeyBind_IDOverlay)) { } else if (key == KeyBind_Get(KeyBind_IDOverlay)) {
if (Gui_OverlaysCount > 0) return true; if (Gui_OverlaysCount > 0) return true;
struct Screen* overlay = TexIdsOverlay_MakeInstance(); struct Screen* overlay = TexIdsOverlay_MakeInstance();
@ -191,6 +165,40 @@ static bool InputHandler_HandleCoreKey(Key key) {
return true; return true;
} }
static bool InputHandler_HandleCoreKey(Key key) {
struct Screen* activeScreen = Gui_GetActiveScreen();
if (key == KeyBind_Get(KeyBind_HideFps)) {
Game_ShowFPS = !Game_ShowFPS;
} else if (key == KeyBind_Get(KeyBind_Fullscreen)) {
UInt8 state = Window_GetWindowState();
if (state != WINDOW_STATE_MAXIMISED) {
bool fullscreen = state == WINDOW_STATE_FULLSCREEN;
Window_SetWindowState(fullscreen ? WINDOW_STATE_NORMAL : WINDOW_STATE_FULLSCREEN);
}
} else if (key == KeyBind_Get(KeyBind_ToggleFog)) {
Int32* viewDists = Game_UseClassicOptions ? input_classicViewDists : input_normViewDists;
Int32 count = Game_UseClassicOptions ? Array_Elems(input_classicViewDists) : Array_Elems(input_normViewDists);
if (Key_IsShiftPressed()) {
InputHandler_CycleDistanceBackwards(viewDists, count);
} else {
InputHandler_CycleDistanceForwards(viewDists, count);
}
} else if ((key == KeyBind_Get(KeyBind_PauseOrExit) || key == Key_Pause) && !activeScreen->HandlesAllInput) {
Gui_FreeActive();
Gui_SetActive(PauseScreen_MakeInstance());
} else if (key == KeyBind_Get(KeyBind_Inventory) && activeScreen == Gui_HUD) {
Gui_ReplaceActive(InventoryScreen_MakeInstance());
} else if (key == Key_F5 && Game_ClassicMode) {
Int32 weather = WorldEnv_Weather == WEATHER_SUNNY ? WEATHER_RAINY : WEATHER_SUNNY;
WorldEnv_SetWeather(weather);
} else if (!Game_ClassicMode) {
return InputHandler_HandleNonClassicKey(key);
}
return true;
}
static bool InputHandler_TouchesSolid(BlockID b) { return Block_Collide[b] == COLLIDE_SOLID; } static bool InputHandler_TouchesSolid(BlockID b) { return Block_Collide[b] == COLLIDE_SOLID; }
static bool InputHandler_PushbackPlace(struct AABB* blockBB) { static bool InputHandler_PushbackPlace(struct AABB* blockBB) {
struct Entity* p = &LocalPlayer_Instance.Base; struct Entity* p = &LocalPlayer_Instance.Base;
@ -298,15 +306,18 @@ void InputHandler_PickBlocks(bool cooldown, bool left, bool middle, bool right)
if (Gui_GetActiveScreen()->HandlesAllInput || !Inventory_CanPick) return; if (Gui_GetActiveScreen()->HandlesAllInput || !Inventory_CanPick) return;
if (left) { if (left) {
if (GameMode_PickingLeft()) return; /* always play delete animations, even if we aren't picking a block */
HeldBlockRenderer_ClickAnim(true);
Vector3I pos = Game_SelectedPos.BlockPos; Vector3I pos = Game_SelectedPos.BlockPos;
if (!Game_SelectedPos.Valid || !World_IsValidPos_3I(pos)) return; if (!Game_SelectedPos.Valid || !World_IsValidPos_3I(pos)) return;
BlockID old = World_GetBlock_3I(pos); BlockID old = World_GetBlock_3I(pos);
if (Block_Draw[old] == DRAW_GAS || !Block_CanDelete[old]) return; if (Block_Draw[old] == DRAW_GAS || !Block_CanDelete[old]) return;
GameMode_PickLeft(old);
Game_UpdateBlock(pos.X, pos.Y, pos.Z, BLOCK_AIR);
Event_RaiseBlock(&UserEvents_BlockChanged, pos, old, BLOCK_AIR);
} else if (right) { } else if (right) {
if (GameMode_PickingRight()) return;
Vector3I pos = Game_SelectedPos.TranslatedPos; Vector3I pos = Game_SelectedPos.TranslatedPos;
if (!Game_SelectedPos.Valid || !World_IsValidPos_3I(pos)) return; if (!Game_SelectedPos.Valid || !World_IsValidPos_3I(pos)) return;
@ -317,15 +328,40 @@ void InputHandler_PickBlocks(bool cooldown, bool left, bool middle, bool right)
if (Game_CanPick(old) || !Block_CanPlace[block]) return; if (Game_CanPick(old) || !Block_CanPlace[block]) return;
/* air-ish blocks can only replace over other air-ish blocks */ /* air-ish blocks can only replace over other air-ish blocks */
if (Block_Draw[block] == DRAW_GAS && Block_Draw[old] != DRAW_GAS) return; if (Block_Draw[block] == DRAW_GAS && Block_Draw[old] != DRAW_GAS) return;
if (!InputHandler_CheckIsFree(block)) return; if (!InputHandler_CheckIsFree(block)) return;
GameMode_PickRight(old, block);
Game_UpdateBlock(pos.X, pos.Y, pos.Z, block);
Event_RaiseBlock(&UserEvents_BlockChanged, pos, old, block);
} else if (middle) { } else if (middle) {
Vector3I pos = Game_SelectedPos.BlockPos; Vector3I pos = Game_SelectedPos.BlockPos;
if (!Game_SelectedPos.Valid || !World_IsValidPos_3I(pos)) return; if (!Game_SelectedPos.Valid || !World_IsValidPos_3I(pos)) return;
BlockID old = World_GetBlock_3I(pos); BlockID cur = World_GetBlock_3I(pos);
GameMode_PickMiddle(old); if (Block_Draw[cur] == DRAW_GAS) return;
if (!(Block_CanPlace[cur] || Block_CanDelete[cur])) return;
if (!Inventory_CanChangeSelected() || Inventory_SelectedBlock == cur) return;
UInt32 i;
/* Is the currently selected block an empty slot */
if (Inventory_Get(Inventory_SelectedIndex) == BLOCK_AIR) {
Inventory_SetSelectedBlock(cur); return;
}
/* Try to replace same block */
for (i = 0; i < INVENTORY_BLOCKS_PER_HOTBAR; i++) {
if (Inventory_Get(i) != cur) continue;
Inventory_SetSelectedIndex(i); return;
}
/* Try to replace empty slots */
for (i = 0; i < INVENTORY_BLOCKS_PER_HOTBAR; i++) {
if (Inventory_Get(i) != BLOCK_AIR) continue;
Inventory_Set(i, cur);
Inventory_SetSelectedIndex(i); return;
}
/* Finally, replace the currently selected block */
Inventory_SetSelectedBlock(cur);
} }
} }

View File

@ -95,13 +95,21 @@ void Inventory_Remove(BlockID block) {
} }
} }
static void Inventory_ResetState(void) { static void Inventory_Reset(void) {
Inventory_SetDefaultMapping(); Inventory_SetDefaultMapping();
Inventory_CanChangeHeldBlock = true; Inventory_CanChangeHeldBlock = true;
Inventory_CanPick = true; Inventory_CanPick = true;
} }
void Inventory_MakeComponent(struct IGameComponent* comp) { static void Inventory_Init(void) {
comp->Init = Inventory_ResetState; Inventory_Reset();
comp->Reset = Inventory_ResetState; BlockID* inv = Inventory_Table;
inv[0] = BLOCK_STONE; inv[1] = BLOCK_COBBLE; inv[2] = BLOCK_BRICK;
inv[3] = BLOCK_DIRT; inv[4] = BLOCK_WOOD; inv[5] = BLOCK_LOG;
inv[6] = BLOCK_LEAVES; inv[7] = BLOCK_GRASS; inv[8] = BLOCK_SLAB;
}
void Inventory_MakeComponent(struct IGameComponent* comp) {
comp->Init = Inventory_Init;
comp->Reset = Inventory_Reset;
} }

View File

@ -1,7 +1,7 @@
#ifndef CC_MAP_GEN_H #ifndef CC_MAP_GEN_H
#define CC_MAP_GEN_H #define CC_MAP_GEN_H
#include "String.h" #include "String.h"
#include "Random.h" #include "ExtMath.h"
#include "Vectors.h" #include "Vectors.h"
/* Implements flatgrass map generator, and original classic vanilla map generation (with perlin noise) /* Implements flatgrass map generator, and original classic vanilla map generation (with perlin noise)
Based on: https://github.com/UnknownShadow200/ClassicalSharp/wiki/Minecraft-Classic-map-generation-algorithm Based on: https://github.com/UnknownShadow200/ClassicalSharp/wiki/Minecraft-Classic-map-generation-algorithm
@ -20,16 +20,16 @@ BlockID* Gen_Blocks;
void FlatgrassGen_Generate(void); void FlatgrassGen_Generate(void);
void NotchyGen_Generate(void); void NotchyGen_Generate(void);
#define NOISE_TABLE_SIZE 512 #define NOISE_TABLE_SIZE 512
void ImprovedNoise_Init(UInt8* p, Random* rnd); void ImprovedNoise_Init(UInt8* p, Random* rnd);
Real32 ImprovedNoise_Calc(UInt8* p, Real32 x, Real32 y); Real32 ImprovedNoise_Calc(UInt8* p, Real32 x, Real32 y);
struct OctaveNoise { UInt8 p[8][NOISE_TABLE_SIZE]; Int32 octaves; }; struct OctaveNoise { UInt8 p[8][NOISE_TABLE_SIZE]; Int32 octaves; };
void OctaveNoise_Init(struct OctaveNoise* n, Random* rnd, Int32 octaves); void OctaveNoise_Init(struct OctaveNoise* n, Random* rnd, Int32 octaves);
Real32 OctaveNoise_Calc(struct OctaveNoise* n, Real32 x, Real32 y); Real32 OctaveNoise_Calc(struct OctaveNoise* n, Real32 x, Real32 y);
struct CombinedNoise { struct OctaveNoise noise1, noise2; }; struct CombinedNoise { struct OctaveNoise noise1, noise2; };
void CombinedNoise_Init(struct CombinedNoise* n, Random* rnd, Int32 octaves1, Int32 octaves2); void CombinedNoise_Init(struct CombinedNoise* n, Random* rnd, Int32 octaves1, Int32 octaves2);
Real32 CombinedNoise_Calc(struct CombinedNoise* n, Real32 x, Real32 y); Real32 CombinedNoise_Calc(struct CombinedNoise* n, Real32 x, Real32 y);

View File

@ -18,7 +18,6 @@
#include "Camera.h" #include "Camera.h"
#include "AsyncDownloader.h" #include "AsyncDownloader.h"
#include "Block.h" #include "Block.h"
#include "Random.h"
#include "World.h" #include "World.h"
#include "Formats.h" #include "Formats.h"
#include "ErrorHandler.h" #include "ErrorHandler.h"

View File

@ -7,16 +7,8 @@
*/ */
struct IModel; struct IModel;
struct CachedModel { struct CachedModel { String Name; struct IModel* Instance; };
String Name; /* Name associated with the model, all lowercase. */ struct CachedTexture { String Name; GfxResourceID TexID; };
struct IModel* Instance; /* Pointer to the actual model instance. */
};
struct CachedTexture {
String Name; /* Filename of the texture. */
GfxResourceID TexID; /* Native texture ID. */
};
#if FALSE #if FALSE
public CustomModel[] CustomModels = new CustomModel[256]; public CustomModel[] CustomModels = new CustomModel[256];
#endif #endif

View File

@ -4,7 +4,6 @@
#include "ExtMath.h" #include "ExtMath.h"
#include "Lighting.h" #include "Lighting.h"
#include "Entity.h" #include "Entity.h"
#include "Random.h"
#include "TerrainAtlas.h" #include "TerrainAtlas.h"
#include "GraphicsAPI.h" #include "GraphicsAPI.h"
#include "GraphicsCommon.h" #include "GraphicsCommon.h"

View File

@ -70,7 +70,7 @@ int main(void) {
String title = String_FromConst(PROGRAM_APP_NAME); String title = String_FromConst(PROGRAM_APP_NAME);
String rawArgs = Platform_GetCommandLineArgs(); String rawArgs = Platform_GetCommandLineArgs();
/* NOTE: Make sure to comment this out before pushing a commit */ /* NOTE: Make sure to comment this out before pushing a commit */
rawArgs = String_FromReadonly("UnknownShadow200 fff 127.0.0.1 25566"); //rawArgs = String_FromReadonly("UnknownShadow200 fff 127.0.0.1 25566");
String args[5]; Int32 argsCount = Array_Elems(args); String args[5]; Int32 argsCount = Array_Elems(args);
String_UNSAFE_Split(&rawArgs, ' ', args, &argsCount); String_UNSAFE_Split(&rawArgs, ' ', args, &argsCount);

View File

@ -1,42 +0,0 @@
#include "Random.h"
#include "Platform.h"
#include "Utils.h"
#define RND_VALUE (0x5DEECE66DLL)
#define RND_MASK ((1LL << 48) - 1)
void Random_Init(Random* seed, Int32 seedInit) { Random_SetSeed(seed, seedInit); }
void Random_InitFromCurrentTime(Random* rnd) {
DateTime now; Platform_CurrentUTCTime(&now);
Int64 totalMs = DateTime_TotalMs(&now);
Random_Init(rnd, (Int32)totalMs);
}
void Random_SetSeed(Random* seed, Int32 seedInit) {
*seed = (seedInit ^ RND_VALUE) & RND_MASK;
}
Int32 Random_Range(Random* seed, Int32 min, Int32 max) {
return min + Random_Next(seed, max - min);
}
Int32 Random_Next(Random* seed, Int32 n) {
if ((n & -n) == n) { /* i.e., n is a power of 2 */
*seed = (*seed * RND_VALUE + 0xBLL) & RND_MASK;
Int64 raw = (Int64)((UInt64)(*seed) >> (48 - 31));
return (Int32)((n * raw) >> 31);
}
Int32 bits, val;
do {
*seed = (*seed * RND_VALUE + 0xBLL) & RND_MASK;
bits = (Int32)((UInt64)(*seed) >> (48 - 31));
val = bits % n;
} while (bits - val + (n - 1) < 0);
return val;
}
Real32 Random_Float(Random* seed) {
*seed = (*seed * RND_VALUE + 0xBLL) & RND_MASK;
Int32 raw = (Int32)((UInt64)(*seed) >> (48 - 24));
return raw / ((Real32)(1 << 24));
}

View File

@ -1,19 +0,0 @@
#ifndef CC_RANDOM_H
#define CC_RANDOM_H
#include "Typedefs.h"
/* Implemented a random number generation algorithm.
Based on Java's implementation from https://docs.oracle.com/javase/7/docs/api/java/util/Random.html
Copyright 2014 - 2017 ClassicalSharp | Licensed under BSD-3
*/
typedef Int64 Random;
void Random_Init(Random* rnd, Int32 seed);
void Random_InitFromCurrentTime(Random* rnd);
void Random_SetSeed(Random* rnd, Int32 seed);
/* Returns integer from min inclusive to max exclusive */
Int32 Random_Range(Random* rnd, Int32 min, Int32 max);
/* Returns integer from 0 inclusive to n exclusive */
Int32 Random_Next(Random* rnd, Int32 n);
/* Returns real from 0 inclusive to 1 exclusive */
Real32 Random_Float(Random* rnd);
#endif