Skip to content

Commit

Permalink
Allow selling city improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
jzeiber committed Nov 16, 2023
1 parent f2282a4 commit 72ff7c4
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 23 deletions.
2 changes: 1 addition & 1 deletion bundle/index.html

Large diffs are not rendered by default.

Binary file modified cart/cart.wasm
Binary file not shown.
35 changes: 18 additions & 17 deletions src/src/icondata.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
#include "icondata.h"

const IconData icondata[]={
// icontype, name, xidx, yidx
{/*ICON_NONE,*/ nullptr, 0, 0},
{/*ICON_NEXTUNIT,*/ "Next Unit", 6, 3},
{/*ICON_NEXTLOCUNIT,*/ "Next Unit At Loc", 7, 3},
{/*ICON_NEXTCITY,*/ "Next City", 4, 3},
{/*ICON_SCROLLMAP,*/ "Scroll Map", 4, 2},
{/*ICON_TOGGLEINFO,*/ "Toggle Info", 5, 2},
{/*ICON_ENDTURN,*/ "End Turn", 5, 1},
{/*ICON_VIEWMAP,*/ "View Map", 1, 2},
{/*ICON_FOUNDCITY,*/ "Found City", 6, 2},
{/*ICON_CLOSE,*/ "Close", 7, 2},
{/*ICON_CIVDATA,*/ "Civ Data", 0, 2},
{/*ICON_ENTERCITY,*/ "Enter City", 11, 3},
{/*ICON_DISBAND,*/ "Disband", 10, 3},
{/*ICON_EXPANDCITY,*/ "Expand City", 6, 2},
{/*ICON_CHANGEBUILD,*/ "Change Build", 6, 2},
{/*ICON_BUYBUILD,*/ "Buy", 5, 0}
// icontype, name, xidx, yidx
{/*ICON_NONE,*/ nullptr, 0, 0},
{/*ICON_NEXTUNIT,*/ "Next Unit", 6, 3},
{/*ICON_NEXTLOCUNIT,*/ "Next Unit At Loc", 7, 3},
{/*ICON_NEXTCITY,*/ "Next City", 4, 3},
{/*ICON_SCROLLMAP,*/ "Scroll Map", 4, 2},
{/*ICON_TOGGLEINFO,*/ "Toggle Info", 5, 2},
{/*ICON_ENDTURN,*/ "End Turn", 5, 1},
{/*ICON_VIEWMAP,*/ "View Map", 1, 2},
{/*ICON_FOUNDCITY,*/ "Found City", 6, 2},
{/*ICON_CLOSE,*/ "Close", 7, 2},
{/*ICON_CIVDATA,*/ "Civ Data", 0, 2},
{/*ICON_ENTERCITY,*/ "Enter City", 11, 3},
{/*ICON_DISBAND,*/ "Disband", 10, 3},
{/*ICON_EXPANDCITY,*/ "Expand City", 6, 2},
{/*ICON_CHANGEBUILD,*/ "Change Build", 6, 2},
{/*ICON_BUYBUILD,*/ "Buy", 5, 0},
{/*ICON_SELLIMPROVEMENTS,*/ "Sell Improvement", 6, 0}
};
3 changes: 2 additions & 1 deletion src/src/icondata.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ enum IconType
ICON_DISBAND=12,
ICON_EXPANDCITY=13,
ICON_CHANGEBUILD=14,
ICON_BUYBUILD=15
ICON_BUYBUILD=15,
ICON_SELLIMPROVEMENT=16
};

struct IconData
Expand Down
2 changes: 1 addition & 1 deletion src/src/improvementdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const ImprovementData improvementdata[]={
{/*IMPROVEMENT_MARKET,*/ "Market", 500, 500, 8, 400},
{/*IMPROVEMENT_BANK,*/ "Bank", 500, 500, 8, 400},
{/*IMPROVEMENT_FACTORY,*/ "Factory", 500, 500, 8, 400},
{/*IMPROVEMENT_AQUEDUCT,*/ "Aqueduct", 500, 500, 5, 400}
{/*IMPROVEMENT_AQUEDUCT,*/ "Aqueduct", 500, 500, 8, 400}
};

const BuildingXref buildingxref[]={
Expand Down
47 changes: 44 additions & 3 deletions src/src/stategame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ StateGameParams::~StateGameParams()

}

StateGame::StateGame(Map *map):m_game(nullptr),m_map(map),m_showinfo(false),m_mapx(0),m_mapy(0),m_scrollticks(0),m_blinkticks(0),m_view(0),m_menuidx(-1),m_submenuidx(-1),m_selecttype(SELECT_NONE),m_selectidx(-1),m_lastunitidx(-1),
StateGame::StateGame(Map *map):m_game(nullptr),m_map(map),m_showinfo(false),m_mapx(0),m_mapy(0),m_scrollticks(0),m_blinkticks(0),m_view(0),m_menuidx(-1),m_submenuidx(-1),m_submenuidx2(-1),m_selecttype(SELECT_NONE),m_selectidx(-1),m_lastunitidx(-1),
m_availableicons{ICON_NONE}
{

Expand Down Expand Up @@ -172,6 +172,19 @@ bool StateGame::HandleInput(const Input *input, const uint8_t playerindex)
{
m_submenuidx--;
}
if(input->GamepadButtonPress(playerindex+1,BUTTON_DOWN))
{
while(++m_submenuidx2<IMPROVEMENT_MAX && !(m_game->GetGameData().m_city[m_selectidx].improvements & (0x01 << m_submenuidx2)))
{
}
m_submenuidx2=(m_submenuidx2==IMPROVEMENT_MAX ? -1 : m_submenuidx2);
}
if(input->GamepadButtonPress(playerindex+1,BUTTON_UP))
{
while(--m_submenuidx2>-1 && !(m_game->GetGameData().m_city[m_selectidx].improvements & (0x01 << m_submenuidx2)))
{
}
}
}

