6.22.2006

Short String Summary with MYSQL

I got this snippet from a friend...thanks Andrew!

SELECT CONCAT(SUBSTRING(content,1,300),'...') FROM foo

The above snippets selects the first 300 characters(1-300) from the foo.content column, and then appends '...' to it.

Handy!

6.08.2006

CGI Cookies

Assuming that we're inside a module or something, and this is being passed to us as arguments:

Prelimary



use CGI;
use strict;
my $query = new CGI;
my $cookie;

...

sub writeCookie {
my($expires,$name,$contents,$rememberBoolean) = @_;
if($rememberBoolean) {
$expires = "+6M"; ## give them a 6 month remembered period
} else {
$expires = ""; ## delete the cookie after the browser has been closed
}
$cookie = $query->cookie(
-name=>$name,
-value=>$contents,
-expires->$expires
);
print $query->header(-cookie=>[$cookie]);
}

6.07.2006

Villager Class


#include <iostream>
#include <stdlib.h>

using namespace std;

class Villager {
public:
// constructors
Villager(string name);
Villager(string name, string gender);
void setHealth(int max, int cur);
void setDefaults();
// destructors
~Villager();
void die();

// methods
int move();
int speak() const;
void heal();
void heal(int healAmount);
void takeDamage();

// other

// accessors
int getMoveSpeed() const;
bool getSleeping() const;
bool getMoving() const;
int getAttraction() const;
void setAttraction(int newAttraction);
string getName() const;
string getGender() const;
int getHealth() const;
int getHealthRemaining() const;
void printGender() const;

private:

// member variables
bool itsSleeping;
bool itsMoving;
int itsMoveSpeed;
int itsHeight;
int itsEmotion;
signed int itsAttraction;
string itsGender;
int itsMaxHealth;
int itsCurrentHealth;
string itsName;
bool itsDead;
protected:
};

// constructor with name argument
Villager::Villager(string name) {
itsName = name;
// no gender supplied? Hermaphrodite.
itsGender = "Hermaphrodite";
setDefaults();
setHealth(12,12);
}

// constructor with name and gender arguments
Villager::Villager(string name, string gender) {
itsName = name;
// standardize gender assignments using some conditionals.
if(gender == "female" || gender == "Female") {
gender = "Girl";
}
if(gender == "male" || gender == "Male") {
gender = "Boy";
}
itsGender = gender;
setHealth(28,4);
setDefaults();
}

// destructor
Villager::~Villager() {
// if they're moving when we destroy them, make sure they come to a stop.
// but only do this if they AREN'T dead yet.
if(itsMoving && !itsDead) {
move();
}
}

// check whether villager is sleeping or not.
bool Villager::getSleeping() const {
return itsSleeping;
}

// set new attraction levels
void Villager::setAttraction(int newAttraction) {
itsAttraction += newAttraction;
}

// get the villager's attraction level
int Villager::getAttraction() const {
return itsAttraction;
}

// get the villager's movement speed
int Villager::getMoveSpeed() const {
return itsMoveSpeed;
}

// get whether the villager is moving or not
bool Villager::getMoving() const {
return itsMoving;
}

// toggle whether the villager is moving or not
int Villager::move() {
if(!itsMoving) {
itsMoving = true;
cout << getName() << " is on the move." << endl;
} else {
itsMoving = false;
cout << getName() << " comes to a stop." << endl;
}
}

// get the villager's name
string Villager::getName() const {
return itsName;
}

// get the villager's gender
string Villager::getGender() const {
return itsGender;
}

// make the villager speak
int Villager::speak() const {
cout << getName() << "(" << getHealthRemaining() << "/" << getHealth() << ") says 'Hello.'" << endl;
}

// print out the villager's gender
void Villager::printGender() const {
cout << getName() << "(" << getHealthRemaining() << "/" << getHealth() << ") is a " << getGender() << "." << endl;
}

// get the max health for the villager
int Villager::getHealth() const {
return itsMaxHealth;
}

// get the remaining health for the villager
int Villager::getHealthRemaining() const {
return itsCurrentHealth;
}

// set the default health value for the villager
void Villager::setHealth(int max, int cur) {
itsMaxHealth = max;
itsCurrentHealth = cur;
itsDead = false;
}

// default heal method, for villagers to regain health
void Villager::heal() {
if((itsMaxHealth - itsCurrentHealth) >= 2) {
cout << getName() << " healed for 2 Hit Points." << endl;
itsCurrentHealth += 2;
} else {
cout << getName() << " was unable to heal!" << endl;
}
}

// overloaded heal method, for supplied healing amounts.
void Villager::heal(int healAmount) {
if((itsMaxHealth - itsCurrentHealth) >= healAmount) {
cout << getName() << " healed for " << healAmount << " Hit Points." << endl;
itsCurrentHealth += healAmount;
} else {
cout << getName() << " was unable to heal!" << endl;
}
}

// death method; villager has died!
void Villager::die() {
cout << getName() << " is dead!" << endl;
itsDead = true;
// need a way to call a destructor.
// ~Villager();
}

// damage taking class, for villagers to lose health
void Villager::takeDamage() {
if((itsCurrentHealth - 1) > 0 && !itsDead) {
itsCurrentHealth -= 1;
cout << getName() << " took 1 point of damage! "<< "(" << getHealthRemaining() << "/" << getHealth() << ")" << endl;
} else if(!itsDead) {
itsCurrentHealth -= 1;
die();
}
}

// setting the default values, so that we don't have any zombie villagers.
void Villager::setDefaults() {
itsDead = false;
itsMoving = false;
itsSleeping = false;
}

5.25.2006

My Second OpenGL Program

/*

*/

#include <GLUT/glut.h>

void display(void);
void reshape(int width, int height);
void init(void);

int main(int argc, char **argv) {
glutInit(&argc, argv); // Initialize opengl
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(320, 240);
glutCreateWindow("Book Example 2");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
}

void display(void) {
// clear pixels with the clear color
glClear(GL_COLOR_BUFFER_BIT);
// draw the four points, in four different colors
glBegin(GL_POINTS); // We are using the GL_POINTS primitive
glColor3f(0.0,1.0,0.0); // green
glVertex2f(10,10); // Color3f: 3 arguments, floating point
glColor3f(1.0, 1.0, 0.0); // yellow
glVertex2f(10,110); // Vertex2f: 2 arguments, floating point
glColor3f(0.0,0.0,1.0); // blue
glVertex2f(150,110);
glColor3f(1.0,1.0,1.0); // white
glVertex2f(150,10);
// test below
glColor3f(1.0,0.6,0.7); // fleshy pink color
glVertex2i(25,25);
glEnd();

// begin flushing OpenGL calls to display buffer
glFlush();
}

void reshape(int width, int height) {
// on reshape AND startup, make the viewport the size of the entire window
glViewport(0,0,(GLsizei) width, (GLsizei) height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// keep our world coordinate system constant, though
gluOrtho2D(0.0,160.0,0.0,120.0);
}

void init(void) {
glClearColor(0.0,0.6,0.5,1.0);
// set the points to be 5 pixels big
glPointSize(5.0);
}

1.09.2006

Virtual Item Class


class Item {
public:
virtual int itsWeight;
virtual *char itsName;
virtual int itsUniqueIdNumber;
virtual int itsVelocity;
virtual int itsMaxStack; /* how high can they stack it in their inventory? */
virtual int doSomeAction(int action);
private:

protected:
}