Compare commits

..

No commits in common. "master" and "v1.4.1" have entirely different histories.

78 changed files with 7000 additions and 9193 deletions

0
.gitattributes vendored Executable file → Normal file
View file

1
.gitignore vendored Executable file → Normal file
View file

@ -58,3 +58,4 @@ build
.project
# Copyrighted Executables
*.exe

22
Makefile Executable file → Normal file
View file

@ -27,17 +27,14 @@ include $(DEVKITARM)/3ds_rules
# - <libctru folder>/default_icon.png
#---------------------------------------------------------------------------------
TARGET := result/Minicraft3DS
ICON_TARGET := icons-banners/icon
RESULT := result
BUILD := build
SOURCES := source source/minizip
DATA := data
INCLUDES := include
ROMFS := romfs
APP_TITLE := Minicraft 3DS
APP_DESCRIPTION := Originally created by Notch.
APP_AUTHOR := Davideesk/andre111/ElijahZAwesome/tognee
APP_DESCRIPTION := Minicraft was originally created by Markus "Notch" Perrson. Ported to the 3DS by Davideesk.
APP_AUTHOR := Davideesk/andre111/ElijahZAwesome
#---------------------------------------------------------------------------------
# options for code generation
@ -109,7 +106,13 @@ export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
ifeq ($(strip $(ICON)),)
icons := $(wildcard *.png)
export APP_ICON := $(TOPDIR)/$(ICON_TARGET).png
ifneq (,$(findstring $(TARGET).png,$(icons)))
export APP_ICON := $(TOPDIR)/$(TARGET).png
else
ifneq (,$(findstring /icons-banners/icon.png,$(icons)))
export APP_ICON := $(TOPDIR)/icons-banners/icon.png
endif
endif
else
export APP_ICON := $(TOPDIR)/$(ICON)
endif
@ -118,17 +121,12 @@ ifeq ($(strip $(NO_SMDH)),)
export _3DSXFLAGS += --smdh=$(CURDIR)/$(TARGET).smdh
endif
ifneq ($(ROMFS),)
export _3DSXFLAGS += --romfs=$(CURDIR)/$(ROMFS)
endif
.PHONY: $(BUILD) clean all
#---------------------------------------------------------------------------------
all: $(BUILD)
$(BUILD):
@test -d $(RESULT) || mkdir $(RESULT)
@[ -d $@ ] || mkdir -p $@
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
@ -145,7 +143,7 @@ cci: $(TARGET)-strip.elf
@echo "built ... Minicraft3DS.3ds"
#---------------------------------------------------------------------------------
cia: $(TARGET)-strip.elf
@makerom -f cia -o $(TARGET).cia -elf $(TARGET)-strip.elf -rsf icons-banners/$(TARGET).rsf -exefslogo -target t
@makerom -f cia -o $(TARGET).cia -elf $(TARGET)-strip.elf -rsf resources/$(TARGET).rsf -exefslogo -target t
@echo "built ... Minicraft3DS.cia"
#---------------------------------------------------------------------------------
send: $(BUILD)

67
README.md Executable file → Normal file
View file

@ -1,72 +1,33 @@
# Minicraft3DS
3DS Homebrew port of Notch's ludum dare game "Minicraft"
Current Version: Version 1.6.1
----------
# Dependencies:
**Download:**
If you just want to download the game prebuilt check the releases tab in Github:
//TODO
For building the game yourself look below.
----------
**Dependencies:**
For building and installing the dependencies look below.
make by GNUWin32: http://gnuwin32.sourceforge.net/packages/make.htm (Should probably work, you may have to add it to PATH)
ctrulib by smea: https://github.com/smealum/ctrulib
citro3d by fincs: https://github.com/fincs/citro3d
sf2dlib by xerpi: https://github.com/xerpi/sf2dlib
sfillib by xerpi: https://github.com/xerpi/sfillib
zlib: http://www.zlib.net/
zlib: http://www.zlib.net/ (Pst, pro tip: get this from the link below instead! I'ts much easier!)
----------
Then make sure to have libpng and libjpeg installed, I recommend installing them with this: https://github.com/devkitPro/3ds_portlibs
# Building
**Building:**
Check the wiki for an indepth look into dependencies and building, but the synopsis is:
**1. Install devkitARM by devkitPro**
- On Windows download https://sourceforge.net/projects/devkitpro/files/Automated%20Installer/
- And install atleast Minimal System and devkitARM
- This includes make, ctrulib and citro3d
As well as the dependencies, put bannertool, and makerom either in your path or in the directory. Then simply run build.bat and everything will be there.
**2. Install zlib, libjpeg-turbo and libpng**
- Download 3DS-Portlibs: https://github.com/devkitPro/3ds_portlibs
- Run these commands:
# Version
```
make zlib
make install-zlib
make libjpeg-turbo
make libpng
make install
```
**3. Install sf2dlib**
- Download https://github.com/xerpi/sf2dlib
- In the libsf2d directory run these commands:
```
make
make install
```
**4. Install sfillib**
- Download https://github.com/xerpi/sfillib
- In the libsfil directory run these commands:
```
make
make install
```
**5. You can now build Minicraft3DS 3dsx, elf, cia, and 3ds files by running the build.bat file.**
----------
Current Version: Version 1.4
# License
You can do anything with the source code (besides sell it) as long as you give proper credit to the right people.
If you are going to make a mod of this version, be sure to give credit to Markus "Notch" Perrson because he did create the original game after all.
@ -75,4 +36,4 @@ If you are going to make a mod of this version, be sure to give credit to Markus
This source code is subject to a lot of change for better optimization/cleanliness.
Forum thread: //TODO
Forum thread: https://gbatemp.net/threads/release-new-minicraft3ds-fork-v1-4.494947/

13
build.bat Executable file → Normal file
View file

@ -2,16 +2,11 @@
echo Building 3DSX/ELF/SMDH...
make
echo Creating banner...
bannertool makebanner -i icons-banners/banner.png -a icons-banners/audio.wav -o icons-banners/banner.bnr
bannertool.exe makebanner -i icons-banners/banner.png -a icons-banners/audio.wav -o icons-banners/banner.bnr
echo Creating icon...
bannertool makesmdh -s "Minicraft3DS" -l "3DS Homebrew port of Notch's ludum dare game 'Minicraft', updated." -p "Davideesk/Andre111/ElijahZAwesome" -i icons-banners/icon.png -o icons-banners/icon.icn
echo Creating ROMFS...
3dstool -cvtf romfs icons-banners/romfs.bin --romfs-dir romfs/
bannertool.exe makesmdh -s "Minicraft3DS" -l "3DS Homebrew port of Notch's ludum dare game 'Minicraft', updated." -p "Davideesk/Andre111/ElijahZAwesome" -i icons-banners/icon.png -o icons-banners/icon.icn
echo Creating CIA...
makerom -f cia -o result/Minicraft3DS.cia -DAPP_ENCRYPTED=false -rsf icons-banners/Minicraft3DS.rsf -target t -exefslogo -elf result/Minicraft3DS.elf -icon icons-banners/icon.icn -banner icons-banners/banner.bnr -romfs icons-banners/romfs.bin
makerom -f cia -o result/Minicraft3DS.cia -DAPP_ENCRYPTED=false -rsf icons-banners/Minicraft3DS.rsf -target t -exefslogo -elf result/Minicraft3DS.elf -icon icons-banners/icon.icn -banner icons-banners/banner.bnr
echo Creating 3DS/CCI...
makerom -f cci -o result/Minicraft3DS.3ds -DAPP_ENCRYPTED=true -rsf icons-banners/Minicraft3DS.rsf -target t -exefslogo -elf result/Minicraft3DS.elf -icon icons-banners/icon.icn -banner icons-banners/banner.bnr -romfs icons-banners/romfs.bin
echo Cleaning Up...
del /s result\Minicraft3DS.elf
del /s result\Minicraft3DS.smdh
makerom -f cci -o result/Minicraft3DS.3ds -DAPP_ENCRYPTED=true -rsf icons-banners/Minicraft3DS.rsf -target t -exefslogo -elf result/Minicraft3DS.elf -icon icons-banners/icon.icn -banner icons-banners/banner.bnr
pause

View file

@ -1,12 +0,0 @@
#!/bin/bash
cd "`dirname $0`"
echo Building 3DSX/ELF/SMDH...
make
echo Creating banner...
bannertool makebanner -i icons-banners/banner.png -a icons-banners/audio.wav -o icons-banners/banner.bnr
echo Creating icon...
bannertool makesmdh -s "Minicraft3DS" -l "Original game by Notch" -p "Davideesk/Andre111/ElijahZAwesome/tognee" -i icons-banners/icon.png -o icons-banners/icon.icn
echo Creating CIA...
makerom -f cia -o result/Minicraft3DS.cia -DAPP_ENCRYPTED=false -rsf icons-banners/Minicraft3DS.rsf -target t -exefslogo -elf result/Minicraft3DS.elf -icon icons-banners/icon.icn -banner icons-banners/banner.bnr
echo Creating 3DS/CCI...
makerom -f cci -o result/Minicraft3DS.3ds -DAPP_ENCRYPTED=true -rsf icons-banners/Minicraft3DS.rsf -target t -exefslogo -elf result/Minicraft3DS.elf -icon icons-banners/icon.icn -banner icons-banners/banner.bnr

0
data/Font.png Executable file → Normal file
View file

Before

Width:  |  Height:  |  Size: 3.9 KiB

After

Width:  |  Height:  |  Size: 3.9 KiB

0
data/bottombg.png Executable file → Normal file
View file

Before

Width:  |  Height:  |  Size: 6.5 KiB

After

Width:  |  Height:  |  Size: 6.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 41 KiB

BIN
data/icons2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.4 KiB

0
icons-banners/Minicraft3DS.rsf Executable file → Normal file
View file

0
icons-banners/audio.wav Executable file → Normal file
View file

0
icons-banners/banner.png Executable file → Normal file
View file

Before

Width:  |  Height:  |  Size: 6 KiB

After

Width:  |  Height:  |  Size: 6 KiB

0
icons-banners/icon.png Executable file → Normal file
View file

Before

Width:  |  Height:  |  Size: 415 B

After

Width:  |  Height:  |  Size: 415 B

Binary file not shown.

36
source/Crafting.c Executable file → Normal file
View file