if(input->GamepadButtonPress(playerindex+1,BUTTON_1)==true && m_menuidx>=0 && m_menuidx<countof(m_availableicons) && m_availableicons[m_menuidx]!=ICON_NONE)
Expand Down Expand Up @@ -214,6 +227,8 @@ bool StateGame::HandleInput(const Input *input, const uint8_t playerindex)
m_selectidx=idx;
m_mapx=m_game->GetGameData().m_city[m_selectidx].x;
m_mapy=m_game->GetGameData().m_city[m_selectidx].y;
m_submenuidx=0;
m_submenuidx2=-1;
}
break;
}
Expand Down Expand Up @@ -294,6 +309,7 @@ bool StateGame::HandleInput(const Input *input, const uint8_t playerindex)
m_selecttype=SELECT_CITY;
m_selectidx=m_game->CityIndexAtLocation(m_mapx,m_mapy);
m_submenuidx=0;
m_submenuidx2=-1;
m_view=VIEW_CITYDETAIL;
m_menuidx=0; // 1st option is next city, so select it by default
break;
Expand Down Expand Up @@ -324,6 +340,17 @@ bool StateGame::HandleInput(const Input *input, const uint8_t playerindex)
}
break;
}
case ICON_SELLIMPROVEMENT:
{
if(m_selecttype==SELECT_CITY && m_selectidx>=0 && m_selectidx<countof(m_game->GetGameData().m_city) && m_submenuidx2>=0 && (m_game->GetGameData().m_city[m_selectidx].improvements & (0x01 << m_submenuidx2)))
{
// sell improvement m_submenuidx2
m_game->GetGameData().m_city[m_selectidx].improvements=m_game->GetGameData().m_city[m_selectidx].improvements & (~(0x01 << m_submenuidx2));
m_game->GetGameData().m_civ[m_game->GetGameData().m_city[m_selectidx].owner].gold+=improvementdata[m_submenuidx2].sellgold;
m_submenuidx2=-1;
}
break;
}
default:
{
trace("Unknown Icon");
Expand Down Expand Up @@ -455,6 +482,11 @@ void StateGame::Update(const int ticks, const uint8_t playerindex, Game *game=nu
m_availableicons[idx++]=ICON_BUYBUILD;
}

if(m_submenuidx2>=0)
{
m_availableicons[idx++]=ICON_SELLIMPROVEMENT;
}

// TODO - handle selling improvement

m_availableicons[idx++]=ICON_CLOSE;
Expand Down Expand Up @@ -1217,9 +1249,9 @@ void StateGame::DrawCityDetail(const uint8_t playerindex)
if((c->improvements & (0x01 << i)) == (0x01 << i))
{
// print improvement name
tp.Print(improvementdata[i].name,96,sy,20,PALETTE_BROWN);
tp.Print(improvementdata[i].name,96,sy,20,(m_submenuidx2==i ? PALETTE_WHITE : PALETTE_BROWN));
// show upkeep gold if submenu is 1
if(m_submenuidx==1)
if(m_submenuidx==1 && m_submenuidx2<0)
{
int32_t x=SCREEN_SIZE-8;
for(int32_t g=0; g<improvementdata[i].upkeepgold; g++)
Expand All @@ -1229,6 +1261,15 @@ void StateGame::DrawCityDetail(const uint8_t playerindex)
x-=3;
}
}
if(m_submenuidx==1 && m_submenuidx2==i)
{
ostr.Clear();
ostr << improvementdata[i].sellgold;
int32_t w=tp.Print(ostr.Buffer(),SCREEN_SIZE+1,0,10,PALETTE_WHITE); // "print" off the screen just to get the length
tp.PrintWrapped(ostr.Buffer(),80,sy,10,80,PALETTE_CYAN,TextPrinter::JUSTIFY_RIGHT);
*DRAW_COLORS=(PALETTE_WHITE << 4) | PALETTE_BLACK;
blitMasked(icongfx,icongfxalpha,SCREEN_SIZE-(w+8),sy,8,8,0,8,icongfxwidth,BLIT_1BPP);
}
sy+=8;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/src/stategame.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class StateGame:public IPlayerState
int8_t m_view;
int8_t m_menuidx;
int8_t m_submenuidx;
int8_t m_submenuidx2;
int8_t m_selecttype;
int32_t m_selectidx; // 32 bit in case 8 bit isn't large enough for index values
int32_t m_lastunitidx;
Expand Down

0 comments on commit 72ff7c4

Please sign in to comment.