@ -1,15 +1,5 @@
#include "Crafting.h"
void cloneRecipeManager(RecipeManager *from, RecipeManager *to) {
//free old manager recipes
free(to->recipes);
//copy over recipes
to->size = from->size;
to->recipes = (Recipe*)malloc(sizeof(Recipe) * to->size);
memcpy(to->recipes, from->recipes, sizeof(Recipe) * to->size);
}
void checkCanCraftRecipes(RecipeManager * rm, Inventory * inv){
int i, j;
for(i = 0; i < rm->size; i++){
@ -87,16 +77,11 @@ Recipe defineRecipe(int item, int amountOrLevel, int numArgs, ...){
}
void initRecipes(){
curPlace = 0;
inHeadRecipes.size = 2;
inHeadRecipes.recipes = (Recipe*)malloc(sizeof(Recipe) * (inHeadRecipes.size));
inHeadRecipes.recipes[0] = defineRecipe(ITEM_WORKBENCH,1,1,ITEM_WOOD,10);
inHeadRecipes.recipes[1] = defineRecipe(ITEM_TORCH,1,2,ITEM_WOOD,1,ITEM_COAL,1);
workbenchRecipes.size = 24;
workbenchRecipes.size = 22;
workbenchRecipes.recipes = (Recipe*)malloc(sizeof(Recipe) * (workbenchRecipes.size));
workbenchRecipes.recipes[0] = defineRecipe(ITEM_WORKBENCH,1,1,ITEM_WOOD,10);
workbenchRecipes.recipes[0] = defineRecipe(ITEM_WORKBENCH,1,1,ITEM_WOOD,20);
workbenchRecipes.recipes[1] = defineRecipe(ITEM_FURNACE,1,1,ITEM_STONE,20);
workbenchRecipes.recipes[2] = defineRecipe(ITEM_OVEN,1,1,ITEM_STONE,20);
workbenchRecipes.recipes[3] = defineRecipe(ITEM_CHEST,1,1,ITEM_WOOD,20);
@ -118,10 +103,8 @@ void initRecipes(){
workbenchRecipes.recipes[19] = defineRecipe(ITEM_ARROW_STONE,1,3,ITEM_WOOD,1,ITEM_STONE,1,ITEM_STRING,1);
workbenchRecipes.recipes[20] = defineRecipe(ITEM_WALL_WOOD,1,1,ITEM_WOOD,4);
workbenchRecipes.recipes[21] = defineRecipe(ITEM_WALL_STONE,1,1,ITEM_STONE,4);
workbenchRecipes.recipes[22] = defineRecipe(ITEM_FISHING_ROD,1,2,ITEM_WOOD,10,ITEM_STRING,3);
workbenchRecipes.recipes[23] = defineRecipe(ITEM_TORCH,1,2,ITEM_WOOD,1,ITEM_COAL,1);
anvilRecipes.size = 19;
anvilRecipes.size = 18;
anvilRecipes.recipes = (Recipe*)malloc(sizeof(Recipe) * (anvilRecipes.size));
anvilRecipes.recipes[0] = defineRecipe(TOOL_SWORD,2,2,ITEM_WOOD,5,ITEM_IRONINGOT,5);
anvilRecipes.recipes[1] = defineRecipe(TOOL_AXE,2,2,ITEM_WOOD,5,ITEM_IRONINGOT,5);
@ -141,7 +124,6 @@ void initRecipes(){
anvilRecipes.recipes[15] = defineRecipe(ITEM_WALL_GOLD,1,1,ITEM_GOLDINGOT,2);
anvilRecipes.recipes[16] = defineRecipe(ITEM_COIN,3,1,ITEM_IRONINGOT,1);
anvilRecipes.recipes[17] = defineRecipe(ITEM_POTION_MAKER,1,3,ITEM_IRONINGOT,5,ITEM_GOLDINGOT,25,ITEM_GEM,5);
anvilRecipes.recipes[18] = defineRecipe(ITEM_SHEARS,1,1,ITEM_IRONINGOT,2);
furnaceRecipes.size = 3;
furnaceRecipes.recipes = (Recipe*)malloc(sizeof(Recipe) * (furnaceRecipes.size));
@ -149,17 +131,15 @@ void initRecipes(){
furnaceRecipes.recipes[1] = defineRecipe(ITEM_GOLDINGOT,1,2,ITEM_GOLDORE,4,ITEM_COAL,1);
furnaceRecipes.recipes[2] = defineRecipe(ITEM_GLASS,1,2,ITEM_SAND,4,ITEM_COAL,1);
ovenRecipes.size = 4;
ovenRecipes.size = 3;
ovenRecipes.recipes = (Recipe*)malloc(sizeof(Recipe) * (ovenRecipes.size));
ovenRecipes.recipes[0] = defineRecipe(ITEM_BREAD,1,1,ITEM_WHEAT,4);
ovenRecipes.recipes[1] = defineRecipe(ITEM_PORK_COOKED,1,2,ITEM_PORK_RAW,1,ITEM_COAL,1);
ovenRecipes.recipes[2] = defineRecipe(ITEM_BEEF_COOKED,1,2,ITEM_BEEF_RAW,1,ITEM_COAL,1);
ovenRecipes.recipes[3] = defineRecipe(ITEM_FISH_COOKED,1,2,ITEM_FISH_RAW,1,ITEM_COAL,1);
loomRecipes.size = 2;
loomRecipes.size = 1;
loomRecipes.recipes = (Recipe*)malloc(sizeof(Recipe) * (loomRecipes.size));
loomRecipes.recipes[0] = defineRecipe(ITEM_STRING,1,1,ITEM_WOOL,1);
loomRecipes.recipes[1] = defineRecipe(ITEM_BED,1,2,ITEM_WOOD,5,ITEM_WOOL,3);
enchanterRecipes.size = 8;
enchanterRecipes.recipes = (Recipe*)malloc(sizeof(Recipe) * (enchanterRecipes.size));
@ -172,17 +152,15 @@ void initRecipes(){
enchanterRecipes.recipes[6] = defineRecipe(ITEM_WALL_GEM,1,1,ITEM_GEM,10);
enchanterRecipes.recipes[7] = defineRecipe(ITEM_GOLD_APPLE,1,2,ITEM_APPLE,1,ITEM_GOLDINGOT,15);
potionMakerRecipes.size = 4;
potionMakerRecipes.size = 3;
potionMakerRecipes.recipes = (Recipe*)malloc(sizeof(Recipe) * (potionMakerRecipes.size));
potionMakerRecipes.recipes[0] = defineRecipe(ITEM_STRENGTH_POTION,1,3,ITEM_GOLD_APPLE,1,ITEM_GLASS,10,ITEM_IRONINGOT,10);
potionMakerRecipes.recipes[1] = defineRecipe(ITEM_SPEED_POTION,1,4,ITEM_GEM,2,ITEM_GLASS,10,ITEM_IRONINGOT,10, ITEM_GOLDINGOT,15);
potionMakerRecipes.recipes[2] = defineRecipe(ITEM_REGEN_POTION,1,3,ITEM_GOLD_APPLE,2,ITEM_GLASS,10,ITEM_GEM,10);
potionMakerRecipes.recipes[3] = defineRecipe(ITEM_SWIM_BREATH_POTION,1,4,ITEM_GOLD_APPLE,2,ITEM_GLASS,10,ITEM_GEM,10, ITEM_STRING,15);
}
/* Free up allocated memory */
void freeRecipes(){
free(inHeadRecipes.recipes);
free(workbenchRecipes.recipes);
free(ovenRecipes.recipes);
free(furnaceRecipes.recipes);

9
source/Crafting.h Executable file → Normal file
View file

@ -2,12 +2,12 @@
#include <stdarg.h>
#include "Item.h"
typedef struct _recipecost {
typedef struct {
int costItem;
int costAmount;
} Cost;
typedef struct _recipe {
typedef struct {
bool canCraft;
int itemResult;
int itemAmountLevel;
@ -16,12 +16,12 @@ typedef struct _recipe {
u8 order; // Used for stable sorting.
} Recipe;
typedef struct _recipeManager {
typedef struct {
int size;
Recipe * recipes;
} RecipeManager;
RecipeManager inHeadRecipes;
RecipeManager workbenchRecipes;
RecipeManager furnaceRecipes;
RecipeManager ovenRecipes;
@ -32,7 +32,6 @@ RecipeManager potionMakerRecipes;
Recipe defineRecipe(int item, int amountOrLevel, int numArgs, ...);
void cloneRecipeManager(RecipeManager *from, RecipeManager *to);
void checkCanCraftRecipes(RecipeManager * rm, Inventory * inv);
void sortRecipes(RecipeManager * rm);
bool craftItem(RecipeManager * rm, Recipe* r, Inventory* inv);

29
source/Entity.c Executable file → Normal file
View file

@ -1,6 +1,23 @@
#include "Entity.h"
#include "Synchronizer.h"
#define PI 3.141592654
double gaussrand()
{
static double U, V;
static int phase = 0;
double Z;
if(phase == 0) {
U = (rand() + 1.) / (RAND_MAX + 2.);
V = rand() / (RAND_MAX + 1.);
Z = sqrt(-2 * log(U)) * sin(2 * PI * V);
} else
Z = sqrt(-2 * log(U)) * cos(2 * PI * V);
phase = 1 - phase;
return Z;
}
Entity newItemEntity(Item item, int x, int y, int level){
Entity e;
@ -17,8 +34,8 @@ Entity newItemEntity(Item item, int x, int y, int level){
e.entityItem.xx = x;
e.entityItem.yy = y;
e.entityItem.zz = 2;
e.entityItem.xa = gaussrand(false) * 0.1;
e.entityItem.ya = gaussrand(false) * 0.1;
e.entityItem.xa = gaussrand() * 0.1;
e.entityItem.ya = gaussrand() * 0.1;
e.entityItem.za = ((float)rand() / RAND_MAX) * 0.45 + 1;
return e;
@ -279,8 +296,8 @@ Entity newTextParticleEntity(char * str, u32 color, int x, int y, int level){
e.textParticle.xx = x;
e.textParticle.yy = y;
e.textParticle.zz = 2;
e.textParticle.xa = gaussrand(false) * 0.3;
e.textParticle.ya = gaussrand(false) * 0.2;
e.textParticle.xa = gaussrand() * 0.3;
e.textParticle.ya = gaussrand() * 0.2;
e.textParticle.za = ((float)rand() / RAND_MAX) * 0.7 + 2;
return e;
@ -293,7 +310,7 @@ Entity newSmashParticleEntity(int x, int y, int level){
e.x = x;
e.y = y;
e.canPass = true;
playSoundPositioned(snd_monsterHurt, e.level, e.x, e.y); //TODO: This is a wierd location for the effect
playSound(snd_monsterHurt);
return e;
}

16
source/Entity.h Executable file → Normal file
View file

@ -27,8 +27,6 @@
typedef struct Entity Entity;
typedef struct _plrd PlayerData; //in order to not include Player.h and cause all sorts of problems
typedef struct {
s8 ax;
s8 ay;
@ -49,10 +47,10 @@ typedef struct {
int swimTimer;
int regenTimer;
int strengthTimer;
int swimBreathTimer;
int speedTimer;
int score;
PlayerData *data;
Inventory* inv;
Item* activeItem;
} Player;
@ -70,8 +68,9 @@ typedef struct {
typedef struct {
s16 itemID;
bool active;
s8 r; // light radius for lantern.
s8 r; // light radius for lantern. window select for chests.
Inventory* inv; // Points to chest inventory.
s16 oSel; // other selection inside the chest inv.
} EntityFurniture;
typedef struct {
@ -219,14 +218,16 @@ struct Entity {
typedef struct {
Entity entities[6][1000];
s16 lastSlot[6];
Inventory invs[300];
Inventory invs[301];//1 for the player, 300 for chests.
s16 nextInv;
} EntityManager;
EntityManager eManager;
Entity nullEntity;
s8 currentLevel;
double gaussrand();
Entity newItemEntity(Item item, int x, int y, int level);
Entity newFurnitureEntity(int itemID,Inventory * invPtr, int x, int y, int level);
Entity newPassiveEntity(int type, int x, int y, int level);
@ -246,3 +247,6 @@ Entity newGlowwormEntity(int x, int y, int level);
Entity newNPCEntity(int type, int x, int y, int level);
void addEntityToList(Entity e, EntityManager* em);
void removeEntityFromList(Entity * e,int level,EntityManager* em);

1628
source/Globals.c Executable file → Normal file

File diff suppressed because it is too large Load diff

126
source/Globals.h Executable file → Normal file
View file

@ -1,14 +1,11 @@
#pragma once
#include <3ds.h>
#include "Entity.h"
#include "Player.h"
#include "SaveLoad.h"
#include "Input.h"
#include "MapGen.h"
#include "Quests.h"
#include "icons_png.h"
#include "player_png.h"
#include "icons2_png.h"
#include "Font_png.h"
#include "bottombg_png.h"
@ -20,23 +17,18 @@
#define MENU_TUTORIAL 2
#define MENU_ABOUT 3
#define MENU_SETTINGS 4
#define MENU_LOADGAME 5
#define MENU_SETTINGS_REBIND 6
#define MENU_SETTINGS_TP 7
#define MENU_MULTIPLAYER_HOST 8
#define MENU_MULTIPLAYER_JOIN 9
#define MENU_MULTIPLAYER_WAIT 10
#define MENU_LOADING 11
#define MENU_PAUSED 100
#define MENU_INVENTORY 101
#define MENU_CRAFTING 102
#define MENU_CONTAINER 103
#define MENU_WIN 104
#define MENU_LOSE 105
#define MENU_DUNGEON 106
#define MENU_NPC 107
#define MENU_CHARACTER_CUSTOMIZE 108
#define MENU_INVENTORY 5
#define MENU_CRAFTING 6
#define MENU_CONTAINER 7
#define MENU_WIN 8
#define MENU_LOSE 9
#define MENU_PAUSED 10
#define MENU_LOADGAME 11
#define MENU_SETTINGS_REBIND 12
#define MENU_SETTINGS_TP 13
#define MENU_DUNGEON 14
#define MENU_NPC 15
#define MENU_MULTIPLAYER 16
#define NPC_GIRL 0
#define NPC_PRIEST 1
@ -86,32 +78,31 @@
#define SWAP_UINT32(x) (((x) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | ((x) << 24))
//WARNING: Having this set to different values in different clients will break multiplayer!
#define TESTGODMODE false
bool paused;
u32 localUID;
bool screenShot;
int loadedtp;
u8 MODEL_3DS;
extern char versionText[34];
Entity player;
bool shouldRenderDebug;
bool shouldSpeedup;
bool shouldRenderMap;
bool UnderStrengthEffect;
bool UnderSpeedEffect;
bool regening;
bool UnderSwimBreathEffect;
u8 zoomLevel;
char mapText[32];
s16 mScrollX, mScrollY;
sf2d_texture *icons;
sf2d_texture *playerSprites;
sf2d_texture *font;
sf2d_texture *bottombg;
sf2d_texture * minimap[6];
u8 map[6][128*128];
u8 data[6][128*128];
u8 minimapData[128*128];
u8 compassData[6][3];
u32 dirtColor[5];
u32 grassColor;
@ -133,72 +124,65 @@ char currentFileName[256];
extern u8 currentMenu;
extern char fpsstr[];
u8 initGame;
u8 initMPGame;
u8 initBGMap;
Item noItem;
int airWizardHealthDisplay;
s16 awX, awY;
u32 tickCount;
RecipeManager* currentRecipes;
Entity* curChestEntity;
char* currentCraftTitle;
s16 curInvSel;
bool quitGame;
s8 currentSelection;
typedef struct _worldData {
u8 map[6][128*128];
u8 data[6][128*128];
bool isRemote;
u16 daytime;
int day;
u8 season;
bool rain;
u8 compassData[6][3];
} WorldData;
WorldData worldData;
//TODO: cleanup the order
int getEntities(Entity** result, s8 level, int x0, int y0, int x1, int y1);
bool moveMob(Entity* e, int xa, int ya);
void hurtEntity(Entity *e, int damage, int dir, u32 hurtColor, Entity *damager);
void tickTile(s8 level, int x, int y);
void tickTile(int x, int y);
bool tileIsSolid(int tile, Entity * e);
s8 itemTileInteract(int tile, PlayerData *pd, Item *item, s8 level, int x, int y, int px, int py, int dir);
s8 itemTileInteract(int tile, Item* item, int x, int y, int px, int py, int dir);
void tickEntity(Entity* e);
void tickTouchMap();
void tickTouchQuickSelect();
void trySpawn(int count, int level);
int getTile(s8 level, int x, int y);
void setTile(int id, s8 level, int x, int y);
int getData(s8 level, int x, int y);
void setData(int id, s8 level, int x, int y);
int getTile(int x, int y);
u32 getTileColor(int tile);
void setTile(int id, int x, int y);
int getData(int x, int y);
void setData(int id, int x, int y);
bool intersectsEntity(int x, int y, int r, Entity* e);
bool EntityBlocksEntity(Entity* e1, Entity* e2);
void EntityVsEntity(Entity* e1, Entity* e2);
bool ItemVsEntity(PlayerData *pd, Item *item, Entity *e, int dir);
void entityTileInteract(Entity* e, int tile, s8 level, int x, int y);
void entityTileInteract(Entity* e, int tile,int x, int y);
void openCraftingMenu(PlayerData *pd, RecipeManager *rm, char *title);
bool useEntity(PlayerData *pd, Entity* e);
void initPlayer();
void tickPlayer();
void playerAttack();
bool isSwimming();
bool playerUseEnergy(int amount);
void playerHurtTile(int tile, int xt, int yt, int damage, int dir);
bool playerIntersectsEntity(Entity* e);
void playerEntityInteract(Entity* e);
void playerSetActiveItem(Item * item);
bool isWater(s8 level, int xt, int yt);
void enterDungeon();
void leaveDungeon();
void playerHurtTile(PlayerData *pd, int tile, s8 level, int xt, int yt, int damage, int dir);
void playerEntityInteract(PlayerData *pd, Entity* e);
bool dungeonActive();
void enterDungeon(PlayerData *pd);
void leaveDungeon(PlayerData *pd);
void setMinimapVisible(PlayerData *pd, int level, int x, int y, bool visible);
bool getMinimapVisible(PlayerData *pd, int level, int x, int y);
u32 getMinimapColor(PlayerData *pd, int level, int x, int y);
void initMinimapLevel(PlayerData *pd, int level);
void setMinimapVisible(int level, int x, int y, bool visible);
bool getMinimapVisible(int level, int x, int y);
u32 getMinimapColor(int level, int x, int y);
void initMinimapLevel(int level, bool loadUpWorld);
void updateLevel1Map();
void reloadColors();

View file

@ -1,379 +0,0 @@
#include "Ingame.h"
#include "Globals.h"
#include "Player.h"
#include "Entity.h"
#include "IngameMenu.h"
#include "Render.h"
#include "MapGen.h"
#include "Synchronizer.h"
#include "SaveLoad.h"
#include "Network.h"
#define STALL_TIME 120
int stallCounter;
bool stallAreYouSure;
//generates stairs up and creates compass data
void generatePass2() {
int level, x, y;
for (level = 0; level < 5; ++level) {
for (x = 0; x < 128; ++x) {
for (y = 0; y < 128; ++y) {
//generate stairs up matching stairs down
switch (worldData.map[level][x + y * 128]) {
case TILE_STAIRS_DOWN:
if(level < 4) {
worldData.map[level + 1][x + y * 128] = TILE_STAIRS_UP;
if (level == 0) {
worldData.map[level + 1][(x + 1) + y * 128] = TILE_HARDROCK;
worldData.map[level + 1][x + (y + 1) * 128] = TILE_HARDROCK;
worldData.map[level + 1][(x - 1) + y * 128] = TILE_HARDROCK;
worldData.map[level + 1][x + (y - 1) * 128] = TILE_HARDROCK;
worldData.map[level + 1][(x + 1) + (y + 1) * 128] = TILE_HARDROCK;
worldData.map[level + 1][(x - 1) + (y - 1) * 128] = TILE_HARDROCK;
worldData.map[level + 1][(x - 1) + (y + 1) * 128] = TILE_HARDROCK;
worldData.map[level + 1][(x + 1) + (y - 1) * 128] = TILE_HARDROCK;
} else {
worldData.map[level + 1][(x + 1) + y * 128] = TILE_DIRT;
worldData.map[level + 1][x + (y + 1) * 128] = TILE_DIRT;
worldData.map[level + 1][(x - 1) + y * 128] = TILE_DIRT;
worldData.map[level + 1][x + (y - 1) * 128] = TILE_DIRT;
worldData.map[level + 1][(x + 1) + (y + 1) * 128] = TILE_DIRT;
worldData.map[level + 1][(x - 1) + (y - 1) * 128] = TILE_DIRT;
worldData.map[level + 1][(x - 1) + (y + 1) * 128] = TILE_DIRT;
worldData.map[level + 1][(x + 1) + (y - 1) * 128] = TILE_DIRT;
}
}
}
//calculate compass data
//choose one stair down and store for magic compass
switch (worldData.map[level][x + y * 128]) {
case TILE_STAIRS_DOWN:
case TILE_DUNGEON_ENTRANCE:
worldData.compassData[level][2] = worldData.compassData[level][2] + 1;
if((worldData.compassData[level][2]==1) || (rand()%(worldData.compassData[level][2])==0)) {
worldData.compassData[level][0] = x;
worldData.compassData[level][1] = y;
}
}
}
}
}
}
void initNewMap() {
createAndValidateSkyMap(128, 128, 0, worldData.map[0], worldData.data[0]);
createAndValidateTopMap(128, 128, 1, worldData.map[1], worldData.data[1]);
createAndValidateUndergroundMap(128, 128, 1, 2, worldData.map[2], worldData.data[2]);
createAndValidateUndergroundMap(128, 128, 2, 3, worldData.map[3], worldData.data[3]);
createAndValidateUndergroundMap(128, 128, 3, 4, worldData.map[4], worldData.data[4]);
generatePass2();
}
void initMiniMap(PlayerData *pd) {
int i;
for (i = 0; i < 5; ++i) {
initMinimapLevel(pd, i);
}
}
void startGame(bool load, char *filename) {
// Reset entity manager.
memset(&eManager, 0, sizeof(eManager));
// Reset players
for(int i=0; i<playerCount; i++) {
initPlayer(players+i);
}
if (playerCount > 1) {
shouldRenderDebug = false;
}
if (!load) {
initNewMap();
airWizardHealthDisplay = 2000;
int i;
for (i = 0; i < 5; ++i) {
trySpawn(500, i);
}
addEntityToList(newAirWizardEntity(630, 820, 0), &eManager);
worldData.daytime = 6000;
worldData.day = 0;
worldData.season = 0;
worldData.rain = false;
} else {
if(!loadWorld(filename, &eManager, &worldData, players, playerCount)) {
//TODO: What do?
networkDisconnect();
sf2d_set_clear_color(0xFF);
currentSelection = 0;
currentMenu = MENU_TITLE;
}
}
// Spawn players
for(int i=0; i<playerCount; i++) {
if(!players[i].isSpawned) {
playerSpawn(players+i);
}
}
// Init player maps
for(int i=0; i<playerCount; i++) {
initMiniMap(players+i);
}
stallCounter = 0;
}
void syncedTick() {
int i;
setListenerPosition(getLocalPlayer()->entity.level, getLocalPlayer()->entity.x, getLocalPlayer()->entity.y);
//win/death menus
for(i=0; i<playerCount; i++) {
if (players[i].entity.p.isDead) {
if (players[i].entity.p.endTimer < 1) {
players[i].ingameMenu = MENU_LOSE;
}
--players[i].entity.p.endTimer;
} else if (players[i].entity.p.hasWon) {
if (players[i].entity.p.endTimer < 1) {
players[i].ingameMenu = MENU_WIN;
}
--players[i].entity.p.endTimer;
}
}
//update worldData (daytime,season and weather)
++worldData.daytime;
if(worldData.daytime>=24000) {
worldData.daytime -= 24000;
++worldData.day;
//TODO: maybe make season length not as hardcoded + make the transition better (fade to black and back maybe?)
if(worldData.day%7==0) {
++worldData.season;
if(worldData.season==4) worldData.season = 0;
}
worldData.rain = false;
if(worldData.season!=3 && rand()%5==0) worldData.rain = true;
}
//update music
updateMusic(getLocalPlayer()->entity.level, worldData.daytime);
//for every active level
s8 level;
for(level = 0; level < 6; level++) {
bool hasPlayer = false;
for(i=0; i<playerCount; i++) {
if(players[i].entity.level==level) {
hasPlayer = true;
}
}
if(!hasPlayer) continue;
//tick tiles
for (i = 0; i < 324; ++i) {
int xx = rand() & 127;
int yy = rand() & 127;
tickTile(level, xx, yy);
}
}
for(i=0; i<playerCount; i++) {
ingameMenuTickTouch(players+i);
bool inmenu = false;
if(players[i].ingameMenu != MENU_NONE) {
ingameMenuTick(players+i, players[i].ingameMenu);
inmenu = true;
}
tickPlayer(players+i, inmenu);
}
//for every active level
for(level = 0; level < 6; level++) {
if(level==5 && !dungeonActive()) continue;
bool hasPlayer = false;
for(i=0; i<playerCount; i++) {
if(players[i].entity.level==level) {
hasPlayer = true;
}
}
if(!hasPlayer) continue;
//spawn entities
if(eManager.lastSlot[level]<80 && level != 5) {
trySpawn(1, level);
}
//update entities
for (i = 0; i < eManager.lastSlot[level]; ++i) {
Entity * e = &eManager.entities[level][i];
PlayerData * p = getNearestPlayer(level, e->x, e->y);
//should never happen, but just for safety to prevent hard crashes
if(p==NULL) continue;
if ((e->type != ENTITY_ZOMBIE && e->type != ENTITY_SKELETON && e->type != ENTITY_KNIGHT && e->type != ENTITY_SLIME && e->type != ENTITY_PASSIVE && e->type != ENTITY_GLOWWORM)
|| (e->type == ENTITY_GLOWWORM && (worldData.daytime>6000 || worldData.daytime<18000))
|| (e->x > p->entity.x - 160 && e->y > p->entity.y - 125 && e->x < p->entity.x + 160 && e->y < p->entity.y + 125)) {
tickEntity(e);
}
}
}
stallCounter = 0;
}
void tickGame() {
synchronizerTick(&syncedTick);
if(synchronizerIsRunning()) {
stallCounter++;
//game stalled -> most likely a player disconnected -> present option to exit game
if(stallCounter>=STALL_TIME) {
if(stallCounter==STALL_TIME) stallAreYouSure = false;
//scan local inputs, because synchronizer only updates them when not stalled
hidScanInput();
tickKeys(&localInputs, hidKeysHeld(), hidKeysDown());
if (localInputs.k_accept.clicked) {
if(stallAreYouSure) {
//create backup save
if(playerLocalID==0) {
char backupSaveFileName[256+32];
backupSaveFileName[0] = '\0';
strncat(backupSaveFileName, currentFileName, strlen(currentFileName)-4);
strcat(backupSaveFileName, ".exit.msv");
saveWorld(backupSaveFileName, &eManager, &worldData, players, playerCount);
}
exitGame();
} else {
stallAreYouSure = true;
}
}
if (localInputs.k_decline.clicked) {
stallAreYouSure = false;
}
}
}
}
//for rendering -> move to a better place
int xscr = 0, yscr = 0;
void renderGame() {
//Important: all code called from this function should never affect game state!
sf2d_start_frame(GFX_TOP, GFX_LEFT);
int xscr = getLocalPlayer()->entity.x - 100;
int yscr = getLocalPlayer()->entity.y - 56;
if (xscr < 16)
xscr = 16;
else if (xscr > 1832)
xscr = 1832;
if (yscr < 16)
yscr = 16;
else if (yscr > 1912)
yscr = 1912;
offsetX = xscr;
offsetY = yscr;
sf2d_draw_rectangle(0, 0, 400, 240, 0xFF0C0C0C); //RGBA8(12, 12, 12, 255)); //You might think "real" black would be better, but it actually looks better that way
renderLightsToStencil(getLocalPlayer(), false, false, true);
renderBackground(getLocalPlayer()->entity.level, xscr, yscr);
renderEntities(getLocalPlayer()->entity.level, getLocalPlayer()->entity.x, getLocalPlayer()->entity.y, &eManager);
for(int i=0; i<playerCount; i++) {
renderPlayer(players+i);
}
renderWeather(getLocalPlayer()->entity.level, xscr, yscr);
resetStencilStuff();
renderDayNight(getLocalPlayer());
offsetX = 0;
offsetY = 0;
if(shouldRenderDebug){
sprintf(fpsstr, " FPS: %.0f, X:%d, Y:%d, E:%d", sf2d_get_fps(), getLocalPlayer()->entity.x, getLocalPlayer()->entity.y, eManager.lastSlot[getLocalPlayer()->entity.level]);
drawText(fpsstr, 2, 225);
}
if(getLocalPlayer()->ingameMenu != MENU_NONE) {
ingameMenuRender(getLocalPlayer(), getLocalPlayer()->ingameMenu);
}
//game stalled -> most likely a player disconnected -> present option to exit game
if(stallCounter>STALL_TIME) {
renderFrame(1,1,24,14,0xFF1010AF);
drawText("Waiting for a long time", (400 - (23 * 12))/2, 32);
char text[50];
sprintf(text, "Last response %is ago", stallCounter/60);
drawText(text, (400 - (strlen(text) * 12))/2, 64);
if(playerLocalID==0) {
drawText("Press to leave the game", (400 - (25 * 12))/2, 160);
renderButtonIcon(localInputs.k_accept.input & -localInputs.k_accept.input, 120, 157, 1);
drawText("A backup save will be created", (400 - (29 * 12))/2, 192);
} else {
drawText("Press to leave the game", (400 - (25 * 12))/2, 192);
renderButtonIcon(localInputs.k_accept.input & -localInputs.k_accept.input, 120, 189, 1);
}
if(stallAreYouSure){
renderFrame(6,5,19,10,0xFF10108F);
drawText("Are you sure?",122,96);
drawText(" Yes", 164, 117);
renderButtonIcon(localInputs.k_accept.input & -localInputs.k_accept.input, 166, 114, 1);
drawText(" No", 170, 133);
renderButtonIcon(localInputs.k_decline.input & -localInputs.k_decline.input, 166, 130, 1);
}
}
sf2d_end_frame();
sf2d_start_frame(GFX_BOTTOM, GFX_LEFT);
if(!players[playerLocalID].mapShouldRender){
sf2d_draw_texture(bottombg, 0, 0);
renderGui(getLocalPlayer());
} else {
renderZoomedMap(getLocalPlayer());
}
sf2d_end_frame();
}
void exitGame() {
networkDisconnect();
synchronizerReset();
sf2d_set_clear_color(0xFF);
currentSelection = 0;
currentMenu = MENU_TITLE;
playMusic(&music_menu);
}

View file

@ -1,9 +0,0 @@
#pragma once
#include <3ds.h>
void startGame(bool load, char *filename);
void tickGame();
void renderGame();
void exitGame();

View file

@ -1,528 +0,0 @@
#include "IngameMenu.h"
#include "Globals.h"
#include "Menu.h"
#include "Ingame.h"
#include "Player.h"
#include "SaveLoad.h"
#include "Synchronizer.h"
char pOptions[][24] = {"Return to game", "Save Progress", "Exit to title"};
void ingameMenuTick(PlayerData *pd, int menu) {
switch(menu) {
case MENU_PAUSED:
if(!pd->ingameMenuAreYouSure && !pd->ingameMenuAreYouSureSave){
if (pd->ingameMenuTimer > 0) --pd->ingameMenuTimer;
if (pd->inputs.k_pause.clicked || pd->inputs.k_decline.clicked) pd->ingameMenu = MENU_NONE;
if (pd->inputs.k_up.clicked) { --pd->ingameMenuSelection; if(pd->ingameMenuSelection < 0) pd->ingameMenuSelection=2; }
if (pd->inputs.k_down.clicked) { ++pd->ingameMenuSelection; if(pd->ingameMenuSelection > 2) pd->ingameMenuSelection=0; }
if (pd->inputs.k_accept.clicked){
switch(pd->ingameMenuSelection){
case 0:
pd->ingameMenu = MENU_NONE;
break;
case 1:
if(!dungeonActive()) pd->ingameMenuAreYouSureSave = true;
break;
case 2:
pd->ingameMenuAreYouSure = true;
break;
}
}
} else if(pd->ingameMenuAreYouSureSave) {
if (pd->inputs.k_accept.clicked){
pd->ingameMenuTimer = 60;
if(playerLocalID==0) {
saveWorld(currentFileName, &eManager, &worldData, players, playerCount);
}
pd->ingameMenuAreYouSureSave = false;
pd->ingameMenuAreYouSure = false;
} else if (pd->inputs.k_decline.clicked){
pd->ingameMenuAreYouSureSave = false;
pd->ingameMenuAreYouSure = false;
}
} else if(pd->ingameMenuAreYouSure) {
if (pd->inputs.k_accept.clicked){
pd->ingameMenuAreYouSure = false;
pd->ingameMenuAreYouSureSave = false;
exitGame();
} else if (pd->inputs.k_decline.clicked){
pd->ingameMenuAreYouSure = false;
pd->ingameMenuAreYouSureSave = false;
}
}
break;
case MENU_INVENTORY:
if (pd->inputs.k_menu.clicked || pd->inputs.k_use.clicked || pd->inputs.k_decline.clicked){
pd->ingameMenu = MENU_NONE;
pd->activeItem = &noItem;
pd->entity.p.isCarrying = false;
}
if (pd->inputs.k_accept.clicked){ // Select item from inventory
if(pd->inventory.lastSlot!=0){
Item median = pd->inventory.items[pd->ingameMenuInvSel]; // create copy of item.
removeItemFromInventory(pd->ingameMenuInvSel, &(pd->inventory)); // remove original
pushItemToInventoryFront(median, &(pd->inventory)); // add copy to front
playerSetActiveItem(pd, &(pd->inventory.items[0])); // active item = copy.
}
pd->ingameMenu = MENU_NONE;
}
if (pd->inputs.k_up.clicked) { --pd->ingameMenuInvSel; if(pd->ingameMenuInvSel < 0)pd->ingameMenuInvSel=pd->inventory.lastSlot-1; }
if (pd->inputs.k_down.clicked) { ++pd->ingameMenuInvSel; if(pd->ingameMenuInvSel > pd->inventory.lastSlot-1)pd->ingameMenuInvSel=0; }
break;
case MENU_CRAFTING:
if (pd->inputs.k_menu.clicked || pd->inputs.k_use.clicked || pd->inputs.k_decline.clicked) pd->ingameMenu = MENU_NONE;
if (pd->inputs.k_accept.clicked){
if(craftItem(&(pd->currentRecipes), &(pd->currentRecipes.recipes[pd->ingameMenuInvSel]), &(pd->inventory))){
playSoundPositioned(snd_craft, pd->entity.level, pd->entity.x, pd->entity.y);
//reset active item pointer, because it could posibly point to garbage now
pd->activeItem = &noItem;
}
}
if (pd->inputs.k_up.clicked) { --pd->ingameMenuInvSel; if(pd->ingameMenuInvSel < 0)pd->ingameMenuInvSel=pd->currentRecipes.size-1; }
if (pd->inputs.k_down.clicked) { ++pd->ingameMenuInvSel; if(pd->ingameMenuInvSel > pd->currentRecipes.size-1)pd->ingameMenuInvSel=0; }
break;
case MENU_WIN:
if (pd->inputs.k_accept.clicked){
pd->ingameMenu = MENU_NONE;
pd->entity.p.hasWon = false;
}
break;
case MENU_LOSE:
if (pd->inputs.k_accept.clicked){
pd->ingameMenu = MENU_NONE;
pd->entity.p.isDead = false;
pd->entity.p.health = 10;
pd->entity.level = 1;
playerSpawn(pd);
//TODO: This canceled to main menu, but what should I do in multiplayer?
}
pd->entity.hurtTime = 10;
break;
case MENU_CONTAINER:
if (pd->inputs.k_menu.clicked || pd->inputs.k_decline.clicked) pd->ingameMenu = MENU_NONE;
if (pd->inputs.k_left.clicked) {
pd->curChestEntityR = 0;
int tmp = pd->ingameMenuInvSel;
pd->ingameMenuInvSel = pd->ingameMenuInvSelOther;
pd->ingameMenuInvSelOther = tmp;
}
if (pd->inputs.k_right.clicked) {
pd->curChestEntityR = 1;
int tmp = pd->ingameMenuInvSel;
pd->ingameMenuInvSel = pd->ingameMenuInvSelOther;
pd->ingameMenuInvSelOther = tmp;
}
Inventory* i1 = pd->curChestEntityR == 1 ? &(pd->inventory) : pd->curChestEntity->entityFurniture.inv;
Inventory* i2 = pd->curChestEntityR == 0 ? &(pd->inventory) : pd->curChestEntity->entityFurniture.inv;
int len = i1->lastSlot;
if (pd->ingameMenuInvSel < 0) pd->ingameMenuInvSel = 0;
if (pd->ingameMenuInvSel >= len) pd->ingameMenuInvSel = len - 1;
if (pd->inputs.k_up.clicked) --pd->ingameMenuInvSel;
if (pd->inputs.k_down.clicked) ++pd->ingameMenuInvSel;
if (len == 0) pd->ingameMenuInvSel = 0;
if (pd->ingameMenuInvSel < 0) pd->ingameMenuInvSel += len;
if (pd->ingameMenuInvSel >= len) pd->ingameMenuInvSel -= len;
if(pd->inputs.k_accept.clicked && len > 0){
Item* pullItem = &i1->items[pd->ingameMenuInvSel];
Item pushItem = newItem(pullItem->id, pullItem->countLevel);
pushItem.chestPtr = pullItem->chestPtr;
pushItemToInventoryFront(pushItem, i2);
if(i2 == &(pd->inventory)){
int newslot = pd->activeItem->slotNum + 1;
pd->activeItem = &(pd->inventory.items[newslot]);
} else if(pullItem == pd->activeItem){
pd->activeItem = &noItem;
}
removeItemFromCurrentInv(pullItem);
if (pd->ingameMenuInvSel >= i1->lastSlot) pd->ingameMenuInvSel = i1->lastSlot - 1;
}
break;
case MENU_DUNGEON:
if (pd->inputs.k_menu.clicked || pd->inputs.k_decline.clicked) pd->ingameMenu = MENU_NONE;
if(pd->inputs.k_accept.clicked) {
if(pd->entity.level!=5) {
Item * item = getItemFromInventory(ITEM_DUNGEON_KEY, &(pd->inventory));
if(item!=NULL) {
--item->countLevel;
if(item->countLevel==0) {
removeItemFromCurrentInv(item);
}
enterDungeon(pd);
} else if(shouldRenderDebug) {
enterDungeon(pd);
}
} else {
leaveDungeon(pd);
}
pd->ingameMenu = MENU_NONE;
}
break;
case MENU_NPC:
tickNPCMenu(pd);
break;
case MENU_CHARACTER_CUSTOMIZE:
if (pd->inputs.k_up.clicked) { --pd->ingameMenuSelection; if(pd->ingameMenuSelection < 0) pd->ingameMenuSelection=6; }
if (pd->inputs.k_down.clicked) { ++pd->ingameMenuSelection; if(pd->ingameMenuSelection > 6) pd->ingameMenuSelection=0; }
u8 wrap = 0;
wrap = wrap - 1;
pd->entity.p.health = 10;
pd->entity.hurtTime = 10;
//head
if(pd->ingameMenuSelection==0) {
if (pd->inputs.k_left.clicked) { --pd->sprite.head; if(pd->sprite.head == wrap) pd->sprite.head=PLAYER_SPRITE_HEAD_COUNT-1; }
if (pd->inputs.k_right.clicked) { ++pd->sprite.head; if(pd->sprite.head > PLAYER_SPRITE_HEAD_COUNT-1) pd->sprite.head=0; }
//eyes
} else if(pd->ingameMenuSelection==1) {
if (pd->inputs.k_left.clicked) { --pd->sprite.eyes; if(pd->sprite.eyes == wrap) pd->sprite.eyes=PLAYER_SPRITE_EYES_COUNT-1; }
if (pd->inputs.k_right.clicked) { ++pd->sprite.eyes; if(pd->sprite.eyes > PLAYER_SPRITE_EYES_COUNT-1) pd->sprite.eyes=0; }
//body
} else if(pd->ingameMenuSelection==2) {
if (pd->inputs.k_left.clicked) { --pd->sprite.body; if(pd->sprite.body == wrap) pd->sprite.body=PLAYER_SPRITE_BODY_COUNT-1; }
if (pd->inputs.k_right.clicked) { ++pd->sprite.body; if(pd->sprite.body > PLAYER_SPRITE_BODY_COUNT-1) pd->sprite.body=0; }
//arms
} else if(pd->ingameMenuSelection==3) {
if (pd->inputs.k_left.clicked) { --pd->sprite.arms; if(pd->sprite.arms == wrap) pd->sprite.arms=PLAYER_SPRITE_ARMS_COUNT-1; }
if (pd->inputs.k_right.clicked) { ++pd->sprite.arms; if(pd->sprite.arms > PLAYER_SPRITE_ARMS_COUNT-1) pd->sprite.arms=0; }
//legs
} else if(pd->ingameMenuSelection==4) {
if (pd->inputs.k_left.clicked) { --pd->sprite.legs; if(pd->sprite.legs == wrap) pd->sprite.legs=PLAYER_SPRITE_LEGS_COUNT-1; }
if (pd->inputs.k_right.clicked) { ++pd->sprite.legs; if(pd->sprite.legs > PLAYER_SPRITE_LEGS_COUNT-1) pd->sprite.legs=0; }
//rotation
} else if(pd->ingameMenuSelection==5) {
if (pd->inputs.k_left.clicked) { --pd->entity.p.dir; if(pd->entity.p.dir == wrap) pd->entity.p.dir=3; }
if (pd->inputs.k_right.clicked) { ++pd->entity.p.dir; if(pd->entity.p.dir > 3) pd->entity.p.dir=0; }
//done
} else if(pd->ingameMenuSelection==6) {
if(pd->inputs.k_accept.clicked) {
//TODO: are you sure dialog?
pd->ingameMenu = 0;
pd->ingameMenuSelection = 0;
pd->sprite.choosen = true;
}
}
break;
}
}
u8 opacity = 255;
bool rev = true;
char scoreText[15];
void ingameMenuRender(PlayerData *pd, int menu) {
int i;
switch(menu) {
case MENU_PAUSED:
renderFrame(1,1,24,14,0xFF1010AF);
drawText("Paused",164,32);
for(i = 3; i >= 0; --i){
char* msg = pOptions[i];
u32 color = 0xFF7F7F7F;
if(i == pd->ingameMenuSelection) color = 0xFFFFFFFF;
if((i == 1 && dungeonActive())) {
color = 0xFF7F7FFF;
if(i == pd->ingameMenuSelection) color = 0xFFAFAFFF;
}
drawTextColor(msg,(400 - (strlen(msg) * 12))/2, (i * 24) + 88, color);
}
if(pd->ingameMenuTimer > 0) drawTextColor("Game Saved!", (400-(11*12))/2, 64,0xFF20FF20);
if(pd->ingameMenuAreYouSure || pd->ingameMenuAreYouSureSave){
if(pd->ingameMenuAreYouSure)renderFrame(6,5,19,10,0xFF10108F);
else renderFrame(6,5,19,10,0xFF108F10);
drawText("Are you sure?",122,96);
drawText(" Yes", 164, 117);
renderButtonIcon(localInputs.k_accept.input & -localInputs.k_accept.input, 166, 114, 1);
drawText(" No", 170, 133);
renderButtonIcon(localInputs.k_decline.input & -localInputs.k_decline.input, 166, 130, 1);
}
break;
case MENU_WIN:
renderFrame(5,3,21,12,0xFFFF1010);
if(!rev){ opacity+=5; if(opacity == 255) rev = true; }
else { opacity-=5; if(opacity == 100) rev = false; }
sprintf(scoreText,"Score: %d", pd->score);
drawTextColor("You Win!",158,76,0x0000AFAF + (opacity << 24));
drawText(scoreText, 200-((strlen(scoreText)-1)*6), 100);
drawText("Press to continue", 96, 150);
renderButtonIcon(localInputs.k_attack.input & -localInputs.k_attack.input, 166, 148, 1);
//printf("0x%08X",localInputs.k_attack.input & -localInputs.k_attack.input);
break;
case MENU_LOSE:
renderFrame(5,3,21,12,0xFFFF1010);
if(!rev){ opacity+=5; if(opacity == 255) rev = true; }
else { opacity-=5; if(opacity == 100) rev = false; }
sprintf(scoreText,"Score: %d", pd->score);
drawTextColor("You DIED!",158,76,0x000000AF + (opacity << 24));
drawText(scoreText, 200-((strlen(scoreText)-1)*6), 100);
drawText("Press to continue", 96, 150);
renderButtonIcon(localInputs.k_attack.input & -localInputs.k_attack.input, 166, 148, 1);
//printf("0x%08X",localInputs.k_attack.input & -localInputs.k_attack.input);
break;
case MENU_INVENTORY:
renderFrame(1,1,24,14,0xFFFF1010);
drawTextColor("Inventory",24+1,14+1,0xFF000000);
drawTextColor("Inventory",24,14,0xFF6FE2E2);
renderItemList(&(pd->inventory), 1,1,24,14, pd->ingameMenuInvSel);
break;
case MENU_CRAFTING:
renderFrame(15,1,24,4,0xFFFF1010);
drawTextColor("Have",248+1,14+1,0xFF000000);
drawTextColor("Have",248,14,0xFF6FE2E2);
renderFrame(15,5,24,14,0xFFFF1010);
drawTextColor("Cost",248+1,78+1,0xFF000000);
drawTextColor("Cost",248,78,0xFF6FE2E2);
renderFrame(1,1,14,14,0xFFFF1010);
drawTextColor(pd->currentCraftTitle,24+1,14+1,0xFF000000);
drawTextColor(pd->currentCraftTitle,24,14,0xFF6FE2E2);
renderRecipes(&(pd->currentRecipes), 1, 1, 14, 14, pd->ingameMenuInvSel);
Recipe* rec = &(pd->currentRecipes.recipes[pd->ingameMenuInvSel]);
renderItemIcon(rec->itemResult,rec->itemAmountLevel,128,16);
char craftText[12];
sprintf(craftText, "%d", countItemInv(rec->itemResult, rec->itemAmountLevel, &(pd->inventory)));
drawText(craftText,274,34);
if(rec->numOfCosts > 0){
int i;
for(i = 0; i < rec->numOfCosts; i++){
int amnt = countItemInv(rec->costs[i].costItem,0, &(pd->inventory));
int ttlCst = rec->costs[i].costAmount;
int col = 0xFFFFFFFF; if(amnt<ttlCst) col = 0xFF7F7F7F;
renderItemIcon(rec->costs[i].costItem,1,128,48+(i*8));
sprintf(craftText,"%d/%d",ttlCst,amnt);
drawTextColor(craftText,274,96+(i*18),col);
}
}
break;
case MENU_CONTAINER:
if (pd->curChestEntityR == 1){ offsetX = 48; offsetY = 0;}
else {offsetX = 0; offsetY = 0;}
renderFrame(1,1,15,14,0xFFFF1010);
drawTextColor("Chest",24+1,14+1,0xFF000000);
drawTextColor("Chest",24,14,0xFF6FE2E2);
renderItemList(pd->curChestEntity->entityFurniture.inv,1,1,15,14,
pd->curChestEntityR == 0 ? pd->ingameMenuInvSel : -pd->ingameMenuInvSelOther - 1);
renderFrame(16,1,30,14,0xFFFF1010);
drawTextColor("Inventory",264+1,14+1,0xFF000000);
drawTextColor("Inventory",264,14,0xFF6FE2E2);
renderItemList(&(pd->inventory),16,1,30,14,
pd->curChestEntityR == 1 ? pd->ingameMenuInvSel : -pd->ingameMenuInvSelOther - 1);
offsetX = 0;offsetY = 0;
break;
case MENU_DUNGEON:
renderFrame(1,1,24,14,0xFFFF1010);
if(pd->entity.level!=5) {
drawTextColor("Dungeon Entrance",24+1,14+1,0xFF000000);
drawTextColor("Dungeon Entrance",24,14,0xFF6FE2E2);
drawText("Warning: ", 32, 32);
drawText("You need a Dungeon Key to ", 32, 56);
drawText("enter and cannot save while ", 32, 72);
drawText("being in the Dungeon! ", 32, 88);
drawText("After leaving you will need ", 32, 112);
drawText("a new Dungeon Key for ", 32, 128);
drawText("entering another Dungeon! ", 32, 144);
drawText(" Enter", 148, 171);
} else {
drawTextColor("Dungeon Exit",24+1,14+1,0xFF000000);
drawTextColor("Dungeon Exit",24,14,0xFF6FE2E2);
drawText("Warning: ", 32, 32);
drawText("The Dungeon and everything ", 32, 56);
drawText("in it will disappear when ", 32, 72);
drawText("you leave it! ", 32, 88);
drawText("You will need a new Dungeon ", 32, 112);
drawText("Key for entering another ", 32, 128);
drawText("Dungeon again! ", 32, 144);
drawText(" Leave", 148, 171);
}
renderButtonIcon(localInputs.k_accept.input & -localInputs.k_accept.input, 150, 168, 1);
drawText(" Stay", 148, 195);
renderButtonIcon(localInputs.k_decline.input & -localInputs.k_decline.input, 150, 192, 1);
break;
case MENU_NPC:
renderNPCMenu(&(pd->npcMenuData));
break;
case MENU_CHARACTER_CUSTOMIZE:
renderFrame(1,1,24,14,0xFFFF1010);
drawTextColor("Character",24+1,14+1,0xFF000000);
drawTextColor("Character",24,14,0xFF6FE2E2);
drawText("Head: ", 32, 56);
drawText("Eyes: ", 32, 72);
drawText("Body: ", 32, 88);
drawText("Arms: ", 32, 104);
drawText("Legs: ", 32, 120);
drawText("Rot.: ", 32, 144);
//for the dynamic part
char display[30];
sprintf(display, pd->ingameMenuSelection==0 ? "< %02i/%02i >" : " %02i/%02i ", pd->sprite.head+1, PLAYER_SPRITE_HEAD_COUNT);
drawText(display, 96, 56);
sprintf(display, pd->ingameMenuSelection==1 ? "< %02i/%02i >" : " %02i/%02i ", pd->sprite.eyes+1, PLAYER_SPRITE_EYES_COUNT);
drawText(display, 96, 72);
sprintf(display, pd->ingameMenuSelection==2 ? "< %02i/%02i >" : " %02i/%02i ", pd->sprite.body+1, PLAYER_SPRITE_BODY_COUNT);
drawText(display, 96, 88);
sprintf(display, pd->ingameMenuSelection==3 ? "< %02i/%02i >" : " %02i/%02i ", pd->sprite.arms+1, PLAYER_SPRITE_ARMS_COUNT);
drawText(display, 96, 104);
sprintf(display, pd->ingameMenuSelection==4 ? "< %02i/%02i >" : " %02i/%02i ", pd->sprite.legs+1, PLAYER_SPRITE_LEGS_COUNT);
drawText(display, 96, 120);
sprintf(display, pd->ingameMenuSelection==5 ? "< %02i/%02i >" : " %02i/%02i ", pd->entity.p.dir+1, 4);
drawText(display, 96, 144);
sprintf(display, pd->ingameMenuSelection==6 ? "< Done >" : " Done ");
drawText(display, 96, 172);
int oox = offsetX;
int ooy = offsetY;
int osx = playerScale;
renderFrame(13,3,22,12,0xFF909090);
//move player sprite to 0/0
offsetX = pd->entity.x - 8;
offsetY = pd->entity.y - 8;
//move to where I want it
offsetX -= 108;
offsetY -= 28;
playerScale = 8;
renderPlayer(pd);
offsetX = oox;
offsetY = ooy;
playerScale = osx;
break;
}
}
//touch menu
void tickTouchMap(PlayerData *pd){
if(pd->mapShouldRender){
if(pd->inputs.k_touch.px > 0 || pd->inputs.k_touch.py > 0){
// Plus/Minus zoom button
if(pd->inputs.k_touch.py > 204 && pd->inputs.k_touch.py < 232){
if(pd->inputs.k_touch.px > 284 && pd->inputs.k_touch.px < 312){
if(pd->mapZoomLevel > 4) return;
if(!pd->touchIsChangingSize && !pd->touchIsDraggingMap){
pd->mapZoomLevel += 2;
pd->mapScrollX -= (50 * (pd->mapZoomLevel/2));
pd->mapScrollY -= (40 * (pd->mapZoomLevel/2));
pd->touchIsChangingSize = true;
sprintf(pd->mapText, "x%d", pd->mapZoomLevel);
}
if(pd->mapScrollX < 320-(128*pd->mapZoomLevel)) pd->mapScrollX = 320-(128*pd->mapZoomLevel);
else if(pd->mapScrollX > 0) pd->mapScrollX = 0;
if(pd->mapScrollY < 240-(128*pd->mapZoomLevel)) pd->mapScrollY = 240-(128*pd->mapZoomLevel);
else if(pd->mapScrollY > 0) pd->mapScrollY = 0;
return;
} else if(pd->inputs.k_touch.px > 256 && pd->inputs.k_touch.px < 284){
if(pd->mapZoomLevel < 4) return;
if(!pd->touchIsChangingSize && !pd->touchIsDraggingMap){
pd->mapScrollX += (50 * (pd->mapZoomLevel/2));
pd->mapScrollY += (40 * (pd->mapZoomLevel/2));
pd->mapZoomLevel -= 2;
pd->touchIsChangingSize = true;
sprintf(pd->mapText, "x%d", pd->mapZoomLevel);
}
if(pd->mapScrollX < 320-(128*pd->mapZoomLevel)) pd->mapScrollX = 320-(128*pd->mapZoomLevel);
else if(pd->mapScrollX > 0) pd->mapScrollX = 0;
if(pd->mapScrollY < 240-(128*pd->mapZoomLevel)) pd->mapScrollY = 240-(128*pd->mapZoomLevel);
else if(pd->mapScrollY > 0) pd->mapScrollY = 0;
return;
}
} else if(pd->inputs.k_touch.py > 8 && pd->inputs.k_touch.py < 40 && pd->inputs.k_touch.px > 284 && pd->inputs.k_touch.px < 312){
// Exit Button
if(!pd->touchIsChangingSize && !pd->touchIsDraggingMap){
pd->mapShouldRender = false;
return;
}
}
if(!pd->touchIsDraggingMap){
pd->touchLastX = pd->inputs.k_touch.px;
pd->touchLastY = pd->inputs.k_touch.py;
}
if(pd->mapZoomLevel > 2){
int dx = pd->touchLastX - pd->inputs.k_touch.px;
if(dx > 1 || dx < -1){
pd->mapScrollX -= dx;
if(pd->mapScrollX < 320-(128*pd->mapZoomLevel)) pd->mapScrollX = 320-(128*pd->mapZoomLevel);
else if(pd->mapScrollX > 0) pd->mapScrollX = 0;
}
pd->touchLastX = pd->inputs.k_touch.px;
}
int dy = pd->touchLastY - pd->inputs.k_touch.py;
if(dy > 1 || dy < -1){
pd->mapScrollY -= dy;
if(pd->mapScrollY < 240-(128*pd->mapZoomLevel)) pd->mapScrollY = 240-(128*pd->mapZoomLevel);
else if(pd->mapScrollY > 0) pd->mapScrollY = 0;
}
pd->touchLastY = pd->inputs.k_touch.py;
pd->touchIsDraggingMap = true;
} else {
pd->touchIsDraggingMap = false;
pd->touchIsChangingSize = false;
}
} else {
// touch minimap to bring up zoomed map.
if(pd->inputs.k_touch.py > 100 && pd->inputs.k_touch.py < 228 && pd->inputs.k_touch.px > 10 && pd->inputs.k_touch.px < 142){
pd->mapShouldRender = true;
}
}
}
void tickTouchQuickSelect(PlayerData *pd) {
if (pd->ingameMenu == MENU_NONE && !pd->mapShouldRender) {
int i = 0;
Inventory * inv = &(pd->inventory);
for (i = 0; i < 8; ++i) {
if((inv->lastSlot) > i) {
int xip = i % 4;
int yip = i / 4;
if(pd->inputs.k_touch.py > 72*2+yip*21*2 && pd->inputs.k_touch.py < 72*2+yip*21*2+21*2 && pd->inputs.k_touch.px > 76*2+xip*21*2 && pd->inputs.k_touch.px < 76*2+xip*21*2+21*2) {
playerSetActiveItem(pd, &inv->items[i]);
}
}
}
}
}
void ingameMenuTickTouch(PlayerData *pd) {
tickTouchMap(pd);
tickTouchQuickSelect(pd);
}

View file

@ -1,8 +0,0 @@
#pragma once
#include "Player.h"
void ingameMenuTick(PlayerData *pd, int menu);
void ingameMenuRender(PlayerData *pd, int menu);
void ingameMenuTickTouch(PlayerData *pd);

66
source/Input.c Executable file → Normal file
View file

@ -5,57 +5,19 @@ void toggleKey(Key* key, bool held, bool down){
key->clicked = down;
}
void tickKeys(Inputs *inputs, u32 held, u32 down){
hidTouchRead(&(inputs->k_touch)); // Update touch position
toggleKey(&(inputs->k_up), held & localInputs.k_up.input, down & localInputs.k_up.input);
toggleKey(&(inputs->k_down), held & localInputs.k_down.input, down & localInputs.k_down.input);
toggleKey(&(inputs->k_left), held & localInputs.k_left.input, down & localInputs.k_left.input);
toggleKey(&(inputs->k_right), held & localInputs.k_right.input, down & localInputs.k_right.input);
toggleKey(&(inputs->k_pause), held & localInputs.k_pause.input, down & localInputs.k_pause.input);
toggleKey(&(inputs->k_attack), held & localInputs.k_attack.input, down & localInputs.k_attack.input);
toggleKey(&(inputs->k_pickup), held & localInputs.k_pickup.input, down & localInputs.k_pickup.input);
toggleKey(&(inputs->k_use), held & localInputs.k_use.input, down & localInputs.k_use.input);
toggleKey(&(inputs->k_menu), held & localInputs.k_menu.input, down & localInputs.k_menu.input);
toggleKey(&(inputs->k_accept), held & localInputs.k_accept.input, down & localInputs.k_accept.input);
toggleKey(&(inputs->k_decline), held & localInputs.k_decline.input, down & localInputs.k_decline.input);
toggleKey(&(inputs->k_delete), held & localInputs.k_delete.input, down & localInputs.k_delete.input);
toggleKey(&(inputs->k_menuNext), held & localInputs.k_menuNext.input, down & localInputs.k_menuNext.input);
toggleKey(&(inputs->k_menuPrev), held & localInputs.k_menuPrev.input, down & localInputs.k_menuPrev.input);
void tickKeys(u32 held, u32 down){
hidTouchRead(&k_touch); // Update touch position
toggleKey(&k_up, held & k_up.input, down & k_up.input);
toggleKey(&k_down, held & k_down.input, down & k_down.input);
toggleKey(&k_left, held & k_left.input, down & k_left.input);
toggleKey(&k_right, held & k_right.input, down & k_right.input);
toggleKey(&k_pause, held & k_pause.input, down & k_pause.input);
toggleKey(&k_attack, held & k_attack.input, down & k_attack.input);
toggleKey(&k_menu, held & k_menu.input, down & k_menu.input);
toggleKey(&k_accept, held & k_accept.input, down & k_accept.input);
toggleKey(&k_decline, held & k_decline.input, down & k_decline.input);
toggleKey(&k_delete, held & k_delete.input, down & k_delete.input);
toggleKey(&k_menuNext, held & k_menuNext.input, down & k_menuNext.input);
toggleKey(&k_menuPrev, held & k_menuPrev.input, down & k_menuPrev.input);
}
void resetKeys(Inputs *inputs) {
(inputs->k_touch).px = -1;
(inputs->k_touch).py = -1;
toggleKey(&(inputs->k_up), false, false);
toggleKey(&(inputs->k_down), false, false);
toggleKey(&(inputs->k_left), false, false);
toggleKey(&(inputs->k_right), false, false);
toggleKey(&(inputs->k_pause), false, false);
toggleKey(&(inputs->k_attack), false, false);
toggleKey(&(inputs->k_pickup), false, false);
toggleKey(&(inputs->k_use), false, false);
toggleKey(&(inputs->k_menu), false, false);
toggleKey(&(inputs->k_accept), false, false);
toggleKey(&(inputs->k_decline), false, false);
toggleKey(&(inputs->k_delete), false, false);
toggleKey(&(inputs->k_menuNext), false, false);
toggleKey(&(inputs->k_menuPrev), false, false);
}
void resetClicked(Inputs *inputs) {
inputs->k_up.clicked = false;
inputs->k_down.clicked = false;
inputs->k_left.clicked = false;
inputs->k_right.clicked = false;
inputs->k_pause.clicked = false;
inputs->k_attack.clicked = false;
inputs->k_pickup.clicked = false;
inputs->k_use.clicked = false;
inputs->k_menu.clicked = false;
inputs->k_accept.clicked = false;
inputs->k_decline.clicked = false;
inputs->k_delete.clicked = false;
inputs->k_menuNext.clicked = false;
inputs->k_menuPrev.clicked = false;
}

13
source/Input.h Executable file → Normal file
View file

@ -1,22 +1,16 @@
#pragma once
#include <3ds.h>
//only down and clicked need to be send, input is for config stuff
typedef struct {
bool down, clicked;
int input;
} Key;
typedef struct {
Key k_null;
Key k_up;
Key k_down;
Key k_left;
Key k_right;
Key k_attack;
Key k_pickup;
Key k_use;
Key k_menu;
Key k_pause;
Key k_accept;
@ -25,11 +19,6 @@ typedef struct {
Key k_menuNext;
Key k_menuPrev;
touchPosition k_touch;
} Inputs;
Inputs localInputs;
void tickKeys(Inputs *inputs, u32 held, u32 down);
void resetKeys(Inputs *inputs);
void resetClicked(Inputs *inputs);
void tickKeys(u32 held, u32 down);
bool clicked(Key key);

30
source/Item.c Executable file → Normal file
View file

@ -155,7 +155,6 @@ char* getItemName(int itemID, int countLevel) {
case ITEM_STRENGTH_POTION: sprintf(currentName,"%d Strength Potion", countLevel); return currentName;
case ITEM_SPEED_POTION: sprintf(currentName,"%d Speed Potion", countLevel); return currentName;
case ITEM_REGEN_POTION: sprintf(currentName,"%d Regen Potion", countLevel); return currentName;
case ITEM_SWIM_BREATH_POTION: sprintf(currentName,"%d Swim Potion", countLevel); return currentName;
case ITEM_COAL: sprintf(currentName,"%d Coal", countLevel); return currentName;
case ITEM_IRONORE: sprintf(currentName,"%d Iron ore", countLevel); return currentName;
case ITEM_GOLDORE: sprintf(currentName,"%d Gold ore", countLevel); return currentName;
@ -200,20 +199,6 @@ char* getItemName(int itemID, int countLevel) {
}
case TOOL_BOW: return "Bow";
case TOOL_MAGIC_COMPASS: return "Magic Compass";
case TOOL_CLAYMORE:
switch(countLevel){
case 1: return "Rock Claymore";
case 2: return "Iron Claymore";
case 3: return "Gold Claymore";
case 4: return "Gem Claymore";
default: return "Wood Claymore";
}
case ITEM_TORCH: return "Torch";
case ITEM_BED: return "Bed";
case ITEM_FISHING_ROD: return "Fishing Rod";
case ITEM_SHEARS: return "Shears";
case ITEM_FISH_RAW: sprintf(currentName,"%d Fish", countLevel); return currentName;
case ITEM_FISH_COOKED: sprintf(currentName,"%d Cooked Fish", countLevel); return currentName;
default: return ""; // null
}
}
@ -321,7 +306,6 @@ char* getBasicItemName(int itemID, int countLevel) {
case ITEM_STRENGTH_POTION: return "Strength Potion";
case ITEM_SPEED_POTION: return "Speed Potion";
case ITEM_REGEN_POTION: return "Regen Potion";
case ITEM_SWIM_BREATH_POTION: return "Water Potion";
case TOOL_BUCKET:
switch(countLevel){
case 1: return "Water Bucket";
@ -330,20 +314,6 @@ char* getBasicItemName(int itemID, int countLevel) {
}
case TOOL_BOW: return "Bow";
case TOOL_MAGIC_COMPASS: return "Magic Compass";
case TOOL_CLAYMORE:
switch(countLevel){
case 1: return "Rock Claymore";
case 2: return "Iron Claymore";
case 3: return "Gold Claymore";
case 4: return "Gem Claymore";
default: return "Wood Claymore";
}
case ITEM_TORCH: return "Torch";
case ITEM_BED: return "Bed";
case ITEM_FISHING_ROD: return "Fishing Rod";
case ITEM_SHEARS: return "Shears";
case ITEM_FISH_RAW: return "Fish";
case ITEM_FISH_COOKED: return "Cooked Fish";
default: return ""; // null
}

10
source/Item.h Executable file → Normal file
View file

@ -46,9 +46,6 @@
#define ITEM_LOOM 34
#define ITEM_ENCHANTER 35
#define ITEM_POTION_MAKER 36
#define TOOL_CLAYMORE 37
#define ITEM_TORCH 38
#define ITEM_BED 39
#define ITEM_WALL_WOOD 51
#define ITEM_WALL_STONE 52
@ -79,17 +76,10 @@
#define ITEM_STRENGTH_POTION 77
#define ITEM_SPEED_POTION 78
#define ITEM_REGEN_POTION 79
#define ITEM_SWIM_BREATH_POTION 80
#define ITEM_FISH_RAW 81
#define ITEM_FISH_COOKED 82
#define TOOL_BUCKET 101
#define TOOL_BOW 102
#define TOOL_MAGIC_COMPASS 103
#define ITEM_FISHING_ROD 104
#define ITEM_SHEARS 105
#define ARMOR_TEST 120
typedef struct Inventory Inventory;

26
source/MapGen.c Executable file → Normal file
View file

@ -63,6 +63,10 @@ double * Noise(int width, int height, int featureSize) {
return values;
}
void newSeed(){
srand(time(NULL));
}
//TODO: Will need to reset entity manager if generation is retried
void createAndValidateTopMap(int w, int h, int level, u8 * map, u8 * data) {
do {
@ -327,10 +331,8 @@ void createUndergroundMap(int w, int h, int depthLevel, int level, u8 * map, u8
}
}
//generate dwarf house
if(depthLevel==3) {
createDwarfHouse(w, h, level, map, data);
//generate mushroom patches
} else if(depthLevel==2) {
for (i = 0; i < w * h / 5400; ++i) {
int xs = rand()%w;
@ -359,7 +361,6 @@ void createUndergroundMap(int w, int h, int depthLevel, int level, u8 * map, u8
}
}
//generate ores
for (i = 0; i < w * h / 400; ++i) {
int x = rand()%w;
int y = rand()%h;
@ -373,8 +374,6 @@ void createUndergroundMap(int w, int h, int depthLevel, int level, u8 * map, u8
}
}
}
//generate stairs down
if (depthLevel < 3){
int sCount, attempts = 0;
for (sCount = 0; sCount < 4;) {
@ -397,22 +396,6 @@ void createUndergroundMap(int w, int h, int depthLevel, int level, u8 * map, u8
if(attempts < (w*h/100)) ++attempts; else break;
}
}
//generate dungeon entrance
if(depthLevel==3) {
map[w/2+0 + (h/2+0) * w] = TILE_DUNGEON_ENTRANCE;
map[w/2-1 + (h/2+0) * w] = TILE_DIRT;
map[w/2+1 + (h/2+0) * w] = TILE_DIRT;
map[w/2+0 + (h/2-1) * w] = TILE_DIRT;
map[w/2+1 + (h/2+1) * w] = TILE_DIRT;
map[w/2-1 + (h/2-1) * w] = TILE_DUNGEON_WALL;
map[w/2-1 + (h/2+1) * w] = TILE_DUNGEON_WALL;
map[w/2+1 + (h/2-1) * w] = TILE_DUNGEON_WALL;
map[w/2+1 + (h/2+1) * w] = TILE_DUNGEON_WALL;
}
free(mnoise1);
free(mnoise2);
free(mnoise3);
@ -875,3 +858,4 @@ void createDungeonRoom(int w, int h, bool dragon, int level, u8 * map, u8 * data
break;
}
}

2
source/MapGen.h Executable file → Normal file
View file

@ -2,6 +2,7 @@
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <3ds.h>
#include "Globals.h"
@ -9,6 +10,7 @@
float nextFloat();
double sample(double * values, int x, int y);
double * Noise(int w, int h, int featureSize);
void newSeed();
void createAndValidateTopMap(int w, int h, int level, u8 * map, u8 * data);
void createTopMap(int w, int h, int level, u8 * map, u8 * data);
void createAndValidateUndergroundMap(int w, int h, int depthLevel, int level, u8 * map, u8 * data);

871
source/Menu.c Executable file → Normal file

File diff suppressed because it is too large Load diff

0
source/Menu.h Executable file → Normal file
View file

48
source/MenuTutorial.c Executable file → Normal file
View file

@ -25,18 +25,18 @@ void renderTutorialPage(bool topScreen){
case 0: // Moving the character
drawTextColor("Movement",(400-8*12)/2,40,0xFF007FBF);
drawText("Press to move up",92,90);
renderButtonIcon(biasedCirclePad(localInputs.k_up.input), 164, 88, 1);
renderButtonIcon(biasedCirclePad(k_up.input), 164, 88, 1);
drawText("Press to move down",80,120);
renderButtonIcon(biasedCirclePad(localInputs.k_down.input), 152, 118, 1);
renderButtonIcon(biasedCirclePad(k_down.input), 152, 118, 1);
drawText("Press to move left",80,150);
renderButtonIcon(biasedCirclePad(localInputs.k_left.input), 152, 148, 1);
renderButtonIcon(biasedCirclePad(k_left.input), 152, 148, 1);
drawText("Press to move right",74,180);
renderButtonIcon(biasedCirclePad(localInputs.k_right.input), 146, 178, 1);
renderButtonIcon(biasedCirclePad(k_right.input), 146, 178, 1);
break;
case 1: // Attacking
drawTextColor("Attacking",(400-9*12)/2,40,0xFF007FBF);
drawText("Press to Attack",98,80);
renderButtonIcon(localInputs.k_attack.input & -localInputs.k_attack.input, 168, 78, 1);
renderButtonIcon(k_attack.input & -k_attack.input, 168, 78, 1);
drawText("Attack with an item to use it",26,120);
drawText("Use the axe to cut down trees",26,140);
drawText("Use the sword to attack enemies",14,160);
@ -46,21 +46,21 @@ void renderTutorialPage(bool topScreen){
case 2: // Inventory
drawTextColor("Inventory",(400-9*12)/2,40,0xFF007FBF);
drawText("Press to open the menu",56,80);
renderButtonIcon(biasedMenuXY(localInputs.k_menu.input), 126, 78, 1);
renderButtonIcon(biasedMenuXY(k_menu.input), 126, 78, 1);
drawText("Press to scroll up",80,110);
renderButtonIcon(biasedCirclePad(localInputs.k_up.input), 152, 108, 1);
renderButtonIcon(biasedCirclePad(k_up.input), 152, 108, 1);
drawText("Press to scroll down",68,140);
renderButtonIcon(biasedCirclePad(localInputs.k_down.input), 140, 138, 1);
renderButtonIcon(biasedCirclePad(k_down.input), 140, 138, 1);
drawText("Press to select an item",50,170);
renderButtonIcon(localInputs.k_accept.input & -localInputs.k_accept.input, 120, 168, 1);
renderButtonIcon(k_accept.input & -k_accept.input, 120, 168, 1);
drawText("Press to close the menu",50,200);
renderButtonIcon(localInputs.k_decline.input & -localInputs.k_decline.input, 120, 198, 1);
renderButtonIcon(k_decline.input & -k_decline.input, 120, 198, 1);
break;
case 3: // Furniture
drawTextColor("Furniture",(400-9*12)/2,40,0xFF007FBF);
drawText("Use furniture for item crafting",(400-31*12)/2,74);
drawText("Press to open the menu",56,100);
renderButtonIcon(biasedMenuXY(localInputs.k_menu.input), 126, 98, 1);
renderButtonIcon(biasedMenuXY(k_menu.input), 126, 98, 1);
drawText("while infront of the furniture",(400-30*12)/2,116);
drawText("Use the lantern item to light",(400-29*12)/2,144);
drawText("up underground areas",(400-20*12)/2,160);
@ -72,10 +72,10 @@ void renderTutorialPage(bool topScreen){
drawText("Create new items and tools",(400-26*12)/2,74);
drawText("Go up to a furniture item and",(400-29*12)/2,104);
drawText("Press to open the menu",56,120);
renderButtonIcon(biasedMenuXY(localInputs.k_menu.input), 126, 118, 1);
renderButtonIcon(biasedMenuXY(k_menu.input), 126, 118, 1);
drawText("Gather up the required materials",(400-32*12)/2,150);
drawText("and then press to craft it",(400-28*12)/2,166);
renderButtonIcon(localInputs.k_accept.input & -localInputs.k_accept.input, 210, 164, 1);
renderButtonIcon(k_accept.input & -k_accept.input, 210, 164, 1);
break;
case 5: // Farming
drawTextColor("Farming",(400-7*12)/2,40,0xFF007FBF);
@ -108,17 +108,17 @@ void renderTutorialPage(bool topScreen){
switch(pageNum){
case 0: // Moving the character
render16(30,56,16,112,0);//Player up
renderButtonIcon(biasedCirclePad(localInputs.k_up.input), 30,40, 2);
renderButtonIcon(biasedCirclePad(k_up.input), 30,40, 2);
render16(60,56,0,112,0);//Player down
renderButtonIcon(biasedCirclePad(localInputs.k_down.input), 60,40, 2);
renderButtonIcon(biasedCirclePad(k_down.input), 60,40, 2);
render16(90,56,48,112,1);//Player left
renderButtonIcon(biasedCirclePad(localInputs.k_left.input), 90,40, 2);
renderButtonIcon(biasedCirclePad(k_left.input), 90,40, 2);
render16(120,56,48,112,0);//Player right
renderButtonIcon(biasedCirclePad(localInputs.k_right.input), 120,40, 2);
renderButtonIcon(biasedCirclePad(k_right.input), 120,40, 2);
break;
case 1: // Attacking
render16(60,56,0,112,0);//Player-down
renderButtonIcon(localInputs.k_attack.input & -localInputs.k_attack.input, 80, 56, 2);
renderButtonIcon(k_attack.input & -k_attack.input, 80, 56, 2);
renderc(60,68,16,160,16,8,2);//Slash
menuRenderTilePit(12,20,256,0);// grass pit
@ -143,9 +143,9 @@ void renderTutorialPage(bool topScreen){
renderItemStuffWithText(ITEM_IRONINGOT,11,false,80,142);
sf2d_draw_rectangle(64, 110, 12, 12, 0xFF);
drawText(">", 64, 110);
renderButtonIcon(biasedCirclePad(localInputs.k_up.input), 44, 92, 1);
renderButtonIcon(localInputs.k_accept.input & -localInputs.k_accept.input, 44, 108, 1);
renderButtonIcon(biasedCirclePad(localInputs.k_down.input), 44, 125, 1);
renderButtonIcon(biasedCirclePad(k_up.input), 44, 92, 1);
renderButtonIcon(k_accept.input & -k_accept.input, 44, 108, 1);
renderButtonIcon(biasedCirclePad(k_down.input), 44, 125, 1);
break;
case 3: // Furniture
sf2d_draw_rectangle(64, 48, 192, 32, grassColor);
@ -248,13 +248,13 @@ void renderTutorialPage(bool topScreen){
drawText(pageText,(320-(strlen(pageText))*12)/2,12);
if(pageNum > 0){
drawText("<",2,16);
renderButtonIcon(localInputs.k_menuPrev.input & -localInputs.k_menuPrev.input, 8, 2, 2);
renderButtonIcon(k_menuPrev.input & -k_menuPrev.input, 8, 2, 2);
}
if(pageNum < maxPageNum){
drawText(">",306,16);
renderButtonIcon(localInputs.k_menuNext.input & -localInputs.k_menuNext.input, 136, 2, 2);
renderButtonIcon(k_menuNext.input & -k_menuNext.input, 136, 2, 2);
}
drawText("Press to exit",(320-(15*12))/2,218);
renderButtonIcon(localInputs.k_decline.input & -localInputs.k_decline.input, 140, 216, 1);
renderButtonIcon(k_decline.input & -k_decline.input, 140, 216, 1);
}
}

0
source/MenuTutorial.h Executable file → Normal file
View file

492
source/Network.c Executable file → Normal file
View file

@ -14,245 +14,11 @@ bool isConnected;
bool isServer;
size_t networkBufferSize;
void *networkBuffer;
u32 *networkBuffer;
udsNetworkStruct networkStruct;
udsBindContext networkBindCtx;
udsConnectionStatus networkStatus;
u16 networkConnectedMask;
//new code
//structure in buffer is u16(seqID),u16(size),size(data), ...
void *networkSendBuffer;
size_t networkSendBufferStartPos;
size_t networkSendBufferEndPos;
size_t networkSendBufferWrapPos;
u16 networkSeqSendNext;
u16 networkSeqSendConf[UDS_MAXNODES+1];
u16 networkSeqRecvLast[UDS_MAXNODES+1];
void *networkAckBuffer;
//async internal send/recieve handling
Thread networkThread;
volatile bool networkRunThread;
LightLock sendBufferLock;
void networkHandleSend();
void networkHandleRecieve();
void clearSendAckedBuffer();
bool sendAck(u16 target, u16 ack);
void networkThreadMain(void *arg) {
while(networkRunThread) {
if(udsRunning && isConnected) {
networkUpdateStatus();
networkHandleRecieve();
networkHandleSend();
}
//TODO: Set meaningfull value, WARNING: Setting this near 1ms (1000*1000) will make everything super laggy, higher values actually work better!
svcSleepThread(10000 * 1000);
}
}
void networkUpdateStatus() {
/*for(int i=0; i<10; i++) {
Result ret = udsGetConnectionStatus(&networkStatus);
if(!R_FAILED(ret)) {
return;
}
}*/
if(udsWaitConnectionStatusEvent(false, false)) {
udsGetConnectionStatus(&networkStatus);
}
}
void networkHandleRecieve() {
bool recieved = false;
do {
recieved = false;
size_t actualSize = 0;
u16 sourceNetworkNodeID;
u32 ackToSend = 0;
memset(networkBuffer, 0, networkBufferSize);
Result ret = udsPullPacket(&networkBindCtx, networkBuffer, networkBufferSize, &actualSize, &sourceNetworkNodeID);
if(R_FAILED(ret)) {
//TODO: what do?
//actualSize will be 0 if no packet is available
} else if(actualSize) {
void *readPointer = networkBuffer;
//ack frame
if(actualSize==sizeof(u16)) {
networkSeqSendConf[sourceNetworkNodeID] = *((u16*) readPointer);
clearSendAckedBuffer();
//normal frame
} else {
while(actualSize>0) {
//read seqID and size
u16 seqID = *((u16*) readPointer);
readPointer += sizeof(u16);
actualSize -= sizeof(u16);
u16 size = *((u16*) readPointer);
readPointer += sizeof(u16);
actualSize -= sizeof(u16);
//if the seq id was expected handle the packet
u16 nextID = networkGetExpectedSeqFrom(sourceNetworkNodeID);
if(seqID==nextID) {
networkSeqRecvLast[sourceNetworkNodeID] = seqID;
ackToSend = seqID;
//handle data - TODO: WARNING: Do not send sizeof(u16) packets or else they will get confused with this one
if(size==sizeof(u16)) {
networkConnectedMask = *((u16*) readPointer);
} else {
processPacket(readPointer, size);
}
} else if(networkSeqIsLowerThan(seqID, nextID)) {
ackToSend = seqID;
}
readPointer += size;
actualSize -= size;
}
if(ackToSend!=0) {
if(sendAck(sourceNetworkNodeID, ackToSend)) {
}
}
}
recieved = true;
}
} while(recieved);
}
void networkHandleSend() {
if(networkSendBufferStartPos!=networkSendBufferEndPos) {
LightLock_Lock(&sendBufferLock);
//determine send size
size_t currentSize = 0;
while(networkSendBufferStartPos+currentSize<networkSendBufferWrapPos && networkSendBufferStartPos+currentSize!=networkSendBufferEndPos) {
//size of "header info" (seqid,size)
size_t extraSize = sizeof(u16) + sizeof(u16);
//data size
extraSize += *((u16*) ((networkSendBuffer+networkSendBufferStartPos+currentSize) + sizeof(u16)));
//if next packet can fit in frame include it
if(currentSize+extraSize < UDS_DATAFRAME_MAXSIZE) {
currentSize += extraSize;
} else {
break;
}
}
//send frame
if(currentSize>0) {
//TODO: Once we have our own custom mask, no longer broadcast, but send directly because bcast doesn't always reach everyone?
if(networkConnectedMask==0) {
//send frame
Result ret = udsSendTo(UDS_BROADCAST_NETWORKNODEID, NETWORK_CHANNEL, UDS_SENDFLAG_Default, networkSendBuffer+networkSendBufferStartPos, currentSize);
if(UDS_CHECK_SENDTO_FATALERROR(ret)) {
//TODO: what do?
} else if(R_FAILED(ret)) {
//TODO: what do?
}
} else {
for(int i=1; i<=UDS_MAXNODES; i++) {
if(i!=networkGetLocalNodeID()/* && networkIsNodeConnected(i)*/ && networkConnectedMask & (1 << (i-1))) {
//send frame
Result ret = udsSendTo(i, NETWORK_CHANNEL, UDS_SENDFLAG_Default, networkSendBuffer+networkSendBufferStartPos, currentSize);
if(UDS_CHECK_SENDTO_FATALERROR(ret)) {
//TODO: what do?
} else if(R_FAILED(ret)) {
//TODO: what do?
}
}
}
}
}
LightLock_Unlock(&sendBufferLock);
}
}
void clearSendAckedBuffer() {
//find last ack recieved from all com partners
u16 ackID = 0;
for(int i=1; i<=UDS_MAXNODES; i++) {
if(i!=networkGetLocalNodeID()/* && networkIsNodeConnected(i)*/ && networkConnectedMask & (1 << (i-1))) {
if(networkSeqSendConf[i]==0) {
ackID = 0;
return;
}
if(ackID==0) {
ackID = networkSeqSendConf[i];
} else if(networkSeqSendConf[i]<100) {
if(ackID > networkSeqSendConf[i] && ackID<65535-100) ackID = networkSeqSendConf[i];
} else if(networkSeqSendConf[i]>65535-100) {
if(ackID > networkSeqSendConf[i] || ackID<100) ackID = networkSeqSendConf[i];
} else {
if(ackID > networkSeqSendConf[i]) ackID = networkSeqSendConf[i];
}
}
}
if(ackID==0) return;
LightLock_Lock(&sendBufferLock);
//clear buffer of acknowledgt packets
while(networkSendBufferStartPos!=networkSendBufferEndPos) {
//find current seqid and size
u16 seqID = *((u16*) (networkSendBuffer+networkSendBufferStartPos));
u16 size = *((u16*) (networkSendBuffer+networkSendBufferStartPos+sizeof(u16)));
if(seqID<=ackID || (ackID<100 && seqID>65535-100)) {
size_t currentSize = sizeof(u16)*2 + size;
//adjust buffer "pointers"
networkSendBufferStartPos += currentSize;
if(networkSendBufferStartPos==networkSendBufferEndPos) {
networkSendBufferStartPos = 0;
networkSendBufferEndPos = 0;
networkSendBufferWrapPos = 0;
}
//wrap
if(networkSendBufferStartPos==networkSendBufferWrapPos) {
networkSendBufferStartPos = 0;
networkSendBufferWrapPos = networkSendBufferEndPos;
}
} else {
break;
}
}
LightLock_Unlock(&sendBufferLock);
}
bool sendAck(u16 target, u16 ack) {
Result ret = udsSendTo(target, NETWORK_CHANNEL, UDS_SENDFLAG_Default, &ack, sizeof(u16));
if(UDS_CHECK_SENDTO_FATALERROR(ret)) {
//TODO: what do?
return false;
} else if(R_FAILED(ret)) {
//TODO: what do?
return false;
} else {
return true;
}
}
void networkInit() {
Result ret = udsInit(0x3000, NULL);
if(R_FAILED(ret)) {
@ -264,80 +30,20 @@ void networkInit() {
scannedNetworks = NULL;
isConnected = false;
isServer = false;
networkConnectedMask = 0;
networkWriteBuffer = malloc(NETWORK_MAXDATASIZE);
if(networkWriteBuffer==NULL) {
networkExit();
return;
}
networkBufferSize = 0x4000;
networkBuffer = malloc(networkBufferSize);
if(networkBuffer==NULL) {
networkExit();
return;
}
networkSendBufferStartPos = 0;
networkSendBufferEndPos = 0;
networkSendBufferWrapPos = 0;
networkSendBuffer = malloc(NETWORK_SENDBUFFERSIZE);
if(networkSendBuffer==NULL) {
networkExit();
return;
}
networkSeqSendNext = 1;
for(int i=0; i<UDS_MAXNODES+1; i++) {
networkSeqSendConf[i] = 0;
networkSeqRecvLast[i] = 0;
}
networkAckBuffer = malloc(sizeof(u16)+sizeof(u16)+sizeof(u16));
if(networkAckBuffer==NULL) {
networkExit();
return;
}
LightLock_Init(&sendBufferLock);
s32 prio = 0;
svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
//NOTE: It is important the networkThread is prioritized over the main thread (so substract 1) or else nothing will work
networkRunThread = true;
networkThread = threadCreate(networkThreadMain, NULL, NETWORK_STACKSIZE, prio-1, -2, false);
}
}
void networkExit() {
//Additionally to shutting down the service, clear any left over memory!
//TODO: Additionally to shutting down the service, clear any left over memory!
if(udsRunning) {
udsRunning = false;
if(networkRunThread) {
networkRunThread = false;
threadJoin(networkThread, U64_MAX);
threadFree(networkThread);
}
//cleanup any dynamically reserved memory
if(scannedNetworks!=NULL) free(scannedNetworks);
scannedNetworks = NULL;
if(networkWriteBuffer!=NULL) free(networkWriteBuffer);
networkWriteBuffer = NULL;
if(networkBuffer!=NULL) free(networkBuffer);
networkBuffer = NULL;
if(networkSendBuffer!=NULL) free(networkSendBuffer);
networkSendBuffer = NULL;
if(networkAckBuffer!=NULL) free(networkAckBuffer);
networkAckBuffer = NULL;
free(networkBuffer);
networkDisconnect();
udsExit();
@ -360,22 +66,14 @@ bool networkHost() {
if(R_FAILED(ret)) {
return false;
} else {
if(udsWaitConnectionStatusEvent(false, false)) {}
udsGetConnectionStatus(&networkStatus);
udsSetNewConnectionsBlocked(false, true, false);
isConnected = true;
isServer = true;
networkConnectedMask = 0;
return true;
}
}
return false;
}
void networkHostStopConnections() {
udsSetNewConnectionsBlocked(true, true, false);
}
void networkScan() {
if(udsRunning) {
//reset values from last scan
@ -405,7 +103,7 @@ bool networkGetScanName(char *name, int pos) {
if(udsRunning) {
if(pos<0 || pos>=scannedNetworksCount) return false;
Result ret = udsGetNodeInfoUsername(&(scannedNetworks[pos].nodes[0]), name);
Result ret = udsGetNodeInfoUsername(&scannedNetworks[pos].nodes[0], name);
if(R_FAILED(ret)) {
//TODO: what do?
return false;
@ -423,11 +121,8 @@ bool networkConnect(int pos) {
if(R_FAILED(ret)) {
return false;
} else {
if(udsWaitConnectionStatusEvent(false, false)) {}
udsGetConnectionStatus(&networkStatus);
isConnected = true;
isServer = false;
networkConnectedMask = 0;
return true;
}
}
@ -435,186 +130,73 @@ bool networkConnect(int pos) {
}
void networkDisconnect() {
//For clients this just means disconnect, for the server it means destroy the network
//TODO: For clients this just means disconnect, for the server it means destroy the network
if(udsRunning && isConnected) {
isConnected = false;
LightLock_Lock(&sendBufferLock);
//reset send buffer
networkSendBufferStartPos = 0;
networkSendBufferEndPos = 0;
networkSendBufferWrapPos = 0;
//reset ack status
networkSeqSendNext = 1;
for(int i=0; i<UDS_MAXNODES+1; i++) {
networkSeqSendConf[i] = 0;
networkSeqRecvLast[i] = 0;
}
LightLock_Unlock(&sendBufferLock);
//With new changes citra now crashes HOST with "cannot be a router if we are not a host" when exiting game with more than 2 people
svcSleepThread(220000 * 1000); //HACK: prevent citra crash (>20*networkthreadsleep) (wait unti no more stuff gets send)
//TODO
if(isServer) {
udsDisconnectNetwork();
} else {
//TODO: Clients need to cleanup too, how can I tell they got disconnected
udsDestroyNetwork();
} else {
udsDisconnectNetwork();
}
udsUnbind(&networkBindCtx);
isServer = false;
isConnected = false;
}
}
void networkStart() {
//TODO: This sends the node_bitmask from server to everyone else, because it is uncorrect on some clients?
if(udsRunning && isConnected && isServer) {
void *buffer = networkWriteBuffer;
*((u16*) buffer) = networkStatus.node_bitmask;
networkConnectedMask = networkStatus.node_bitmask;
networkSend(networkWriteBuffer, sizeof(u16));
networkSendWaitFlush();
}
}
bool networkConnected() {
return isConnected;
}
int networkGetNodeCount() {
bool networkIsServer() {
return isServer;
}
void networkSend(networkPacket *packet, size_t size) {
if(udsRunning && isConnected) {
return networkStatus.total_nodes;
//Server broadcasts to all clients but clients only send info to server
if(isServer) {
networkSendTo(packet, size, UDS_BROADCAST_NETWORKNODEID);
} else {
return 0;
networkSendTo(packet, size, UDS_HOST_NETWORKNODEID);
}
}
}
u16 networkGetLocalNodeID() {
//TODO: I somehow need to implement a relieable protocol on top of uds
void networkSendTo(networkPacket *packet, size_t size, u16 reciever) {
if(udsRunning && isConnected) {
return networkStatus.cur_NetworkNodeID;
} else {
return 0;
Result ret = udsSendTo(reciever, NETWORK_CHANNEL, UDS_SENDFLAG_Default, packet, size);
if(UDS_CHECK_SENDTO_FATALERROR(ret)) {
//TODO: what do?
}
}
}
bool networkIsNodeConnected(u16 id) {
void networkRecieve() {
//TODO: Should recieve actually handle all the packets or just return them?
if(udsRunning && isConnected) {
return networkStatus.node_bitmask & (1 << (id-1));
} else {
return false;
}
}
size_t actualSize = 0;
u16 sourceNetworkNodeID;
bool networkGetNodeName(u16 id, char *name) {
if(udsRunning && isConnected && networkIsNodeConnected(id)) {
udsNodeInfo nodeInfo;
udsGetNodeInformation(id, &nodeInfo);
memset(networkBuffer, 0, networkBufferSize);
Result ret = udsGetNodeInfoUsername(&nodeInfo, name);
Result ret = udsPullPacket(&networkBindCtx, networkBuffer, networkBufferSize, &actualSize, &sourceNetworkNodeID);
if(R_FAILED(ret)) {
//TODO: what do?
return false;
}
return true;
}
return false;
}
u16 networkGetExpectedSeqFrom(u16 id) {
u16 nextID = networkSeqRecvLast[id];
nextID += 1;
if(nextID==0) {
nextID = 1;
}
return nextID;
}
//actualSize will be 0 if no packet is available
if(actualSize) {
networkPacket *packet = (networkPacket*) networkBuffer;
bool networkSeqIsLowerThan(u16 firstID, u16 secondID) {
if (secondID < 100) {
return (firstID < secondID) || (firstID > 65536-100);
} else if (secondID > 65536-100) {
return (firstID < secondID) && (firstID > 100);
} else {
return (firstID < secondID);
processPacket(packet, packet->analyze.type, sourceNetworkNodeID);
}
}
int fitInSendBuffer(size_t size) {
//add "header" length
size += sizeof(u16)*2;
//we have no wrap currently
if(networkSendBufferStartPos<=networkSendBufferEndPos) {
//and can fit without wrap
if(networkSendBufferEndPos+size<NETWORK_SENDBUFFERSIZE) {
networkSendBufferEndPos += size;
networkSendBufferWrapPos = networkSendBufferEndPos;
return networkSendBufferEndPos-size;
//we need to wrap
} else {
if(size<networkSendBufferStartPos) {
networkSendBufferEndPos = size;
return 0;
}
}
//we have wrap currently
} else {
int available = networkSendBufferStartPos - networkSendBufferEndPos - 1;
if(available>size) {
networkSendBufferEndPos += size;
return networkSendBufferEndPos-size;
}
}
return -1;
}
void networkSend(void *packet, size_t size) {
//search for fit in buffer (and BLOCK until free space is found)
LightLock_Lock(&sendBufferLock);
int pos = fitInSendBuffer(size);
while(pos==-1) {
LightLock_Unlock(&sendBufferLock);
svcSleepThread(4500 * 1000); //TODO: Set meaningfull value
LightLock_Lock(&sendBufferLock);
pos = fitInSendBuffer(size);
}
//fit found -> space is allready "reserved" -> write packet to buffer
void *writePointer = networkSendBuffer + pos;
//write seq number
*((u16*) writePointer) = networkSeqSendNext;
networkSeqSendNext++;
if(networkSeqSendNext==0) {
networkSeqSendNext = 1;
}
writePointer += sizeof(u16);
//write size
*((u16*) writePointer) = (u16) size;
writePointer += sizeof(u16);
//write data
memcpy(writePointer, packet, size);
writePointer += size;
LightLock_Unlock(&sendBufferLock);
}
void networkSendWaitFlush() {
while(networkSendBufferStartPos!=networkSendBufferEndPos) {
svcSleepThread(4500 * 1000);
}
}

58
source/Network.h Executable file → Normal file
View file

@ -3,19 +3,45 @@
#include <3ds.h>
#define NETWORK_WLANCOMMID 0x11441850
#define NETWORK_PASSPHRASE "minicraft3dsLP"
#define NETWORK_PASSPHRASE "Minicraft3DS localplay passphrase"
#define NETWORK_CHANNEL 1
#define NETWORK_RECVBUFSIZE UDS_DEFAULT_RECVBUFSIZE
#define NETWORK_MAXDATASIZE 1024
#define NETWORK_SENDBUFFERSIZE ((NETWORK_MAXDATASIZE+256)*10)
#define NETWORK_MAXPLAYERS UDS_MAXNODES
#define NETWORK_STACKSIZE (8*1024)
//packet type ids
#define PACKET_REQUEST_MAPDATA 1
#define PACKET_MAPDATA 2
#define NETWORK_MAXPLAYERS 8
//TODO: Every other packet struct should start with a u8 type to match this
typedef struct {
u8 type;
} packetAnalyze;
void *networkWriteBuffer;
typedef struct {
u8 type;
u8 level;
} packetRequestMapData;
typedef struct {
u8 type;
u8 level;
u8 offset; //-> data is at offset*128 to offset*128+127
u8 map[128];
u8 data[128];
} packetMapData;
typedef struct {
union {
packetAnalyze analyze;
packetRequestMapData requestMapData;
packetMapData mapData;
};
} networkPacket;
void networkInit();
void networkExit();
@ -23,25 +49,15 @@ void networkExit();
bool networkAvailable();
bool networkHost();
void networkHostStopConnections();
void networkScan();
int networkGetScanCount();
bool networkGetScanName(char *name, int pos);
bool networkGetScanName(char *name, int pos); //TODO: Name should be long enough to handle all allowed names (char[256])
bool networkConnect(int pos);
void networkDisconnect();
void networkStart();
void networkUpdateStatus();
bool networkConnected();
bool networkIsServer();
int networkGetNodeCount();
u16 networkGetLocalNodeID();
bool networkIsNodeConnected(u16 id);
bool networkGetNodeName(u16 id, char *name);
u16 networkGetExpectedSeqFrom(u16 id);
bool networkSeqIsLowerThan(u16 firstID, u16 secondID);
void networkSend(void *packet, size_t size);
void networkSendWaitFlush();
void networkSend(networkPacket *packet, size_t size); //TODO: Should this be a pointer? Calling function needs to cleanup itself
void networkSendTo(networkPacket *packet, size_t size, u16 reciever); //TODO: Should this be a pointer? Calling function needs to cleanup itself
void networkRecieve(); //TODO: Should recieve actually handle all the packets or just return them?

377
source/PacketHandler.c Executable file → Normal file
View file

@ -1,357 +1,48 @@
#include "PacketHandler.h"
#include <stdlib.h>
#include "Synchronizer.h"
FILE *recvFile;
size_t recvFileSize;
#include "Globals.h"
void * writeBool(void *buffer, size_t *size, bool value) {
*((bool*) buffer) = value;
*(size) += sizeof(bool);
return buffer + sizeof(bool);
void processPacket(networkPacket *packet, u8 type, u16 sender) {
//TODO: Differenciate the packets and process them
if(networkIsServer()) {
if(type==PACKET_REQUEST_MAPDATA) {
u8 level = packet->requestMapData.level;
if(level>=0 && level<=5) {
//send back tile data
for(int y=0; y<128; y++) {
networkPacket packet = {
.mapData = {
.type = PACKET_MAPDATA,
.level = level,
.offset = y
}
void * writeU8(void *buffer, size_t *size, u8 value) {
*((u8*) buffer) = value;
*(size) += sizeof(u8);
return buffer + sizeof(u8);
};
for(int x=0; x<128; x++) {
packet.mapData.map[x] = map[level][x+y*128];
packet.mapData.data[x] = data[level][x+y*128];
}
void * writeU16(void *buffer, size_t *size, u16 value) {
*((u16*) buffer) = value;
*(size) += sizeof(u16);
return buffer + sizeof(u16);
}
void * writeU32(void *buffer, size_t *size, u32 value) {
*((u32*) buffer) = value;
*(size) += sizeof(u32);
return buffer + sizeof(u32);
}
void * writeSizeT(void *buffer, size_t *size, size_t value) {
*((size_t*) buffer) = value;
*(size) += sizeof(size_t);
return buffer + sizeof(size_t);
}
void * readBool(void *buffer, size_t *size, bool *value) {
*value = *((bool*) buffer);
*(size) -= sizeof(bool);
return buffer + sizeof(bool);
}
void * readU8(void *buffer, size_t *size, u8 *value) {
*value = *((u8*) buffer);
*(size) -= sizeof(u8);
return buffer + sizeof(u8);
}
void * readU16(void *buffer, size_t *size, u16 *value) {
*value = *((u16*) buffer);
*(size) -= sizeof(u16);
return buffer + sizeof(u16);
}
void * readU32(void *buffer, size_t *size, u32 *value) {
*value = *((u32*) buffer);
*(size) -= sizeof(u32);
return buffer + sizeof(u32);
}
void * readSizeT(void *buffer, size_t *size, size_t *value) {
*value = *((size_t*) buffer);
*(size) -= sizeof(size_t);
return buffer + sizeof(size_t);
}
void processPacket(void *packet, size_t size) {
//Differenciate the packets and process them
switch(packetGetID(packet)) {
case PACKET_START: {
void *buffer = packetGetDataStart(packet);
size = packetGetDataSize(size);
//find player index based on network node id
//and set player uuid in synchronizer
u32 seed;
u32 playerCount = 1;
int playerIndex = 0;
buffer = readU32(buffer, &size, &seed);
buffer = readU32(buffer, &size, &playerCount);
for(int i=0; i<playerCount; i++) {
u16 nodeID;
buffer = readU16(buffer, &size, &nodeID);
if(nodeID==networkGetLocalNodeID()) {
playerIndex = i;
networkSendTo(&packet, sizeof(packetMapData), sender);
}
}
//cleanup transfer tmp file
FILE *file = fopen("tmpTransfer.bin", "wb");
if(file!=NULL) {
fclose(file);
remove("tmpTransfer.bin");
} else {
//TODO: Unknown packet - how to handle?
}
} else {
if(type==PACKET_MAPDATA) {
u8 level = packet->mapData.level;
//init synchronizer
synchronizerInit(seed, playerCount, playerIndex);
break;
}
case PACKET_START_FILEHEADER: {
void *data = packetGetDataStart(packet);
u8 type;
u8 id;
size_t fsize;
data = readU8(data, &size, &type);
data = readU8(data, &size, &id);
data = readSizeT(data, &size, &fsize);
recvFile = fopen("tmpTransfer.bin", "wb");
recvFileSize = fsize;
break;
}
case PACKET_START_FILEDATA: {
void *data = packetGetDataStart(packet);
size_t dsize = packetGetDataSize(size);
size_t toread = dsize;
fwrite(data, 1, toread, recvFile);
recvFileSize -= toread;
if(recvFileSize<=0) {
fclose(recvFile);
recvFile = NULL;
}
break;
}
case PACKET_START_REQUEST_IDS: {
synchronizerSendUID();
break;
}
case PACKET_START_ID: {
u8 playerID = packetGetSender(packet);
u32 uid;
void *data = packetGetDataStart(packet);
size_t dsize = packetGetDataSize(size);
data = readU32(data, &dsize, &uid);
synchronizerSetPlayerUID(playerID, uid);
synchronizerSendIfReady();
break;
}
case PACKET_START_READY: {
synchronizerSetPlayerReady(packetGetSender(packet));
if(playerLocalID==0) {
if(synchronizerAllReady()) {
sendStartSyncPacket();
synchronizerStart(); //server needs to call this here, all others do when they recieve the packet
if(level>=0 && level<=5) {
//recieve tile data
//TODO: This should really check whether the values are in valid range
int y = packet->mapData.offset;
for(int x=0; x<128; x++) {
map[level][x+y*128] = packet->mapData.map[x];
data[level][x+y*128] = packet->mapData.data[x];
}
}
break;
}
case PACKET_TURN_START: {
synchronizerStart();
break;
}
case PACKET_TURN_INPUT: {
synchronizerOnInputPacket(packetGetSender(packet), packetGetTurn(packet), packetGetDataStart(packet), packetGetDataSize(size));
break;
} else {
//TODO: Unknown packet - how to handle?
}
}
}
u8 packetGetID(void *packet) {
return *((u8*) packet);
}
u8 packetGetSender(void *packet) {
return *((u8*) packet+sizeof(u8));
}
u32 packetGetTurn(void *packet) {
return *((u32*) (packet+sizeof(u8)+sizeof(u8)));
}
void * packetGetDataStart(void *packet) {
return packet+sizeof(u8)+sizeof(u8)+sizeof(u32);
}
size_t packetGetDataSize(size_t size) {
return size-sizeof(u8)-sizeof(u8)-sizeof(u32);
}
size_t writeStartPacket(void *buffer, u32 seed) {
size_t size = 0;
buffer = writeU8(buffer, &size, PACKET_START);
buffer = writeU8(buffer, &size, 0);
buffer = writeU32(buffer, &size, 0);
buffer = writeU32(buffer, &size, seed);
buffer = writeU32(buffer, &size, (u32)networkGetNodeCount());
for(int i=1; i<=UDS_MAXNODES; i++) {
if(networkIsNodeConnected(i)) {
buffer = writeU16(buffer, &size, (u16)i);
}
}
return size;
}
size_t writeStartRequestPacket(void *buffer) {
size_t size = 0;
buffer = writeU8(buffer, &size, PACKET_START_REQUEST_IDS);
buffer = writeU8(buffer, &size, 0);
buffer = writeU32(buffer, &size, 0);
return size;
}
size_t writeInputPacket(void *buffer, Inputs *inputs, u8 playerID, u32 turnNumber) {
size_t size = 0;
buffer = writeU8(buffer, &size, PACKET_TURN_INPUT);
buffer = writeU8(buffer, &size, playerID);
buffer = writeU32(buffer, &size, turnNumber);
buffer = writeU16(buffer, &size, inputs->k_touch.px);
buffer = writeU16(buffer, &size, inputs->k_touch.py);
buffer = writeBool(buffer, &size, inputs->k_up.down); buffer = writeBool(buffer, &size, inputs->k_up.clicked);
buffer = writeBool(buffer, &size, inputs->k_down.down); buffer = writeBool(buffer, &size, inputs->k_down.clicked);
buffer = writeBool(buffer, &size, inputs->k_left.down); buffer = writeBool(buffer, &size, inputs->k_left.clicked);
buffer = writeBool(buffer, &size, inputs->k_right.down); buffer = writeBool(buffer, &size, inputs->k_right.clicked);
buffer = writeBool(buffer, &size, inputs->k_attack.down); buffer = writeBool(buffer, &size, inputs->k_attack.clicked);
buffer = writeBool(buffer, &size, inputs->k_pickup.down); buffer = writeBool(buffer, &size, inputs->k_pickup.clicked);
buffer = writeBool(buffer, &size, inputs->k_use.down); buffer = writeBool(buffer, &size, inputs->k_use.clicked);
buffer = writeBool(buffer, &size, inputs->k_menu.down); buffer = writeBool(buffer, &size, inputs->k_menu.clicked);
buffer = writeBool(buffer, &size, inputs->k_pause.down); buffer = writeBool(buffer, &size, inputs->k_pause.clicked);
buffer = writeBool(buffer, &size, inputs->k_accept.down); buffer = writeBool(buffer, &size, inputs->k_accept.clicked);
buffer = writeBool(buffer, &size, inputs->k_decline.down); buffer = writeBool(buffer, &size, inputs->k_decline.clicked);
buffer = writeBool(buffer, &size, inputs->k_delete.down); buffer = writeBool(buffer, &size, inputs->k_delete.clicked);
buffer = writeBool(buffer, &size, inputs->k_menuNext.down); buffer = writeBool(buffer, &size, inputs->k_menuNext.clicked);
buffer = writeBool(buffer, &size, inputs->k_menuPrev.down); buffer = writeBool(buffer, &size, inputs->k_menuPrev.clicked);
return size;
}
bool readInputPacketData(void *buffer, size_t size, Inputs *inputs) {
buffer = readU16(buffer, &size, &(inputs->k_touch.px));
if(size<=0) return false;
buffer = readU16(buffer, &size, &(inputs->k_touch.py));
if(size<=0) return false;
buffer = readBool(buffer, &size, &(inputs->k_up.down)); buffer = readBool(buffer, &size, &(inputs->k_up.clicked));
if(size<=0) return false;
buffer = readBool(buffer, &size, &(inputs->k_down.down)); buffer = readBool(buffer, &size, &(inputs->k_down.clicked));
if(size<=0) return false;
buffer = readBool(buffer, &size, &(inputs->k_left.down)); buffer = readBool(buffer, &size, &(inputs->k_left.clicked));
if(size<=0) return false;
buffer = readBool(buffer, &size, &(inputs->k_right.down)); buffer = readBool(buffer, &size, &(inputs->k_right.clicked));
if(size<=0) return false;
buffer = readBool(buffer, &size, &(inputs->k_attack.down)); buffer = readBool(buffer, &size, &(inputs->k_attack.clicked));
if(size<=0) return false;
buffer = readBool(buffer, &size, &(inputs->k_pickup.down)); buffer = readBool(buffer, &size, &(inputs->k_pickup.clicked));
if(size<=0) return false;
buffer = readBool(buffer, &size, &(inputs->k_use.down)); buffer = readBool(buffer, &size, &(inputs->k_use.clicked));
if(size<=0) return false;
buffer = readBool(buffer, &size, &(inputs->k_menu.down)); buffer = readBool(buffer, &size, &(inputs->k_menu.clicked));
if(size<=0) return false;
buffer = readBool(buffer, &size, &(inputs->k_pause.down)); buffer = readBool(buffer, &size, &(inputs->k_pause.clicked));
if(size<=0) return false;
buffer = readBool(buffer, &size, &(inputs->k_accept.down)); buffer = readBool(buffer, &size, &(inputs->k_accept.clicked));
if(size<=0) return false;
buffer = readBool(buffer, &size, &(inputs->k_decline.down)); buffer = readBool(buffer, &size, &(inputs->k_decline.clicked));
if(size<=0) return false;
buffer = readBool(buffer, &size, &(inputs->k_delete.down)); buffer = readBool(buffer, &size, &(inputs->k_delete.clicked));
if(size<=0) return false;
buffer = readBool(buffer, &size, &(inputs->k_menuNext.down)); buffer = readBool(buffer, &size, &(inputs->k_menuNext.clicked));
if(size<=0) return false;
buffer = readBool(buffer, &size, &(inputs->k_menuPrev.down)); buffer = readBool(buffer, &size, &(inputs->k_menuPrev.clicked));
return size==0;
}
void sendFile(FILE *file, u8 fileType, u8 id) {
fseek(file, 0, SEEK_END); // seek to end of file
size_t fsize = ftell(file); // get current file pointer
fseek(file, 0, SEEK_SET); // seek back to beginning of file
//send file header
void *buffer = networkWriteBuffer;
size_t size = 0;
buffer = writeU8(buffer, &size, PACKET_START_FILEHEADER);
buffer = writeU8(buffer, &size, 0);
buffer = writeU32(buffer, &size, 0);
buffer = writeU8(buffer, &size, fileType);
buffer = writeU8(buffer, &size, id);
buffer = writeSizeT(buffer, &size, fsize);
networkSend(networkWriteBuffer, size);
//send file data
while(fsize>0) {
buffer = networkWriteBuffer;
size = 0;
buffer = writeU8(buffer, &size, PACKET_START_FILEDATA);
buffer = writeU8(buffer, &size, 0);
buffer = writeU32(buffer, &size, 0);
//read file data
size_t towrite = NETWORK_MAXDATASIZE - size;
if(towrite>fsize) towrite = fsize;
fread(buffer, 1, towrite, file);
size += towrite;
fsize -= towrite;
//send file data
networkSend(networkWriteBuffer, size);
}
}
void sendIDPacket(u8 playerID, u32 uid) {
void *buffer = networkWriteBuffer;
size_t size = 0;
buffer = writeU8(buffer, &size, PACKET_START_ID);
buffer = writeU8(buffer, &size, playerID);
buffer = writeU32(buffer, &size, 0);
buffer = writeU32(buffer, &size, uid);
networkSend(networkWriteBuffer, size);
}
void sendStartReadyPacket(u8 playerID) {
void *buffer = networkWriteBuffer;
size_t size = 0;
buffer = writeU8(buffer, &size, PACKET_START_READY);
buffer = writeU8(buffer, &size, playerID);
buffer = writeU32(buffer, &size, 0);
networkSend(networkWriteBuffer, size);
}
void sendStartSyncPacket() {
void *buffer = networkWriteBuffer;
size_t size = 0;
buffer = writeU8(buffer, &size, PACKET_TURN_START);
buffer = writeU8(buffer, &size, 0);
buffer = writeU32(buffer, &size, 0);
networkSend(networkWriteBuffer, size);
}

31
source/PacketHandler.h Executable file → Normal file
View file

@ -1,34 +1,5 @@
#pragma once
#include <stdlib.h>
#include "Network.h"
#include "Player.h"
#define PACKET_START 0
#define PACKET_START_FILEHEADER 1
#define PACKET_START_FILEDATA 2
#define PACKET_START_REQUEST_IDS 3
#define PACKET_START_ID 4
#define PACKET_START_READY 5
#define PACKET_TURN_START 10
#define PACKET_TURN_INPUT 11
void processPacket(void *packet, size_t size);
u8 packetGetID(void *packet);
u8 packetGetSender(void *packet);
u32 packetGetTurn(void *packet);
void * packetGetDataStart(void *packet);
size_t packetGetDataSize(size_t size);
size_t writeStartPacket(void *buffer, u32 seed);
size_t writeStartRequestPacket(void *buffer);
size_t writeInputPacket(void *buffer, Inputs *inputs, u8 playerID, u32 turnNumber);
bool readInputPacketData(void *buffer, size_t size, Inputs *inputs);
void sendFile(FILE *file, u8 fileType, u8 id);
void sendIDPacket(u8 playerID, u32 uid);
void sendStartReadyPacket(u8 playerID);
void sendStartSyncPacket();
void processPacket(networkPacket *packet, u8 type, u16 sender);

View file

@ -1,657 +0,0 @@
#include "Player.h"
#include <limits.h>
#include "Globals.h"
void potionEffect(int type) {
if(type == 1) {
UnderStrengthEffect = true;
}
if(type == 2) {
UnderSpeedEffect = true;
}
if (type == 3) {
regening = true;
}
if (type == 4) {
UnderSwimBreathEffect = true;
}
}
void initPlayers() {
for(int i=0; i<MAX_PLAYERS; i++) {
initPlayer(players+i);
}
}
void freePlayers() {
for(int i=0; i<MAX_PLAYERS; i++) {
freePlayer(players+i);
}
}
void playerInitMiniMapData(u8 *minimapData) {
int i;
for(i = 0; i < 128 * 128; ++i) {
minimapData[i] = 0;
}
}
void playerInitEntity(PlayerData *pd) {
pd->entity.type = ENTITY_PLAYER;
pd->entity.level = 1;
pd->entity.xr = 4;
pd->entity.yr = 3;
pd->entity.canSwim = true;
pd->entity.p.ax = 0;
pd->entity.p.ay = 0;
pd->entity.p.health = 10;
pd->entity.p.stamina = 10;
pd->entity.p.walkDist = 0;
pd->entity.p.attackTimer = 0;
pd->entity.p.dir = 0;
pd->entity.p.isDead = false;
pd->entity.p.hasWon = false;
pd->entity.p.data = pd;
}
void playerInitInventory(PlayerData *pd) {
//reset inventory
pd->inventory.lastSlot = 0;
pd->activeItem = &noItem;
if(shouldRenderDebug && playerCount < 2) {
addItemToInventory(newItem(ITEM_POWGLOVE,0), &(pd->inventory));
addItemToInventory(newItem(ITEM_WORKBENCH,0), &(pd->inventory));
addItemToInventory(newItem(ITEM_GOLD_APPLE,1), &(pd->inventory));
addItemToInventory(newItem(ITEM_STRENGTH_POTION,1), &(pd->inventory));
addItemToInventory(newItem(ITEM_REGEN_POTION,1), &(pd->inventory));
addItemToInventory(newItem(ITEM_SWIM_BREATH_POTION,1), &(pd->inventory));
addItemToInventory(newItem(ITEM_SPEED_POTION,1), &(pd->inventory));
addItemToInventory(newItem(ITEM_POTION_MAKER,0), &(pd->inventory));
addItemToInventory(newItem(TOOL_SHOVEL,1), &(pd->inventory));
addItemToInventory(newItem(TOOL_HOE,4), &(pd->inventory));
addItemToInventory(newItem(TOOL_SWORD,4), &(pd->inventory));
addItemToInventory(newItem(TOOL_PICKAXE,4), &(pd->inventory));
addItemToInventory(newItem(TOOL_AXE,4), &(pd->inventory));
addItemToInventory(newItem(ITEM_ANVIL,0), &(pd->inventory));
addItemToInventory(newItem(ITEM_CHEST,0), &(pd->inventory));
addItemToInventory(newItem(ITEM_OVEN,0), &(pd->inventory));
addItemToInventory(newItem(ITEM_FURNACE,0), &(pd->inventory));
addItemToInventory(newItem(ITEM_LANTERN,0), &(pd->inventory));
addItemToInventory(newItem(TOOL_MAGIC_COMPASS,1), &(pd->inventory));
int i;
for (i = 7;i < 28;++i) addItemToInventory(newItem(i,50), &(pd->inventory));
}
}
void playerInitSprite(PlayerData *pd) {
pd->sprite.choosen = false;
pd->sprite.legs = 0;
pd->sprite.body = 0;
pd->sprite.arms = 0;
pd->sprite.head = 0;
pd->sprite.eyes = 0;
}
void playerInitMenus(PlayerData *pd) {
pd->ingameMenu = MENU_NONE;
pd->ingameMenuSelection = 0;
pd->ingameMenuInvSel = 0;
pd->ingameMenuInvSelOther = 0;
pd->ingameMenuAreYouSure = false;
pd->ingameMenuAreYouSureSave = false;
pd->ingameMenuTimer = 0;
resetNPCMenuData(&(pd->npcMenuData));
pd->mapShouldRender = false;
pd->mapScrollX = 0;
pd->mapScrollY = 0;
pd->mapZoomLevel = 2;
sprintf(pd->mapText,"x%d", pd->mapZoomLevel);
pd->touchLastX = -1;
pd->touchLastY = -1;
pd->touchIsDraggingMap = false;
pd->touchIsChangingSize = false;
}
void initPlayer(PlayerData *pd) {
pd->isSpawned = false;
playerInitMiniMapData(pd->minimapData);
playerInitEntity(pd);
playerInitInventory(pd);
playerInitSprite(pd);
initQuests(&(pd->questManager));
resetQuests(&(pd->questManager));
playerInitMenus(pd);
pd->score = 0;
}
void freePlayer(PlayerData *pd) {
freeQuests(&(pd->questManager));
}
PlayerData* getNearestPlayer(s8 level, s16 x, s16 y) {
int nearest = -1;
unsigned int nearestDist = UINT_MAX;
for(int i=0; i<playerCount; i++) {
if(players[i].entity.level!=level) continue;
int xdif = players[i].entity.x - x;
int ydif = players[i].entity.y - y;
unsigned int dist = xdif*xdif + ydif*ydif;
if(dist<nearestDist) {
nearest = i;
nearestDist = dist;
}
}
if(nearest==-1) return NULL;
return players+nearest;
}
PlayerData* getLocalPlayer() {
return players+playerLocalID;
}
//player update functions
bool playerUseItem(PlayerData *pd) {
int aitemID = 0;
Item * aitem;
Item * item ;
switch(pd->activeItem->id){
//shooting arrows
case TOOL_BOW:
item = getItemFromInventory(ITEM_ARROW_WOOD, &(pd->inventory));
if(item!=NULL) {
aitemID = ITEM_ARROW_WOOD;
aitem = item;
}
item = getItemFromInventory(ITEM_ARROW_STONE, &(pd->inventory));
if(item!=NULL) {
aitemID = ITEM_ARROW_STONE;
aitem = item;
}
item = getItemFromInventory(ITEM_ARROW_IRON, &(pd->inventory));
if(item!=NULL) {
aitemID = ITEM_ARROW_IRON;
aitem = item;
}
item = getItemFromInventory(ITEM_ARROW_GOLD, &(pd->inventory));
if(item!=NULL) {
aitemID = ITEM_ARROW_GOLD;
aitem = item;
}
item = getItemFromInventory(ITEM_ARROW_GEM, &(pd->inventory));
if(item!=NULL) {
aitemID = ITEM_ARROW_GEM;
aitem = item;
}
if(aitemID!=0) {
--aitem->countLevel;
if (isItemEmpty(aitem)) {
removeItemFromInventory(aitem->slotNum, &(pd->inventory));
}
switch(pd->entity.p.dir) {
case 0:
addEntityToList(newArrowEntity(&(pd->entity), aitemID, 0, 2, pd->entity.level), &eManager);
break;
case 1:
addEntityToList(newArrowEntity(&(pd->entity), aitemID, 0, -2, pd->entity.level), &eManager);
break;
case 2:
addEntityToList(newArrowEntity(&(pd->entity), aitemID, -2, 0, pd->entity.level), &eManager);
break;
case 3:
addEntityToList(newArrowEntity(&(pd->entity), aitemID, 2, 0, pd->entity.level), &eManager);
break;
}
return true;
}
break;
// Health items
case ITEM_APPLE:
if(pd->entity.p.health < 10 && playerUseEnergy(pd, 2)){
playerHeal(pd, 1);
--(pd->activeItem->countLevel);
}
break;
case ITEM_STRENGTH_POTION:
if(pd->entity.p.health < 20 && pd->entity.p.strengthTimer == 0){
potionEffect(1);
--(pd->activeItem->countLevel);
}
return 0;
case ITEM_SPEED_POTION:
if(pd->entity.p.health < 20 && pd->entity.p.speedTimer == 0){
potionEffect(2);
--(pd->activeItem->countLevel);
}
return 0;
case ITEM_REGEN_POTION:
if(pd->entity.p.health < 20 && pd->entity.p.regenTimer == 0){
potionEffect(3);
--(pd->activeItem->countLevel);
}
return 0;
case ITEM_SWIM_BREATH_POTION:
if(pd->entity.p.health < 20 && pd->entity.p.swimBreathTimer == 0){
potionEffect(4);
--(pd->activeItem->countLevel);
}
return 0;
case ITEM_GOLD_APPLE:
if(pd->entity.p.health < 10 && playerUseEnergy(pd, 1)){
playerHeal(pd, 8);
playerUseEnergy(pd, -10);
--(pd->activeItem->countLevel);
}
return 0;
case ITEM_FLESH:
if(pd->entity.p.health < 10 && playerUseEnergy(pd, 4+(rand()%4))){
playerHeal(pd, 1);
--(pd->activeItem->countLevel);
}
break;
case ITEM_BREAD:
if(pd->entity.p.health < 10 && playerUseEnergy(pd, 3)){
playerHeal(pd, 2);
--(pd->activeItem->countLevel);
}
break;
case ITEM_PORK_RAW:
if(pd->entity.p.health < 10 && playerUseEnergy(pd, 4+(rand()%4))){
playerHeal(pd, 1);
--(pd->activeItem->countLevel);
}
break;
case ITEM_PORK_COOKED:
if(pd->entity.p.health < 10 && playerUseEnergy(pd, 3)){
playerHeal(pd, 3);
--(pd->activeItem->countLevel);
}
break;
case ITEM_BEEF_RAW:
if(pd->entity.p.health < 10 && playerUseEnergy(pd, 4+(rand()%4))){
playerHeal(pd, 1);
--(pd->activeItem->countLevel);
}
break;
case ITEM_BEEF_COOKED:
if(pd->entity.p.health < 10 && playerUseEnergy(pd, 3)){
playerHeal(pd, 4);
--(pd->activeItem->countLevel);
}
break;
//special item
case ITEM_WIZARD_SUMMON:
if(pd->entity.level==0) {
--(pd->activeItem->countLevel);
airWizardHealthDisplay = 2000;
addEntityToList(newAirWizardEntity(630, 820, 0), &eManager);
}
break;
}
if (isItemEmpty(pd->activeItem)) {
removeItemFromInventory(pd->activeItem->slotNum, &(pd->inventory));
pd->activeItem = &noItem;
}
return false;
}
bool playerInteract(PlayerData *pd, int x0, int y0, int x1, int y1) {
Entity * es[eManager.lastSlot[pd->entity.level]];
int eSize = getEntities(es, pd->entity.level, x0, y0, x1, y1);
int i;
for (i = 0; i < eSize; ++i) {
Entity * ent = es[i];
if (ent != &(pd->entity)){
if (ItemVsEntity(pd, pd->activeItem, ent, pd->entity.p.dir)) return true;
}
}
return false;
}
void playerAttack(PlayerData *pd) {
bool done = false;
pd->entity.p.attackTimer = 5;
int yo = -2;
int range = 12;
//directly using an item
if(playerUseItem(pd)) return;
//interacting with entities
switch(pd->entity.p.dir){
case 0: if(playerInteract(pd, pd->entity.x - 8, pd->entity.y + 4 + yo, pd->entity.x + 8, pd->entity.y + range + yo)) return; break;
case 1: if(playerInteract(pd, pd->entity.x - 8, pd->entity.y - range + yo, pd->entity.x + 8, pd->entity.y - 4 + yo)) return; break;
case 2: if(playerInteract(pd, pd->entity.x - range, pd->entity.y - 8 + yo, pd->entity.x - 4, pd->entity.y + 8 + yo)) return; break;
case 3: if(playerInteract(pd, pd->entity.x + 4, pd->entity.y - 8 + yo, pd->entity.x + range, pd->entity.y + 8 + yo)) return; break;
}
int xt = pd->entity.x >> 4;
int yt = (pd->entity.y + yo) >> 4;
int r = 12;
if (pd->entity.p.dir == 0) yt = (pd->entity.y + r + yo) >> 4;
if (pd->entity.p.dir == 1) yt = (pd->entity.y - r + yo) >> 4;
if (pd->entity.p.dir == 2) xt = (pd->entity.x - r) >> 4;
if (pd->entity.p.dir == 3) xt = (pd->entity.x + r) >> 4;
//interacting with tiles
if (xt >= 0 && yt >= 0 && xt < 128 && yt < 128) {
s8 itract = itemTileInteract(getTile(pd->entity.level, xt, yt), pd, pd->activeItem, pd->entity.level, xt, yt, pd->entity.x, pd->entity.y, pd->entity.p.dir);
if(itract > 0){
if(itract==2) pd->entity.p.isCarrying = false;
done = true;
}
if (pd->activeItem != &noItem && isItemEmpty(pd->activeItem)) {
removeItemFromInventory(pd->activeItem->slotNum, &(pd->inventory));
pd->activeItem = &noItem;
}
}
if(done) return;
//breaking tiles
if (pd->activeItem == &noItem || pd->activeItem->id == TOOL_SWORD || pd->activeItem->id == TOOL_AXE) {
if (xt >= 0 && yt >= 0 && xt < 128 && 128) {
playerHurtTile(pd, getTile(pd->entity.level, xt, yt), pd->entity.level, xt, yt, (rand()%3) + 1, pd->entity.p.dir);
}
}
}
bool playerUseArea(PlayerData *pd, int x0, int y0, int x1, int y1) {
Entity * entities[eManager.lastSlot[pd->entity.level]];
int i;
int ae = getEntities(entities, pd->entity.level, x0, y0, x1, y1);
for(i = 0; i < ae; ++i){
if(useEntity(pd, entities[i])) return true;
}
return false;
}
bool playerUse(PlayerData *pd) {
int yo = -2;
if (pd->entity.p.dir == 0 && playerUseArea(pd, pd->entity.x - 8, pd->entity.y + 4 + yo, pd->entity.x + 8, pd->entity.y + 12 + yo)) return true;
if (pd->entity.p.dir == 1 && playerUseArea(pd, pd->entity.x - 8, pd->entity.y - 12 + yo, pd->entity.x + 8, pd->entity.y - 4 + yo)) return true;
if (pd->entity.p.dir == 3 && playerUseArea(pd, pd->entity.x + 4, pd->entity.y - 8 + yo, pd->entity.x + 12, pd->entity.y + 8 + yo)) return true;
if (pd->entity.p.dir == 2 && playerUseArea(pd, pd->entity.x - 12, pd->entity.y - 8 + yo, pd->entity.x - 4, pd->entity.y + 8 + yo)) return true;
return false;
}
void tickPlayer(PlayerData *pd, bool inmenu) {
if (pd->entity.p.isDead) return;
//invincibility time
if (pd->entity.hurtTime > 0) pd->entity.hurtTime--;
//stamina recharging
bool swimming = isWater(pd->entity.level, pd->entity.x>>4, pd->entity.y>>4);
if (pd->entity.p.stamina <= 0 && pd->entity.p.staminaRechargeDelay == 0 && pd->entity.p.staminaRecharge == 0) {
pd->entity.p.staminaRechargeDelay = 40;
}
if (pd->entity.p.staminaRechargeDelay > 0) {
--pd->entity.p.staminaRechargeDelay;
}
if (pd->entity.p.staminaRechargeDelay == 0) {
++pd->entity.p.staminaRecharge;
if (swimming) pd->entity.p.staminaRecharge = 0;
while (pd->entity.p.staminaRecharge > 10) {
pd->entity.p.staminaRecharge -= 10;
if (pd->entity.p.stamina < 10) ++pd->entity.p.stamina;
}
}
if(!inmenu) {
if(!pd->sprite.choosen) {
pd->ingameMenu = MENU_CHARACTER_CUSTOMIZE;
pd->ingameMenuSelection = 0;
return;
}
//movement
pd->entity.p.ax = 0;
pd->entity.p.ay = 0;
if (pd->inputs.k_left.down){
if(UnderSpeedEffect) {
pd->entity.p.ax -= 2;
pd->entity.p.dir = 2;
++pd->entity.p.walkDist;
++pd->entity.p.walkDist;
} else {
pd->entity.p.ax -= 1;
pd->entity.p.dir = 2;
++pd->entity.p.walkDist;
}
}
if (pd->inputs.k_right.down){
if(UnderSpeedEffect) {
pd->entity.p.ax += 2;
pd->entity.p.dir = 3;
++pd->entity.p.walkDist;
++pd->entity.p.walkDist;
} else {
pd->entity.p.ax += 1;
pd->entity.p.dir = 3;
++pd->entity.p.walkDist;
}
}
if (pd->inputs.k_up.down){
if(UnderSpeedEffect) {
pd->entity.p.ay -= 2;
pd->entity.p.dir = 1;
++pd->entity.p.walkDist;
++pd->entity.p.walkDist;
} else {
pd->entity.p.ay -= 1;
pd->entity.p.dir = 1;
++pd->entity.p.walkDist;
}
}
if (pd->inputs.k_down.down){
if(UnderSpeedEffect) {
pd->entity.p.ay += 2;
pd->entity.p.dir = 0;
++pd->entity.p.walkDist;
++pd->entity.p.walkDist;
} else {
pd->entity.p.ay += 1;
pd->entity.p.dir = 0;
++pd->entity.p.walkDist;
}
}
if (pd->entity.p.staminaRechargeDelay % 2 == 0) moveMob(&(pd->entity), pd->entity.p.ax, pd->entity.p.ay);
//"pausing", TODO: since multiplayer this will no longer pause
if (pd->inputs.k_pause.clicked){
pd->ingameMenuSelection = 0;
pd->ingameMenu = MENU_PAUSED;
}
//attacking
if(pd->inputs.k_attack.clicked){
if (pd->entity.p.stamina != 0) {
if(!shouldRenderDebug) pd->entity.p.stamina--;
pd->entity.p.staminaRecharge = 0;
playerAttack(pd);
}
}
//picking up furniture
if(pd->inputs.k_pickup.clicked){
if (pd->entity.p.isCarrying){
if (pd->entity.p.stamina != 0) {
if(!shouldRenderDebug) pd->entity.p.stamina--;
pd->entity.p.staminaRecharge = 0;
playerAttack(pd);
}
}else{
int yo = -2;
int range = 12;
// see if entity near player
int x0, y0, x1, y1 = 0;
switch(pd->entity.p.dir){
case 0:
x0 = pd->entity.x - 8;
y0 = pd->entity.y + 4 + yo;
x1 = pd->entity.x + 8;
y1 = pd->entity.y + range + yo;
break;
case 1:
x0 = pd->entity.x - 8;
y0 = pd->entity.y - range + yo;
x1 = pd->entity.x + 8;
y1 = pd->entity.y - 4 + yo;
break;
case 2:
x0 = pd->entity.x - range;
y0 = pd->entity.y - 8 + yo;
x1 = pd->entity.x - 4;
y1 = pd->entity.y + 8 + yo;
break;
case 3:
x0 = pd->entity.x + 4;
y0 = pd->entity.y - 8 + yo;
x1 = pd->entity.x + range;
y1 = pd->entity.y + 8 + yo;
break;
}
Entity * es[eManager.lastSlot[pd->entity.level]];
int eSize = getEntities(es, pd->entity.level, x0, y0, x1, y1);
int i;
for (i = 0; i < eSize; ++i) {
Entity * ent = es[i];
if (ent != &(pd->entity)){
if(ent->type == ENTITY_FURNITURE){
//Important: close all crafting windows using this furniture (only applies to chest) or else they will write invalid memory
for(int i=0; i<playerCount; i++) {
if(players[i].curChestEntity==ent) {
players[i].ingameMenu = MENU_NONE;
}
}
Item nItem = newItem(ent->entityFurniture.itemID,0);
if(ent->entityFurniture.itemID == ITEM_CHEST) nItem.chestPtr = ent->entityFurniture.inv;
pushItemToInventoryFront(nItem, &(pd->inventory));
removeEntityFromList(ent, ent->level, &eManager);
pd->activeItem = &(pd->inventory.items[0]);
pd->entity.p.isCarrying = true;
}
}
}
}
}
// in head crafting
if(pd->inputs.k_use.clicked){
pd->ingameMenuInvSel = 0;
if(!playerUse(pd)){
pd->currentCraftTitle = "Crafting";
openCraftingMenu(pd, &inHeadRecipes, "Crafting");
}
}
if (pd->inputs.k_menu.clicked){
pd->ingameMenuInvSel = 0;
if(!playerUse(pd)) pd->ingameMenu = MENU_INVENTORY;
}
}
//swimming stamina and drowning
if (swimming && pd->entity.p.swimTimer % 60 == 0 && UnderSwimBreathEffect != true) {
if (pd->entity.p.stamina > 0) {
if(!shouldRenderDebug) --pd->entity.p.stamina;
} else {
hurtEntity(&(pd->entity), 1, -1, 0xFFAF00FF, NULL);
}
}
//Regen healing
if (regening && pd->entity.p.regenTimer % 75 == 0) {
playerHeal(pd, 1);
}
if(isWater(pd->entity.level, pd->entity.x>>4, pd->entity.y>>4)) ++pd->entity.p.swimTimer;
if(regening) ++pd->entity.p.regenTimer;
if(UnderSpeedEffect) ++pd->entity.p.speedTimer;
if(UnderStrengthEffect) ++pd->entity.p.strengthTimer;
if(UnderSwimBreathEffect) ++pd->entity.p.swimBreathTimer;
if(pd->entity.p.attackTimer > 0) --pd->entity.p.attackTimer;
//TODO - maybe move to own function
//Update Minimap
int xp;
int yp;
for(xp = (pd->entity.x>>4)-5; xp<(pd->entity.x>>4)+5; ++xp) {
for(yp = (pd->entity.y>>4)-5; yp<(pd->entity.y>>4)+5; ++yp) {
if(xp>=0 && xp<128 && yp>=0 && yp<128) {
if(!getMinimapVisible(pd, pd->entity.level, xp, yp)) {
setMinimapVisible(pd, pd->entity.level, xp, yp, true);
}
}
}
}
}
void playerSetActiveItem(PlayerData *pd, Item *item) {
pd->activeItem = item;
if(pd->activeItem->id > 27 && pd->activeItem->id < 51) pd->entity.p.isCarrying = true;
else pd->entity.p.isCarrying = false;
}
bool playerUseEnergy(PlayerData *pd, int amount) {
if(shouldRenderDebug) return true;
if(amount > pd->entity.p.stamina) return false;
pd->entity.p.stamina -= amount;
return true;
}
void playerHeal(PlayerData *pd, int amount) {
pd->entity.p.health += amount;
if(pd->entity.p.health > 10) pd->entity.p.health = 10;
char healText[11];
sprintf(healText, "%d", amount);
addEntityToList(newTextParticleEntity(healText,0xFF00FF00, pd->entity.x, pd->entity.y, pd->entity.level), &eManager);
}
void playerSpawn(PlayerData *pd) {
while(true){
int rx = rand()%128;
int ry = rand()%128;
if(getTile(pd->entity.level, rx, ry) == TILE_GRASS){
pd->entity.x = (rx << 4) + 8;
pd->entity.y = (ry << 4) + 8;
pd->isSpawned = true;
break;
}
}
}

View file

@ -1,96 +0,0 @@
#pragma once
#include "Input.h"
#include "Entity.h"
#include "QuestsData.h"
#include "Crafting.h"
#define MAX_PLAYERS 8
#define MAX_INPUT_BUFFER 3
#define PLAYER_SPRITE_HEAD_COUNT 4
#define PLAYER_SPRITE_EYES_COUNT 5
#define PLAYER_SPRITE_BODY_COUNT 6
#define PLAYER_SPRITE_ARMS_COUNT 6
#define PLAYER_SPRITE_LEGS_COUNT 5
typedef struct _plrsp {
bool choosen;
u8 legs;
u8 body;
u8 arms;
u8 head;
u8 eyes;
} PlayerSprite;
typedef struct _plrd {
//for identification in save data and sync game start
u32 id;
bool idSet;
bool ready;
//input/multiplayer/synchronization
Inputs inputs;
Inputs nextInputs[MAX_INPUT_BUFFER];
bool nextTurnReady[MAX_INPUT_BUFFER];
//
bool isSpawned;
u8 minimapData[128*128];
int score;
QuestlineManager questManager;
Entity entity;
Inventory inventory;
Item *activeItem;
PlayerSprite sprite;
//menu data
u8 ingameMenu;
s8 ingameMenuSelection;
s16 ingameMenuInvSel;
s16 ingameMenuInvSelOther;
bool ingameMenuAreYouSure;
bool ingameMenuAreYouSureSave;
s16 ingameMenuTimer;
NPC_MenuData npcMenuData;
RecipeManager currentRecipes;
char *currentCraftTitle;
Entity *curChestEntity;
s8 curChestEntityR;
bool mapShouldRender;
u8 mapZoomLevel;
s16 mapScrollX;
s16 mapScrollY;
char mapText[32];
s16 touchLastX;
s16 touchLastY;
bool touchIsDraggingMap;
bool touchIsChangingSize;
} PlayerData;
PlayerData players[MAX_PLAYERS];
int playerCount;
int playerLocalID;
void initPlayers();
void freePlayers();
void initPlayer(PlayerData *pd);
void freePlayer(PlayerData *pd);
PlayerData* getNearestPlayer(s8 level, s16 x, s16 y);
PlayerData* getLocalPlayer();
void tickPlayer(PlayerData *pd, bool inmenu);
void playerSetActiveItem(PlayerData *pd, Item * item);
bool playerUseEnergy(PlayerData * pd, int amount);
void playerHeal(PlayerData *pd, int amount);
void playerSpawn(PlayerData *pd);

579
source/Quests.c Executable file → Normal file
View file

@ -1,9 +1,27 @@
#include "Quests.h"
#include "Globals.h"
#include "Render.h"
u8 currentNPC;
int currentNPCMenu;
int currentNPCVal;
int currentTalkSel;
bool currentTalkDone;
int currentTalkOptions;
char * currentTalkOption0;
char * currentTalkOption1;
char * currentTalkOption2;
char * currentTalk0;
char * currentTalk1;
char * currentTalk2;
char * currentTalk3;
char * currentTalk4;
char * currentTalk5;
void initQuests() {
questManager.size = 2;
questManager.questlines = (Questline*)malloc(sizeof(Questline) * (questManager.size));
void initTrades() {
priestTrades.size = 5;
priestTrades.recipes = (Recipe*)malloc(sizeof(Recipe) * (priestTrades.size));
priestTrades.recipes[0] = defineRecipe(ITEM_DUNGEON_KEY,1,1,ITEM_MAGIC_DUST,2);
@ -29,364 +47,335 @@ void initTrades() {
//TODO: Trade Dragon Scales for something really nice
}
void freeTrades() {
void resetQuests() {
int i;
for(i=0; i<questManager.size; ++i) {
questManager.questlines[i].currentQuest = 0;
questManager.questlines[i].currentQuestDone = false;
}
}
void freeQuests() {
free(questManager.questlines);
free(priestTrades.recipes);
free(farmerTrades.recipes);
free(dwarfTrades.recipes);
}
void initQuests(QuestlineManager *questManager) {
if(questManager->questlines!=NULL) {
freeQuests(questManager);
}
void openNPCMenu(int npc) {
currentNPC = npc;
currentNPCVal = 0;
currentMenu = MENU_NPC;
currentNPCMenu = NPC_MENU_TALK;
questManager->size = 2;
questManager->questlines = (Questline*)malloc(sizeof(Questline) * (questManager->size));
}
void resetQuests(QuestlineManager *questManager) {
int i;
for(i=0; i<questManager->size; ++i) {
questManager->questlines[i].currentQuest = 0;
questManager->questlines[i].currentQuestDone = false;
}
}
void freeQuests(QuestlineManager *questManager) {
free(questManager->questlines);
questManager->questlines = NULL;
}
void resetNPCMenuData(NPC_MenuData *data) {
data->currentNPC = 0;
data->currentNPCMenu = 0;
data->currentNPCVal = 0;
data->currentTalkSel = 0;
data->currentTalkDone = false;
data->currentTalkOptions = 0;
data->currentTalkOption0 = "";
data->currentTalkOption1 = "";
data->currentTalkOption2 = "";
data->currentTalk0 = "";
data->currentTalk1 = "";
data->currentTalk2 = "";
data->currentTalk3 = "";
data->currentTalk4 = "";
data->currentTalk5 = "";
}
void openNPCMenu(PlayerData *pd, int npc) {
pd->ingameMenu = MENU_NPC;
NPC_MenuData *data = &(pd->npcMenuData);
QuestlineManager *questManager = &(pd->questManager);
data->currentNPC = npc;
data->currentNPCVal = 0;
data->currentNPCMenu = NPC_MENU_TALK;
data->currentTalkSel = 0;
data->currentTalkDone = false;
data->currentTalkOptions = 1;
data->currentTalkOption0 = "Bye";
data->currentTalkOption1 = "";
data->currentTalkOption2 = "";
data->currentTalk0 = "";
data->currentTalk1 = "";
data->currentTalk2 = "";
data->currentTalk3 = "";
data->currentTalk4 = "";
data->currentTalk5 = "";
currentTalkSel = 0;
currentTalkDone = false;
currentTalkOptions = 1;
currentTalkOption0 = "Bye";
currentTalkOption1 = "";
currentTalkOption2 = "";
currentTalk0 = "";
currentTalk1 = "";
currentTalk2 = "";
currentTalk3 = "";
currentTalk4 = "";
currentTalk5 = "";
//TODO: Handle upon currentNPC as well as the fitting quest progress
switch(data->currentNPC) {
switch(currentNPC) {
case NPC_GIRL:
data->currentTalkOptions = 1;
data->currentTalkOption0 = "...";
currentTalkOptions = 2;
currentTalkOption0 = "Trade";
currentTalkOption1 = "What do you do?";
data->currentTalk0 = "Hello?";
data->currentTalk1 = "I have a feeling of having";
data->currentTalk2 = "forgotten something very";
data->currentTalk3 = "important.";
data->currentTalk4 = "Hopefully I will remember";
data->currentTalk5 = "it soon...";
currentTalk0 = "Hello!";
currentTalk1 = "It gets a bit lonely here.";
currentTalk2 = "I hope you stay...";
currentTalk3 = "But if you don't thats fine.";
currentTalk4 = "sigh";
currentTalk5 = "";
break;
case NPC_PRIEST:
data->currentTalkOptions = 3;
data->currentTalkOption1 = "Trade";
data->currentTalkOption2 = "Why are you so few?";
currentTalkOptions = 3;
currentTalkOption1 = "Trade";
currentTalkOption2 = "Why are you so few?";
data->currentTalk0 = "Welcome to our small village";
data->currentTalk1 = "I am the leader of our group.";
data->currentTalk2 = "If you have anything usefull";
data->currentTalk3 = "for us I might be able to";
data->currentTalk4 = "provide something nice in";
data->currentTalk5 = "exchange.";
currentTalk0 = "Welcome to our small village";
currentTalk1 = "I am the leader of our group.";
currentTalk2 = "If you have anything usefull";
currentTalk3 = "for us I might be able to";
currentTalk4 = "provide something nice in";
currentTalk5 = "exchange.";
break;
case NPC_FARMER:
data->currentTalkOptions = 2;
data->currentTalkOption0 = "Maybe some other time";
data->currentTalkOption1 = "Trade";
currentTalkOptions = 2;
currentTalkOption0 = "Maybe some other time";
currentTalkOption1 = "Trade";
data->currentTalk0 = "Hello friend!";
data->currentTalk1 = "Nice seeing somebody else";
data->currentTalk2 = "visit my little farm.";
data->currentTalk3 = "Interested in buying some";
data->currentTalk4 = "fresh farm goods?";
data->currentTalk5 = "";
currentTalk0 = "Hello friend!";
currentTalk1 = "Nice seeing somebody else";
currentTalk2 = "visit my little farm.";
currentTalk3 = "Interested in buying some";
currentTalk4 = "fresh farm goods?";
currentTalk5 = "";
break;
case NPC_LIBRARIAN:
data->currentTalkOptions = 2;
data->currentTalkOption0 = "Nothing";
data->currentTalkOption1 = "What are you doing here?";
if(questManager->questlines[1].currentQuest==1) {
data->currentTalkOptions = 3;
data->currentTalkOption2 = "Dwarvish language";
currentTalkOptions = 2;
currentTalkOption0 = "Nothing";
currentTalkOption1 = "What are you doing here?";
if(questManager.questlines[1].currentQuest==1) {
currentTalkOptions = 3;
currentTalkOption2 = "Dwarvish language";
}
data->currentTalk0 = "Oh hello?";
data->currentTalk1 = "You must be quite brave";
data->currentTalk2 = "or stupid to be walking";
data->currentTalk3 = "around in this dungeon.";
data->currentTalk4 = "";
data->currentTalk5 = "How can I help you?";
currentTalk0 = "Oh hello?";
currentTalk1 = "You must be quite brave";
currentTalk2 = "or stupid to be walking";
currentTalk3 = "around in this dungeon.";
currentTalk4 = "";
currentTalk5 = "How can I help you?";
break;
case NPC_DWARF:
if(questManager->questlines[1].currentQuest<=1) {
questManager->questlines[1].currentQuest = 1;
if(questManager.questlines[1].currentQuest<=1) {
questManager.questlines[1].currentQuest = 1;
data->currentTalkOptions = 1;
data->currentTalkOption0 = "?";
currentTalkOptions = 1;
currentTalkOption0 = "?";
data->currentTalk0 = "Dwo neal bet reck da lo";
data->currentTalk1 = "dhum don lir lugn at el";
data->currentTalk2 = "nur tor erno ur yo trad";
data->currentTalk3 = "thra so tir kho ukk tin";
data->currentTalk4 = "hel dro ic";
data->currentTalk5 = "";
currentTalk0 = "Dwo neal bet reck da lo";
currentTalk1 = "dhum don lir lugn at el";
currentTalk2 = "nur tor erno ur yo trad";
currentTalk3 = "thra so tir kho ukk tin";
currentTalk4 = "hel dro ic";
currentTalk5 = "";
//TODO: set to 2 once translation book has been bought from librarian(can only be done once it is 1, so the dwarf has been found once)
} else if(questManager->questlines[1].currentQuest==2) {
data->currentTalkOptions = 2;
data->currentTalkOption0 = "Not really";
data->currentTalkOption1 = "Trade";
} else if(questManager.questlines[1].currentQuest==2) {
currentTalkOptions = 2;
currentTalkOption0 = "Not really";
currentTalkOption1 = "Trade";
data->currentTalk0 = "How are ya?";
data->currentTalk1 = "Pretty unusal meeting a";
data->currentTalk2 = "human down here.";
data->currentTalk3 = "";
data->currentTalk4 = "have something valuable";
data->currentTalk5 = "to trade?";
currentTalk0 = "How are ya?";
currentTalk1 = "Pretty unusal meeting a";
currentTalk2 = "human down here.";
currentTalk3 = "";
currentTalk4 = "have something valuable";
currentTalk5 = "to trade?";
}
break;
}
}
void tickTalkMenu(PlayerData *pd, NPC_MenuData *data) {
if (pd->inputs.k_menu.clicked || pd->inputs.k_decline.clicked) pd->ingameMenu = MENU_NONE;
void tickTalkMenu() {
if (k_menu.clicked || k_decline.clicked) currentMenu = MENU_NONE;
if (pd->inputs.k_up.clicked){ ++data->currentTalkSel; if(data->currentTalkSel >= data->currentTalkOptions) data->currentTalkSel=0;}
if (pd->inputs.k_down.clicked){ --data->currentTalkSel; if(data->currentTalkSel < 0) data->currentTalkSel=data->currentTalkOptions-1;}
if (k_up.clicked){ ++currentTalkSel; if(currentTalkSel >= currentTalkOptions) currentTalkSel=0;}
if (k_down.clicked){ --currentTalkSel; if(currentTalkSel < 0) currentTalkSel=currentTalkOptions-1;}
if(pd->inputs.k_accept.clicked){
data->currentTalkDone = true;
if(k_accept.clicked){
currentTalkDone = true;
}
}
void tickNPCMenu(PlayerData *pd) {
NPC_MenuData *data = &(pd->npcMenuData);
QuestlineManager *questManager = &(pd->questManager);
void tickNPCMenu() {
//TODO: Handle upon currentNPC as well as the fitting quest progress
if(data->currentNPCMenu==NPC_MENU_TALK) tickTalkMenu(pd, data);
if(currentNPCMenu==NPC_MENU_TALK) tickTalkMenu();
switch(data->currentNPC) {
switch(currentNPC) {
case NPC_GIRL:
if(data->currentNPCMenu==NPC_MENU_TALK && data->currentTalkDone) {
if(data->currentNPCVal==0) pd->ingameMenu = MENU_NONE;
}
break;
case NPC_PRIEST:
if(data->currentNPCMenu==NPC_MENU_TALK && data->currentTalkDone) {
if(data->currentNPCVal==0) {
if(data->currentTalkSel==0) {
pd->ingameMenu = MENU_NONE;
} else if(data->currentTalkSel==1) {
openCraftingMenu(pd, &priestTrades, "Trading");
} else if(data->currentTalkSel==2) {
data->currentNPCVal = 1;
if(currentNPCMenu==NPC_MENU_TALK && currentTalkDone) {
if(currentNPCVal==0) {
if(currentTalkSel==0) {
currentMenu = MENU_NONE;
} else if(currentTalkSel==1) {
currentRecipes = &priestTrades;
currentCraftTitle = "Trading";
currentMenu = MENU_CRAFTING;
checkCanCraftRecipes(currentRecipes, player.p.inv);
sortRecipes(currentRecipes);
} else if(currentTalkSel==2) {
currentNPCVal = 1;
data->currentTalkSel = 0;
data->currentTalkDone = false;
data->currentTalkOptions = 1;
data->currentTalkOption0 = "...";
currentTalkSel = 0;
currentTalkDone = false;
currentTalkOptions = 1;
currentTalkOption0 = "...";
data->currentTalk0 = "For quite some time now this";
data->currentTalk1 = "village has been tyrannized";
data->currentTalk2 = "by a powerfull Air Wizard.";
data->currentTalk3 = "We are the only ones who";
data->currentTalk4 = "still have not given up";
data->currentTalk5 = "our old homes.";
currentTalk0 = "For quite some time now this";
currentTalk1 = "village has been tyrannized";
currentTalk2 = "by a powerfull Air Wizard.";
currentTalk3 = "We are the only ones who";
currentTalk4 = "still have not given up";
currentTalk5 = "our old homes.";
}
} else if(data->currentNPCVal==1) {
if(data->currentTalkSel==0) {
data->currentNPCVal = 2;
} else if(currentNPCVal==1) {
if(currentTalkSel==0) {
currentNPCVal = 2;
data->currentTalkSel = 0;
data->currentTalkDone = false;
data->currentTalkOptions = 1;
data->currentTalkOption0 = "...";
currentTalkSel = 0;
currentTalkDone = false;
currentTalkOptions = 1;
currentTalkOption0 = "...";
data->currentTalk0 = "Most of the time the wizard";
data->currentTalk1 = "hides somewhere in the";
data->currentTalk2 = "cloudes. They can only be";
data->currentTalk3 = "reached by a stairwell";
data->currentTalk4 = "protected by an almost";
data->currentTalk5 = "undestroyable stone barrier.";
currentTalk0 = "Most of the time the wizard";
currentTalk1 = "hides somewhere in the";
currentTalk2 = "cloudes. They can only be";
currentTalk3 = "reached by a stairwell";
currentTalk4 = "protected by an almost";
currentTalk5 = "undestroyable stone barrier.";
}
} else if(data->currentNPCVal==2) {
if(data->currentTalkSel==0) {
data->currentNPCVal = 3;
} else if(currentNPCVal==2) {
if(currentTalkSel==0) {
currentNPCVal = 3;
data->currentTalkSel = 0;
data->currentTalkDone = false;
data->currentTalkOptions = 1;
data->currentTalkOption0 = "...";
currentTalkSel = 0;
currentTalkDone = false;
currentTalkOptions = 1;
currentTalkOption0 = "...";
data->currentTalk0 = "I am guessing you would ";
data->currentTalk1 = "need tools atleast as";
data->currentTalk2 = "strong as diamonds to be";
data->currentTalk3 = "able to destroy it.";
data->currentTalk4 = "";
data->currentTalk5 = "";
currentTalk0 = "I am guessing you would ";
currentTalk1 = "need tools atleast as";
currentTalk2 = "strong as diamonds to be";
currentTalk3 = "able to destroy it.";
currentTalk4 = "";
currentTalk5 = "";
}
} else if(data->currentNPCVal==3) {
if(data->currentTalkSel==0) {
data->currentNPCVal = 4;
} else if(currentNPCVal==3) {
if(currentTalkSel==0) {
currentNPCVal = 4;
data->currentTalkSel = 0;
data->currentTalkDone = false;
data->currentTalkOptions = 2;
data->currentTalkOption0 = "Let me do it!";
data->currentTalkOption1 = "I am not sure";
currentTalkSel = 0;
currentTalkDone = false;
currentTalkOptions = 2;
currentTalkOption0 = "Let me do it!";
currentTalkOption1 = "I am not sure";
data->currentTalk0 = "I am willing to give an";
data->currentTalk1 = "ancient artifact passed";
data->currentTalk2 = "down over generations to";
data->currentTalk3 = "anybody who manages to";
data->currentTalk4 = "chase the wizard away and";
data->currentTalk5 = "come back with proof.";
currentTalk0 = "I am willing to give an";
currentTalk1 = "ancient artifact passed";
currentTalk2 = "down over generations to";
currentTalk3 = "anybody who manages to";
currentTalk4 = "chase the wizard away and";
currentTalk5 = "come back with proof.";
}
} else if(data->currentNPCVal==4) {
pd->ingameMenu = MENU_NONE;
} else if(currentNPCVal==4) {
currentMenu = MENU_NONE;
}
}
break;
case NPC_FARMER:
if(data->currentNPCMenu==NPC_MENU_TALK && data->currentTalkDone) {
if(data->currentNPCVal==0) {
if(data->currentTalkSel==0) {
pd->ingameMenu = MENU_NONE;
} else if(data->currentTalkSel==1) {
openCraftingMenu(pd, &farmerTrades, "Trading");
if(currentNPCMenu==NPC_MENU_TALK && currentTalkDone) {
if(currentNPCVal==0) {
if(currentTalkSel==0) {
currentMenu = MENU_NONE;
} else if(currentTalkSel==1) {
currentRecipes = &farmerTrades;
currentCraftTitle = "Trading";
currentMenu = MENU_CRAFTING;
checkCanCraftRecipes(currentRecipes, player.p.inv);
sortRecipes(currentRecipes);
}
}
}
break;
case NPC_LIBRARIAN:
if(data->currentNPCMenu==NPC_MENU_TALK && data->currentTalkDone) {
if(data->currentNPCVal==0) {
if(data->currentTalkSel==0) {
pd->ingameMenu = MENU_NONE;
} else if(data->currentTalkSel==1) {
data->currentNPCVal = 2;
if(currentNPCMenu==NPC_MENU_TALK && currentTalkDone) {
if(currentNPCVal==0) {
if(currentTalkSel==0) {
currentMenu = MENU_NONE;
} else if(currentTalkSel==1) {
currentNPCVal = 2;
data->currentTalkSel = 0;
data->currentTalkDone = false;
data->currentTalkOptions = 1;
data->currentTalkOption0 = "Ok";
currentTalkSel = 0;
currentTalkDone = false;
currentTalkOptions = 1;
currentTalkOption0 = "Ok";
data->currentTalk0 = "The books in this dungeon";
data->currentTalk1 = "house secrets that cannot be";
data->currentTalk2 = "found anywhere else in the";
data->currentTalk3 = "world. So I came to study";
data->currentTalk4 = "them. Most are written in";
data->currentTalk5 = "an ancient language.";
} else if(data->currentTalkSel==2) {
data->currentNPCVal = 1;
currentTalk0 = "The books in this dungeon";
currentTalk1 = "house secrets that cannot be";
currentTalk2 = "found anywhere else in the";
currentTalk3 = "world. So I came to study";
currentTalk4 = "them. Most are written in";
currentTalk5 = "an ancient language.";
} else if(currentTalkSel==2) {
currentNPCVal = 1;
data->currentTalkSel = 0;
data->currentTalkDone = false;
data->currentTalkOptions = 2;
data->currentTalkOption0 = "I need to think about it";
data->currentTalkOption1 = "Here they are";
currentTalkSel = 0;
currentTalkDone = false;
currentTalkOptions = 2;
currentTalkOption0 = "I need to think about it";
currentTalkOption1 = "Here they are";
data->currentTalk0 = "So you have met a dwarf but";
data->currentTalk1 = "had a little communication";
data->currentTalk2 = "problem? I do have a dwarvish";
data->currentTalk3 = "translation book but I havent";
data->currentTalk4 = "read it yet. For 10 Gold bars";
data->currentTalk5 = "I will give it to you anyway.";
currentTalk0 = "So you have met a dwarf but";
currentTalk1 = "had a little communication";
currentTalk2 = "problem? I do have a dwarvish";
currentTalk3 = "translation book but I havent";
currentTalk4 = "read it yet. For 10 Gold bars";
currentTalk5 = "I will give it to you anyway.";
}
} else if(data->currentNPCVal==1) {
if(data->currentTalkSel==0) {
pd->ingameMenu = MENU_NONE;
} else if(data->currentTalkSel==1) {
data->currentNPCVal = 2;
} else if(currentNPCVal==1) {
if(currentTalkSel==0) {
currentMenu = MENU_NONE;
} else if(currentTalkSel==1) {
currentNPCVal = 2;
data->currentTalkSel = 0;
data->currentTalkDone = false;
data->currentTalkOptions = 1;
data->currentTalkOption0 = "";
currentTalkSel = 0;
currentTalkDone = false;
currentTalkOptions = 1;
currentTalkOption0 = "";
if(countItemInv(ITEM_GOLDINGOT, 0, &(pd->inventory))>=10) {
if(countItemInv(ITEM_GOLDINGOT,0,player.p.inv)>=10) {
//remove gold from player inventory
//TODO: Maybe I should make a generic substract items method sometime
Item* item = getItemFromInventory(ITEM_GOLDINGOT, &(pd->inventory));
Item* item = getItemFromInventory(ITEM_GOLDINGOT, player.p.inv);
item->countLevel -= 10;
if(item->countLevel < 1) removeItemFromInventory(item->slotNum, &(pd->inventory));
if(item->countLevel < 1) removeItemFromInventory(item->slotNum, player.p.inv);
questManager->questlines[1].currentQuest = 2;
questManager.questlines[1].currentQuest = 2;
data->currentTalk0 = "Thank you these will be";
data->currentTalk1 = "really helpfull.";
data->currentTalk2 = "Here take this book with";
data->currentTalk3 = "it you should be able to";
data->currentTalk4 = "easily understand anything";
data->currentTalk5 = "a dwarf can say.";
currentTalk0 = "Thank you these will be";
currentTalk1 = "really helpful.";
currentTalk2 = "Here take this book with";
currentTalk3 = "it you should be able to";
currentTalk4 = "easily understand anything";
currentTalk5 = "a dwarf can say.";
data->currentTalkOption0 = "Thanks";
currentTalkOption0 = "Thanks";
} else {
data->currentTalk0 = "You do not seem to have";
data->currentTalk1 = "enough Gold Bars with you.";
data->currentTalk2 = "";
data->currentTalk3 = "Ask again when you have";
data->currentTalk4 = "collected the 10 Bars.";
data->currentTalk5 = "";
currentTalk0 = "You do not seem to have";
currentTalk1 = "enough Gold Bars with you.";
currentTalk2 = "";
currentTalk3 = "Ask again when you have";
currentTalk4 = "collected the 10 Bars.";
currentTalk5 = "";
data->currentTalkOption0 = "Ok";
currentTalkOption0 = "Ok";
}
}
} else if(data->currentNPCVal==2) {
if(data->currentTalkSel==0) {
pd->ingameMenu = MENU_NONE;
} else if(currentNPCVal==2) {
if(currentTalkSel==0) {
currentMenu = MENU_NONE;
}
}
}
break;
case NPC_DWARF:
if(questManager->questlines[1].currentQuest<=1) {
if(data->currentNPCMenu==NPC_MENU_TALK && data->currentTalkDone) {
if(data->currentNPCVal==0) pd->ingameMenu = MENU_NONE;
if(questManager.questlines[1].currentQuest<=1) {
if(currentNPCMenu==NPC_MENU_TALK && currentTalkDone) {
if(currentNPCVal==0) currentMenu = MENU_NONE;
}
} else if(questManager->questlines[1].currentQuest==2) {
if(data->currentNPCMenu==NPC_MENU_TALK && data->currentTalkDone) {
if(data->currentTalkSel==0) {
pd->ingameMenu = MENU_NONE;
} else if(data->currentTalkSel==1) {
openCraftingMenu(pd, &dwarfTrades, "Trading");
} else if(questManager.questlines[1].currentQuest==2) {
if(currentNPCMenu==NPC_MENU_TALK && currentTalkDone) {
if(currentTalkSel==0) {
currentMenu = MENU_NONE;
} else if(currentTalkSel==1) {
currentRecipes = &dwarfTrades;
currentCraftTitle = "Trading";
currentMenu = MENU_CRAFTING;
checkCanCraftRecipes(currentRecipes, player.p.inv);
sortRecipes(currentRecipes);
}
}
}
@ -394,44 +383,44 @@ void tickNPCMenu(PlayerData *pd) {
}
}
void renderTalkMenu(NPC_MenuData *data, char * name) {
void renderTalkMenu(char * name) {
renderFrame(1,1,24,14,0xFFFF1010);
drawTextColor(name,24+1,14+1,0xFF000000);
drawTextColor(name,24,14,0xFF6FE2E2);
drawText(data->currentTalk0, 32, 32);
drawText(data->currentTalk1, 32, 48);
drawText(data->currentTalk2, 32, 64);
drawText(data->currentTalk3, 32, 80);
drawText(data->currentTalk4, 32, 96);
drawText(data->currentTalk5, 32, 112);
drawText(currentTalk0, 32, 32);
drawText(currentTalk1, 32, 48);
drawText(currentTalk2, 32, 64);
drawText(currentTalk3, 32, 80);
drawText(currentTalk4, 32, 96);
drawText(currentTalk5, 32, 112);
if(data->currentTalkOptions>=3) drawText(data->currentTalkOption2, 64, 147);
if(data->currentTalkOptions>=2) drawText(data->currentTalkOption1, 64, 171);
if(data->currentTalkOptions>=1) drawText(data->currentTalkOption0, 64, 195);
if(currentTalkOptions>=3) drawText(currentTalkOption2, 64, 147);
if(currentTalkOptions>=2) drawText(currentTalkOption1, 64, 171);
if(currentTalkOptions>=1) drawText(currentTalkOption0, 64, 195);
if(data->currentTalkOptions>=3 && data->currentTalkSel==2) drawText(">", 48, 147);
if(data->currentTalkOptions>=2 && data->currentTalkSel==1) drawText(">", 48, 171);
if(data->currentTalkOptions>=1 && data->currentTalkSel==0) drawText(">", 48, 195);
if(currentTalkOptions>=3 && currentTalkSel==2) drawText(">", 48, 147);
if(currentTalkOptions>=2 && currentTalkSel==1) drawText(">", 48, 171);
if(currentTalkOptions>=1 && currentTalkSel==0) drawText(">", 48, 195);
}
void renderNPCMenu(NPC_MenuData *data) {
void renderNPCMenu(int xscr, int yscr) {
//TODO: Handle upon currentNPC as well as the fitting quest progress
switch(data->currentNPC) {
switch(currentNPC) {
case NPC_GIRL:
if(data->currentNPCMenu==NPC_MENU_TALK) renderTalkMenu(data, "Maria");
if(currentNPCMenu==NPC_MENU_TALK) renderTalkMenu("Girl Jill");
break;
case NPC_PRIEST:
if(data->currentNPCMenu==NPC_MENU_TALK) renderTalkMenu(data, "Priest Brom");
if(currentNPCMenu==NPC_MENU_TALK) renderTalkMenu("Priest Brom");
break;
case NPC_FARMER:
if(data->currentNPCMenu==NPC_MENU_TALK) renderTalkMenu(data, "Farmer Garrow");
if(currentNPCMenu==NPC_MENU_TALK) renderTalkMenu("Farmer Garrow");
break;
case NPC_LIBRARIAN:
if(data->currentNPCMenu==NPC_MENU_TALK) renderTalkMenu(data, "Librarian Ajihad");
if(currentNPCMenu==NPC_MENU_TALK) renderTalkMenu("Librarian Ajihad");
break;
case NPC_DWARF:
if(data->currentNPCMenu==NPC_MENU_TALK) renderTalkMenu(data, "Dwarf Orik");
if(currentNPCMenu==NPC_MENU_TALK) renderTalkMenu("Dwarf Orik");
break;
}
}

31
source/Quests.h Executable file → Normal file
View file

@ -1,25 +1,32 @@
#pragma once
#include <stdlib.h>
#include <stdbool.h>
#include "QuestsData.h"
#include "Player.h"
#include "render.h"
#include "Crafting.h"
#define NPC_MENU_TALK 0
typedef struct {
int currentQuest;
bool currentQuestDone;
} Questline;
typedef struct {
int size;
Questline * questlines;
} QuestlineManager;
QuestlineManager questManager;
RecipeManager priestTrades;
RecipeManager farmerTrades;
RecipeManager dwarfTrades;
void initTrades();
void freeTrades();
void initQuests();
void resetQuests();
void freeQuests();
void initQuests(QuestlineManager *questManager);
void resetQuests(QuestlineManager *questManager);
void freeQuests(QuestlineManager *questManager);
void openNPCMenu(int npc);
void resetNPCMenuData(NPC_MenuData *data);
void openNPCMenu(PlayerData *pd, int npc);
void tickNPCMenu(PlayerData *pd);
void renderNPCMenu(NPC_MenuData *data);
void renderNPCMenu(int xscr, int yscr);
void tickNPCMenu();

View file

@ -1,35 +0,0 @@
#pragma once
#include <3ds.h>
typedef struct _questline {
int currentQuest;
bool currentQuestDone;
} Questline;
typedef struct _questlineManager {
int size;
Questline * questlines;
} QuestlineManager;
typedef struct _npcMenuData {
u8 currentNPC;
int currentNPCMenu;
int currentNPCVal;
int currentTalkSel;
bool currentTalkDone;
int currentTalkOptions;
char * currentTalkOption0;
char * currentTalkOption1;
char * currentTalkOption2;
char * currentTalk0;
char * currentTalk1;
char * currentTalk2;
char * currentTalk3;
char * currentTalk4;
char * currentTalk5;
} NPC_MenuData;
//TODO: Actually move the data here

532
source/Render.c Executable file → Normal file
View file

@ -1,7 +1,5 @@
#include "Render.h"
extern u32 syncTickCount;
void render(s32 xp, s32 yp, u32 xTile, u32 yTile, u8 bits) {
xp -= offsetX;
yp -= offsetY;
@ -160,18 +158,10 @@ void render32(s32 xp, s32 yp, u32 xTile, u32 yTile, u8 bits) {
scaleX, scaleY);
}
int playerScale = 2;
void renderp(s32 xp, s32 yp, u32 xTile, u32 yTile) {
xp -= offsetX;
yp -= offsetY;
int scaleX = playerScale, scaleY = playerScale;
sf2d_draw_texture_part_scale(playerSprites, xp << 1, yp << 1, xTile, yTile, 16, 16,
scaleX, scaleY);
}
void renderTitle(int x, int y) {
sf2d_draw_texture_part_scale(icons, (x - 26) << 1, y << 1, 0, 240, 104, 16, 2.0, 2.0); // MINICRAFT
sf2d_draw_texture_part(icons, x + 48, y + 44, 104, 240, 152, 16); // 3DS HOMEBREW EDITION
sf2d_draw_texture_part_scale(icons, (x - 26) << 1, y << 1, 0, 240, 104, 16,
2.0, 2.0); // MINICRAFT
sf2d_draw_texture_part(icons, x + 48, y + 52, 104, 240, 152, 16); // 3DS HOMEBREW EDITION
}
void renderButtonIcon(u32 keyIcon, int x, int y, float scale) {
@ -304,23 +294,24 @@ void freeLightBakes() {
sf2d_free_texture(glowwormBigLightBake);
}
void renderLightsToStencil(PlayerData *pd, bool force, bool invert, bool rplayer) {
if (force || (pd->entity.level > 1 && pd->entity.level != 5)) {
void renderLightsToStencil(bool force, bool invert, bool rplayer) {
if (force || (currentLevel > 1 && currentLevel != 5)) {
C3D_DepthTest(true, GPU_NEVER, 0);
C3D_StencilTest(true, GPU_NEVER, 1, 0xFF, 0xFF);
C3D_StencilOp(GPU_STENCIL_REPLACE, GPU_STENCIL_KEEP, GPU_STENCIL_KEEP);
C3D_AlphaTest(true, GPU_GREATER, 0);
if(pd->activeItem->id == ITEM_LANTERN) renderLight(pd->entity.x, pd->entity.y, lanternLightBake);
else if(rplayer) renderLight(pd->entity.x, pd->entity.y, playerLightBake);
if(player.p.activeItem->id == ITEM_LANTERN) renderLight(player.x, player.y, lanternLightBake);
else if(rplayer) renderLight(player.x, player.y, playerLightBake);
int i;
for (i = 0; i < eManager.lastSlot[pd->entity.level]; ++i) {
Entity e = eManager.entities[pd->entity.level][i];
for (i = 0; i < eManager.lastSlot[currentLevel]; ++i) {
Entity e = eManager.entities[currentLevel][i];
if (e.type == ENTITY_FURNITURE) {
if (e.entityFurniture.itemID == ITEM_LANTERN && e.x > pd->entity.x - 160 && e.y > pd->entity.y - 125 && e.x < pd->entity.x + 160 && e.y < pd->entity.y + 125)
if (e.entityFurniture.itemID == ITEM_LANTERN && e.x > player.x - 160 && e.y > player.y - 125 && e.x < player.x + 160 && e.y < player.y + 125)
renderLight(e.x, e.y, lanternLightBake);
} else if(e.type == ENTITY_GLOWWORM && e.x > pd->entity.x - 160 && e.y > pd->entity.y - 125 && e.x < pd->entity.x + 160 && e.y < pd->entity.y + 125) { //TODO could be made smaller becuase of smaller light radius
} else if(e.type == ENTITY_GLOWWORM && e.x > player.x - 160 && e.y > player.y - 125 && e.x < player.x + 160 && e.y < player.y + 125) { //TODO could be made smaller becuase of smaller light radius
if(rand()%10==0) continue;
else if(rand()%100==0) renderLight(e.x+20, e.y-20, glowwormBigLightBake);
else renderLight(e.x+8, e.y-8, glowwormLightBake);
@ -334,9 +325,9 @@ void renderLightsToStencil(PlayerData *pd, bool force, bool invert, bool rplayer
//TODO: Even this is not performant enough for old 3DS, when there is a lot of lava on screen
for (x = xo-2; x <= 13 + xo+2; ++x) {
for (y = yo-2; y <= 8 + yo+2; ++y) {
if(getTile(pd->entity.level, x, y) == TILE_LAVA) {
if(getTile(x, y) == TILE_LAVA) {
//experimental "speedhack"
if(getTile(pd->entity.level, x+1,y)==TILE_LAVA && getTile(pd->entity.level, x-1,y)==TILE_LAVA && getTile(pd->entity.level, x,y+1)==TILE_LAVA && getTile(pd->entity.level, x,y-1)==TILE_LAVA) {
if(getTile(x+1,y)==TILE_LAVA && getTile(x-1,y)==TILE_LAVA && getTile(x,y+1)==TILE_LAVA && getTile(x,y-1)==TILE_LAVA) {
if((x+y)%2 == 0) continue;
}
renderLight((x << 4) + 8, (y << 4) + 8, playerLightBake);
@ -357,8 +348,10 @@ void renderLightsToStencil(PlayerData *pd, bool force, bool invert, bool rplayer
}
void resetStencilStuff() {
//if (currentLevel > 1) {
C3D_StencilTest(false, GPU_ALWAYS, 0x00, 0xFF, 0x00);
C3D_StencilOp(GPU_STENCIL_KEEP, GPU_STENCIL_KEEP, GPU_STENCIL_KEEP);
//}
}
void renderLight(int x, int y, sf2d_texture* texture) {
@ -440,60 +433,60 @@ void resetSurrTiles() {
tdr = false;
}
void checkSurrTiles8(u8 level, int xt, int yt, int id) {
if (getTile(level, xt, yt - 1) == id)
void checkSurrTiles8(int xt, int yt, int id) {
if (getTile(xt, yt - 1) == id)
tu = true;
if (getTile(level, xt - 1, yt) == id)
if (getTile(xt - 1, yt) == id)
tl = true;
if (getTile(level, xt + 1, yt) == id)
if (getTile(xt + 1, yt) == id)
tr = true;
if (getTile(level, xt, yt + 1) == id)
if (getTile(xt, yt + 1) == id)
td = true;
if (getTile(level, xt - 1, yt - 1) == id)
if (getTile(xt - 1, yt - 1) == id)
tul = true;
if (getTile(level, xt + 1, yt - 1) == id)
if (getTile(xt + 1, yt - 1) == id)
tur = true;
if (getTile(level, xt - 1, yt + 1) == id)
if (getTile(xt - 1, yt + 1) == id)
tdl = true;
if (getTile(level, xt + 1, yt + 1) == id)
if (getTile(xt + 1, yt + 1) == id)
tdr = true;
}
void checkSurrTiles4(u8 level, int xt, int yt, int id) {
if (getTile(level, xt, yt - 1) == id)
void checkSurrTiles4(int xt, int yt, int id) {
if (getTile(xt, yt - 1) == id)
tu = true;
if (getTile(level, xt - 1, yt) == id)
if (getTile(xt - 1, yt) == id)
tl = true;
if (getTile(level, xt + 1, yt) == id)
if (getTile(xt + 1, yt) == id)
tr = true;
if (getTile(level, xt, yt + 1) == id)
if (getTile(xt, yt + 1) == id)
td = true;
}
u8 tData = 0;
void renderTile(int i, int d, u8 level, int x, int y) {
void renderTile(int i, int d, int x, int y) {
int age = 0;
switch (i) {
case TILE_GRASS:
checkSurrTiles4(level, x >> 4, y >> 4, TILE_GRASS);
checkSurrTiles4(level, x >> 4, y >> 4, TILE_TREE);
checkSurrTiles4(level, x >> 4, y >> 4, TILE_FLOWER);
checkSurrTiles4(level, x >> 4, y >> 4, TILE_SAPLING_TREE);
checkSurrTiles4(x >> 4, y >> 4, TILE_GRASS);
checkSurrTiles4(x >> 4, y >> 4, TILE_TREE);
checkSurrTiles4(x >> 4, y >> 4, TILE_FLOWER);
checkSurrTiles4(x >> 4, y >> 4, TILE_SAPLING_TREE);
if(level==1 && worldData.season==3) renderConnectedTile4(x, y, 256, 112);
else if(level==1 && worldData.season==2) renderConnectedTile4(x, y, 256, 128);
if(currentLevel==1 && season==3) renderConnectedTile4(x, y, 256, 112);
else if(currentLevel==1 && season==2) renderConnectedTile4(x, y, 256, 128);
else renderConnectedTile4(x, y, 256, 0);
break;
case TILE_TREE:
renderTile(TILE_GRASS, 0, level, x, y);
renderTile(TILE_GRASS, 0, x, y);
checkSurrTiles8(level, x >> 4, y >> 4, TILE_TREE);
checkSurrTiles8(x >> 4, y >> 4, TILE_TREE);
if(worldData.season==2) {
if(season==2) {
render(x, y, 352+((tu && tl && tul) ? 16 : 0), 96, 0);
render(x+8, y, 360+((tu && tr && tur) ? 16 : 0), 96, 0);
render(x, y+8, 352+((td && tl && tdl) ? 16 : 0), 104, 0);
render(x+8, y+8, 360+((td && tr && tdr) ? 16 : 0), 104, 0);
} else if(worldData.season==3) {
} else if(season==3) {
render(x, y, 352+((tu && tl && tul) ? 16 : 0), 112, 0);
render(x+8, y, 360+((tu && tr && tur) ? 16 : 0), 112, 0);
render(x, y+8, 352+((td && tl && tdl) ? 16 : 0), 120, 0);
@ -507,28 +500,28 @@ void renderTile(int i, int d, u8 level, int x, int y) {
break;
case TILE_ROCK:
checkSurrTiles8(level, x >> 4, y >> 4, TILE_ROCK);
if(level>1)
checkSurrTiles8(x >> 4, y >> 4, TILE_ROCK);
if(currentLevel>1)
renderConnectedTile8(x, y, 256, 96);
else
renderConnectedTile8(x, y, 336, 64);
break;
case TILE_HARDROCK:
checkSurrTiles8(level, x >> 4, y >> 4, TILE_HARDROCK);
checkSurrTiles8(x >> 4, y >> 4, TILE_HARDROCK);
renderConnectedTile8(x, y, 416, 64);
break;
case TILE_DIRT: // render dots.
if (level > 1)
if (currentLevel > 1)
render16(x, y, 320, 80, 0);
else
render16(x, y, 336, 80, 0);
break;
case TILE_SAND:
checkSurrTiles4(level, x >> 4, y >> 4, TILE_SAND);
checkSurrTiles4(level, x >> 4, y >> 4, TILE_CACTUS);
checkSurrTiles4(level, x >> 4, y >> 4, TILE_SAPLING_CACTUS);
checkSurrTiles4(x >> 4, y >> 4, TILE_SAND);
checkSurrTiles4(x >> 4, y >> 4, TILE_CACTUS);
checkSurrTiles4(x >> 4, y >> 4, TILE_SAPLING_CACTUS);
if(level==1 && worldData.season==3) {
if(currentLevel==1 && season==3) {
renderConnectedTile4(x, y, 256, 112);
} else {
renderConnectedTile4(x, y, 320, 0);
@ -539,43 +532,43 @@ void renderTile(int i, int d, u8 level, int x, int y) {
}
break;
case TILE_WATER:
checkSurrTiles4(level, x >> 4, y >> 4, TILE_WATER);
checkSurrTiles4(level, x >> 4, y >> 4, TILE_HOLE);
checkSurrTiles4(level, x >> 4, y >> 4, TILE_ICE);
checkSurrTiles4(x >> 4, y >> 4, TILE_WATER);
checkSurrTiles4(x >> 4, y >> 4, TILE_HOLE);
checkSurrTiles4(x >> 4, y >> 4, TILE_ICE);
renderConnectedTile4(x, y, 384, 0);
srand((syncTickCount + (x / 2 - y) * 4311) / 10);
srand((tickCount + (x / 2 - y) * 4311) / 10);
renderDots(x, y, rand() & 3, rand() & 3, rand() & 3, rand() & 3, 288, 64);
break;
case TILE_LAVA:
checkSurrTiles4(level, x >> 4, y >> 4, TILE_LAVA);
checkSurrTiles4(level, x >> 4, y >> 4, TILE_HOLE);
checkSurrTiles4(x >> 4, y >> 4, TILE_LAVA);
checkSurrTiles4(x >> 4, y >> 4, TILE_HOLE);
renderConnectedTile4(x, y, 448, 0);
srand((syncTickCount + (x / 2 - y) * 4311) / 10);
srand((tickCount + (x / 2 - y) * 4311) / 10);
renderDots(x, y, rand() & 3, rand() & 3, rand() & 3, rand() & 3, 304, 64);
break;
case TILE_HOLE:
checkSurrTiles4(level, x >> 4, y >> 4, TILE_HOLE);
checkSurrTiles4(level, x >> 4, y >> 4, TILE_WATER);
checkSurrTiles4(level, x >> 4, y >> 4, TILE_LAVA);
checkSurrTiles4(x >> 4, y >> 4, TILE_HOLE);
checkSurrTiles4(x >> 4, y >> 4, TILE_WATER);
checkSurrTiles4(x >> 4, y >> 4, TILE_LAVA);
renderConnectedTile4(x, y, 256, 16);
break;
case TILE_CACTUS:
renderTile(TILE_SAND, 0, level, x, y);
renderTile(TILE_SAND, 0, x, y);
render16(x, y, 304, 48, 0);
break;
case TILE_FLOWER:
renderTile(TILE_GRASS, 0, level, x, y);
if(level==1 && worldData.season==3) render16(x, y, 320, 112, d);
else render16(x, y, 320, 48, d);
renderTile(TILE_GRASS, 0, x, y);
if(currentLevel==1 && season==3) render16(x, y, 320, 112, getData(x >> 4, y >> 4));
else render16(x, y, 320, 48, getData(x >> 4, y >> 4));
break;
case TILE_STAIRS_DOWN:
if (level == 0)
renderTile(TILE_CLOUD, 0, level, x, y);
if (currentLevel == 0)
renderTile(TILE_CLOUD, 0, x, y);
render16(x, y, 256, 64, 0);
break;
case TILE_STAIRS_UP:
@ -591,60 +584,60 @@ void renderTile(int i, int d, u8 level, int x, int y) {
render16(x, y, 496, 48, 0);
break;
case TILE_CLOUD:
checkSurrTiles4(level, x >> 4, y >> 4, TILE_CLOUD);
checkSurrTiles4(level, x >> 4, y >> 4, TILE_STAIRS_DOWN);
checkSurrTiles4(level, x >> 4, y >> 4, TILE_CLOUDCACTUS);
checkSurrTiles4(x >> 4, y >> 4, TILE_CLOUD);
checkSurrTiles4(x >> 4, y >> 4, TILE_STAIRS_DOWN);
checkSurrTiles4(x >> 4, y >> 4, TILE_CLOUDCACTUS);
renderConnectedTile4(x, y, 320, 16);
break;
case TILE_CLOUDCACTUS:
renderTile(TILE_CLOUD, 0, level, x, y);
renderTile(TILE_CLOUD, 0, x, y);
render16(x, y, 496, 64, 0);
break;
case TILE_SAPLING_TREE:
renderTile(TILE_GRASS, 0, level, x, y);
renderTile(TILE_GRASS, 0, x, y);
render16(x, y, 288, 48, 0);
break;
case TILE_SAPLING_CACTUS:
renderTile(TILE_SAND, 0, level, x, y);
renderTile(TILE_SAND, 0, x, y);
render16(x, y, 288, 48, 0);
break;
case TILE_FARM:
render16(x, y, 352, 48, 0);
break;
case TILE_WHEAT:
age = d / 20;
age = getData(x >> 4, y >> 4) / 20;
if (age > 5)
age = 5;
render16(x, y, 368 + (age << 4), 48, 0);
break;
case TILE_WOOD_WALL:
checkSurrTiles4(level, x >> 4, y >> 4, TILE_WOOD_WALL);
checkSurrTiles4(x >> 4, y >> 4, TILE_WOOD_WALL);
renderConnectedTile4(x, y, 384, 16);
break;
case TILE_STONE_WALL:
checkSurrTiles4(level, x >> 4, y >> 4, TILE_STONE_WALL);
checkSurrTiles4(x >> 4, y >> 4, TILE_STONE_WALL);
renderConnectedTile4(x, y, 256, 80);
break;
case TILE_IRON_WALL:
checkSurrTiles4(level, x >> 4, y >> 4, TILE_IRON_WALL);
checkSurrTiles4(x >> 4, y >> 4, TILE_IRON_WALL);
renderConnectedTile4(x, y, 448, 16);
break;
case TILE_GOLD_WALL:
checkSurrTiles4(level, x >> 4, y >> 4, TILE_GOLD_WALL);
checkSurrTiles4(x >> 4, y >> 4, TILE_GOLD_WALL);
renderConnectedTile4(x, y, 256, 32);
break;
case TILE_GEM_WALL:
checkSurrTiles4(level, x >> 4, y >> 4, TILE_GEM_WALL);
checkSurrTiles4(x >> 4, y >> 4, TILE_GEM_WALL);
renderConnectedTile4(x, y, 320, 32);
break;
case TILE_DUNGEON_WALL:
checkSurrTiles8(level, x >> 4, y >> 4, TILE_DUNGEON_WALL);
checkSurrTiles8(x >> 4, y >> 4, TILE_DUNGEON_WALL);
renderConnectedTile8(x, y, 384, 32);
break;
@ -652,22 +645,21 @@ void renderTile(int i, int d, u8 level, int x, int y) {
render16(x, y, 464 + d*16, 32, 0);
break;
case TILE_DUNGEON_ENTRANCE:
render16(x, y, 352 + (level==5 ? 16 : 0), 80, 0);
render16(x, y, 352 + (currentLevel==5 ? 16 : 0), 80, 0);
break;
case TILE_MAGIC_BARRIER:
renderTile(TILE_DUNGEON_FLOOR, 0, level, x, y);
render16(x, y, 320, 64, d);
renderTile(TILE_DUNGEON_FLOOR, 0, x, y);
render16(x, y, 320, 64, getData(x >> 4, y >> 4));
//draw remaining pillar count
PlayerData *lp = getLocalPlayer();
if((lp->entity.x - (x+8))*(lp->entity.x - (x+8)) + (lp->entity.y - (y+8))*(lp->entity.y - (y+8)) <= 24*24) {
if((player.x - (x+8))*(player.x - (x+8)) + (player.y - (y+8))*(player.y - (y+8)) <= 24*24) {
x -= offsetX;
y -= offsetY;
int data = 0;
int i = 0;
for (i = 0; i < eManager.lastSlot[level]; ++i) {
Entity * e = &eManager.entities[level][i];
for (i = 0; i < eManager.lastSlot[currentLevel]; ++i) {
Entity * e = &eManager.entities[currentLevel][i];
if(e->type == ENTITY_MAGIC_PILLAR) {
++data;
@ -683,7 +675,7 @@ void renderTile(int i, int d, u8 level, int x, int y) {
break;
case TILE_BOOKSHELVES:
checkSurrTiles4(level, x >> 4, y >> 4, TILE_BOOKSHELVES);
checkSurrTiles4(x >> 4, y >> 4, TILE_BOOKSHELVES);
renderConnectedTile4(x, y, 384, 80 + d*16);
break;
@ -691,26 +683,26 @@ void renderTile(int i, int d, u8 level, int x, int y) {
render16(x, y, 336, 96, 0);
break;
case TILE_MYCELIUM:
checkSurrTiles4(level, x >> 4, y >> 4, TILE_MYCELIUM);
checkSurrTiles4(level, x >> 4, y >> 4, TILE_MUSHROOM_BROWN);
checkSurrTiles4(level, x >> 4, y >> 4, TILE_MUSHROOM_RED);
checkSurrTiles4(x >> 4, y >> 4, TILE_MYCELIUM);
checkSurrTiles4(x >> 4, y >> 4, TILE_MUSHROOM_BROWN);
checkSurrTiles4(x >> 4, y >> 4, TILE_MUSHROOM_RED);
if(level==1 && worldData.season==3) renderConnectedTile4(x, y, 256, 112);
if(currentLevel==1 && season==3) renderConnectedTile4(x, y, 256, 112);
else renderConnectedTile4(x, y, 448, 80);
break;
case TILE_MUSHROOM_BROWN:
renderTile(TILE_MYCELIUM, 0, level, x, y);
renderTile(TILE_MYCELIUM, 0, x, y);
render16(x, y, 448 + (d&0x1)*16, 96, 0);
break;
case TILE_MUSHROOM_RED:
renderTile(TILE_MYCELIUM, 0, level, x, y);
renderTile(TILE_MYCELIUM, 0, x, y);
render16(x, y, 480 + (d&0x1)*16, 96, 0);
break;
case TILE_ICE:
renderTile(TILE_WATER, 0, level, x, y);
renderTile(TILE_WATER, 0, x, y);
//checkSurrTiles4(x >> 4, y >> 4, TILE_WATER);
//checkSurrTiles4(x >> 4, y >> 4, TILE_HOLE);
checkSurrTiles4(level, x >> 4, y >> 4, TILE_ICE);
checkSurrTiles4(x >> 4, y >> 4, TILE_ICE);
renderConnectedTile4(x, y, 448, 112);
break;
@ -755,78 +747,70 @@ void renderConnectedTile8(int x, int y, u32 xTile, u32 yTile) {
render(x+8, y+8, xTile+8+r+d+((tr && td && tdr) ? 16 : 0), yTile+8, 0);
}
void renderZoomedMap(PlayerData *pd) {
void renderZoomedMap() {
sf2d_draw_rectangle(0, 0, 320, 240, 0xFF0C0C0C); //You might think "real" black would be better, but it actually looks better that way
int mx = pd->mapScrollX;
int my = pd->mapScrollY;
if(pd->mapZoomLevel == 2) mx = 32;
sf2d_draw_texture_scale(minimap[pd->entity.level], mx, my, pd->mapZoomLevel, pd->mapZoomLevel); // zoomed map
int mx = mScrollX;
int my = mScrollY;
if(zoomLevel == 2) mx = 32;
sf2d_draw_texture_scale(minimap[currentLevel], mx, my, zoomLevel, zoomLevel); // zoomed map
// Airwizard on zoomed map
if(pd->entity.level == 0){
if(currentLevel == 0){
if(awX != 0 && awY != 0){
render16c(
(mx+((awX/16)*pd->mapZoomLevel)-16)/2,
(my+((awY/16)*pd->mapZoomLevel)-16)/2,
(mx+((awX/16)*zoomLevel)-16)/2,
(my+((awY/16)*zoomLevel)-16)/2,
160, 112,
((pd->entity.p.walkDist >> 6) & 1) == 0 ? 0 : 1,
((player.p.walkDist >> 6) & 1) == 0 ? 0 : 1,
2, 2
);
}
}
// Player on zoomed map
//TODO: Maybe also render other players?
render16c(
(mx+((pd->entity.x/16)*pd->mapZoomLevel)-16)/2,
(my+((pd->entity.y/16)*pd->mapZoomLevel)-16)/2,
(mx+((player.x/16)*zoomLevel)-16)/2,
(my+((player.y/16)*zoomLevel)-16)/2,
0, 112,
((pd->entity.p.walkDist >> 6) & 1) == 0 ? 0 : 1,
((player.p.walkDist >> 6) & 1) == 0 ? 0 : 1,
2, 2
);
drawText(pd->mapText,224, 214); // "x2"/"x4"/"x6"
drawText(mapText,224, 214); // "x2"/"x4"/"x6"
render16(142, 2, 72, 208, 0); // Exit button
renderc(126, 102, 40, 208, 32, 16, 0); // Plus/Minus zoom buttons
if(pd->mapZoomLevel < 3) sf2d_draw_rectangle(258, 210, 26, 20, 0x7F4F4F4F); // gray out minus button
else if(pd->mapZoomLevel > 5) sf2d_draw_rectangle(284, 210, 26, 20, 0x7F4F4F4F); // gray out minus button
if(zoomLevel < 3) sf2d_draw_rectangle(258, 210, 26, 20, 0x7F4F4F4F); // gray out minus button
else if(zoomLevel > 5) sf2d_draw_rectangle(284, 210, 26, 20, 0x7F4F4F4F); // gray out minus button
}
char scoreT[32];
void renderGui(PlayerData *pd) {
void renderGui() {
int i;
//health and stamina
for (i = 0; i < 10; ++i) {
if (i < pd->entity.p.health)
if (i < player.p.health)
render(i * 8 + 6, 5, 168, 152, 0);
else
render(i * 8 + 6, 5, 176, 152, 0);
if (i < pd->entity.p.stamina)
if (i < player.p.stamina)
render(i * 8 + 6, 14, 184, 152, 0);
else
render(i * 8 + 6, 14, 191, 152, 0);
}
//minimap
sf2d_draw_texture(minimap[pd->entity.level], 10, 102);
//active item
renderItemWithTextCentered(pd->activeItem, 320, 66);
itoa(pd->score, scoreT, 10); // integer to base10 string
sf2d_draw_texture(minimap[currentLevel], 10, 102);
renderItemWithTextCentered(player.p.activeItem, 320, 66);
itoa(player.p.score, scoreT, 10); // integer to base10 string
drawText("Score:",214,12);
drawText(scoreT,(140-(strlen(scoreT)*12))/2 + 180,29);
if(pd->entity.level == 0){
if(currentLevel == 0){
if(awX != 0 && awY != 0){
renderc(1 + (awX/32), 47 + (awY/32), 88, 216, 8, 8, 0); // Mini-AWizard head.
}
}
//TODO: Maybe also render other players?
renderc(1 + (pd->entity.x/32), 47 + (pd->entity.y/32), 88, 208, 8, 8, 0); // Mini-Player head.
renderc(1 + (player.x/32), 47 + (player.y/32), 88, 208, 8, 8, 0); // Mini-Player head.
//quick select
drawText("Quickselect:",164,118);
Inventory * inv = &(pd->inventory);
Inventory * inv = player.p.inv;
Item * item;
for (i = 0; i < 8; ++i) {
if((inv->lastSlot) > i) {
@ -839,112 +823,103 @@ void renderGui(PlayerData *pd) {
}
}
void renderPlayer(PlayerData *pd) {
if (pd->entity.level!=getLocalPlayer()->entity.level) {
void renderPlayer() {
if (player.p.isDead)
return;
}
if (pd->entity.p.isDead) {
return;
}
int xo = pd->entity.x - 8;
int yo = pd->entity.y - 8;
int xo = player.x - 8;
int yo = player.y - 8;
//attack animation upwards
if (pd->entity.p.attackTimer > 0 && pd->entity.p.dir == 1) {
if (player.p.attackTimer > 0 && player.p.dir == 1) {
renderc(xo, yo - 4, 16, 160, 16, 8, 0);
renderItemIcon(pd->activeItem->id, pd->activeItem->countLevel, xo + 4, yo - 4);
renderItemIcon(player.p.activeItem->id, player.p.activeItem->countLevel,
xo + 4, yo - 4);
}
//find basic indices
int aIndexBig = 0;
int aIndexSmall = 0;
switch(pd->entity.p.dir) {
u8 walk = (player.p.walkDist >> 4) & 1;
bool swimming = isSwimming();
switch (player.p.dir) {
case 0: // down
aIndexBig = 0;
aIndexSmall = 0;
if (swimming)
renderc(xo, yo + 4, 48,
160 + (((player.p.swimTimer >> 4) & 1) << 3), 16, 8, 0);
else
renderc(xo, yo + 8, 0, 120 + (player.p.isCarrying ? 16 : 0), 16, 8,
walk == 0 ? 0 : 1);
renderc(xo, yo, 0, 112 + (player.p.isCarrying ? 16 : 0), 16, 8,
walk == 0 ? 0 : 1);
break;
case 1: // up
aIndexBig = 2;
aIndexSmall = 1;
if (swimming)
renderc(xo, yo + 4, 48,
160 + (((player.p.swimTimer >> 4) & 1) << 3), 16, 8, 0);
else
renderc(xo, yo + 8, 16, 120 + (player.p.isCarrying ? 16 : 0), 16, 8,
walk == 0 ? 0 : 1);
renderc(xo, yo, 16, 112 + (player.p.isCarrying ? 16 : 0), 16, 8,
walk == 0 ? 0 : 1);
break;
case 2: // left
aIndexBig = 7;
aIndexSmall = 3;
if (swimming)
renderc(xo, yo + 4, 48,
160 + (((player.p.swimTimer >> 4) & 1) << 3), 16, 8, 0);
else
renderc(xo, yo + 8, 32 + (walk << 4),
120 + (player.p.isCarrying ? 16 : 0), 16, 8, 1);
renderc(xo, yo, 32 + (walk << 4), 112 + (player.p.isCarrying ? 16 : 0),
16, 8, 1);
break;
case 3: // right
aIndexBig = 4;
aIndexSmall = 2;
if (swimming)
renderc(xo, yo + 4, 48,
160 + (((player.p.swimTimer >> 4) & 1) << 3), 16, 8, 0);
else
renderc(xo, yo + 8, 32 + (walk << 4),
120 + (player.p.isCarrying ? 16 : 0), 16, 8, 0);
renderc(xo, yo, 32 + (walk << 4), 112 + (player.p.isCarrying ? 16 : 0),
16, 8, 0);
break;
}
//find index offset based on walk state
u8 walkingOffset = (pd->entity.p.walkDist >> 4) % 2;
if(pd->entity.p.dir==2 || pd->entity.p.dir==3) {
walkingOffset = (pd->entity.p.walkDist >> 4) % 4;
if(walkingOffset==2) walkingOffset = 0;
if(walkingOffset==3) walkingOffset = 2;
if (player.p.isCarrying) {
renderFurniture(player.p.activeItem->id, xo, yo - 12);
}
bool swimming = isWater(pd->entity.level, pd->entity.x>>4, pd->entity.y>>4);
//render water anim when swimming
if (swimming) {
renderc(xo, yo + 5, 48, 160 + (((pd->entity.p.swimTimer >> 4) & 1) << 3), 16, 8, 0);
}
//render the different parts
//legs
if(!swimming) {
renderp(xo, yo, (0+aIndexBig+walkingOffset)*16, pd->sprite.legs*16);
}
//body
renderp(xo, yo, (10+aIndexBig+walkingOffset)*16, pd->sprite.body*16);
//arms (normal)
if(!(pd->entity.p.isCarrying)) {
renderp(xo, yo, (20+aIndexBig+walkingOffset)*16, pd->sprite.arms*16);
}
//head
renderp(xo, yo, (30+aIndexSmall)*16, pd->sprite.head*16);
//eyes
renderp(xo, yo, (34+aIndexSmall)*16, pd->sprite.eyes*16);
//arms (carrying)
if(pd->entity.p.isCarrying) {
renderp(xo, yo, (38+aIndexSmall)*16, pd->sprite.arms*16);
}
//furniture
if (pd->entity.p.isCarrying) {
renderFurniture(pd->activeItem->id, xo, yo - 12);
}
//attack animation (other directios)
if (pd->entity.p.attackTimer > 0) {
switch (pd->entity.p.dir) {
if (player.p.attackTimer > 0) {
switch (player.p.dir) {
case 0:
renderc(xo - pd->entity.p.ax, yo - pd->entity.p.ay + 12, 16, 168, 16, 8, 0);
renderItemIcon(pd->activeItem->id, pd->activeItem->countLevel, xo + 4, yo + 12);
renderc(xo - player.p.ax, yo - player.p.ay + 12, 16, 168, 16, 8, 0);
renderItemIcon(player.p.activeItem->id,
player.p.activeItem->countLevel, xo + 4, yo + 12);
break;
case 2:
renderc(xo - pd->entity.p.ax - 4, yo - pd->entity.p.ay, 32, 160, 8, 16, 0);
renderItemIcon(pd->activeItem->id, pd->activeItem->countLevel, xo - 4, yo + 4);
renderc(xo - player.p.ax - 4, yo - player.p.ay, 32, 160, 8, 16, 0);
renderItemIcon(player.p.activeItem->id,
player.p.activeItem->countLevel, xo - 4, yo + 4);
break;
case 3:
renderc(xo - pd->entity.p.ax + 12, yo - pd->entity.p.ay, 40, 160, 8, 16, 0);
renderItemIcon(pd->activeItem->id, pd->activeItem->countLevel, xo + 12, yo + 4);
renderc(xo - player.p.ax + 12, yo - player.p.ay, 40, 160, 8, 16, 0);
renderItemIcon(player.p.activeItem->id,
player.p.activeItem->countLevel, xo + 12, yo + 4);
break;
}
}
}
void renderWeather(u8 level, int xScroll, int yScroll) {
if(level==1) {
if(worldData.season==3) {
int xp = -128 + ((syncTickCount>>2) - xScroll*2)%128;
int yp = -128 + ((syncTickCount>>1) - yScroll*2)%128;
int xp2 = 0 - ((syncTickCount>>2) + xScroll*2)%128;
int yp2 = -128 + ((syncTickCount>>1)+64 - yScroll*2)%128;
void renderMenuBackground(int xScroll, int yScroll) {
sf2d_draw_rectangle(0, 0, 400, 240, 0xFF0C0C0C); //You might think "real" black would be better, but it actually looks better that way
renderLightsToStencil(false, false, true);
renderBackground(xScroll, yScroll);
resetStencilStuff();
renderDayNight();
}
void renderWeather(int xScroll, int yScroll) {
if(currentLevel==1) {
if(season==3) {
int xp = -128 + ((tickCount>>2) - xScroll*2)%128;
int yp = -128 + ((tickCount>>1) - yScroll*2)%128;
int xp2 = 0 - ((tickCount>>2) + xScroll*2)%128;
int yp2 = -128 + ((tickCount>>1)+64 - yScroll*2)%128;
int xt;
int yt;
for(xt=0; xt<4; ++xt) {
@ -955,11 +930,11 @@ void renderWeather(u8 level, int xScroll, int yScroll) {
}
}
if(worldData.rain) {
if(rain) {
int xp = -((xScroll*2)%128);
int yp = -128 + ((syncTickCount<<2) - yScroll*2)%128;
int yp = -128 + ((tickCount<<2) - yScroll*2)%128;
int xp2 = -((xScroll*2+8)%128);
int yp2 = -128 + ((syncTickCount<<1)+64 - yScroll*2)%128;
int yp2 = -128 + ((tickCount<<1)+64 - yScroll*2)%128;
int xt;
int yt;
for(xt=0; xt<4; ++xt) {
@ -972,46 +947,46 @@ void renderWeather(u8 level, int xScroll, int yScroll) {
}
}
void renderDayNight(PlayerData *pd) {
if(pd->entity.level==1 && (worldData.daytime<6000 || worldData.daytime>18000)) {
void renderDayNight() {
if(currentLevel==1 && (daytime<6000 || daytime>18000)) {
int color1 = 0x000C0C0C;
int color2 = 0x00100C0C;
int alpha1 = 0x88;
int alpha2 = 0xDD;
if(worldData.daytime>5000 && worldData.daytime<6000) {
alpha2 = (alpha2 * (1000-(worldData.daytime-5000)))/1000;
alpha1 = (alpha1 * (1000-(worldData.daytime-5000)))/1000;
} else if(worldData.daytime>18000 && worldData.daytime<19000) {
alpha1 = (alpha1 * (worldData.daytime-18000))/1000;
alpha2 = (alpha2 * (worldData.daytime-18000))/1000;
if(daytime>5000 && daytime<6000) {
alpha1 = (alpha1 * (1000-(daytime-5000)))/1000;
alpha2 = (alpha2 * (1000-(daytime-5000)))/1000;
} else if(daytime>18000 && daytime<19000) {
alpha1 = (alpha1 * (daytime-18000))/1000;
alpha2 = (alpha2 * (daytime-18000))/1000;
}
color1 = color1 | (alpha1 << 24);
color2 = color2 | (alpha2 << 24);
sf2d_draw_rectangle(0, 0, 400, 240, color1); //You might think "real" black would be better, but it actually looks better that way
renderLightsToStencil(pd, true, true, false);
renderLightsToStencil(true, true, false);
sf2d_draw_rectangle(0, 0, 400, 240, color2); //You might think "real" black would be better, but it actually looks better that way
resetStencilStuff();
}
}
void renderBackground(s8 level, int xScroll, int yScroll) {
if(level == 0) {
void renderBackground(int xScroll, int yScroll) {
if(currentLevel == 0) {
sf2d_draw_texture_part_scale(minimap[1], (-xScroll / 3) - 256, (-yScroll / 3) - 32, 0, 0, 128, 128, 12.5, 7.5);
sf2d_draw_rectangle(0, 0, 400, 240, 0xAFDFDFDF);
} else if(level == 5) {
} else if(currentLevel == 5) {
sf2d_draw_rectangle(0, 0, 400, 240, dungeonColor[1]);
} else {
sf2d_draw_rectangle(0, 0, 400, 240, dirtColor[level]); // dirt color
sf2d_draw_rectangle(0, 0, 400, 240, dirtColor[currentLevel]); // dirt color
}
int xo = xScroll >> 4;
int yo = yScroll >> 4;
int x, y;
for (x = xo; x <= 13 + xo; ++x) {
for (y = yo; y <= 8 + yo; ++y)
renderTile(getTile(level, x, y), getData(level, x, y), level, x << 4, y << 4);
renderTile(getTile(x, y), getData(x, y), x << 4, y << 4);
}
}
@ -1118,7 +1093,7 @@ void renderFurniture(int itemID, int x, int y) {
render16(x, y, 240, 128, 0);
break;
case ITEM_POTION_MAKER:
render16(x, y, 192, 168, 0);
render16(x, y, 240, 96, 0);
break;
}
}
@ -1191,10 +1166,10 @@ void renderEntity(Entity* e, int x, int y) {
case ENTITY_AIRWIZARD:
e->wizard.spriteAdjust = 0;
if (e->wizard.health < 200) {
if (syncTickCount / 4 % 3 < 2)
if (tickCount / 4 % 3 < 2)
e->wizard.spriteAdjust = 16;
} else if (e->wizard.health < 1000) {
if (syncTickCount / 5 % 4 < 2)
if (tickCount / 5 % 4 < 2)
e->wizard.spriteAdjust = 16;
}
switch (e->wizard.dir) {
@ -1319,10 +1294,10 @@ void renderEntity(Entity* e, int x, int y) {
}
}
void renderEntities(u8 level, int x, int y, EntityManager* em) {
void renderEntities(int x, int y, EntityManager* em) {
int i;
for (i = 0; i < em->lastSlot[level]; ++i) {
Entity e = em->entities[level][i];
for (i = 0; i < em->lastSlot[currentLevel]; ++i) {
Entity e = em->entities[currentLevel][i];
if (e.x > x - 200 && e.y > y - 125 && e.x < x + 200 && e.y < y + 125)
renderEntity(&e, e.x, e.y);
}
@ -1363,49 +1338,6 @@ void renderItemList(Inventory * inv, int xo, int yo, int x1, int y1,
}
}
void renderArmorList(Inventory * inv, int xo, int yo, int x1, int y1,
int selected) {
// If lastSlot is 0, then there are no items are in the inventory.
bool drawCursor = true;
int w = x1 - xo;
int h = y1 - yo - 2;
int i1 = inv->lastSlot;
if (i1 > h)
i1 = h;
int i;
int io = selected - h / 2;
int armorcount;
for (i = 0; i < i1; ++i) {
if(inv->items[i + io].id > 119 && inv->items[i + io].id < 141) {
armorcount = armorcount + 1;
}
}
if (selected < 0) {
drawCursor = false;
selected = 0;
}
if (io > inv->lastSlot - h)
io = armorcount;
if (io < 0)
io = 1;
for (i = 0; i < i1; ++i) {
if(inv->items[i + io].id > 119 && inv->items[i + io].id < 141) {
renderItemWithText(&inv->items[i + io], (1 + xo) << 4,
(i + 1 + yo) << 4);
}
}
if (drawCursor) {
int yy = selected + 1 - io + yo;
sf2d_draw_rectangle((xo << 4) - (offsetX << 1),
(yy << 4) - (offsetY << 1), 12, 12, 0xFF);
drawText(">", (xo << 4), yy << 4);
sf2d_draw_rectangle(((xo + w) << 4) - 12 - (offsetX << 1),
(yy << 4) - (offsetY << 1), 12, 12, 0xFF);
drawText("<", ((xo + w) << 4) - 12, yy << 4);
}
}
void renderRecipes(RecipeManager * r, int xo, int yo, int x1, int y1, int selected) {
int size = r->size;
if (size < 1)
@ -1523,7 +1455,7 @@ void renderItemIcon(int itemID, int countLevel, int x, int y) {
render(x, y, 0, 152, 0);
break;
case ITEM_WOOD:
render(x, y, 160, 168, 0);
render(x, y, 8, 152, 0);
break;
case ITEM_STONE:
renderb(x, y, 16, 152, 0, rockColor[1]);
@ -1559,19 +1491,16 @@ void renderItemIcon(int itemID, int countLevel, int x, int y) {
render(x, y, 80, 152, 0);
break;
case ITEM_GOLD_APPLE:
render(x, y, 144, 168, 0);
render(x, y, 177, 160, 0);
break;
case ITEM_STRENGTH_POTION:
render(x, y, 176, 160, 0);
break;
case ITEM_SPEED_POTION:
render(x, y, 184, 160, 0);
break;
case ITEM_REGEN_POTION:
render(x, y, 192, 160, 0);
case ITEM_SPEED_POTION:
render(x, y, 191, 160, 0);
break;
case ITEM_SWIM_BREATH_POTION:
render(x, y, 200, 160, 0);
case ITEM_REGEN_POTION:
render(x, y, 198, 160, 0);
break;
case ITEM_SLIME:
renderb(x, y, 88, 152, 0, 0xFF4DC04D);
@ -1604,7 +1533,7 @@ void renderItemIcon(int itemID, int countLevel, int x, int y) {
render(x, y, 144, 160, 0);
break;
case ITEM_POTION_MAKER:
render(x, y, 192, 168, 0);
render(x, y, 216, 152, 0);
break;
case ITEM_WALL_WOOD:
renderb(x, y, 224, 144, 0, woodColor);
@ -1688,8 +1617,8 @@ void renderItemIcon(int itemID, int countLevel, int x, int y) {
render(x, y, 64, 168, 0);
break;
case TOOL_MAGIC_COMPASS:
xd = worldData.compassData[getLocalPlayer()->entity.level][0] - (getLocalPlayer()->entity.x>>4);
yd = worldData.compassData[getLocalPlayer()->entity.level][1] - (getLocalPlayer()->entity.y>>4);
xd = compassData[currentLevel][0] - (player.x>>4);
yd = compassData[currentLevel][1] - (player.y>>4);
if(abs(yd)>abs(xd)) {
if(yd<0) render(x, y, 112, 168, 0);
else render(x, y, 120, 168, 0);
@ -1698,26 +1627,5 @@ void renderItemIcon(int itemID, int countLevel, int x, int y) {
else render(x, y, 136, 168, 0);
}
break;
case TOOL_CLAYMORE:
render(x, y, countLevel * 8, 176, 0);
break;
case ITEM_TORCH:
render(x, y, 168, 168, 0);
break;
case ITEM_BED:
render(x, y, 176, 168, 0);
break;
case ITEM_SHEARS:
render(x, y, 40, 176, 0);
break;
case ITEM_FISHING_ROD:
render(x, y, 48, 176, 0);
break;
case ITEM_FISH_RAW:
render(x, y, 56, 176, 0);
break;
case ITEM_FISH_COOKED:
render(x, y, 64, 176, 0);
break;
}
}

21
source/Render.h Executable file → Normal file
View file

@ -12,7 +12,6 @@ sf2d_texture *lanternLightBake;
sf2d_texture *glowwormLightBake;
sf2d_texture *glowwormBigLightBake;
int offsetX, offsetY;
int playerScale;
void render(s32 xp, s32 yp, u32 xTile, u32 yTile, u8 bits);
void renderb(s32 xp, s32 yp, u32 xTile, u32 yTile, u8 bits, u32 color);
@ -27,24 +26,25 @@ void render32(s32 xp, s32 yp, u32 xTile, u32 yTile, u8 bits);
void renderTitle(int x, int y);
void renderFrame(int x1, int y1, int x2, int y2, u32 bgColor);
void renderTile(int i, int d, u8 level, int x, int y);
void renderTile(int i, int d, int x, int y);
void renderConnectedTile4(int x, int y, u32 xTile, u32 yTile);
void renderConnectedTile8(int x, int y, u32 xTile, u32 yTile);
void renderBackground(s8 level, int xScroll, int yScroll);
void renderWeather(u8 level, int xScroll, int yScroll);
void renderDayNight(PlayerData *pd);
void renderBackground(int xScroll, int yScroll);
void renderMenuBackground(int xScroll, int yScroll); //Renders the darkness
void renderWeather(int xScroll, int yScroll);
void renderDayNight();
void renderButtonIcon(u32 icon, int x, int y, float scale);
void bakeLights();
void freeLightBakes();
void renderLightsToStencil(PlayerData *pd, bool force, bool invert, bool rplayer);
void renderLightsToStencil(bool force, bool invert, bool rplayer);
void resetStencilStuff();
void bakeLight(sf2d_texture* texture, int x, int y, int r);
void renderLight(int x, int y, sf2d_texture* texture);
void renderGui(PlayerData *pd);
void renderZoomedMap(PlayerData *pd);
void renderPlayer(PlayerData *pd);
void renderGui();
void renderZoomedMap();
void renderPlayer();
void drawText(char * msg, u32 x, u32 y);
void drawSizedText(char * msg, u32 x, u32 y, float size);
@ -54,11 +54,10 @@ void drawSizedTextColor(char * msg, int x, int y, float size, u32 color);
void renderFurniture(int itemID, int x, int y);
void renderEntity(Entity* e, int x, int y);
void renderEntities(u8 level, int x, int y, EntityManager* em);
void renderEntities(int x, int y, EntityManager* em);
void renderRecipes(RecipeManager * r, int xo, int yo, int x1, int y1, int selected);
void renderItemList(Inventory * inv, int xo, int yo, int x1, int y1, int selected);
void renderArmorList(Inventory * inv, int xo, int yo, int x1, int y1, int selected);
void renderItemWithText(Item* item, int x, int y);
void renderItemStuffWithText(int itemID, int itemCL, bool onlyOne, int x, int y);
void renderItemWithTextCentered(Item* item, int width, int y);

452
source/SaveLoad.c Executable file → Normal file
View file

@ -1,7 +1,5 @@
#include "SaveLoad.h"
#include "ZipHelper.h"
bool entityIsImportant(Entity * e){
switch(e->type){
case ENTITY_AIRWIZARD:
@ -32,118 +30,39 @@ s16 calculateImportantEntites(EntityManager * eManager, int level){
return count;
}
//helper methods
char **files;
int fileCount;
void saveCurrentWorld(char * filename, EntityManager * eManager, Entity * player, u8 * map, u8 * mapData){
FILE * file = fopen(filename, "wb");
int i,j;
void saveTrackFileReset() {
if(files!=NULL) {
for(int i=0; i<fileCount; i++) {
free(files[i]);
}
free(files);
}
// Player Data
fwrite(&player->p.score, sizeof(int), 1, file);
fwrite(&player->p.hasWonSaved, sizeof(bool), 1, file);
fwrite(&player->p.health, sizeof(s16), 1, file);
fwrite(&player->x, sizeof(s16), 1, file);
fwrite(&player->y, sizeof(s16), 1, file);
fwrite(&currentLevel, sizeof(s8), 1, file);
files = NULL;
fileCount = 0;
}
int saveTrackFile(char *filename) {
//test if entry allready present
for(int i=0; i<fileCount; i++) {
if(strcmp(filename, files[i])==0) {
return 0;
}
}
//add new entry
fileCount++;
char **newFiles = realloc(files, fileCount*sizeof(char*));
if(!newFiles) {
for(int i=0; i<fileCount-1; i++) {
free(files[i]);
}
free(files);
files = NULL;
return 1;
}
files = newFiles;
files[fileCount-1] = malloc(strlen(filename)+1);
strcpy(files[fileCount-1], filename);
return 0;
}
void saveDeleteTrackedFiles() {
for(int i=0; i<fileCount; i++) {
remove(files[i]);
}
}
bool saveFileCopy(char *target, char*source) {
char buffer[SAVE_COPYBUFFER_SIZE];
FILE *in = fopen(source, "rb");
if(in==NULL) {
return false;
}
FILE *out = fopen(target, "wb");
if(out==NULL) {
fclose(out);
return false;
}
size_t size;
do {
size = fread(buffer, 1, SAVE_COPYBUFFER_SIZE, in);
if(size>0) {
fwrite(buffer, 1, size, out);
}
} while(size>0);
fclose(in);
fclose(out);
return true;
}
//internal save methods
void saveInventory(Inventory *inv, EntityManager *eManager, FILE *file) {
fwrite(&inv->lastSlot, sizeof(s16), 1, file); // write amount of items in inventory;
for(int j = 0; j < inv->lastSlot; ++j) {
fwrite(&(inv->items[j].id), sizeof(s16), 1, file); // write ID of item
fwrite(&(inv->items[j].countLevel), sizeof(s16), 1, file); // write count/level of item
fwrite(&(inv->items[j].onlyOne), sizeof(bool), 1, file);
if(inv->items[j].id == ITEM_CHEST){
int invIndex = inv->items[j].chestPtr - eManager->invs;
// Inventory Data
fwrite(&eManager->nextInv, sizeof(s16), 1, file); // write amount of inventories.
for(i = 0; i < eManager->nextInv; ++i) {
fwrite(&eManager->invs[i].lastSlot, sizeof(s16), 1, file); // write amount of items in inventory;
for(j = 0; j < eManager->invs[i].lastSlot; ++j) {
fwrite(&eManager->invs[i].items[j].id, sizeof(s16), 1, file); // write ID of item
fwrite(&eManager->invs[i].items[j].countLevel, sizeof(s16), 1, file); // write count/level of item
fwrite(&eManager->invs[i].items[j].onlyOne, sizeof(bool), 1, file);
if(eManager->invs[i].items[j].id == ITEM_CHEST){
int invIndex = eManager->invs[i].items[j].chestPtr - eManager->invs;
fwrite(&invIndex, sizeof(int), 1, file);
}
}
}
void saveWorldInternal(char *filename, EntityManager *eManager, WorldData *worldData) {
FILE * file = fopen(filename, "wb"); //TODO: should be checked
int i, j;
//write savefile version
int version = SAVE_VERSION;
fwrite(&version, sizeof(int), 1, file);
// Inventory Data
fwrite(&eManager->nextInv, sizeof(s16), 1, file); // write amount of inventories.
for(i = 0; i < eManager->nextInv; ++i) {
saveInventory(&(eManager->invs[i]), eManager, file);
}
// Entity Data
for(i = 0; i < 5; ++i) { //for every level (except dungeon of course)
for(i = 0; i < 5; ++i){
int amount = calculateImportantEntites(eManager,i);
fwrite(&amount, sizeof(s16), 1, file); // read amount of entities in level.
for(j = 0; j < eManager->lastSlot[i]; ++j){
if(!entityIsImportant(&eManager->entities[i][j])) continue;
fwrite(&eManager->entities[i][j].type, sizeof(s16), 1, file); // write entity's type ID
fwrite(&eManager->entities[i][j].x, sizeof(s16), 1, file); // write entity's x coordinate
fwrite(&eManager->entities[i][j].y, sizeof(s16), 1, file); // write entity's y coordinate
@ -185,107 +104,67 @@ void saveWorldInternal(char *filename, EntityManager *eManager, WorldData *world
}
}
// Day/season Data
fwrite(&worldData->daytime, sizeof(u16), 1, file);
fwrite(&worldData->day, sizeof(int), 1, file);
fwrite(&worldData->season, sizeof(u8), 1, file);
fwrite(&worldData->rain, sizeof(bool), 1, file);
// Compass Data
fwrite(worldData->compassData, sizeof(u8), 6*3, file); //x,y of choosen stair and count per level
// Map Data
//Don't write or load dungeon, so only first 5 levels not 6
fwrite(worldData->map, sizeof(u8), 128*128*5, file); // Map Tile IDs, 128*128*5 bytes = 80KB
fwrite(worldData->data, sizeof(u8), 128*128*5, file); // Map Tile Data (Damage done to trees/rocks, age of wheat & saplings, etc). 80KB
fwrite(map, sizeof(u8), 128*128*5, file); // Map Tile IDs, 128*128*5 bytes = 80KB
fwrite(mapData, sizeof(u8), 128*128*5, file); // Map Tile Data (Damage done to trees/rocks, age of wheat & saplings, etc). 80KB
fclose(file);
}
fwrite(&daytime, sizeof(u16), 1, file);
void savePlayerInternal(char *filename, PlayerData *player, EntityManager *eManager) {
FILE * file = fopen(filename, "wb"); //TODO: should be checked
int i;
//write savefile version
int version = SAVE_VERSION;
fwrite(&version, sizeof(int), 1, file);
// basic player info
fwrite(&player->score, sizeof(int), 1, file);
fwrite(&player->isSpawned, sizeof(bool), 1, file);
fwrite(&player->entity.p.hasWonSaved, sizeof(bool), 1, file);
fwrite(&player->entity.p.health, sizeof(s16), 1, file);
fwrite(&player->entity.x, sizeof(s16), 1, file);
fwrite(&player->entity.y, sizeof(s16), 1, file);
fwrite(&player->entity.level, sizeof(s8), 1, file);
saveInventory(&(player->inventory), eManager, file);
// Sprite info
fwrite(&(player->sprite.choosen), sizeof(bool), 1, file);
fwrite(&(player->sprite.legs), sizeof(u8), 1, file);
fwrite(&(player->sprite.body), sizeof(u8), 1, file);
fwrite(&(player->sprite.arms), sizeof(u8), 1, file);
fwrite(&(player->sprite.head), sizeof(u8), 1, file);
fwrite(&(player->sprite.eyes), sizeof(u8), 1, file);
// Minimap Data
fwrite(player->minimapData, sizeof(u8), 128*128, file); // Minimap, visibility data 16KB
fwrite(minimapData, sizeof(u8), 128*128, file); // Minimap, visibility data 16KB
//Quest Data
fwrite(&(player->questManager.size), sizeof(int), 1, file);
for(i = 0; i < player->questManager.size; ++i) {
fwrite(&(player->questManager.questlines[i].currentQuest), sizeof(int), 1, file);
fwrite(&(player->questManager.questlines[i].currentQuestDone), sizeof(bool), 1, file);
fwrite(&questManager.size, sizeof(int), 1, file);
for(i = 0; i < questManager.size; ++i) {
fwrite(&(questManager.questlines[i].currentQuest), sizeof(int), 1, file);
fwrite(&(questManager.questlines[i].currentQuestDone), sizeof(bool), 1, file);
}
//Potion Data
fwrite(&UnderStrengthEffect, sizeof(bool), 1, file);
fwrite(&UnderSpeedEffect, sizeof(bool), 1, file);
fwrite(&regening, sizeof(bool), 1, file);
fwrite(&UnderSwimBreathEffect, sizeof(bool), 1, file);
fwrite(&player->entity.p.strengthTimer, sizeof(int), 1, file);
fwrite(&player->entity.p.speedTimer, sizeof(int), 1, file);
fwrite(&player->entity.p.swimBreathTimer, sizeof(int), 1, file);
fwrite(&player->entity.p.regenTimer, sizeof(int), 1, file);
//Compass Data
for(i=0; i<6; ++i) {
fwrite(&(compassData[i][0]), sizeof(u8), 1, file); //x of choosen stair
fwrite(&(compassData[i][1]), sizeof(u8), 1, file); //y of choosen stair
fwrite(&(compassData[i][2]), sizeof(u8), 1, file); //count
}
//Day/season Data
fwrite(&day, sizeof(int), 1, file);
fwrite(&season, sizeof(u8), 1, file);
fwrite(&rain, sizeof(bool), 1, file);
fclose(file);
}
//internal load methods
void loadInventory(Inventory *inv, EntityManager *eManager, FILE *file) {
fread(&(inv->lastSlot), sizeof(s16), 1, file); // read amount of items in inventory;
for(int j = 0; j < inv->lastSlot; ++j) {
fread(&(inv->items[j].id), sizeof(s16), 1, file); // write ID of item
fread(&(inv->items[j].countLevel), sizeof(s16), 1, file); // write count/level of item
fread(&(inv->items[j].onlyOne), sizeof(bool), 1, file);
inv->items[j].invPtr = (int*)inv; // setup Inventory pointer
inv->items[j].slotNum = j; // setup slot number
if(inv->items[j].id == ITEM_CHEST){ // for chest item specifically.
int invIndex;
fread(&invIndex, sizeof(int), 1, file);
inv->items[j].chestPtr = (Inventory*)&eManager->invs[invIndex]; // setup chest inventory pointer.
}
}
}
void loadWorldInternal(char *filename, EntityManager *eManager, WorldData *worldData) {
FILE * file = fopen(filename, "rb"); //TODO: should be checked
int loadWorld(char * filename, EntityManager * eManager, Entity * player, u8 * map, u8 * mapData){
FILE * file;
int i,j;
//read savefile version
int version;
fread(&version, sizeof(int), 1, file);
if ((file = fopen(filename, "rb"))){
fread(&player->p.score, sizeof(int), 1, file);
fread(&player->p.hasWonSaved, sizeof(bool), 1, file);
fread(&player->p.health, sizeof(s16), 1, file);
fread(&player->x, sizeof(s16), 1, file);
fread(&player->y, sizeof(s16), 1, file);
fread(&currentLevel, sizeof(s8), 1, file);
// Inventory Data
fread(&eManager->nextInv, sizeof(s16), 1, file);
for(i = 0; i < eManager->nextInv; ++i) {
loadInventory(&(eManager->invs[i]), eManager, file);
fread(&eManager->invs[i].lastSlot, sizeof(s16), 1, file); // read amount of items in inventory;
for(j = 0; j < eManager->invs[i].lastSlot; ++j) {
fread(&eManager->invs[i].items[j].id, sizeof(s16), 1, file); // write ID of item
fread(&eManager->invs[i].items[j].countLevel, sizeof(s16), 1, file); // write count/level of item
fread(&eManager->invs[i].items[j].onlyOne, sizeof(bool), 1, file);
eManager->invs[i].items[j].invPtr = (int*)&eManager->invs[i]; // setup Inventory pointer
eManager->invs[i].items[j].slotNum = j; // setup slot number
if(eManager->invs[i].items[j].id == ITEM_CHEST){ // for chest item specifically.
int invIndex;
fread(&invIndex, sizeof(int), 1, file);
eManager->invs[i].items[j].chestPtr = (Inventory*)&eManager->invs[invIndex]; // setup chest inventory pointer.
}
}
}
// Entity Data
for(i = 0; i < 5; ++i){
fread(&eManager->lastSlot[i], sizeof(s16), 1, file); // read amount of entities in level.
for(j = 0; j < eManager->lastSlot[i]; ++j){
@ -481,183 +360,52 @@ void loadWorldInternal(char *filename, EntityManager *eManager, WorldData *world
}
}
}
// Day/season Data
fread(&worldData->daytime, sizeof(u16), 1, file);
fread(&worldData->day, sizeof(int), 1, file);
fread(&worldData->season, sizeof(u8), 1, file);
fread(&worldData->rain, sizeof(bool), 1, file);
// Compass Data
fread(worldData->compassData, sizeof(u8), 6*3, file); //x,y of choosen stair and count per level
// Map Data
//Don't write or load dungeon, so only first 5 levels not 6
fread(worldData->map, sizeof(u8), 128*128*5, file); // Map Tile IDs, 128*128*5 bytes = 80KB
fread(worldData->data, sizeof(u8), 128*128*5, file); // Map Tile Data (Damage done to trees/rocks, age of wheat & saplings, etc). 80KB
fread(map, sizeof(u8), 128*128*5, file);
fread(mapData, sizeof(u8), 128*128*5, file);
fclose(file);
}
//set to startvalue incase file is old and doesn't contain saved time
daytime = 6001;
fread(&daytime, sizeof(u16), 1, file);
void loadPlayerInternal(char *filename, PlayerData *player, EntityManager *eManager) {
FILE * file = fopen(filename, "rb"); //TODO: should be checked
int i;
//read savefile version
int version;
fread(&version, sizeof(int), 1, file);
// basic player info
fread(&player->score, sizeof(int), 1, file);
fread(&player->isSpawned, sizeof(bool), 1, file);
fread(&player->entity.p.hasWonSaved, sizeof(bool), 1, file);
fread(&player->entity.p.health, sizeof(s16), 1, file);
fread(&player->entity.x, sizeof(s16), 1, file);
fread(&player->entity.y, sizeof(s16), 1, file);
fread(&player->entity.level, sizeof(s8), 1, file);
loadInventory(&(player->inventory), eManager, file);
// Sprite info
fread(&(player->sprite.choosen), sizeof(bool), 1, file);
fread(&(player->sprite.legs), sizeof(u8), 1, file);
fread(&(player->sprite.body), sizeof(u8), 1, file);
fread(&(player->sprite.arms), sizeof(u8), 1, file);
fread(&(player->sprite.head), sizeof(u8), 1, file);
fread(&(player->sprite.eyes), sizeof(u8), 1, file);
// Minimap Data
fread(player->minimapData, sizeof(u8), 128*128, file); // Minimap, visibility data 16KB
fread(minimapData, sizeof(u8), 128*128, file);
//Quest Data
fread(&(player->questManager.size), sizeof(int), 1, file);
for(i = 0; i < player->questManager.size; ++i) {
fread(&(player->questManager.questlines[i].currentQuest), sizeof(int), 1, file);
fread(&(player->questManager.questlines[i].currentQuestDone), sizeof(bool), 1, file);
int qsize = 0;
fread(&qsize, sizeof(int), 1, file);
for(i = 0; i < qsize; ++i) {
fread(&(questManager.questlines[i].currentQuest), sizeof(int), 1, file);
fread(&(questManager.questlines[i].currentQuestDone), sizeof(bool), 1, file);
}
//fill missing questlines with "no progress done"
for(i = qsize; i < questManager.size; ++i) {
questManager.questlines[i].currentQuest = 0;
questManager.questlines[i].currentQuestDone = false;
}
//Potion Data
fread(&UnderStrengthEffect, sizeof(bool), 1, file);
fread(&UnderSpeedEffect, sizeof(bool), 1, file);
fread(&regening, sizeof(bool), 1, file);
fread(&UnderSwimBreathEffect, sizeof(bool), 1, file);
fread(&player->entity.p.strengthTimer, sizeof(int), 1, file);
fread(&player->entity.p.speedTimer, sizeof(int), 1, file);
fread(&player->entity.p.swimBreathTimer, sizeof(int), 1, file);
fread(&player->entity.p.regenTimer, sizeof(int), 1, file);
//Compass Data
//reset incase it is missing in the save
for(i=0; i<6; ++i) {
compassData[i][0] = 0; //x of choosen stair
compassData[i][1] = 0; //y of choosen stair
compassData[i][2] = 0; //count
}
for(i=0; i<6; ++i) {
fread(&(compassData[i][0]), sizeof(u8), 1, file); //x of choosen stair
fread(&(compassData[i][1]), sizeof(u8), 1, file); //y of choosen stair
fread(&(compassData[i][2]), sizeof(u8), 1, file); //count
}
//Day/season Data
day = 0;
season = 0;
rain = false;
fread(&day, sizeof(int), 1, file);
fread(&season, sizeof(u8), 1, file);
fread(&rain, sizeof(bool), 1, file);
fclose(file);
}
bool saveWorld(char *filename, EntityManager *eManager, WorldData *worldData, PlayerData *players, int playerCount) {
//check if old save file exists
bool exists = false;
FILE *testFile = fopen(filename, "rb");
if(testFile) {
fclose(testFile);
exists = true;
}
saveTrackFileReset();
if(exists) {
//create backup copy
char *filenameBackup = malloc(sizeof(filename)+4+1);
if(filenameBackup==NULL) {
return false;
}
strcpy(filenameBackup, filename);
strcat(filenameBackup, ".bak");
if(!saveFileCopy(filenameBackup, filename)) {
return false;
}
//extract files and keep track of references
if(unzipAndLoad(filename, &saveTrackFile, SAVE_COMMENT, ZIPHELPER_KEEP_FILES)!=0) {
saveDeleteTrackedFiles();
return false;
}
remove(filename);
}
//save world data
saveWorldInternal("main.wld", eManager, worldData);
saveTrackFile("main.wld");
//save player data of active players
for(int i=0; i<playerCount; i++) {
char playerFilename[50];
playerFilename[0] = '\0';
sprintf(playerFilename, "%lu.plr", players[i].id);
savePlayerInternal(playerFilename, players+i, eManager);
saveTrackFile(playerFilename);
}
//zip all tracked files
if(zipFiles(filename, files, fileCount, ZIPHELPER_REPLACE, SAVE_COMMENT)!=0) {
remove(filename);
}
saveDeleteTrackedFiles();
return true;
}
bool loadHadWorld;
EntityManager *loadEManager;
WorldData *loadWorldData;
PlayerData *loadPlayers;
int loadPlayerCount;
int loadFile(char *filename) {
//load world
if(strcmp(filename, "main.wld")==0) {
loadWorldInternal(filename, loadEManager, loadWorldData);
loadHadWorld = true;
}
//load player data of active players
for(int i=0; i<playerCount; i++) {
char playerFilename[50];
playerFilename[0] = '\0';
sprintf(playerFilename, "%lu.plr", players[i].id);
if(strcmp(filename, playerFilename)==0) {
loadPlayerInternal(filename, loadPlayers+i, loadEManager);
}
}
return 0;
}
bool loadWorld(char *filename, EntityManager *eManager, WorldData *worldData, PlayerData *players, int playerCount) {
//check if save file exists
bool exists = false;
FILE *testFile = fopen(filename, "rb");
if(testFile) {
fclose(testFile);
exists = true;
}
if(!exists) return false;
loadHadWorld = false;
loadEManager = eManager;
loadWorldData = worldData;
loadPlayers = players;
loadPlayerCount = playerCount;
//extract files
if(unzipAndLoad(filename, &loadFile, SAVE_COMMENT, ZIPHELPER_CLEANUP_FILES)!=0) {
return false;
}
if(!loadHadWorld) {
return false;
}
return true;
return 1;
}

13
source/SaveLoad.h Executable file → Normal file
View file

@ -2,16 +2,7 @@
#include <stdio.h>
#include <string.h>
#include "Entity.h"
#include "Player.h"
#include "Globals.h"
#define SAVE_VERSION 1
#define SAVE_COMMENT "Minicraft3DSSave"
#define SAVE_COPYBUFFER_SIZE 4096
//void saveCurrentWorld(char * filename, EntityManager * eManager, Entity * player, u8 * map, u8 * mapData, WorldData *worldData);
//int loadWorld(char * filename, EntityManager * eManager, Entity * player, u8 * map, u8 * mapData, WorldData *worldData);
bool saveWorld(char *filename, EntityManager *eManager, WorldData *worldData, PlayerData *players, int playerCount);
bool loadWorld(char *filename, EntityManager *eManager, WorldData *worldData, PlayerData *players, int playerCount);
void saveCurrentWorld(char * filename, EntityManager * eManager, Entity * player, u8 * map, u8 * mapData);
int loadWorld(char * filename, EntityManager * eManager, Entity * player, u8 * map, u8 * mapData);

67
source/Sound.c Executable file → Normal file
View file

@ -1,16 +1,12 @@
#include "Sound.h"
u8 soundListenerLevel;
int soundListenerX;
int soundListenerY;
void loadSound(Sound * snd, char * filename){
FILE *file = fopen(filename, "rb");
if(file != NULL){
fseek(file, 0, SEEK_END);
snd->size = ftell(file)/2;
snd->size = ftell(file);
fseek(file, 0, SEEK_SET);
snd->buffer = linearAlloc(snd->size*sizeof(u16));
snd->buffer = linearAlloc(snd->size);
fread(snd->buffer, 1, snd->size, file);
}
fclose(file);
@ -20,27 +16,8 @@ void playSound(Sound snd){
csndPlaySound(8, SOUND_FORMAT_16BIT | SOUND_ONE_SHOT, 44100, 1, 0, snd.buffer, snd.buffer, snd.size);
}
void playSoundPositioned(Sound snd, s8 level, int x, int y) {
if(level != soundListenerLevel) return;
int xd = soundListenerX - x;
int yd = soundListenerY - y;
if (xd * xd + yd * yd > 80 * 80) return;
csndPlaySound(8, SOUND_FORMAT_16BIT | SOUND_ONE_SHOT, 44100, 1, 0, snd.buffer, snd.buffer, snd.size);
}
void setListenerPosition(s8 level, int x, int y) {
soundListenerLevel = level;
soundListenerX = x;
soundListenerY = y;
}
void playMusic(Sound *snd){
static Sound *lastSnd;
if(lastSnd==snd) return;
lastSnd = snd;
csndPlaySound(10, SOUND_FORMAT_16BIT | SOUND_REPEAT, 44100, 1, 0, snd->buffer, snd->buffer, snd->size);
void playMusic(Sound snd){
csndPlaySound(10, SOUND_FORMAT_16BIT | SOUND_REPEAT, 44100, 1, 0, snd.buffer, snd.buffer, snd.size);
}
void stopMusic() {
@ -52,38 +29,38 @@ void stopMusic() {
void updateMusic(int lvl, int time) {
switch(lvl) {
case 0:
playMusic(&music_floor0);
playMusic(music_floor0);
break;
case 1:
if(time>6000 && time<19000) playMusic(&music_floor1);
else playMusic(&music_floor1_night);
if(time>6000 && time<19000) playMusic(music_floor1);
else playMusic(music_floor1_night);
break;
case 2:
case 3:
playMusic(&music_floor23);
playMusic(music_floor23);
break;
case 4:
case 5: //TODO - dungeon needs own music
playMusic(&music_floor4);
playMusic(music_floor4);
break;
}
}
void loadSounds() {
loadSound(&snd_playerHurt, "romfs:/resources/playerhurt.raw");
loadSound(&snd_playerDeath, "romfs:/resources/death.raw");
loadSound(&snd_monsterHurt, "romfs:/resources/monsterhurt.raw");
loadSound(&snd_test, "romfs:/resources/test.raw");
loadSound(&snd_pickup, "romfs:/resources/pickup.raw");
loadSound(&snd_bossdeath, "romfs:/resources/bossdeath.raw");
loadSound(&snd_craft, "romfs:/resources/craft.raw");
loadSound(&snd_playerHurt, "resources/playerhurt.raw");
loadSound(&snd_playerDeath, "resources/playerdeath.raw");
loadSound(&snd_monsterHurt, "resources/monsterhurt.raw");
loadSound(&snd_test, "resources/test.raw");
loadSound(&snd_pickup, "resources/pickup.raw");
loadSound(&snd_bossdeath, "resources/bossdeath.raw");
loadSound(&snd_craft, "resources/craft.raw");
loadSound(&music_menu, "romfs:/resources/music/menu.raw");
loadSound(&music_floor0, "romfs:/resources/music/floor0.raw");
loadSound(&music_floor1, "romfs:/resources/music/floor1.raw");
loadSound(&music_floor1_night, "romfs:/resources/music/floor1_night.raw");
loadSound(&music_floor23, "romfs:/resources/music/floor2_3.raw");
loadSound(&music_floor4, "romfs:/resources/music/floor4.raw");
loadSound(&music_menu, "resources/music/menu.raw");
loadSound(&music_floor0, "resources/music/floor0.raw");
loadSound(&music_floor1, "resources/music/floor1.raw");
loadSound(&music_floor1_night, "resources/music/floor1_night.raw");
loadSound(&music_floor23, "resources/music/floor2_3.raw");
loadSound(&music_floor4, "resources/music/floor4.raw");
}
void freeSounds(){

5
source/Sound.h Executable file → Normal file
View file

@ -13,10 +13,7 @@ typedef struct {
void loadSound(Sound * snd, char * filename);
void playSound(Sound snd);
void playSoundPositioned(Sound snd, s8 level, int x, int y);
void setListenerPosition(s8 level, int x, int y);
void playMusic(Sound *snd);
void playMusic(Sound snd);
void updateMusic(int lvl, int time);
void stopMusic();

View file

@ -1,250 +0,0 @@
#include "Synchronizer.h"
#include "Globals.h"
#include "Player.h"
#include "Input.h"
#include "Network.h"
#include "PacketHandler.h"
#include "Ingame.h"
u32 synchronizerLocalTurn;
unsigned int synchronizerNextSeed;
bool synchronizerRunning;
int synchronizerCurrentTicks;
bool synchronizerTurnReady();
void synchronizerNextTurn();
void synchronizerSendLocalInputs();
int synchronizerGetTurnIndex(u32 turn);
void synchronizerInit(int seed, int initPlayerCount, int initPlayerLocalID) {
synchronizerLocalTurn = 0;
synchronizerNextSeed = seed;
playerCount = initPlayerCount;
playerLocalID = initPlayerLocalID;
syncTickCount = 0;
//reset player turn states (e.g. is turn data recieved), first turn needs to happen with no inputs
for(int i=0; i<playerCount; i++) {
resetKeys(&(players[i].inputs));
for(int j=0; j<MAX_INPUT_BUFFER; j++) {
resetKeys(&(players[i].nextInputs[j]));
}
players[i].nextTurnReady[0] = true;
players[i].idSet = false;
players[i].ready = false;
}
players[playerLocalID].id = localUID;
players[playerLocalID].idSet = true;
players[playerLocalID].ready = true;
//switch menu
currentMenu = MENU_LOADING;
synchronizerRunning = false;
}
void synchronizerSendUID() {
sendIDPacket(playerLocalID, localUID);
}
void synchronizerSetPlayerUID(int playerID, u32 uid) {
players[playerID].id = uid;
players[playerID].idSet = true;
}
void synchronizerSendIfReady() {
//we are ready when we recieved all uids
for(int i=0; i<playerCount; i++) {
if(!players[i].idSet) {
return;
}
}
//ready -> send to server
sendStartReadyPacket(playerLocalID);
}
void synchronizerSetPlayerReady(int playerID) {
players[playerID].ready = true;
}
bool synchronizerAllReady() {
for(int i=0; i<playerCount; i++) {
if(!players[i].ready) {
return false;
}
}
return true;
}
void synchronizerStart() {
//check if save file is present
bool doLoad = false;
char *loadName = NULL;
//host and single player need to load the actual file
if(playerLocalID==0) {
FILE *file = fopen(currentFileName, "rb");
if(file!=NULL) {
fclose(file);
doLoad = true;
loadName = currentFileName;
}
//all others the transfered one
} else {
FILE *file = fopen("tmpTransfer.bin", "rb");
if(file!=NULL) {
fclose(file);
doLoad = true;
loadName = "tmpTransfer.bin";
}
}
// reset random generators
srand(synchronizerNextSeed);
gaussrand(true);
//start the game
startGame(doLoad, loadName);
//remove transfered save file from clients
if(playerLocalID!=0) {
FILE *file = fopen("tmpTransfer.bin", "rb");
if(file!=NULL) {
fclose(file);
remove("tmpTransfer.bin");
}
}
//clear menu
currentMenu = MENU_NONE;
synchronizerRunning = true;
synchronizerCurrentTicks = SYNCHRONIZER_TICKS_PER_TURN;
}
void synchronizerTick(void (*gtick)(void)) {
if(synchronizerRunning && synchronizerTurnReady()) {
synchronizerNextTurn();
// reset random generators
srand(synchronizerNextSeed);
gaussrand(true);
syncTickCount++;
//call game tick
(*gtick)();
synchronizerNextSeed = rand();
}
}
//Test if all players (including single player) input is recieved
bool synchronizerTurnReady() {
if(synchronizerCurrentTicks<SYNCHRONIZER_TICKS_PER_TURN) return true;
for(int i=0; i<playerCount; i++) {
if(!players[i].nextTurnReady[synchronizerGetTurnIndex(synchronizerLocalTurn)]) {
return false;
}
}
return true;
}
void synchronizerNextTurn() {
if(synchronizerCurrentTicks<SYNCHRONIZER_TICKS_PER_TURN) {
synchronizerCurrentTicks++;
//clicked events are only fired for the first tick per turn
for(int i=0; i<playerCount; i++) {
resetClicked(&(players[i].inputs));
}
} else {
//move nextInput of every player to currentInput
for(int i=0; i<playerCount; i++) {
players[i].inputs = players[i].nextInputs[synchronizerGetTurnIndex(synchronizerLocalTurn)];
players[i].nextTurnReady[synchronizerGetTurnIndex(synchronizerLocalTurn)] = false;
}
//Increase turn number
synchronizerLocalTurn++;
synchronizerCurrentTicks = 1;
//send local input
synchronizerSendLocalInputs();
}
}
void synchronizerSendLocalInputs() {
//scan local inputs
hidScanInput();
tickKeys(&localInputs, hidKeysHeld(), hidKeysDown());
//store local input in nextInput
players[playerLocalID].nextInputs[synchronizerGetTurnIndex(synchronizerLocalTurn)] = localInputs;
players[playerLocalID].nextTurnReady[synchronizerGetTurnIndex(synchronizerLocalTurn)] = true;
//send local input
if(playerCount>1) {
size_t size = writeInputPacket(networkWriteBuffer, &localInputs, playerLocalID, synchronizerLocalTurn);
networkSend(networkWriteBuffer, size);
}
}
void synchronizerOnInputPacket(u8 playerID, u32 turnNumber, void *data, size_t dataSize) {
if(turnNumber>=synchronizerLocalTurn && turnNumber<synchronizerLocalTurn+MAX_INPUT_BUFFER && playerID<playerCount) {
if(readInputPacketData(data, dataSize, &(players[playerID].nextInputs[synchronizerGetTurnIndex(turnNumber)]))) {
players[playerID].nextTurnReady[synchronizerGetTurnIndex(turnNumber)] = true;
}
}
}
int synchronizerGetTurnIndex(u32 turn) {
return turn%MAX_INPUT_BUFFER;
}
void synchronizerReset() {
synchronizerRunning = false;
synchronizerCurrentTicks = 0;
}
bool synchronizerIsRunning() {
return synchronizerRunning;
}
// helpers for random numbers
#define PI 3.141592654
double gaussrand(bool reset)
{
static double U, V;
static int phase = 0;
double Z;
if(reset) {
U = 0;
V = 0;
phase = 0;
return 0;
}
if(phase == 0) {
U = (rand() + 1.) / (RAND_MAX + 2.);
V = rand() / (RAND_MAX + 1.);
Z = sqrt(-2 * log(U)) * sin(2 * PI * V);
} else {
Z = sqrt(-2 * log(U)) * cos(2 * PI * V);
}
phase = 1 - phase;
return Z;
}

View file

@ -1,29 +0,0 @@
#pragma once
#include <3ds.h>
//2-3 seem be optimal (at least for 2 players)
#define SYNCHRONIZER_TICKS_PER_TURN 2
void synchronizerInit(int seed, int initPlayerCount, int initPlayerLocalID);
void synchronizerSendUID();
void synchronizerSetPlayerUID(int playerID, u32 uid);
void synchronizerSendIfReady();
void synchronizerSetPlayerReady(int playerID);
bool synchronizerAllReady();
bool synchronizerIsRunning();
void synchronizerStart();
void synchronizerTick(void (*gtick)(void));
void synchronizerReset();
void synchronizerOnInputPacket(u8 playerID, u32 turnNumber, void *data, size_t dataSize);
// values used ingame
u32 syncTickCount;
// helpers for random numbers
double gaussrand(bool reset);

View file

@ -1,213 +0,0 @@
#include "ZipHelper.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "minizip/zip.h"
#include "minizip/unzip.h"
#define dir_delimter '/'
#define MAX_FILENAME 256
#define READ_SIZE 9216
int unzipAndLoad(char *filename, int (*fileCallback)(char *filename), char *expectedComment, int keepFiles) {
// Open the zip file
unzFile *zipfile = unzOpen(filename);
if (zipfile == NULL) {
return 1; // Error: ZipFile could not be opened.
}
// Get info about the zip file
unz_global_info global_info;
if (unzGetGlobalInfo(zipfile, &global_info ) != UNZ_OK) {
unzClose(zipfile);
return 2; // Error: Could not read global info
}
if(expectedComment!=NULL) {
char *buffer = malloc(global_info.size_comment+1);
if(buffer==NULL) {
unzClose(zipfile);
return 3; // Error: Could not read global comment
}
if (unzGetGlobalComment(zipfile, buffer, global_info.size_comment+1) < 0) {
unzClose(zipfile);
return 3; // Error: Could not read global comment
}
if (strcmp(expectedComment, buffer)!=0) {
unzClose(zipfile);
return 4; // Error: Global comment did not have expected value
}
free(buffer);
}
// Buffer to hold data read from the zip file.
void *read_buffer = malloc(READ_SIZE);
if(read_buffer==NULL) {
// Error: Could not allocate read buffer
return 5;
}
// Loop to extract all files
uLong i;
for (i = 0; i < global_info.number_entry; ++i) {
// Get info about current file.
unz_file_info file_info;
char filename[MAX_FILENAME];
if (unzGetCurrentFileInfo(zipfile, &file_info, filename, MAX_FILENAME, NULL, 0, NULL, 0 ) != UNZ_OK) {
free(read_buffer);
unzClose(zipfile);
return 6; // Error: Could not read file info
}
// Check if this entry is NOT a directory or file.
const size_t filename_length = strlen(filename);
if (filename[ filename_length-1 ] != dir_delimter){
if (unzOpenCurrentFile( zipfile ) != UNZ_OK) {
free(read_buffer);
unzClose(zipfile);
return 7;
}
// Open a file to write out the data.
FILE * out = fopen(filename, "wb");
if (out == NULL) {
free(read_buffer);
unzCloseCurrentFile(zipfile);
unzClose(zipfile);
return 8;
}
int error = UNZ_OK;
do {
error = unzReadCurrentFile(zipfile, read_buffer, READ_SIZE);
if ( error < 0 ) {
//printf("error %d\n", error);
free(read_buffer);
unzCloseCurrentFile(zipfile);
unzClose(zipfile);
fclose(out);
remove(filename);
return 9;
}
// Write data to file.
if (error > 0) {
fwrite(read_buffer, error, 1, out); // You should check return of fwrite...
}
} while (error > 0);
fclose(out);
//run callback
if((*fileCallback)(filename) != 0) {
free(read_buffer);
unzClose(zipfile);
remove(filename);
return 10; // Error: Callback error
}
if(keepFiles==ZIPHELPER_CLEANUP_FILES) {
remove(filename);
}
}
unzCloseCurrentFile(zipfile);
// Go the the next entry listed in the zip file.
if (( i+1 ) < global_info.number_entry) {
if (unzGoToNextFile( zipfile ) != UNZ_OK) {
free(read_buffer);
unzClose(zipfile);
return 11;
}
}
}
free(read_buffer);
unzClose(zipfile);
return 0;
}
int zipFiles(char *filename, char **files, int fileCount, int mode, char *comment) {
// Set mode
int zipMode = mode==ZIPHELPER_ADD ? APPEND_STATUS_ADDINZIP : APPEND_STATUS_CREATE;
FILE *testFile = fopen(filename, "r");
if(testFile!=NULL) {
fclose(testFile);
} else {
zipMode = APPEND_STATUS_CREATE;
}
// Open the zip file
zipFile *zipfile = zipOpen(filename, zipMode);
if (zipfile == NULL) return 1; // Error: ZipFile could not be opened.
// Buffer to hold data read from the files.
void *read_buffer = malloc(READ_SIZE);
if(read_buffer==NULL) {
// Error: Could not allocate read buffer
return 2;
}
// Loop all files to add
for(int i = 0; i < fileCount; i++) {
// Open a zipfile to write out the data.
if (zipOpenNewFileInZip(zipfile, files[i], NULL, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION) != ZIP_OK) {
free(read_buffer);
zipClose(zipfile, "");
return 3;
}
// Open a file to read the data.
FILE *in = fopen(files[i], "rb");
if (in == NULL) {
free(read_buffer);
zipCloseFileInZip(zipfile);
zipClose(zipfile, "");
return 4;
}
size_t size;
do
{
size = fread(read_buffer, 1, READ_SIZE, in);
if(size<READ_SIZE) {
if(!feof(in)) {
free(read_buffer);
zipCloseFileInZip(zipfile);
zipClose(zipfile, "");
fclose(in);
return 5;
}
}
if(size>0) {
//write data to zip
if (zipWriteInFileInZip(zipfile, read_buffer, size) != ZIP_OK) {
//printf("error %d\n", error);
free(read_buffer);
zipCloseFileInZip(zipfile);
zipClose(zipfile, "");
fclose(in);
return 6;
}
}
} while (size > 0);
fclose(in);
zipCloseFileInZip(zipfile);
}
free(read_buffer);
zipClose(zipfile, comment);
return 0;
}

View file

@ -1,10 +0,0 @@
#pragma once
#define ZIPHELPER_REPLACE 0
#define ZIPHELPER_ADD 1
#define ZIPHELPER_KEEP_FILES 0
#define ZIPHELPER_CLEANUP_FILES 1
int unzipAndLoad(char *filename, int (*fileCallback)(char *filename), char *expectedComment, int keepFiles);
int zipFiles(char *filename, char **files, int fileCount, int mode, char *comment);

394
source/main.c Executable file → Normal file
View file

@ -13,91 +13,193 @@
#include "Menu.h"
#include "texturepack.h"
#include "Network.h"
#include "SaveLoad.h"
#include "Ingame.h"
#include "Player.h"
#include "Synchronizer.h"
#include "PacketHandler.h"
// TODO: Dungeon is way to difficult
// -> Skeleton arrows are slower, do a little less damage
// -> Or instead of less damage, implement a simple armor system
//TODO: Something still causes desyncs very rarely
//TODO: Multiplayer should use normal drawing code -> so remove this first test again
float tmxscr = 400;
float tmyscr = 400;
float tmenuxa = 0.25;
float tmenuya = 0.25;
static aptHookCookie cookie;
void setupGame() {
synchronizerInit(rand(), 1, 0);
synchronizerSetPlayerUID(0, localUID);
synchronizerStart();
void initMiniMapData() {
int i;
for(i = 0; i < 128 * 128; ++i) {
minimapData[i] = 0;
}
}
void initMiniMap(bool loadUpWorld) {
int i;
for (i = 0; i < 5; ++i) {
initMinimapLevel(i, loadUpWorld);
}
}
void initNewMap() {
newSeed();
createAndValidateSkyMap(128, 128, 0, map[0], data[0]);
createAndValidateTopMap(128, 128, 1, map[1], data[1]);
createAndValidateUndergroundMap(128, 128, 1, 2, map[2], data[2]);
createAndValidateUndergroundMap(128, 128, 2, 3, map[3], data[3]);
createAndValidateUndergroundMap(128, 128, 3, 4, map[4], data[4]);
}
void setupGame(bool loadUpWorld, bool remote) {
currentLevel = 1;
// Reset entity manager.
memset(&eManager, 0, sizeof(eManager));
sf2d_set_clear_color(0xFF6C6D82); //sf2d_set_clear_color(RGBA8(0x82, 0x6D, 0x6C, 0xFF));
initMiniMapData();
if(!remote) {
if (!loadUpWorld) {
initNewMap();
initPlayer();
resetQuests();
airWizardHealthDisplay = 2000;
int i;
for (i = 0; i < 5; ++i) {
trySpawn(500, i);
}
addEntityToList(newAirWizardEntity(630, 820, 0), &eManager);
daytime = 6000;
day = 0;
season = 0;
rain = false;
} else {
initPlayer();
resetQuests();
loadWorld(currentFileName, &eManager, &player, (u8*) map, (u8*) data);
}
updateMusic(currentLevel, daytime);
initMiniMap(loadUpWorld);
} else {
//reset level data so no old data can somehow remain
memset(map, 0, 128*128*5 * sizeof(u8));
memset(data, 0, 128*128*5 * sizeof(u8));
currentLevel = 1;
//TODO: Can Packets get dropped - if yes, should resending be handled by network functions (so I dont need to do it everywhere)
networkPacket packet = {
.requestMapData = {
.type = PACKET_REQUEST_MAPDATA,
.level = 1
}
};
networkSend(&packet, sizeof(packetRequestMapData));
}
shouldRenderMap = false;
mScrollX = 0;
mScrollY = 0;
zoomLevel = 2;
sprintf(mapText,"x%d",zoomLevel);
initGame = 0;
}
void setupGameServer() {
size_t size;
networkHostStopConnections();
networkStart();
//send start info (seed)
size = writeStartPacket(networkWriteBuffer, rand());
networkSend(networkWriteBuffer, size);
processPacket(networkWriteBuffer, size);
networkSendWaitFlush();
//send save file if loading
FILE *file = fopen(currentFileName, "rb");
if(file!=NULL) {
sendFile(file, 0, 0);
networkSendWaitFlush();
fclose(file);
}
//send start command
size = writeStartRequestPacket(networkWriteBuffer);
networkSend(networkWriteBuffer, size);
processPacket(networkWriteBuffer, size);
initMPGame = 0;
}
void setupBGMap() {
void setupBGMap(bool loadUpWorld) {
// Reset entity manager.
memset(&eManager, 0, sizeof(eManager));
sf2d_set_clear_color(0xFF6C6D82);
sf2d_set_clear_color(0xFF6C6D82); //sf2d_set_clear_color(RGBA8(0x82, 0x6D, 0x6C, 0xFF));
srand(time(NULL));
createAndValidateTopMap(128, 128, 1, worldData.map[1], worldData.data[1]);
if(!loadUpWorld) {
newSeed();
createAndValidateTopMap(128, 128, 1, map[1], data[1]);
} else {
loadWorld(currentFileName, &eManager, &player, (u8*) map, (u8*) data);
}
// Reset entity manager.
memset(&eManager, 0, sizeof(eManager));
sf2d_set_clear_color(0xFF6C6D82);
sf2d_set_clear_color(0xFF6C6D82); //sf2d_set_clear_color(RGBA8(0x82, 0x6D, 0x6C, 0xFF));
initBGMap = 0;
}
static void task_apt_hook(APT_HookType hook, void* param) {
switch(hook) {
case APTHOOK_ONSUSPEND:
paused = true;
stopMusic();
break;
default:
break;
int xscr = 0, yscr = 0;
void tick() {
//TODO: Some stuff DOES need to happen even on client side
if(!isRemote) {
if (player.p.isDead) {
if (player.p.endTimer < 1) {
currentMenu = MENU_LOSE;
}
--player.p.endTimer;
return;
} else if (player.p.hasWon) {
if (player.p.endTimer < 1) {
currentMenu = MENU_WIN;
}
--player.p.endTimer;
return;
}
tickTouchMap();
tickTouchQuickSelect();
//for rendering -> move to a better place
extern int xscr, yscr;
++daytime;
//daytime += 20;
if(daytime>=24000) {
daytime -= 24000;
++day;
//TODO: maybe make season length not as hardcoded + make the transition better (fade to black and back maybe?)
if(day%7==0) {
++season;
if(season==4) season = 0;
}
rain = false;
if(season!=3 && rand()%5==0) rain = true;
}
if(daytime==6000 && currentLevel==1) {
playMusic(music_floor1);
} else if(daytime==19000 && currentLevel==1) {
playMusic(music_floor1_night);
}
int i;
for (i = 0; i < 324; ++i) {
int xx = rand() & 127;
int yy = rand() & 127;
tickTile(xx, yy);
}
tickPlayer();
xscr = player.x - 100;
yscr = player.y - 56;
if (xscr < 16)
xscr = 16;
else if (xscr > 1832)
xscr = 1832;
if (yscr < 16)
yscr = 16;
else if (yscr > 1912)
yscr = 1912;
if(eManager.lastSlot[currentLevel]<80 && currentLevel != 5) {
trySpawn(1, currentLevel);
}
for (i = 0; i < eManager.lastSlot[currentLevel]; ++i) {
Entity * e = &eManager.entities[currentLevel][i];
if ((e->type != ENTITY_ZOMBIE && e->type != ENTITY_SKELETON && e->type != ENTITY_KNIGHT && e->type != ENTITY_SLIME && e->type != ENTITY_PASSIVE)
|| (e->type == ENTITY_GLOWWORM && (daytime>6000 || daytime<18000))
|| (e->x > player.x - 160 && e->y > player.y - 125 && e->x < player.x + 160 && e->y < player.y + 125))
tickEntity(e);
}
}
}
char debugText[34];
char bossHealthText[34];
int main() {
romfsInit();
cfguInit();
CFGU_GetSystemModel(&MODEL_3DS);
FILE * file;
@ -112,36 +214,19 @@ int main() {
csndInit();
networkInit();
srand(time(NULL));
//load or create localUID
if ((file = fopen("m3ds_uid.bin", "rb"))) {
fread(&localUID, sizeof(u32), 1, file);
fclose(file);
} else {
localUID = (((u32) (rand()%256))<<24) | (((u32) (rand()%256))<<16) | (((u32) (rand()%256))<<8) | (((u32) (rand()%256)));
if ((file = fopen("m3ds_uid.bin", "wb"))) {
fwrite(&localUID, sizeof(u32), 1, file);
fclose(file);
}
}
noItem = newItem(ITEM_NULL, 0);
initMenus();
currentMenu = MENU_TITLE;
currentSelection = 0;
quitGame = false;
initBGMap = 1;
icons = sfil_load_PNG_buffer(icons_png, SF2D_PLACE_RAM);
playerSprites = sfil_load_PNG_buffer(player_png, SF2D_PLACE_RAM);
icons = sfil_load_PNG_buffer(icons2_png, SF2D_PLACE_RAM);
font = sfil_load_PNG_buffer(Font_png, SF2D_PLACE_RAM);
bottombg = sfil_load_PNG_buffer(bottombg_png, SF2D_PLACE_RAM);
loadSounds();
playMusic(&music_menu);
playMusic(music_menu);
bakeLights();
@ -159,37 +244,33 @@ int main() {
sf2d_set_clear_color(0xFF);
/* Default inputs */
localInputs.k_up.input = KEY_DUP | KEY_CPAD_UP | KEY_CSTICK_UP;
localInputs.k_down.input = KEY_DDOWN | KEY_CPAD_DOWN | KEY_CSTICK_DOWN;
localInputs.k_left.input = KEY_DLEFT | KEY_CPAD_LEFT | KEY_CSTICK_LEFT;
localInputs.k_right.input = KEY_DRIGHT | KEY_CPAD_RIGHT | KEY_CSTICK_RIGHT;
localInputs.k_attack.input = KEY_A;
localInputs.k_pickup.input = KEY_B;
localInputs.k_use.input = KEY_X;
localInputs.k_menu.input = KEY_Y ;
localInputs.k_pause.input = KEY_START;
localInputs.k_accept.input = KEY_A;
localInputs.k_decline.input = KEY_B;
localInputs.k_delete.input = KEY_X;
localInputs.k_menuNext.input = KEY_R | KEY_ZR;
localInputs.k_menuPrev.input = KEY_L | KEY_ZL;
k_up.input = KEY_DUP | KEY_CPAD_UP | KEY_CSTICK_UP;
k_down.input = KEY_DDOWN | KEY_CPAD_DOWN | KEY_CSTICK_DOWN;
k_left.input = KEY_DLEFT | KEY_CPAD_LEFT | KEY_CSTICK_LEFT;
k_right.input = KEY_DRIGHT | KEY_CPAD_RIGHT | KEY_CSTICK_RIGHT;
k_attack.input = KEY_A | KEY_B | KEY_L | KEY_ZR;
k_menu.input = KEY_X | KEY_Y | KEY_R | KEY_ZL;
k_pause.input = KEY_START;
k_accept.input = KEY_A;
k_decline.input = KEY_B;
k_delete.input = KEY_X;
k_menuNext.input = KEY_R;
k_menuPrev.input = KEY_L;
/* If btnSave exists, then use that. */
if ((file = fopen("btnSave.bin", "rb"))) {
fread(&(localInputs.k_up.input), sizeof(int), 1, file);
fread(&(localInputs.k_down.input), sizeof(int), 1, file);
fread(&(localInputs.k_left.input), sizeof(int), 1, file);
fread(&(localInputs.k_right.input), sizeof(int), 1, file);
fread(&(localInputs.k_attack.input), sizeof(int), 1, file);
fread(&(localInputs.k_pickup.input), sizeof(int), 1, file);
fread(&(localInputs.k_use.input), sizeof(int), 1, file);
fread(&(localInputs.k_menu.input), sizeof(int), 1, file);
fread(&(localInputs.k_pause.input), sizeof(int), 1, file);
fread(&(localInputs.k_accept.input), sizeof(int), 1, file);
fread(&(localInputs.k_decline.input), sizeof(int), 1, file);
fread(&(localInputs.k_delete.input), sizeof(int), 1, file);
fread(&(localInputs.k_menuNext.input), sizeof(int), 1, file);
fread(&(localInputs.k_menuPrev.input), sizeof(int), 1, file);
fread(&k_up.input, sizeof(int), 1, file);
fread(&k_down.input, sizeof(int), 1, file);
fread(&k_left.input, sizeof(int), 1, file);
fread(&k_right.input, sizeof(int), 1, file);
fread(&k_attack.input, sizeof(int), 1, file);
fread(&k_menu.input, sizeof(int), 1, file);
fread(&k_pause.input, sizeof(int), 1, file);
fread(&k_accept.input, sizeof(int), 1, file);
fread(&k_decline.input, sizeof(int), 1, file);
fread(&k_delete.input, sizeof(int), 1, file);
fread(&k_menuNext.input, sizeof(int), 1, file);
fread(&k_menuPrev.input, sizeof(int), 1, file);
fclose(file);
}
@ -201,26 +282,101 @@ int main() {
fclose(file);
}
initPlayers();
tickCount = 0;
initRecipes();
initTrades();
aptHook(&cookie, task_apt_hook, NULL);
initQuests();
while (aptMainLoop()) {
if (paused == true) playMusic(&music_menu);
++tickCount;
hidScanInput();
tickKeys(hidKeysHeld(), hidKeysDown());
if (quitGame) break;
if (initGame > 0 && --initGame==0) setupGame();
if (initMPGame > 0 && --initMPGame==0) setupGameServer();
if (initBGMap > 0 && --initBGMap==0) setupBGMap();
if (initGame > 0) setupGame(initGame == 1 ? true : false, isRemote);
if (initBGMap > 0) setupBGMap(initBGMap == 1 ? true : false);
if (currentMenu == MENU_NONE) {
tickGame();
renderGame();
networkRecieve();
if (currentMenu == 0) {
tick();
//TODO: Multiplayer should use normal drawing code -> so remove this first test again
if(!isRemote) {
sf2d_start_frame(GFX_TOP, GFX_LEFT);
offsetX = xscr;
offsetY = yscr;
sf2d_draw_rectangle(0, 0, 400, 240, 0xFF0C0C0C); //RGBA8(12, 12, 12, 255)); //You might think "real" black would be better, but it actually looks better that way
renderLightsToStencil(false, false, true);
renderBackground(xscr, yscr);
renderEntities(player.x, player.y, &eManager);
renderPlayer();
renderWeather(xscr, yscr);
resetStencilStuff();
renderDayNight();
offsetX = 0;
offsetY = 0;
if(shouldRenderDebug){
sprintf(fpsstr, "FPS: %.0f X:%d Y:%d E:%d %d", sf2d_get_fps(), player.x, player.y, eManager.lastSlot[currentLevel], player.p.speedTimer);
drawText(fpsstr, 2, 225);
}
sf2d_end_frame();
sf2d_start_frame(GFX_BOTTOM, GFX_LEFT);
if(!shouldRenderMap){
sf2d_draw_texture(bottombg, 0, 0);
renderGui();
} else {
//input scanning ingame is handled by the synchronizer
hidScanInput();
tickKeys(&localInputs, hidKeysHeld(), hidKeysDown());
renderZoomedMap();
}
sf2d_end_frame();
//TODO: Multiplayer should use normal drawing code -> so remove this first test again
} else {
//TODO: Temporary way of getting back to the menu
if (k_pause.clicked){
sf2d_set_clear_color(0xFF);
currentSelection = 0;
currentMenu = MENU_TITLE;
networkDisconnect();
playMusic(music_menu);
}
tmxscr += tmenuxa;
tmyscr += tmenuya;
if (tmxscr < 16) {
tmxscr = 16;
tmenuxa = -tmenuxa;
} else if (tmxscr > 1832) {
tmxscr = 1832;
tmenuxa = -tmenuxa;
}
if (tmyscr < 16) {
tmyscr = 16;
tmenuya = -tmenuya;
} else if (tmyscr > 1792) {
tmyscr = 1792;
tmenuya = -tmenuya;
}
sf2d_start_frame(GFX_TOP, GFX_LEFT);
offsetX = (int) tmxscr; offsetY = (int) tmyscr;
renderBackground((int) tmxscr, (int) tmyscr);
offsetX = 0; offsetY = 0;
sf2d_end_frame();
sf2d_start_frame(GFX_BOTTOM, GFX_LEFT);
sf2d_end_frame();
}
} else {
tickMenu(currentMenu);
renderMenu(currentMenu, xscr, yscr);
}
@ -230,9 +386,8 @@ int main() {
stopMusic();
freeTrades();
freeQuests();
freeRecipes();
freePlayers();
freeLightBakes();
sf2d_free_texture(icons);
@ -246,7 +401,6 @@ int main() {
networkExit();
csndExit();
cfguExit();
romfsExit();
sf2d_fini();
return 0;
}

4
source/minizip/crypt.h Executable file → Normal file
View file

@ -1,9 +1,9 @@
/* crypt.h -- base code for crypt/uncrypt ZIPfile
Version 1.01e, February 12th, 2005
Version 1.01h, December 28th, 2009
Copyright (C) 1998-2005 Gilles Vollant
Copyright (C) 1998-2009 Gilles Vollant
This code is a modified version of crypting code in Infozip distribution

7
source/minizip/ioapi.c Executable file → Normal file
View file

@ -1,9 +1,9 @@
/* ioapi.c -- IO base function header for compress/uncompress .zip
files using zlib + zip or unzip API
Version 1.01e, February 12th, 2005
Version 1.01h, December 28th, 2009
Copyright (C) 1998-2005 Gilles Vollant
Copyright (C) 1998-2009 Gilles Vollant
*/
#include <stdio.h>
@ -141,7 +141,8 @@ long ZCALLBACK fseek_file_func (opaque, stream, offset, origin)
default: return -1;
}
ret = 0;
fseek((FILE *)stream, offset, fseek_origin);
if (fseek((FILE *)stream, offset, fseek_origin) != 0)
ret = -1;
return ret;
}

4
source/minizip/ioapi.h Executable file → Normal file
View file

@ -1,9 +1,9 @@
/* ioapi.h -- IO base function header for compress/uncompress .zip
files using zlib + zip or unzip API
Version 1.01e, February 12th, 2005
Version 1.01h, December 28th, 2009
Copyright (C) 1998-2005 Gilles Vollant
Copyright (C) 1998-2009 Gilles Vollant
*/
#ifndef _ZLIBIOAPI_H

118
source/minizip/unzip.c Executable file → Normal file
View file

@ -1,7 +1,7 @@
/* unzip.c -- IO for uncompress .zip files using zlib
Version 1.01e, February 12th, 2005
Version 1.01h, December 28th, 2009
Copyright (C) 1998-2005 Gilles Vollant
Copyright (C) 1998-2009 Gilles Vollant
Read unzip.h for more info
*/
@ -103,6 +103,9 @@ typedef struct
{
char *read_buffer; /* internal buffer for compressed data */
z_stream stream; /* zLib stream structure for inflate */
#ifdef HAVE_BZIP2
bz_stream bstream; /* bzLib stream structure for bziped */
#endif
uLong pos_in_zipfile; /* position in byte on the zipfile, for fseek*/
uLong stream_initialised; /* flag set if stream structure is initialised*/
@ -204,7 +207,7 @@ local int unzlocal_getShort (pzlib_filefunc_def,filestream,pX)
uLong *pX;
{
uLong x ;
int i;
int i = 0;
int err;
err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i);
@ -232,7 +235,7 @@ local int unzlocal_getLong (pzlib_filefunc_def,filestream,pX)
uLong *pX;
{
uLong x ;
int i;
int i = 0;
int err;
err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i);
@ -491,8 +494,11 @@ extern unzFile ZEXPORT unzOpen2 (path, pzlib_filefunc_def)
s=(unz_s*)ALLOC(sizeof(unz_s));
if (s!=NULL)
{
*s=us;
unzGoToFirstFile((unzFile)s);
}
return (unzFile)s;
}
@ -608,10 +614,12 @@ local int unzlocal_GetCurrentFileInfoInternal (file,
/* we check the magic */
if (err==UNZ_OK)
{
if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uMagic) != UNZ_OK)
err=UNZ_ERRNO;
else if (uMagic!=0x02014b50)
err=UNZ_BADZIPFILE;
}
if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.version) != UNZ_OK)
err=UNZ_ERRNO;
@ -688,10 +696,13 @@ local int unzlocal_GetCurrentFileInfoInternal (file,
uSizeRead = extraFieldBufferSize;
if (lSeek!=0)
{
if (ZSEEK(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0)
lSeek=0;
else
err=UNZ_ERRNO;
}
if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0))
if (ZREAD(s->z_filefunc, s->filestream,extraField,uSizeRead)!=uSizeRead)
err=UNZ_ERRNO;
@ -713,10 +724,13 @@ local int unzlocal_GetCurrentFileInfoInternal (file,
uSizeRead = commentBufferSize;
if (lSeek!=0)
{
if (ZSEEK(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0)
lSeek=0;
else
err=UNZ_ERRNO;
}
if ((file_info.size_file_comment>0) && (commentBufferSize>0))
if (ZREAD(s->z_filefunc, s->filestream,szComment,uSizeRead)!=uSizeRead)
err=UNZ_ERRNO;
@ -977,10 +991,12 @@ local int unzlocal_CheckCurrentFileCoherencyHeader (s,piSizeVar,
if (err==UNZ_OK)
{
if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uMagic) != UNZ_OK)
err=UNZ_ERRNO;
else if (uMagic!=0x04034b50)
err=UNZ_BADZIPFILE;
}
if (unzlocal_getShort(&s->z_filefunc, s->filestream,&uData) != UNZ_OK)
err=UNZ_ERRNO;
@ -997,6 +1013,9 @@ local int unzlocal_CheckCurrentFileCoherencyHeader (s,piSizeVar,
err=UNZ_BADZIPFILE;
if ((err==UNZ_OK) && (s->cur_file_info.compression_method!=0) &&
/* #ifdef HAVE_BZIP2 */
(s->cur_file_info.compression_method!=Z_BZIP2ED) &&
/* #endif */
(s->cur_file_info.compression_method!=Z_DEFLATED))
err=UNZ_BADZIPFILE;
@ -1111,6 +1130,9 @@ extern int ZEXPORT unzOpenCurrentFile3 (file, method, level, raw, password)
}
if ((s->cur_file_info.compression_method!=0) &&
/* #ifdef HAVE_BZIP2 */
(s->cur_file_info.compression_method!=Z_BZIP2ED) &&
/* #endif */
(s->cur_file_info.compression_method!=Z_DEFLATED))
err=UNZ_BADZIPFILE;
@ -1124,6 +1146,34 @@ extern int ZEXPORT unzOpenCurrentFile3 (file, method, level, raw, password)
pfile_in_zip_read_info->stream.total_out = 0;
if ((s->cur_file_info.compression_method==Z_BZIP2ED) &&
(!raw))
{
#ifdef HAVE_BZIP2
pfile_in_zip_read_info->bstream.bzalloc = (void *(*) (void *, int, int))0;
pfile_in_zip_read_info->bstream.bzfree = (free_func)0;
pfile_in_zip_read_info->bstream.opaque = (voidpf)0;
pfile_in_zip_read_info->bstream.state = (voidpf)0;
pfile_in_zip_read_info->stream.zalloc = (alloc_func)0;
pfile_in_zip_read_info->stream.zfree = (free_func)0;
pfile_in_zip_read_info->stream.opaque = (voidpf)0;
pfile_in_zip_read_info->stream.next_in = (voidpf)0;
pfile_in_zip_read_info->stream.avail_in = 0;
err=BZ2_bzDecompressInit(&pfile_in_zip_read_info->bstream, 0, 0);
if (err == Z_OK)
pfile_in_zip_read_info->stream_initialised=Z_BZIP2ED;
else
{
TRYFREE(pfile_in_zip_read_info);
return err;
}
#else
pfile_in_zip_read_info->raw=1;
#endif
}
else
if ((s->cur_file_info.compression_method==Z_DEFLATED) &&
(!raw))
{
@ -1135,7 +1185,7 @@ extern int ZEXPORT unzOpenCurrentFile3 (file, method, level, raw, password)
err=inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS);
if (err == Z_OK)
pfile_in_zip_read_info->stream_initialised=1;
pfile_in_zip_read_info->stream_initialised=Z_DEFLATED;
else
{
TRYFREE(pfile_in_zip_read_info);
@ -1163,6 +1213,8 @@ extern int ZEXPORT unzOpenCurrentFile3 (file, method, level, raw, password)
s->pfile_in_zip_read = pfile_in_zip_read_info;
s->encrypted = 0;
# ifndef NOUNCRYPT
if (password != NULL)
{
@ -1334,6 +1386,53 @@ extern int ZEXPORT unzReadCurrentFile (file, buf, len)
iRead += uDoCopy;
}
else
if (pfile_in_zip_read_info->compression_method==Z_BZIP2ED)
{
#ifdef HAVE_BZIP2
uLong uTotalOutBefore,uTotalOutAfter;
const Bytef *bufBefore;
uLong uOutThis;
pfile_in_zip_read_info->bstream.next_in = pfile_in_zip_read_info->stream.next_in;
pfile_in_zip_read_info->bstream.avail_in = pfile_in_zip_read_info->stream.avail_in;
pfile_in_zip_read_info->bstream.total_in_lo32 = pfile_in_zip_read_info->stream.total_in;
pfile_in_zip_read_info->bstream.total_in_hi32 = 0;
pfile_in_zip_read_info->bstream.next_out = pfile_in_zip_read_info->stream.next_out;
pfile_in_zip_read_info->bstream.avail_out = pfile_in_zip_read_info->stream.avail_out;
pfile_in_zip_read_info->bstream.total_out_lo32 = pfile_in_zip_read_info->stream.total_out;
pfile_in_zip_read_info->bstream.total_out_hi32 = 0;
uTotalOutBefore = pfile_in_zip_read_info->bstream.total_out_lo32;
bufBefore = pfile_in_zip_read_info->bstream.next_out;
err=BZ2_bzDecompress(&pfile_in_zip_read_info->bstream);
uTotalOutAfter = pfile_in_zip_read_info->bstream.total_out_lo32;
uOutThis = uTotalOutAfter-uTotalOutBefore;
pfile_in_zip_read_info->crc32 =
crc32(pfile_in_zip_read_info->crc32,bufBefore,
(uInt)(uOutThis));
pfile_in_zip_read_info->rest_read_uncompressed -=
uOutThis;
iRead += (uInt)(uTotalOutAfter - uTotalOutBefore);
pfile_in_zip_read_info->stream.next_in = pfile_in_zip_read_info->bstream.next_in;
pfile_in_zip_read_info->stream.avail_in = pfile_in_zip_read_info->bstream.avail_in;
pfile_in_zip_read_info->stream.total_in = pfile_in_zip_read_info->bstream.total_in_lo32;
pfile_in_zip_read_info->stream.next_out = pfile_in_zip_read_info->bstream.next_out;
pfile_in_zip_read_info->stream.avail_out = pfile_in_zip_read_info->bstream.avail_out;
pfile_in_zip_read_info->stream.total_out = pfile_in_zip_read_info->bstream.total_out_lo32;
if (err==BZ_STREAM_END)
return (iRead==0) ? UNZ_EOF : iRead;
if (err!=BZ_OK)
break;
#endif
}
else
{
uLong uTotalOutBefore,uTotalOutAfter;
const Bytef *bufBefore;
@ -1512,8 +1611,12 @@ extern int ZEXPORT unzCloseCurrentFile (file)
TRYFREE(pfile_in_zip_read_info->read_buffer);
pfile_in_zip_read_info->read_buffer = NULL;
if (pfile_in_zip_read_info->stream_initialised)
if (pfile_in_zip_read_info->stream_initialised == Z_DEFLATED)
inflateEnd(&pfile_in_zip_read_info->stream);
#ifdef HAVE_BZIP2
else if (pfile_in_zip_read_info->stream_initialised == Z_BZIP2ED)
BZ2_bzDecompressEnd(&pfile_in_zip_read_info->bstream);
#endif
pfile_in_zip_read_info->stream_initialised = 0;
TRYFREE(pfile_in_zip_read_info);
@ -1534,7 +1637,6 @@ extern int ZEXPORT unzGetGlobalComment (file, szComment, uSizeBuf)
char *szComment;
uLong uSizeBuf;
{
int err=UNZ_OK;
unz_s* s;
uLong uReadThis ;
if (file==NULL)
@ -1567,7 +1669,7 @@ extern uLong ZEXPORT unzGetOffset (file)
unz_s* s;
if (file==NULL)
return UNZ_PARAMERROR;
return 0;
s=(unz_s*)file;
if (!s->current_file_ok)
return 0;

10
source/minizip/unzip.h Executable file → Normal file
View file

@ -1,7 +1,7 @@
/* unzip.h -- IO for uncompress .zip files using zlib
Version 1.01e, February 12th, 2005
Version 1.01h, December 28th, 2009
Copyright (C) 1998-2005 Gilles Vollant
Copyright (C) 1998-2009 Gilles Vollant
This unzip package allow extract file from .ZIP file, compatible with PKZip 2.04g
WinZip, InfoZip tools and compatible.
@ -57,6 +57,12 @@ extern "C" {
#include "ioapi.h"
#endif
#ifdef HAVE_BZIP2
#include "bzlib.h"
#endif
#define Z_BZIP2ED 12
#if defined(STRICTUNZIP) || defined(STRICTZIPUNZIP)
/* like the STRICT of WIN32, we define a pointer that cannot be converted
from (void*) without cast */

84
source/minizip/zip.c Executable file → Normal file
View file

@ -1,10 +1,10 @@
/* zip.c -- IO on .zip files using zlib
Version 1.01e, February 12th, 2005
Version 1.01h, December 28th, 2009
27 Dec 2004 Rolf Kalbermatter
Modification to zipOpen2 to support globalComment retrieval.
Copyright (C) 1998-2005 Gilles Vollant
Copyright (C) 1998-2009 Gilles Vollant
Read zip.h for more info
*/
@ -320,9 +320,9 @@ local uLong ziplocal_TmzDateToDosDate(ptm,dosDate)
uLong dosDate;
{
uLong year = (uLong)ptm->tm_year;
if (year>1980)
if (year>=1980)
year-=1980;
else if (year>80)
else if (year>=80)
year-=80;
return
(uLong) (((ptm->tm_mday) + (32 * (ptm->tm_mon+1)) + (512 * year)) << 16) |
@ -373,7 +373,7 @@ local int ziplocal_getShort (pzlib_filefunc_def,filestream,pX)
uLong *pX;
{
uLong x ;
int i;
int i = 0;
int err;
err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
@ -401,7 +401,7 @@ local int ziplocal_getLong (pzlib_filefunc_def,filestream,pX)
uLong *pX;
{
uLong x ;
int i;
int i = 0;
int err;
err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
@ -432,6 +432,7 @@ local int ziplocal_getLong (pzlib_filefunc_def,filestream,pX)
/*
Locate the Central directory of a zipfile (at the end, just before
the global comment)
Fix from Riccardo Cohen
*/
local uLong ziplocal_SearchCentralDir OF((
const zlib_filefunc_def* pzlib_filefunc_def,
@ -493,6 +494,7 @@ local uLong ziplocal_SearchCentralDir(pzlib_filefunc_def,filestream)
TRYFREE(buf);
return uPosFound;
}
#endif /* !NO_ADDFILEINEXISTINGZIP*/
/************************************************************/
@ -521,6 +523,8 @@ extern zipFile ZEXPORT zipOpen2 (pathname, append, globalcomment, pzlib_filefunc
if (ziinit.filestream == NULL)
return NULL;
if (append == APPEND_STATUS_CREATEAFTER)
ZSEEK(ziinit.z_filefunc,ziinit.filestream,0,SEEK_END);
ziinit.begin_pos = ZTELL(ziinit.z_filefunc,ziinit.filestream);
ziinit.in_opened_file_inzip = 0;
ziinit.ci.stream_initialised = 0;
@ -558,9 +562,10 @@ extern zipFile ZEXPORT zipOpen2 (pathname, append, globalcomment, pzlib_filefunc
uLong size_comment;
central_pos = ziplocal_SearchCentralDir(&ziinit.z_filefunc,ziinit.filestream);
/* disable to allow appending to empty ZIP archive
if (central_pos==0)
err=ZIP_ERRNO;
*/
if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
err=ZIP_ERRNO;
@ -615,7 +620,7 @@ extern zipFile ZEXPORT zipOpen2 (pathname, append, globalcomment, pzlib_filefunc
if (size_comment>0)
{
ziinit.globalcomment = ALLOC(size_comment+1);
ziinit.globalcomment = (char*)ALLOC(size_comment+1);
if (ziinit.globalcomment)
{
size_comment = ZREAD(ziinit.z_filefunc, ziinit.filestream,ziinit.globalcomment,size_comment);
@ -687,12 +692,12 @@ extern zipFile ZEXPORT zipOpen (pathname, append)
return zipOpen2(pathname,append,NULL,NULL);
}
extern int ZEXPORT zipOpenNewFileInZip3 (file, filename, zipfi,
extern int ZEXPORT zipOpenNewFileInZip4 (file, filename, zipfi,
extrafield_local, size_extrafield_local,
extrafield_global, size_extrafield_global,
comment, method, level, raw,
windowBits, memLevel, strategy,
password, crcForCrypting)
password, crcForCrypting, versionMadeBy, flagBase)
zipFile file;
const char* filename;
const zip_fileinfo* zipfi;
@ -709,6 +714,8 @@ extern int ZEXPORT zipOpenNewFileInZip3 (file, filename, zipfi,
int strategy;
const char* password;
uLong crcForCrypting;
uLong versionMadeBy;
uLong flagBase;
{
zip_internal* zi;
uInt size_filename;
@ -755,7 +762,7 @@ extern int ZEXPORT zipOpenNewFileInZip3 (file, filename, zipfi,
else zi->ci.dosDate = ziplocal_TmzDateToDosDate(&zipfi->tmz_date,zipfi->dosDate);
}
zi->ci.flag = 0;
zi->ci.flag = flagBase;
if ((level==8) || (level==9))
zi->ci.flag |= 2;
if ((level==2))
@ -778,7 +785,7 @@ extern int ZEXPORT zipOpenNewFileInZip3 (file, filename, zipfi,
ziplocal_putValue_inmemory(zi->ci.central_header,(uLong)CENTRALHEADERMAGIC,4);
/* version info */
ziplocal_putValue_inmemory(zi->ci.central_header+4,(uLong)VERSIONMADEBY,2);
ziplocal_putValue_inmemory(zi->ci.central_header+4,(uLong)versionMadeBy,2);
ziplocal_putValue_inmemory(zi->ci.central_header+6,(uLong)20,2);
ziplocal_putValue_inmemory(zi->ci.central_header+8,(uLong)zi->ci.flag,2);
ziplocal_putValue_inmemory(zi->ci.central_header+10,(uLong)zi->ci.method,2);
@ -857,6 +864,7 @@ extern int ZEXPORT zipOpenNewFileInZip3 (file, filename, zipfi,
zi->ci.stream.next_out = zi->ci.buffered_data;
zi->ci.stream.total_in = 0;
zi->ci.stream.total_out = 0;
zi->ci.stream.data_type = Z_BINARY;
if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
{
@ -912,14 +920,46 @@ extern int ZEXPORT zipOpenNewFileInZip2(file, filename, zipfi,
int level;
int raw;
{
return zipOpenNewFileInZip3 (file, filename, zipfi,
return zipOpenNewFileInZip4 (file, filename, zipfi,
extrafield_local, size_extrafield_local,
extrafield_global, size_extrafield_global,
comment, method, level, raw,
-MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
NULL, 0);
NULL, 0, VERSIONMADEBY, 0);
}
extern int ZEXPORT zipOpenNewFileInZip3 (file, filename, zipfi,
extrafield_local, size_extrafield_local,
extrafield_global, size_extrafield_global,
comment, method, level, raw,
windowBits, memLevel, strategy,
password, crcForCrypting)
zipFile file;
const char* filename;
const zip_fileinfo* zipfi;
const void* extrafield_local;
uInt size_extrafield_local;
const void* extrafield_global;
uInt size_extrafield_global;
const char* comment;
int method;
int level;
int raw;
int windowBits;
int memLevel;
int strategy;
const char* password;
uLong crcForCrypting;
{
return zipOpenNewFileInZip4 (file, filename, zipfi,
extrafield_local, size_extrafield_local,
extrafield_global, size_extrafield_global,
comment, method, level, raw,
windowBits, memLevel, strategy,
password, crcForCrypting, VERSIONMADEBY, 0);
}
extern int ZEXPORT zipOpenNewFileInZip (file, filename, zipfi,
extrafield_local, size_extrafield_local,
extrafield_global, size_extrafield_global,
@ -935,10 +975,12 @@ extern int ZEXPORT zipOpenNewFileInZip (file, filename, zipfi,
int method;
int level;
{
return zipOpenNewFileInZip2 (file, filename, zipfi,
return zipOpenNewFileInZip4 (file, filename, zipfi,
extrafield_local, size_extrafield_local,
extrafield_global, size_extrafield_global,
comment, method, level, 0);
comment, method, level, 0,
-MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
NULL, 0, VERSIONMADEBY, 0);
}
local int zipFlushWriteBuffer(zi)
@ -978,9 +1020,9 @@ extern int ZEXPORT zipWriteInFileInZip (file, buf, len)
if (zi->in_opened_file_inzip == 0)
return ZIP_PARAMERROR;
zi->ci.stream.next_in = (void*)buf;
zi->ci.stream.next_in = (Bytef*)buf;
zi->ci.stream.avail_in = len;
zi->ci.crc32 = crc32(zi->ci.crc32,buf,len);
zi->ci.crc32 = crc32(zi->ci.crc32,buf,(uInt)len);
while ((err==ZIP_OK) && (zi->ci.stream.avail_in>0))
{
@ -1070,7 +1112,9 @@ extern int ZEXPORT zipCloseFileInZipRaw (file, uncompressed_size, crc32)
if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
{
err=deflateEnd(&zi->ci.stream);
int tmp_err=deflateEnd(&zi->ci.stream);
if (err == ZIP_OK)
err = tmp_err;
zi->ci.stream_initialised = 0;
}
@ -1173,7 +1217,7 @@ extern int ZEXPORT zipClose (file, global_comment)
ldi = ldi->next_datablock;
}
}
free_datablock(zi->central_dir.first_block);
free_linkedlist(&(zi->central_dir));
if (err==ZIP_OK) /* Magic End */
err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)ENDHEADERMAGIC,4);

30
source/minizip/zip.h Executable file → Normal file
View file

@ -1,7 +1,7 @@
/* zip.h -- IO for compress .zip files using zlib
Version 1.01e, February 12th, 2005
Version 1.01h, December 28th, 2009
Copyright (C) 1998-2005 Gilles Vollant
Copyright (C) 1998-2009 Gilles Vollant
This unzip package allow creates .ZIP file, compatible with PKZip 2.04g
WinZip, InfoZip tools and compatible.
@ -191,8 +191,7 @@ extern int ZEXPORT zipOpenNewFileInZip3 OF((zipFile file,
int memLevel,
int strategy,
const char* password,
uLong crcForCtypting));
uLong crcForCrypting));
/*
Same than zipOpenNewFileInZip2, except
windowBits,memLevel,,strategy : see parameter strategy in deflateInit2
@ -200,6 +199,29 @@ extern int ZEXPORT zipOpenNewFileInZip3 OF((zipFile file,
crcForCtypting : crc of file to compress (needed for crypting)
*/
extern int ZEXPORT zipOpenNewFileInZip4 OF((zipFile file,
const char* filename,
const zip_fileinfo* zipfi,
const void* extrafield_local,
uInt size_extrafield_local,
const void* extrafield_global,
uInt size_extrafield_global,
const char* comment,
int method,
int level,
int raw,
int windowBits,
int memLevel,
int strategy,
const char* password,
uLong crcForCrypting,
uLong versionMadeBy,
uLong flagBase));
/*
Same than zipOpenNewFileInZip4, except
versionMadeBy : value for Version made by field
flag : value for flag field (compression level info will be added)
*/
extern int ZEXPORT zipWriteInFileInZip OF((zipFile file,
const void* buf,

142
source/texturepack.c Executable file → Normal file
View file

@ -1,13 +1,9 @@
#include "texturepack.h"
#include "ZipHelper.h"
#define dir_delimter '/'
#define MAX_FILENAME 256
#define READ_SIZE 9216
bool texturepackUseDefaultIcons = true;
bool texturepackUseDefaultPlayer = true;
bool texturepackUseDefaultFont = true;
bool texturepackUseDefaultBottom = true;
void toLowerString(char * str){
int i;
@ -35,66 +31,136 @@ int getTexturePackComment(char * filename, char * cmmtBuf){
return 0;
}
int loadTexture(char * filename) {
int loadTexturePack(char * filename){
bool useDefaultIcons = true;
bool useDefaultFont = true;
bool useDefaultBottom = true;
// Open the zip file
unzFile *zipfile = unzOpen(filename);
if ( zipfile == NULL ) return 1; // Error: ZipFile could not be opened.
// Get info about the zip file
unz_global_info global_info;
if (unzGetGlobalInfo(zipfile, &global_info ) != UNZ_OK )
{
unzClose( zipfile );
return 2; // Error: Could not read global info
}
// Buffer to hold data read from the zip file.
char read_buffer[ READ_SIZE ];
// Loop to extract all files
uLong i;
for ( i = 0; i < global_info.number_entry; ++i )
{
// Get info about current file.
unz_file_info file_info;
char filename[ MAX_FILENAME ];
if (unzGetCurrentFileInfo(zipfile,&file_info,filename,MAX_FILENAME,NULL, 0, NULL, 0 ) != UNZ_OK ){
unzClose( zipfile );
return 3; // Error: Could not read file info
}
// Check if this entry is NOT a directory or file.
const size_t filename_length = strlen( filename );
if ( filename[ filename_length-1 ] != dir_delimter ){
if ( unzOpenCurrentFile( zipfile ) != UNZ_OK )
{
unzClose( zipfile );
return 4;
}
// Open a file to write out the data.
FILE * out = fopen(filename, "wb" );
if ( out == NULL )
{
unzCloseCurrentFile( zipfile );
unzClose( zipfile );
return 5;
}
int error = UNZ_OK;
do
{
error = unzReadCurrentFile( zipfile, read_buffer, READ_SIZE );
if ( error < 0 )
{
//printf( "error %d\n", error );
unzCloseCurrentFile( zipfile );
unzClose( zipfile );
return 6;
}
// Write data to file.
if ( error > 0 )
{
fwrite( read_buffer, error, 1, out ); // You should check return of fwrite...
}
} while ( error > 0 );
fclose(out);
char lowerFilename[MAX_FILENAME];
strcpy(lowerFilename,filename);
toLowerString(lowerFilename);
if(strcmp(lowerFilename,"icons.png") == 0){
if(sfil_load_PNG_file(filename, SF2D_PLACE_RAM) == NULL){
return 0;
unzCloseCurrentFile( zipfile );
unzClose( zipfile );
return 7;
}
icons = sfil_load_PNG_file(filename, SF2D_PLACE_RAM);
reloadColors();
texturepackUseDefaultIcons = false;
} else if(strcmp(lowerFilename, "player.png") == 0){
if(sfil_load_PNG_file(filename, SF2D_PLACE_RAM) == NULL){
return 0;
}
useDefaultIcons = false;
playerSprites = sfil_load_PNG_file(filename, SF2D_PLACE_RAM);
texturepackUseDefaultPlayer = false;
} else if(strcmp(lowerFilename,"font.png") == 0){
if(sfil_load_PNG_file(filename, SF2D_PLACE_RAM) == NULL){
return 0;
unzCloseCurrentFile( zipfile );
unzClose( zipfile );
return 7;
}
font = sfil_load_PNG_file(filename, SF2D_PLACE_RAM);
texturepackUseDefaultFont = false;
useDefaultFont = false;
} else if(strcmp(lowerFilename,"bottombg.png") == 0){
if(sfil_load_PNG_file(filename, SF2D_PLACE_RAM) == NULL){
return 0;
unzCloseCurrentFile( zipfile );
unzClose( zipfile );
return 7;
}
bottombg = sfil_load_PNG_file(filename, SF2D_PLACE_RAM);
texturepackUseDefaultBottom = false;
useDefaultBottom = false;
}
return 0;
remove(filename);
}
int loadTexturePack(char * filename) {
texturepackUseDefaultIcons = true;
texturepackUseDefaultPlayer = true;
texturepackUseDefaultFont = true;
texturepackUseDefaultBottom = true;
unzCloseCurrentFile( zipfile );
if(unzipAndLoad(filename, &loadTexture, NULL, ZIPHELPER_CLEANUP_FILES)!=0) {
return 1;
// Go the the next entry listed in the zip file.
if ( ( i+1 ) < global_info.number_entry )
{
if ( unzGoToNextFile( zipfile ) != UNZ_OK )
{
unzClose( zipfile );
return 7;
}
}
}
if(texturepackUseDefaultIcons){
icons = sfil_load_PNG_buffer(icons_png, SF2D_PLACE_RAM);
if(useDefaultIcons){
icons = sfil_load_PNG_buffer(icons2_png, SF2D_PLACE_RAM);
reloadColors();
}
if(texturepackUseDefaultPlayer) playerSprites = sfil_load_PNG_buffer(player_png, SF2D_PLACE_RAM);
if(texturepackUseDefaultFont) font = sfil_load_PNG_buffer(Font_png, SF2D_PLACE_RAM);
if(texturepackUseDefaultBottom) bottombg = sfil_load_PNG_buffer(bottombg_png, SF2D_PLACE_RAM);
if(useDefaultFont) font = sfil_load_PNG_buffer(Font_png, SF2D_PLACE_RAM);
if(useDefaultBottom) bottombg = sfil_load_PNG_buffer(bottombg_png, SF2D_PLACE_RAM);
unzClose( zipfile );
return 0;
}

3
source/texturepack.h Executable file → Normal file
View file

@ -1,5 +1,6 @@
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sf2d.h>